--- deliantra/Deliantra-Client/DC/DB.pm 2007/04/06 08:09:43 1.2 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/07/24 04:54:47 1.12 @@ -1,6 +1,6 @@ =head1 NAME -CFPlus::DB - async. database access for cfplus +CFPlus::DB - async. database and filesystem access for cfplus =head1 SYNOPSIS @@ -19,7 +19,17 @@ use Carp (); use AnyEvent (); -use Storable (); # finally +use Storable (); +use Config; + +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,18 @@ CFPlus::DB::Server::req (put => @_); } +sub unlink($$) { + CFPlus::DB::Server::req (unlink => @_); +} + +sub write_file($$$) { + CFPlus::DB::Server::req (write_file => @_); +} + +sub prefetch_file($$$) { + CFPlus::DB::Server::req (prefetch_file => @_); +} + our $tilemap; sub get_tile_id_sync($) { @@ -61,7 +87,6 @@ use Fcntl; use BerkeleyDB; -our $DB_HOME = "$Crossfire::VARDIR/cfplus"; our $DB_ENV; our $DB_STATE; our %DB_TABLE; @@ -102,7 +127,7 @@ } } -our $SYNC_INTERVAL = 6; +our $SYNC_INTERVAL = 60; our %CB; our $FH; @@ -176,9 +201,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 +224,8 @@ sub do_put { my ($db, $key, $data) = @_; + utf8::downgrade $key; + utf8::downgrade $data; (table $db)->db_put ($key => $data) } @@ -231,6 +269,34 @@ die "maximum number of transaction retries reached - database problems?"; } +sub do_unlink { + unlink $_[0]; +} + +sub do_write_file { + my ($file, $data) = @_; + + utf8::downgrade $file; + utf8::downgrade $data; + open my $fh, ">:raw", $file + or return; + print $fh $data; + close $fh; + + 1 +} + +sub do_prefetch_file { + my ($file, $size) = @_; + + utf8::downgrade $file; + open my $fh, "<:raw", $file + or return; + sysread $fh, my $buf, $size; + + 1 +} + sub run { ($FH, my $fh) = CFPlus::socketpipe; @@ -245,7 +311,7 @@ close $FH; unless (eval { open_db }) { - File::Path::rmtree $DB_HOME; + eval { File::Path::rmtree $DB_HOME }; open_db; } @@ -280,7 +346,7 @@ } close $fh; - fcntl $FH, F_SETFL, O_NONBLOCK; + CFPlus::fh_nonblocking $FH, 1; $CB{die} = sub { die shift }; @@ -289,6 +355,10 @@ sync_tick; } +sub stop { + close $FH; +} + 1; =back