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.9 by root, Thu Jul 12 17:56:51 2007 UTC vs.
Revision 1.15 by root, Mon Aug 6 02:11:45 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($) { 29sub path_of_res($) {
30 "$DB_HOME/data-$_[0]" 30 utf8::downgrade $_[0]; # bug in unpack "H*"
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 { });
52 CFPlus::DB::Server::req (unlink => @_); 53 CFPlus::DB::Server::req (unlink => @_);
53} 54}
54 55
55sub write_file($$$) { 56sub write_file($$$) {
56 CFPlus::DB::Server::req (write_file => @_); 57 CFPlus::DB::Server::req (write_file => @_);
58}
59
60sub prefetch_file($$$) {
61 CFPlus::DB::Server::req (prefetch_file => @_);
62}
63
64sub logprint($$$) {
65 CFPlus::DB::Server::req (logprint => @_);
57} 66}
58 67
59our $tilemap; 68our $tilemap;
60 69
61sub get_tile_id_sync($) { 70sub get_tile_id_sync($) {
120 -Flags => DB_CREATE | DB_UPGRADE, 129 -Flags => DB_CREATE | DB_UPGRADE,
121 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 130 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
122 } 131 }
123} 132}
124 133
125our $SYNC_INTERVAL = 60;
126
127our %CB; 134our %CB;
128our $FH; 135our $FH;
129our $ID = "aaa0"; 136our $ID = "aaa0";
130our ($fh_r_watcher, $fh_w_watcher); 137our ($fh_r_watcher, $fh_w_watcher);
131our $sync_timer; 138our $sync_timer;
132our $write_buf; 139our $write_buf;
133our $read_buf; 140our $read_buf;
134 141
142our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub {
143 CFPlus::DB::Server::req (sync => sub { });
144 $_[0]->w->stop;
145});
146
135sub fh_write { 147sub fh_write {
136 my $len = syswrite $FH, $write_buf; 148 my $len = syswrite $FH, $write_buf;
137 149
138 substr $write_buf, 0, $len, ""; 150 substr $write_buf, 0, $len, "";
139 151
140 undef $fh_w_watcher 152 $fh_w_watcher->stop
141 unless length $write_buf; 153 unless length $write_buf;
142} 154}
143 155
144sub fh_read { 156sub fh_read {
145 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 157 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
181 193
182 my $id = ++$ID; 194 my $id = ++$ID;
183 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 195 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
184 $CB{$id} = $cb; 196 $CB{$id} = $cb;
185 197
186 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 198 $fh_w_watcher->start;
187} 199 $SYNC->start;
188
189sub sync_tick {
190 req "sync", sub { };
191 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
192} 200}
193 201
194sub do_sync { 202sub do_sync {
195 $DB_ENV->txn_checkpoint (0, 0, 0); 203 $DB_ENV->txn_checkpoint (0, 0, 0);
196 () 204 ()
271sub do_write_file { 279sub do_write_file {
272 my ($file, $data) = @_; 280 my ($file, $data) = @_;
273 281
274 utf8::downgrade $file; 282 utf8::downgrade $file;
275 utf8::downgrade $data; 283 utf8::downgrade $data;
276 open my $fh, ">:raw", CFPlus::DB::path_of $file 284 open my $fh, ">:raw", $file
277 or return; 285 or return;
278 print $fh $data; 286 print $fh $data;
279 close $fh; 287 close $fh;
280 288
281 1 289 1
282} 290}
283 291
292sub do_prefetch_file {
293 my ($file, $size) = @_;
294
295 utf8::downgrade $file;
296 open my $fh, "<:raw", $file
297 or return;
298 sysread $fh, my $buf, $size;
299
300 1
301}
302
303our %LOG_FH;
304
305sub do_logprint {
306 my ($path, $line) = @_;
307
308 $LOG_FH{$path} ||= do {
309 open my $fh, ">>:utf8", $path
310 or warn "Couldn't open logfile $path: $!";
311
312 $fh->autoflush (1);
313
314 $fh
315 };
316
317 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
318
319 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
320 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
321
322 print { $LOG_FH{$path} } "$ts $line\n"
323}
324
284sub run { 325sub run {
285 ($FH, my $fh) = CFPlus::socketpipe; 326 ($FH, my $fh) = CFPlus::socketpipe;
286 327
287 my $oldfh = select $FH; $| = 1; select $oldfh; 328 my $oldfh = select $FH; $| = 1; select $oldfh;
288 my $oldfh = select $fh; $| = 1; select $oldfh; 329 my $oldfh = select $fh; $| = 1; select $oldfh;
289 330
290 my $pid = fork; 331 my $pid = fork;
291 332
292 if (defined $pid && !$pid) { 333 if (defined $pid && !$pid) {
334 local $SIG{QUIT};
293 local $SIG{__DIE__}; 335 local $SIG{__DIE__};
336 local $SIG{__WARN__};
294 eval { 337 eval {
295 close $FH; 338 close $FH;
296 339
297 unless (eval { open_db }) { 340 unless (eval { open_db }) {
298 eval { File::Path::rmtree $DB_HOME }; 341 eval { File::Path::rmtree $DB_HOME };
332 close $fh; 375 close $fh;
333 CFPlus::fh_nonblocking $FH, 1; 376 CFPlus::fh_nonblocking $FH, 1;
334 377
335 $CB{die} = sub { die shift }; 378 $CB{die} = sub { die shift };
336 379
337 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 380 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
338 381 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
339 sync_tick; 382 $SYNC->start;
340} 383}
341 384
342sub stop { 385sub stop {
343 close $FH; 386 close $FH;
344} 387}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines