--- deliantra/Deliantra-Client/DC/DB.pm 2007/08/13 06:47:07 1.18 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/11/13 02:47:30 1.22 @@ -20,7 +20,6 @@ use Carp (); use Storable (); use Config; -use Event (); use CFPlus; @@ -72,17 +71,25 @@ our $tilemap; sub get_tile_id_sync($) { - my ($hash) = @_; + my ($name) = @_; # fetch the full face table first unless ($tilemap) { - CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] }); + CFPlus::DB::Server::req (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# + }); sync; } - $tilemap->{$hash} ||= do { + $tilemap->{$name} ||= do { my $id; - CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); + CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); sync; $id } @@ -112,7 +119,7 @@ # -ErrPrefix => "DATABASE", -Verbose => 1, -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, - -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, + -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; 1 @@ -130,7 +137,7 @@ # -Filename => "database", # -Subname => $table, -Property => DB_CHKSUM, - -Flags => DB_CREATE | DB_UPGRADE, + -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" } } @@ -143,10 +150,10 @@ our $write_buf; our $read_buf; -our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { +our $SYNC = EV::timer_ns 0, 60, sub { + $_[0]->stop; CFPlus::DB::Server::req (sync => sub { }); - $_[0]->w->stop; -}); +}; sub fh_write { my $len = syswrite $FH, $write_buf; @@ -200,7 +207,7 @@ $CB{$id} = $cb; $fh_w_watcher->start; - $SYNC->start; + $SYNC->again unless $SYNC->is_active; } sub do_sync { @@ -250,13 +257,13 @@ } sub do_get_tile_id { - my ($hash) = @_; + my ($name) = @_; my $id; my $table = table "facemap"; return $id - if $table->db_get ($hash, $id) == 0; + if $table->db_get ($name, $id) == 0; for (1..100) { my $txn = $DB_ENV->txn_begin; @@ -264,13 +271,14 @@ if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { $id = ($id || 64) + 1; if ($table->db_put (id => $id) == 0 - && $table->db_put ($hash => $id) == 0) { + && $table->db_put ($name => $id) == 0) { $txn->txn_commit; return $id; } } $txn->txn_abort; + select undef, undef, undef, 0.01 * rand; } die "maximum number of transaction retries reached - database problems?"; @@ -371,13 +379,15 @@ or die "$type: unknown database request type\n"; my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; (syswrite $fh, $res) == length $res - or die; + or die "DB::write: $!"; } }; my $error = $@; eval { + $DB_ENV->txn_checkpoint (0, 0, 0); + undef %DB_TABLE; undef $DB_ENV; @@ -392,9 +402,9 @@ $CB{die} = sub { die shift }; - $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; + $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; + $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; + $SYNC->again unless $SYNC->is_active; } sub stop {