--- deliantra/Deliantra-Client/DC/DB.pm 2007/04/06 21:53:56 1.3 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/07/14 12:05:53 1.11 @@ -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,16 +19,27 @@ 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($) { + "$DB_HOME/res-data-$_[0]" +} + sub sync { # for debugging #CFPlus::DB::Server::req (sync => sub { }); CFPlus::DB::Server::sync (); } +sub exists($$$) { + CFPlus::DB::Server::req (exists => @_); +} + sub get($$$) { CFPlus::DB::Server::req (get => @_); } @@ -37,6 +48,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($) { @@ -63,7 +86,6 @@ use Fcntl; use BerkeleyDB; -our $DB_HOME = "$Crossfire::VARDIR/cfplus"; our $DB_ENV; our $DB_STATE; our %DB_TABLE; @@ -104,7 +126,7 @@ } } -our $SYNC_INTERVAL = 6; +our $SYNC_INTERVAL = 60; our %CB; our $FH; @@ -178,9 +200,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 @@ -190,6 +223,8 @@ sub do_put { my ($db, $key, $data) = @_; + utf8::downgrade $key; + utf8::downgrade $data; (table $db)->db_put ($key => $data) } @@ -233,6 +268,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; @@ -247,7 +310,7 @@ close $FH; unless (eval { open_db }) { - File::Path::rmtree $DB_HOME; + eval { File::Path::rmtree $DB_HOME }; open_db; } @@ -291,6 +354,10 @@ sync_tick; } +sub stop { + close $FH; +} + 1; =back