ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.18 by root, Mon Aug 13 06:47:07 2007 UTC vs.
Revision 1.22 by root, Tue Nov 13 02:47:30 2007 UTC

18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use Storable (); 21use Storable ();
22use Config; 22use Config;
23use Event ();
24 23
25use CFPlus; 24use CFPlus;
26 25
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; 26our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28 27
70} 69}
71 70
72our $tilemap; 71our $tilemap;
73 72
74sub get_tile_id_sync($) { 73sub get_tile_id_sync($) {
75 my ($hash) = @_; 74 my ($name) = @_;
76 75
77 # fetch the full face table first 76 # fetch the full face table first
78 unless ($tilemap) { 77 unless ($tilemap) {
79 CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] }); 78 CFPlus::DB::Server::req (table => facemap => sub {
79 $tilemap = $_[0];
80 delete $tilemap->{id};
81 my %maptile = reverse %$tilemap;#d#
82 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
83 $tilemap = { };#d#
84 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
85 }#d#
86 });
80 sync; 87 sync;
81 } 88 }
82 89
83 $tilemap->{$hash} ||= do { 90 $tilemap->{$name} ||= do {
84 my $id; 91 my $id;
85 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); 92 CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] });
86 sync; 93 sync;
87 $id 94 $id
88 } 95 }
89} 96}
90 97
110 -Cachesize => 8_000_000, 117 -Cachesize => 8_000_000,
111 -ErrFile => "$DB_HOME/errorlog.txt", 118 -ErrFile => "$DB_HOME/errorlog.txt",
112# -ErrPrefix => "DATABASE", 119# -ErrPrefix => "DATABASE",
113 -Verbose => 1, 120 -Verbose => 1,
114 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 121 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
115 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 122 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
116 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; 123 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
117 124
118 1 125 1
119} 126}
120 127
128 -Env => $DB_ENV, 135 -Env => $DB_ENV,
129 -Filename => $table, 136 -Filename => $table,
130# -Filename => "database", 137# -Filename => "database",
131# -Subname => $table, 138# -Subname => $table,
132 -Property => DB_CHKSUM, 139 -Property => DB_CHKSUM,
133 -Flags => DB_CREATE | DB_UPGRADE, 140 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
134 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 141 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
135 } 142 }
136} 143}
137 144
138our %CB; 145our %CB;
141our ($fh_r_watcher, $fh_w_watcher); 148our ($fh_r_watcher, $fh_w_watcher);
142our $sync_timer; 149our $sync_timer;
143our $write_buf; 150our $write_buf;
144our $read_buf; 151our $read_buf;
145 152
146our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { 153our $SYNC = EV::timer_ns 0, 60, sub {
154 $_[0]->stop;
147 CFPlus::DB::Server::req (sync => sub { }); 155 CFPlus::DB::Server::req (sync => sub { });
148 $_[0]->w->stop;
149}); 156};
150 157
151sub fh_write { 158sub fh_write {
152 my $len = syswrite $FH, $write_buf; 159 my $len = syswrite $FH, $write_buf;
153 160
154 substr $write_buf, 0, $len, ""; 161 substr $write_buf, 0, $len, "";
198 my $id = ++$ID; 205 my $id = ++$ID;
199 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 206 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
200 $CB{$id} = $cb; 207 $CB{$id} = $cb;
201 208
202 $fh_w_watcher->start; 209 $fh_w_watcher->start;
203 $SYNC->start; 210 $SYNC->again unless $SYNC->is_active;
204} 211}
205 212
206sub do_sync { 213sub do_sync {
207 $DB_ENV->txn_checkpoint (0, 0, 0); 214 $DB_ENV->txn_checkpoint (0, 0, 0);
208 () 215 ()
248 255
249 \%kv 256 \%kv
250} 257}
251 258
252sub do_get_tile_id { 259sub do_get_tile_id {
253 my ($hash) = @_; 260 my ($name) = @_;
254 261
255 my $id; 262 my $id;
256 my $table = table "facemap"; 263 my $table = table "facemap";
257 264
258 return $id 265 return $id
259 if $table->db_get ($hash, $id) == 0; 266 if $table->db_get ($name, $id) == 0;
260 267
261 for (1..100) { 268 for (1..100) {
262 my $txn = $DB_ENV->txn_begin; 269 my $txn = $DB_ENV->txn_begin;
263 my $status = $table->db_get (id => $id); 270 my $status = $table->db_get (id => $id);
264 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 271 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
265 $id = ($id || 64) + 1; 272 $id = ($id || 64) + 1;
266 if ($table->db_put (id => $id) == 0 273 if ($table->db_put (id => $id) == 0
267 && $table->db_put ($hash => $id) == 0) { 274 && $table->db_put ($name => $id) == 0) {
268 $txn->txn_commit; 275 $txn->txn_commit;
269 276
270 return $id; 277 return $id;
271 } 278 }
272 } 279 }
273 $txn->txn_abort; 280 $txn->txn_abort;
281 select undef, undef, undef, 0.01 * rand;
274 } 282 }
275 283
276 die "maximum number of transaction retries reached - database problems?"; 284 die "maximum number of transaction retries reached - database problems?";
277} 285}
278 286
369 my ($id, $type, @args) = @$req; 377 my ($id, $type, @args) = @$req;
370 my $cb = CFPlus::DB::Server->can ("do_$type") 378 my $cb = CFPlus::DB::Server->can ("do_$type")
371 or die "$type: unknown database request type\n"; 379 or die "$type: unknown database request type\n";
372 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
373 (syswrite $fh, $res) == length $res 381 (syswrite $fh, $res) == length $res
374 or die; 382 or die "DB::write: $!";
375 } 383 }
376 }; 384 };
377 385
378 my $error = $@; 386 my $error = $@;
379 387
380 eval { 388 eval {
389 $DB_ENV->txn_checkpoint (0, 0, 0);
390
381 undef %DB_TABLE; 391 undef %DB_TABLE;
382 undef $DB_ENV; 392 undef $DB_ENV;
383 393
384 Storable::store_fd [die => $error], $fh; 394 Storable::store_fd [die => $error], $fh;
385 }; 395 };
390 close $fh; 400 close $fh;
391 CFPlus::fh_nonblocking $FH, 1; 401 CFPlus::fh_nonblocking $FH, 1;
392 402
393 $CB{die} = sub { die shift }; 403 $CB{die} = sub { die shift };
394 404
395 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); 405 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
396 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); 406 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
397 $SYNC->start; 407 $SYNC->again unless $SYNC->is_active;
398} 408}
399 409
400sub stop { 410sub stop {
401 close $FH; 411 close $FH;
402} 412}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines