--- deliantra/Deliantra-Client/DC/DB.pm 2007/12/26 21:03:21 1.29 +++ deliantra/Deliantra-Client/DC/DB.pm 2008/03/25 21:11:51 1.33 @@ -1,6 +1,6 @@ =head1 NAME -DC::DB - async. database and filesystem access for cfplus +DC::DB - async. database and filesystem access for deliantra =head1 SYNOPSIS @@ -24,19 +24,28 @@ use DC; -our $DBDIR = "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::OLDDIR/$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/$DBDIR to $DB_HOME\n"; + 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; @@ -77,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($$$) { @@ -169,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# - DC::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 { @@ -226,6 +210,8 @@ DC::DB::Server::req (logprint => @_); } +############################################################################# + package DC::DB::Server; use strict; @@ -365,7 +351,7 @@ my $pid = fork; if (defined $pid && !$pid) { - local $SIG{QUIT}; + local $SIG{QUIT} = "IGNORE"; local $SIG{__DIE__}; local $SIG{__WARN__}; eval { @@ -414,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