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.11 by root, Sat Jul 14 12:05:53 2007 UTC vs.
Revision 1.17 by root, Fri Aug 10 04:02:13 2007 UTC

16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent ();
22use Storable (); 21use Storable ();
23use Config; 22use Config;
23use Event ();
24 24
25use CFPlus; 25use CFPlus;
26 26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; 27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28 28
29sub path_of_res($) { 29sub path_of_res($) {
30 utf8::downgrade $_[0]; # bug in unpack "H*"
30 "$DB_HOME/res-data-$_[0]" 31 "$DB_HOME/res-data-" . unpack "H*", $_[0]
31} 32}
32 33
33sub sync { 34sub sync {
34 # for debugging 35 # for debugging
35 #CFPlus::DB::Server::req (sync => sub { }); 36 #CFPlus::DB::Server::req (sync => sub { });
50 51
51sub unlink($$) { 52sub unlink($$) {
52 CFPlus::DB::Server::req (unlink => @_); 53 CFPlus::DB::Server::req (unlink => @_);
53} 54}
54 55
56sub read_file($$) {
57 CFPlus::DB::Server::req (read_file => @_);
58}
59
55sub write_file($$$) { 60sub write_file($$$) {
56 CFPlus::DB::Server::req (write_file => @_); 61 CFPlus::DB::Server::req (write_file => @_);
57} 62}
58 63
59sub prefetch_file($$$) { 64sub prefetch_file($$$) {
60 CFPlus::DB::Server::req (prefetch_file => @_); 65 CFPlus::DB::Server::req (prefetch_file => @_);
66}
67
68sub logprint($$$) {
69 CFPlus::DB::Server::req (logprint => @_);
61} 70}
62 71
63our $tilemap; 72our $tilemap;
64 73
65sub get_tile_id_sync($) { 74sub get_tile_id_sync($) {
124 -Flags => DB_CREATE | DB_UPGRADE, 133 -Flags => DB_CREATE | DB_UPGRADE,
125 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 134 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
126 } 135 }
127} 136}
128 137
129our $SYNC_INTERVAL = 60;
130
131our %CB; 138our %CB;
132our $FH; 139our $FH;
133our $ID = "aaa0"; 140our $ID = "aaa0";
134our ($fh_r_watcher, $fh_w_watcher); 141our ($fh_r_watcher, $fh_w_watcher);
135our $sync_timer; 142our $sync_timer;
136our $write_buf; 143our $write_buf;
137our $read_buf; 144our $read_buf;
138 145
146our $SYNC = Event->idle (min => 10, max => 60, parked => 1, cb => sub {
147 CFPlus::DB::Server::req (sync => sub { });
148 $_[0]->w->stop;
149});
150
139sub fh_write { 151sub fh_write {
140 my $len = syswrite $FH, $write_buf; 152 my $len = syswrite $FH, $write_buf;
141 153
142 substr $write_buf, 0, $len, ""; 154 substr $write_buf, 0, $len, "";
143 155
144 undef $fh_w_watcher 156 $fh_w_watcher->stop
145 unless length $write_buf; 157 unless length $write_buf;
146} 158}
147 159
148sub fh_read { 160sub fh_read {
149 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 161 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
185 197
186 my $id = ++$ID; 198 my $id = ++$ID;
187 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 199 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
188 $CB{$id} = $cb; 200 $CB{$id} = $cb;
189 201
190 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 202 $fh_w_watcher->start;
191} 203 $SYNC->start;
192
193sub sync_tick {
194 req "sync", sub { };
195 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
196} 204}
197 205
198sub do_sync { 206sub do_sync {
199 $DB_ENV->txn_checkpoint (0, 0, 0); 207 $DB_ENV->txn_checkpoint (0, 0, 0);
200 () 208 ()
270 278
271sub do_unlink { 279sub do_unlink {
272 unlink $_[0]; 280 unlink $_[0];
273} 281}
274 282
283sub do_read_file {
284 my ($path) = @_;
285
286 utf8::downgrade $path;
287 open my $fh, "<:raw", $path
288 or return;
289 sysread $fh, my $buf, -s $fh;
290
291 $buf
292}
293
275sub do_write_file { 294sub do_write_file {
276 my ($file, $data) = @_; 295 my ($path, $data) = @_;
277 296
278 utf8::downgrade $file; 297 utf8::downgrade $path;
279 utf8::downgrade $data; 298 utf8::downgrade $data;
280 open my $fh, ">:raw", $file 299 open my $fh, ">:raw", $path
281 or return; 300 or return;
282 print $fh $data; 301 syswrite $fh, $data;
283 close $fh; 302 close $fh;
284 303
285 1 304 1
286} 305}
287 306
288sub do_prefetch_file { 307sub do_prefetch_file {
289 my ($file, $size) = @_; 308 my ($path, $size) = @_;
290 309
291 utf8::downgrade $file; 310 utf8::downgrade $path;
292 open my $fh, "<:raw", $file 311 open my $fh, "<:raw", $path
293 or return; 312 or return;
294 sysread $fh, my $buf, $size; 313 sysread $fh, my $buf, $size;
295 314
296 1 315 1
316}
317
318our %LOG_FH;
319
320sub do_logprint {
321 my ($path, $line) = @_;
322
323 $LOG_FH{$path} ||= do {
324 open my $fh, ">>:utf8", $path
325 or warn "Couldn't open logfile $path: $!";
326
327 $fh->autoflush (1);
328
329 $fh
330 };
331
332 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
333
334 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
335 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
336
337 print { $LOG_FH{$path} } "$ts $line\n"
297} 338}
298 339
299sub run { 340sub run {
300 ($FH, my $fh) = CFPlus::socketpipe; 341 ($FH, my $fh) = CFPlus::socketpipe;
301 342
303 my $oldfh = select $fh; $| = 1; select $oldfh; 344 my $oldfh = select $fh; $| = 1; select $oldfh;
304 345
305 my $pid = fork; 346 my $pid = fork;
306 347
307 if (defined $pid && !$pid) { 348 if (defined $pid && !$pid) {
349 local $SIG{QUIT};
308 local $SIG{__DIE__}; 350 local $SIG{__DIE__};
351 local $SIG{__WARN__};
309 eval { 352 eval {
310 close $FH; 353 close $FH;
311 354
312 unless (eval { open_db }) { 355 unless (eval { open_db }) {
313 eval { File::Path::rmtree $DB_HOME }; 356 eval { File::Path::rmtree $DB_HOME };
347 close $fh; 390 close $fh;
348 CFPlus::fh_nonblocking $FH, 1; 391 CFPlus::fh_nonblocking $FH, 1;
349 392
350 $CB{die} = sub { die shift }; 393 $CB{die} = sub { die shift };
351 394
352 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 395 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
353 396 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
354 sync_tick; 397 $SYNC->start;
355} 398}
356 399
357sub stop { 400sub stop {
358 close $FH; 401 close $FH;
359} 402}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines