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.19 by root, Mon Aug 20 16:16:06 2007 UTC vs.
Revision 1.23 by root, Fri Nov 16 04:54:25 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
91package CFPlus::DB::Server; 98package CFPlus::DB::Server;
92 99
93use strict; 100use strict;
94 101
102use EV ();
95use Fcntl; 103use Fcntl;
96use BerkeleyDB; 104use BerkeleyDB;
97 105
98our $DB_ENV; 106our $DB_ENV;
99our $DB_STATE; 107our $DB_STATE;
141our ($fh_r_watcher, $fh_w_watcher); 149our ($fh_r_watcher, $fh_w_watcher);
142our $sync_timer; 150our $sync_timer;
143our $write_buf; 151our $write_buf;
144our $read_buf; 152our $read_buf;
145 153
146our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { 154our $SYNC = EV::timer_ns 0, 60, sub {
155 $_[0]->stop;
147 CFPlus::DB::Server::req (sync => sub { }); 156 CFPlus::DB::Server::req (sync => sub { });
148 $_[0]->w->stop;
149}); 157};
150 158
151sub fh_write { 159sub fh_write {
152 my $len = syswrite $FH, $write_buf; 160 my $len = syswrite $FH, $write_buf;
153 161
154 substr $write_buf, 0, $len, ""; 162 substr $write_buf, 0, $len, "";
198 my $id = ++$ID; 206 my $id = ++$ID;
199 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 207 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
200 $CB{$id} = $cb; 208 $CB{$id} = $cb;
201 209
202 $fh_w_watcher->start; 210 $fh_w_watcher->start;
203 $SYNC->start; 211 $SYNC->again unless $SYNC->is_active;
204} 212}
205 213
206sub do_sync { 214sub do_sync {
207 $DB_ENV->txn_checkpoint (0, 0, 0); 215 $DB_ENV->txn_checkpoint (0, 0, 0);
208 () 216 ()
248 256
249 \%kv 257 \%kv
250} 258}
251 259
252sub do_get_tile_id { 260sub do_get_tile_id {
253 my ($hash) = @_; 261 my ($name) = @_;
254 262
255 my $id; 263 my $id;
256 my $table = table "facemap"; 264 my $table = table "facemap";
257 265
258 return $id 266 return $id
259 if $table->db_get ($hash, $id) == 0; 267 if $table->db_get ($name, $id) == 0;
260 268
261 for (1..100) { 269 for (1..100) {
262 my $txn = $DB_ENV->txn_begin; 270 my $txn = $DB_ENV->txn_begin;
263 my $status = $table->db_get (id => $id); 271 my $status = $table->db_get (id => $id);
264 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 272 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
265 $id = ($id || 64) + 1; 273 $id = ($id || 64) + 1;
266 if ($table->db_put (id => $id) == 0 274 if ($table->db_put (id => $id) == 0
267 && $table->db_put ($hash => $id) == 0) { 275 && $table->db_put ($name => $id) == 0) {
268 $txn->txn_commit; 276 $txn->txn_commit;
269 277
270 return $id; 278 return $id;
271 } 279 }
272 } 280 }
370 my ($id, $type, @args) = @$req; 378 my ($id, $type, @args) = @$req;
371 my $cb = CFPlus::DB::Server->can ("do_$type") 379 my $cb = CFPlus::DB::Server->can ("do_$type")
372 or die "$type: unknown database request type\n"; 380 or die "$type: unknown database request type\n";
373 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 381 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
374 (syswrite $fh, $res) == length $res 382 (syswrite $fh, $res) == length $res
375 or die; 383 or die "DB::write: $!";
376 } 384 }
377 }; 385 };
378 386
379 my $error = $@; 387 my $error = $@;
380 388
381 eval { 389 eval {
390 $DB_ENV->txn_checkpoint (0, 0, 0);
391
382 undef %DB_TABLE; 392 undef %DB_TABLE;
383 undef $DB_ENV; 393 undef $DB_ENV;
384 394
385 Storable::store_fd [die => $error], $fh; 395 Storable::store_fd [die => $error], $fh;
386 }; 396 };
391 close $fh; 401 close $fh;
392 CFPlus::fh_nonblocking $FH, 1; 402 CFPlus::fh_nonblocking $FH, 1;
393 403
394 $CB{die} = sub { die shift }; 404 $CB{die} = sub { die shift };
395 405
396 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); 406 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
397 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); 407 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
398 $SYNC->start; 408 $SYNC->again unless $SYNC->is_active;
399} 409}
400 410
401sub stop { 411sub stop {
402 close $FH; 412 close $FH;
403} 413}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines