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.15 by root, Mon Aug 6 02:11:45 2007 UTC vs.
Revision 1.21 by root, Mon Nov 12 23:25:37 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
51 50
52sub unlink($$) { 51sub unlink($$) {
53 CFPlus::DB::Server::req (unlink => @_); 52 CFPlus::DB::Server::req (unlink => @_);
54} 53}
55 54
55sub read_file($$) {
56 CFPlus::DB::Server::req (read_file => @_);
57}
58
56sub write_file($$$) { 59sub write_file($$$) {
57 CFPlus::DB::Server::req (write_file => @_); 60 CFPlus::DB::Server::req (write_file => @_);
58} 61}
59 62
60sub prefetch_file($$$) { 63sub prefetch_file($$$) {
66} 69}
67 70
68our $tilemap; 71our $tilemap;
69 72
70sub get_tile_id_sync($) { 73sub get_tile_id_sync($) {
71 my ($hash) = @_; 74 my ($name) = @_;
72 75
73 # fetch the full face table first 76 # fetch the full face table first
74 unless ($tilemap) { 77 unless ($tilemap) {
75 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 });
76 sync; 87 sync;
77 } 88 }
78 89
79 $tilemap->{$hash} ||= do { 90 $tilemap->{$name} ||= do {
80 my $id; 91 my $id;
81 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); 92 CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] });
82 sync; 93 sync;
83 $id 94 $id
84 } 95 }
85} 96}
86 97
106 -Cachesize => 8_000_000, 117 -Cachesize => 8_000_000,
107 -ErrFile => "$DB_HOME/errorlog.txt", 118 -ErrFile => "$DB_HOME/errorlog.txt",
108# -ErrPrefix => "DATABASE", 119# -ErrPrefix => "DATABASE",
109 -Verbose => 1, 120 -Verbose => 1,
110 -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,
111 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 122 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
112 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";
113 124
114 1 125 1
115} 126}
116 127
124 -Env => $DB_ENV, 135 -Env => $DB_ENV,
125 -Filename => $table, 136 -Filename => $table,
126# -Filename => "database", 137# -Filename => "database",
127# -Subname => $table, 138# -Subname => $table,
128 -Property => DB_CHKSUM, 139 -Property => DB_CHKSUM,
129 -Flags => DB_CREATE | DB_UPGRADE, 140 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
130 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 141 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
131 } 142 }
132} 143}
133 144
134our %CB; 145our %CB;
137our ($fh_r_watcher, $fh_w_watcher); 148our ($fh_r_watcher, $fh_w_watcher);
138our $sync_timer; 149our $sync_timer;
139our $write_buf; 150our $write_buf;
140our $read_buf; 151our $read_buf;
141 152
142our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { 153our $SYNC = EV::timer_ns 0, 60, sub {
154 $_[0]->stop;
143 CFPlus::DB::Server::req (sync => sub { }); 155 CFPlus::DB::Server::req (sync => sub { });
144 $_[0]->w->stop;
145}); 156};
146 157
147sub fh_write { 158sub fh_write {
148 my $len = syswrite $FH, $write_buf; 159 my $len = syswrite $FH, $write_buf;
149 160
150 substr $write_buf, 0, $len, ""; 161 substr $write_buf, 0, $len, "";
194 my $id = ++$ID; 205 my $id = ++$ID;
195 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 206 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
196 $CB{$id} = $cb; 207 $CB{$id} = $cb;
197 208
198 $fh_w_watcher->start; 209 $fh_w_watcher->start;
199 $SYNC->start; 210 $SYNC->again unless $SYNC->is_active;
200} 211}
201 212
202sub do_sync { 213sub do_sync {
203 $DB_ENV->txn_checkpoint (0, 0, 0); 214 $DB_ENV->txn_checkpoint (0, 0, 0);
204 () 215 ()
244 255
245 \%kv 256 \%kv
246} 257}
247 258
248sub do_get_tile_id { 259sub do_get_tile_id {
249 my ($hash) = @_; 260 my ($name) = @_;
250 261
251 my $id; 262 my $id;
252 my $table = table "facemap"; 263 my $table = table "facemap";
253 264
254 return $id 265 return $id
255 if $table->db_get ($hash, $id) == 0; 266 if $table->db_get ($name, $id) == 0;
256 267
257 for (1..100) { 268 for (1..100) {
258 my $txn = $DB_ENV->txn_begin; 269 my $txn = $DB_ENV->txn_begin;
259 my $status = $table->db_get (id => $id); 270 my $status = $table->db_get (id => $id);
260 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 271 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
261 $id = ($id || 64) + 1; 272 $id = ($id || 64) + 1;
262 if ($table->db_put (id => $id) == 0 273 if ($table->db_put (id => $id) == 0
263 && $table->db_put ($hash => $id) == 0) { 274 && $table->db_put ($name => $id) == 0) {
264 $txn->txn_commit; 275 $txn->txn_commit;
265 276
266 return $id; 277 return $id;
267 } 278 }
268 } 279 }
269 $txn->txn_abort; 280 $txn->txn_abort;
281 select undef, undef, undef, 0.01 * rand;
270 } 282 }
271 283
272 die "maximum number of transaction retries reached - database problems?"; 284 die "maximum number of transaction retries reached - database problems?";
273} 285}
274 286
275sub do_unlink { 287sub do_unlink {
276 unlink $_[0]; 288 unlink $_[0];
277} 289}
278 290
291sub do_read_file {
292 my ($path) = @_;
293
294 utf8::downgrade $path;
295 open my $fh, "<:raw", $path
296 or return;
297 sysread $fh, my $buf, -s $fh;
298
299 $buf
300}
301
279sub do_write_file { 302sub do_write_file {
280 my ($file, $data) = @_; 303 my ($path, $data) = @_;
281 304
282 utf8::downgrade $file; 305 utf8::downgrade $path;
283 utf8::downgrade $data; 306 utf8::downgrade $data;
284 open my $fh, ">:raw", $file 307 open my $fh, ">:raw", $path
285 or return; 308 or return;
286 print $fh $data; 309 syswrite $fh, $data;
287 close $fh; 310 close $fh;
288 311
289 1 312 1
290} 313}
291 314
292sub do_prefetch_file { 315sub do_prefetch_file {
293 my ($file, $size) = @_; 316 my ($path, $size) = @_;
294 317
295 utf8::downgrade $file; 318 utf8::downgrade $path;
296 open my $fh, "<:raw", $file 319 open my $fh, "<:raw", $path
297 or return; 320 or return;
298 sysread $fh, my $buf, $size; 321 sysread $fh, my $buf, $size;
299 322
300 1 323 1
301} 324}
354 my ($id, $type, @args) = @$req; 377 my ($id, $type, @args) = @$req;
355 my $cb = CFPlus::DB::Server->can ("do_$type") 378 my $cb = CFPlus::DB::Server->can ("do_$type")
356 or die "$type: unknown database request type\n"; 379 or die "$type: unknown database request type\n";
357 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
358 (syswrite $fh, $res) == length $res 381 (syswrite $fh, $res) == length $res
359 or die; 382 or die "DB::write: $!";
360 } 383 }
361 }; 384 };
362 385
363 my $error = $@; 386 my $error = $@;
364 387
367 undef $DB_ENV; 390 undef $DB_ENV;
368 391
369 Storable::store_fd [die => $error], $fh; 392 Storable::store_fd [die => $error], $fh;
370 }; 393 };
371 394
395 $DB_ENV->txn_checkpoint (0, 0, 0);
372 CFPlus::_exit 0; 396 CFPlus::_exit 0;
373 } 397 }
374 398
375 close $fh; 399 close $fh;
376 CFPlus::fh_nonblocking $FH, 1; 400 CFPlus::fh_nonblocking $FH, 1;
377 401
378 $CB{die} = sub { die shift }; 402 $CB{die} = sub { die shift };
379 403
380 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); 404 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
381 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); 405 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
382 $SYNC->start; 406 $SYNC->again unless $SYNC->is_active;
383} 407}
384 408
385sub stop { 409sub stop {
386 close $FH; 410 close $FH;
387} 411}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines