ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.3 by root, Fri Apr 6 21:53:56 2007 UTC vs.
Revision 1.14 by root, Sun Jul 29 19:32:29 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFPlus::DB - async. database access for cfplus 3CFPlus::DB - async. database and filesystem access for cfplus
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFPlus::DB; 7 use CFPlus::DB;
8 8
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use Storable ();
22use Config;
21use AnyEvent (); 23use Event ();
22use Storable (); # finally
23 24
24use CFPlus; 25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of_res($) {
30 utf8::downgrade $_[0]; # bug in unpack "H*"
31 "$DB_HOME/res-data-" . unpack "H*", $_[0]
32}
25 33
26sub sync { 34sub sync {
27 # for debugging 35 # for debugging
28 #CFPlus::DB::Server::req (sync => sub { }); 36 #CFPlus::DB::Server::req (sync => sub { });
29 CFPlus::DB::Server::sync (); 37 CFPlus::DB::Server::sync ();
30} 38}
31 39
40sub exists($$$) {
41 CFPlus::DB::Server::req (exists => @_);
42}
43
32sub get($$$) { 44sub get($$$) {
33 CFPlus::DB::Server::req (get => @_); 45 CFPlus::DB::Server::req (get => @_);
34} 46}
35 47
36sub put($$$$) { 48sub put($$$$) {
37 CFPlus::DB::Server::req (put => @_); 49 CFPlus::DB::Server::req (put => @_);
50}
51
52sub unlink($$) {
53 CFPlus::DB::Server::req (unlink => @_);
54}
55
56sub write_file($$$) {
57 CFPlus::DB::Server::req (write_file => @_);
58}
59
60sub prefetch_file($$$) {
61 CFPlus::DB::Server::req (prefetch_file => @_);
38} 62}
39 63
40our $tilemap; 64our $tilemap;
41 65
42sub get_tile_id_sync($) { 66sub get_tile_id_sync($) {
61use strict; 85use strict;
62 86
63use Fcntl; 87use Fcntl;
64use BerkeleyDB; 88use BerkeleyDB;
65 89
66our $DB_HOME = "$Crossfire::VARDIR/cfplus";
67our $DB_ENV; 90our $DB_ENV;
68our $DB_STATE; 91our $DB_STATE;
69our %DB_TABLE; 92our %DB_TABLE;
70 93
71sub open_db { 94sub open_db {
102 -Flags => DB_CREATE | DB_UPGRADE, 125 -Flags => DB_CREATE | DB_UPGRADE,
103 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 126 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
104 } 127 }
105} 128}
106 129
107our $SYNC_INTERVAL = 6;
108
109our %CB; 130our %CB;
110our $FH; 131our $FH;
111our $ID = "aaa0"; 132our $ID = "aaa0";
112our ($fh_r_watcher, $fh_w_watcher); 133our ($fh_r_watcher, $fh_w_watcher);
113our $sync_timer; 134our $sync_timer;
114our $write_buf; 135our $write_buf;
115our $read_buf; 136our $read_buf;
116 137
138our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub {
139 CFPlus::DB::Server::req (sync => sub { });
140 $_[0]->w->stop;
141});
142
117sub fh_write { 143sub fh_write {
118 my $len = syswrite $FH, $write_buf; 144 my $len = syswrite $FH, $write_buf;
119 145
120 substr $write_buf, 0, $len, ""; 146 substr $write_buf, 0, $len, "";
121 147
122 undef $fh_w_watcher 148 $fh_w_watcher->stop
123 unless length $write_buf; 149 unless length $write_buf;
124} 150}
125 151
126sub fh_read { 152sub fh_read {
127 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 153 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
163 189
164 my $id = ++$ID; 190 my $id = ++$ID;
165 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 191 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
166 $CB{$id} = $cb; 192 $CB{$id} = $cb;
167 193
168 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 194 $fh_w_watcher->start;
169} 195 $SYNC->start;
170
171sub sync_tick {
172 req "sync", sub { };
173 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
174} 196}
175 197
176sub do_sync { 198sub do_sync {
177 $DB_ENV->txn_checkpoint (0, 0, 0); 199 $DB_ENV->txn_checkpoint (0, 0, 0);
178 () 200 ()
179} 201}
180 202
203sub do_exists {
204 my ($db, $key) = @_;
205
206 utf8::downgrade $key;
207 my $data;
208 (table $db)->db_get ($key, $data) == 0
209 ? length $data
210 : ()
211}
212
181sub do_get { 213sub do_get {
182 my ($db, $key) = @_; 214 my ($db, $key) = @_;
183 215
216 utf8::downgrade $key;
184 my $data; 217 my $data;
185 (table $db)->db_get ($key, $data) == 0 218 (table $db)->db_get ($key, $data) == 0
186 ? $data 219 ? $data
187 : () 220 : ()
188} 221}
189 222
190sub do_put { 223sub do_put {
191 my ($db, $key, $data) = @_; 224 my ($db, $key, $data) = @_;
192 225
226 utf8::downgrade $key;
227 utf8::downgrade $data;
193 (table $db)->db_put ($key => $data) 228 (table $db)->db_put ($key => $data)
194} 229}
195 230
196sub do_table { 231sub do_table {
197 my ($db) = @_; 232 my ($db) = @_;
231 } 266 }
232 267
233 die "maximum number of transaction retries reached - database problems?"; 268 die "maximum number of transaction retries reached - database problems?";
234} 269}
235 270
271sub do_unlink {
272 unlink $_[0];
273}
274
275sub do_write_file {
276 my ($file, $data) = @_;
277
278 utf8::downgrade $file;
279 utf8::downgrade $data;
280 open my $fh, ">:raw", $file
281 or return;
282 print $fh $data;
283 close $fh;
284
285 1
286}
287
288sub do_prefetch_file {
289 my ($file, $size) = @_;
290
291 utf8::downgrade $file;
292 open my $fh, "<:raw", $file
293 or return;
294 sysread $fh, my $buf, $size;
295
296 1
297}
298
236sub run { 299sub run {
237 ($FH, my $fh) = CFPlus::socketpipe; 300 ($FH, my $fh) = CFPlus::socketpipe;
238 301
239 my $oldfh = select $FH; $| = 1; select $oldfh; 302 my $oldfh = select $FH; $| = 1; select $oldfh;
240 my $oldfh = select $fh; $| = 1; select $oldfh; 303 my $oldfh = select $fh; $| = 1; select $oldfh;
245 local $SIG{__DIE__}; 308 local $SIG{__DIE__};
246 eval { 309 eval {
247 close $FH; 310 close $FH;
248 311
249 unless (eval { open_db }) { 312 unless (eval { open_db }) {
250 File::Path::rmtree $DB_HOME; 313 eval { File::Path::rmtree $DB_HOME };
251 open_db; 314 open_db;
252 } 315 }
253 316
254 while () { 317 while () {
255 4 == read $fh, my $len, 4 318 4 == read $fh, my $len, 4
284 close $fh; 347 close $fh;
285 CFPlus::fh_nonblocking $FH, 1; 348 CFPlus::fh_nonblocking $FH, 1;
286 349
287 $CB{die} = sub { die shift }; 350 $CB{die} = sub { die shift };
288 351
289 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 352 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
353 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
354 $SYNC->start;
355}
290 356
291 sync_tick; 357sub stop {
358 close $FH;
292} 359}
293 360
2941; 3611;
295 362
296=back 363=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines