--- deliantra/Deliantra-Client/DC/DB.pm 2007/04/06 07:45:34 1.1 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/08/13 06:47:07 1.18 @@ -1,6 +1,6 @@ =head1 NAME -CFPlus::DB - async. database access for cfplus +CFPlus::DB - async. database and filesystem access for cfplus =head1 SYNOPSIS @@ -18,8 +18,18 @@ use utf8; use Carp (); -use AnyEvent (); -use Storable (); # finally +use Storable (); +use Config; +use Event (); + +use CFPlus; + +our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; + +sub path_of_res($) { + utf8::downgrade $_[0]; # bug in unpack "H*" + "$DB_HOME/res-data-" . unpack "H*", $_[0] +} sub sync { # for debugging @@ -27,6 +37,10 @@ CFPlus::DB::Server::sync (); } +sub exists($$$) { + CFPlus::DB::Server::req (exists => @_); +} + sub get($$$) { CFPlus::DB::Server::req (get => @_); } @@ -35,6 +49,26 @@ CFPlus::DB::Server::req (put => @_); } +sub unlink($$) { + CFPlus::DB::Server::req (unlink => @_); +} + +sub read_file($$) { + CFPlus::DB::Server::req (read_file => @_); +} + +sub write_file($$$) { + CFPlus::DB::Server::req (write_file => @_); +} + +sub prefetch_file($$$) { + CFPlus::DB::Server::req (prefetch_file => @_); +} + +sub logprint($$$) { + CFPlus::DB::Server::req (logprint => @_); +} + our $tilemap; sub get_tile_id_sync($) { @@ -61,7 +95,6 @@ use Fcntl; use BerkeleyDB; -our $DB_HOME = "$Crossfire::VARDIR/cfplus"; our $DB_ENV; our $DB_STATE; our %DB_TABLE; @@ -102,8 +135,6 @@ } } -our $SYNC_INTERVAL = 6; - our %CB; our $FH; our $ID = "aaa0"; @@ -112,12 +143,17 @@ our $write_buf; our $read_buf; +our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { + CFPlus::DB::Server::req (sync => sub { }); + $_[0]->w->stop; +}); + sub fh_write { my $len = syswrite $FH, $write_buf; substr $write_buf, 0, $len, ""; - undef $fh_w_watcher + $fh_w_watcher->stop unless length $write_buf; } @@ -163,12 +199,8 @@ $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; $CB{$id} = $cb; - $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); -} - -sub sync_tick { - req "sync", sub { }; - $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); + $fh_w_watcher->start; + $SYNC->start; } sub do_sync { @@ -176,9 +208,20 @@ () } +sub do_exists { + my ($db, $key) = @_; + + utf8::downgrade $key; + my $data; + (table $db)->db_get ($key, $data) == 0 + ? length $data + : () +} + sub do_get { my ($db, $key) = @_; + utf8::downgrade $key; my $data; (table $db)->db_get ($key, $data) == 0 ? $data @@ -188,6 +231,8 @@ sub do_put { my ($db, $key, $data) = @_; + utf8::downgrade $key; + utf8::downgrade $data; (table $db)->db_put ($key => $data) } @@ -231,6 +276,67 @@ die "maximum number of transaction retries reached - database problems?"; } +sub do_unlink { + unlink $_[0]; +} + +sub do_read_file { + my ($path) = @_; + + utf8::downgrade $path; + open my $fh, "<:raw", $path + or return; + sysread $fh, my $buf, -s $fh; + + $buf +} + +sub do_write_file { + my ($path, $data) = @_; + + utf8::downgrade $path; + utf8::downgrade $data; + open my $fh, ">:raw", $path + or return; + syswrite $fh, $data; + close $fh; + + 1 +} + +sub do_prefetch_file { + my ($path, $size) = @_; + + utf8::downgrade $path; + open my $fh, "<:raw", $path + or return; + sysread $fh, my $buf, $size; + + 1 +} + +our %LOG_FH; + +sub do_logprint { + my ($path, $line) = @_; + + $LOG_FH{$path} ||= do { + open my $fh, ">>:utf8", $path + or warn "Couldn't open logfile $path: $!"; + + $fh->autoflush (1); + + $fh + }; + + my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; + + my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", + $year + 1900, $mon + 1, $mday, $hour, $min, $sec; + + print { $LOG_FH{$path} } "$ts $line\n" +} + sub run { ($FH, my $fh) = CFPlus::socketpipe; @@ -240,12 +346,14 @@ my $pid = fork; if (defined $pid && !$pid) { + local $SIG{QUIT}; local $SIG{__DIE__}; + local $SIG{__WARN__}; eval { close $FH; unless (eval { open_db }) { - File::Path::rmtree $DB_HOME; + eval { File::Path::rmtree $DB_HOME }; open_db; } @@ -280,15 +388,17 @@ } close $fh; - fcntl $FH, F_SETFL, O_NONBLOCK; + CFPlus::fh_nonblocking $FH, 1; - $CB{die} = sub { - die shift; - }; + $CB{die} = sub { die shift }; - $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); + $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); + $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); + $SYNC->start; +} - sync_tick; +sub stop { + close $FH; } 1;