--- deliantra/Deliantra-Client/DC/DB.pm 2007/12/11 03:38:36 1.25 +++ deliantra/Deliantra-Client/DC/DB.pm 2008/03/25 21:11:51 1.33 @@ -1,10 +1,10 @@ =head1 NAME -CFPlus::DB - async. database and filesystem access for cfplus +DC::DB - async. database and filesystem access for deliantra =head1 SYNOPSIS - use CFPlus::DB; + use DC::DB; =head1 DESCRIPTION @@ -12,7 +12,7 @@ =cut -package CFPlus::DB; +package DC::DB; use strict; use utf8; @@ -22,15 +22,30 @@ use Config; use BDB; -use CFPlus; +use DC; -our $DB_HOME = "$Crossfire::VARDIR/cfplus-" . BDB::VERSION . "-$Config{archname}"; +our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; +our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; +our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; + +if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { + rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; + print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; +} + +if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { + rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; + print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; +} + +BDB::max_poll_time 0.03; +BDB::max_parallel 1; our $DB_ENV; our $DB_STATE; our %DB_TABLE; -sub open_db { +sub try_open_db { mkdir $DB_HOME, 0777; $DB_ENV = db_env_create; @@ -71,19 +86,8 @@ ############################################################################# -unless (eval { open_db }) { - warn "$@";#d# - eval { File::Path::rmtree $DB_HOME }; - open_db; -} - -our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; - -our $SYNC = EV::timer_ns 0, 60, sub { - $_[0]->stop; - db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; -}; - +our $WATCHER; +our $SYNC; our $tilemap; sub exists($$$) { @@ -163,20 +167,6 @@ sub get_tile_id_sync($) { my ($name) = @_; - # fetch the full face table first - unless ($tilemap) { - do_table facemap => sub { - $tilemap = $_[0]; - delete $tilemap->{id}; - my %maptile = reverse %$tilemap;#d# - if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# - $tilemap = { };#d# - CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# - }#d# - }; - BDB::flush; - } - $tilemap->{$name} ||= do { my $id; do_get_tile_id $name, sub { @@ -196,31 +186,33 @@ sub sync { # for debugging - #CFPlus::DB::Server::req (sync => sub { }); - CFPlus::DB::Server::sync (); + #DC::DB::Server::req (sync => sub { }); + DC::DB::Server::sync (); } sub unlink($$) { - CFPlus::DB::Server::req (unlink => @_); + DC::DB::Server::req (unlink => @_); } sub read_file($$) { - CFPlus::DB::Server::req (read_file => @_); + DC::DB::Server::req (read_file => @_); } sub write_file($$$) { - CFPlus::DB::Server::req (write_file => @_); + DC::DB::Server::req (write_file => @_); } sub prefetch_file($$$) { - CFPlus::DB::Server::req (prefetch_file => @_); + DC::DB::Server::req (prefetch_file => @_); } sub logprint($$$) { - CFPlus::DB::Server::req (logprint => @_); + DC::DB::Server::req (logprint => @_); } -package CFPlus::DB::Server; +############################################################################# + +package DC::DB::Server; use strict; @@ -351,7 +343,7 @@ } sub run { - ($FH, my $fh) = CFPlus::socketpipe; + ($FH, my $fh) = DC::socketpipe; my $oldfh = select $FH; $| = 1; select $oldfh; my $oldfh = select $fh; $| = 1; select $oldfh; @@ -359,7 +351,7 @@ my $pid = fork; if (defined $pid && !$pid) { - local $SIG{QUIT}; + local $SIG{QUIT} = "IGNORE"; local $SIG{__DIE__}; local $SIG{__WARN__}; eval { @@ -375,7 +367,7 @@ $req = Storable::thaw $req; my ($id, $type, @args) = @$req; - my $cb = CFPlus::DB::Server->can ("do_$type") + my $cb = DC::DB::Server->can ("do_$type") or die "$type: unknown database request type\n"; my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; (syswrite $fh, $res) == length $res @@ -392,11 +384,11 @@ warn $error if $error; - CFPlus::_exit 0; + DC::_exit 0; } close $fh; - CFPlus::fh_nonblocking $FH, 1; + DC::fh_nonblocking $FH, 1; $CB{die} = sub { die shift }; @@ -408,6 +400,40 @@ close $FH; } +package DC::DB; + +sub open_db { + unless (eval { try_open_db }) { + warn "$@";#d# + eval { File::Path::rmtree $DB_HOME }; + try_open_db; + } + + # fetch the full face table first + unless ($tilemap) { + do_table facemap => sub { + $tilemap = $_[0]; + delete $tilemap->{id}; + my %maptile = reverse %$tilemap;#d# + if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# + $tilemap = { };#d# + DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# + }#d# + }; + } + + $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; + $SYNC = EV::timer_ns 0, 60, sub { + $_[0]->stop; + db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; + }; +} + +END { + %DB_TABLE = (); + undef $DB_ENV; +} + 1; =back