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.12 by root, Tue Jul 24 04:54:47 2007 UTC vs.
Revision 1.21 by root, Mon Nov 12 23:25:37 2007 UTC

16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent ();
22use Storable (); 21use Storable ();
23use Config; 22use Config;
24 23
25use CFPlus; 24use CFPlus;
26 25
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($$$) {
61 CFPlus::DB::Server::req (prefetch_file => @_); 64 CFPlus::DB::Server::req (prefetch_file => @_);
62} 65}
63 66
67sub logprint($$$) {
68 CFPlus::DB::Server::req (logprint => @_);
69}
70
64our $tilemap; 71our $tilemap;
65 72
66sub get_tile_id_sync($) { 73sub get_tile_id_sync($) {
67 my ($hash) = @_; 74 my ($name) = @_;
68 75
69 # fetch the full face table first 76 # fetch the full face table first
70 unless ($tilemap) { 77 unless ($tilemap) {
71 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 });
72 sync; 87 sync;
73 } 88 }
74 89
75 $tilemap->{$hash} ||= do { 90 $tilemap->{$name} ||= do {
76 my $id; 91 my $id;
77 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); 92 CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] });
78 sync; 93 sync;
79 $id 94 $id
80 } 95 }
81} 96}
82 97
102 -Cachesize => 8_000_000, 117 -Cachesize => 8_000_000,
103 -ErrFile => "$DB_HOME/errorlog.txt", 118 -ErrFile => "$DB_HOME/errorlog.txt",
104# -ErrPrefix => "DATABASE", 119# -ErrPrefix => "DATABASE",
105 -Verbose => 1, 120 -Verbose => 1,
106 -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,
107 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 122 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
108 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";
109 124
110 1 125 1
111} 126}
112 127
120 -Env => $DB_ENV, 135 -Env => $DB_ENV,
121 -Filename => $table, 136 -Filename => $table,
122# -Filename => "database", 137# -Filename => "database",
123# -Subname => $table, 138# -Subname => $table,
124 -Property => DB_CHKSUM, 139 -Property => DB_CHKSUM,
125 -Flags => DB_CREATE | DB_UPGRADE, 140 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
126 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 141 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
127 } 142 }
128} 143}
129
130our $SYNC_INTERVAL = 60;
131 144
132our %CB; 145our %CB;
133our $FH; 146our $FH;
134our $ID = "aaa0"; 147our $ID = "aaa0";
135our ($fh_r_watcher, $fh_w_watcher); 148our ($fh_r_watcher, $fh_w_watcher);
136our $sync_timer; 149our $sync_timer;
137our $write_buf; 150our $write_buf;
138our $read_buf; 151our $read_buf;
139 152
153our $SYNC = EV::timer_ns 0, 60, sub {
154 $_[0]->stop;
155 CFPlus::DB::Server::req (sync => sub { });
156};
157
140sub fh_write { 158sub fh_write {
141 my $len = syswrite $FH, $write_buf; 159 my $len = syswrite $FH, $write_buf;
142 160
143 substr $write_buf, 0, $len, ""; 161 substr $write_buf, 0, $len, "";
144 162
145 undef $fh_w_watcher 163 $fh_w_watcher->stop
146 unless length $write_buf; 164 unless length $write_buf;
147} 165}
148 166
149sub fh_read { 167sub fh_read {
150 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 168 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
186 204
187 my $id = ++$ID; 205 my $id = ++$ID;
188 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 206 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
189 $CB{$id} = $cb; 207 $CB{$id} = $cb;
190 208
191 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 209 $fh_w_watcher->start;
192} 210 $SYNC->again unless $SYNC->is_active;
193
194sub sync_tick {
195 req "sync", sub { };
196 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
197} 211}
198 212
199sub do_sync { 213sub do_sync {
200 $DB_ENV->txn_checkpoint (0, 0, 0); 214 $DB_ENV->txn_checkpoint (0, 0, 0);
201 () 215 ()
241 255
242 \%kv 256 \%kv
243} 257}
244 258
245sub do_get_tile_id { 259sub do_get_tile_id {
246 my ($hash) = @_; 260 my ($name) = @_;
247 261
248 my $id; 262 my $id;
249 my $table = table "facemap"; 263 my $table = table "facemap";
250 264
251 return $id 265 return $id
252 if $table->db_get ($hash, $id) == 0; 266 if $table->db_get ($name, $id) == 0;
253 267
254 for (1..100) { 268 for (1..100) {
255 my $txn = $DB_ENV->txn_begin; 269 my $txn = $DB_ENV->txn_begin;
256 my $status = $table->db_get (id => $id); 270 my $status = $table->db_get (id => $id);
257 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 271 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
258 $id = ($id || 64) + 1; 272 $id = ($id || 64) + 1;
259 if ($table->db_put (id => $id) == 0 273 if ($table->db_put (id => $id) == 0
260 && $table->db_put ($hash => $id) == 0) { 274 && $table->db_put ($name => $id) == 0) {
261 $txn->txn_commit; 275 $txn->txn_commit;
262 276
263 return $id; 277 return $id;
264 } 278 }
265 } 279 }
266 $txn->txn_abort; 280 $txn->txn_abort;
281 select undef, undef, undef, 0.01 * rand;
267 } 282 }
268 283
269 die "maximum number of transaction retries reached - database problems?"; 284 die "maximum number of transaction retries reached - database problems?";
270} 285}
271 286
272sub do_unlink { 287sub do_unlink {
273 unlink $_[0]; 288 unlink $_[0];
274} 289}
275 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
276sub do_write_file { 302sub do_write_file {
277 my ($file, $data) = @_; 303 my ($path, $data) = @_;
278 304
279 utf8::downgrade $file; 305 utf8::downgrade $path;
280 utf8::downgrade $data; 306 utf8::downgrade $data;
281 open my $fh, ">:raw", $file 307 open my $fh, ">:raw", $path
282 or return; 308 or return;
283 print $fh $data; 309 syswrite $fh, $data;
284 close $fh; 310 close $fh;
285 311
286 1 312 1
287} 313}
288 314
289sub do_prefetch_file { 315sub do_prefetch_file {
290 my ($file, $size) = @_; 316 my ($path, $size) = @_;
291 317
292 utf8::downgrade $file; 318 utf8::downgrade $path;
293 open my $fh, "<:raw", $file 319 open my $fh, "<:raw", $path
294 or return; 320 or return;
295 sysread $fh, my $buf, $size; 321 sysread $fh, my $buf, $size;
296 322
297 1 323 1
324}
325
326our %LOG_FH;
327
328sub do_logprint {
329 my ($path, $line) = @_;
330
331 $LOG_FH{$path} ||= do {
332 open my $fh, ">>:utf8", $path
333 or warn "Couldn't open logfile $path: $!";
334
335 $fh->autoflush (1);
336
337 $fh
338 };
339
340 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
341
342 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
343 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
344
345 print { $LOG_FH{$path} } "$ts $line\n"
298} 346}
299 347
300sub run { 348sub run {
301 ($FH, my $fh) = CFPlus::socketpipe; 349 ($FH, my $fh) = CFPlus::socketpipe;
302 350
304 my $oldfh = select $fh; $| = 1; select $oldfh; 352 my $oldfh = select $fh; $| = 1; select $oldfh;
305 353
306 my $pid = fork; 354 my $pid = fork;
307 355
308 if (defined $pid && !$pid) { 356 if (defined $pid && !$pid) {
357 local $SIG{QUIT};
309 local $SIG{__DIE__}; 358 local $SIG{__DIE__};
359 local $SIG{__WARN__};
310 eval { 360 eval {
311 close $FH; 361 close $FH;
312 362
313 unless (eval { open_db }) { 363 unless (eval { open_db }) {
314 eval { File::Path::rmtree $DB_HOME }; 364 eval { File::Path::rmtree $DB_HOME };
327 my ($id, $type, @args) = @$req; 377 my ($id, $type, @args) = @$req;
328 my $cb = CFPlus::DB::Server->can ("do_$type") 378 my $cb = CFPlus::DB::Server->can ("do_$type")
329 or die "$type: unknown database request type\n"; 379 or die "$type: unknown database request type\n";
330 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
331 (syswrite $fh, $res) == length $res 381 (syswrite $fh, $res) == length $res
332 or die; 382 or die "DB::write: $!";
333 } 383 }
334 }; 384 };
335 385
336 my $error = $@; 386 my $error = $@;
337 387
340 undef $DB_ENV; 390 undef $DB_ENV;
341 391
342 Storable::store_fd [die => $error], $fh; 392 Storable::store_fd [die => $error], $fh;
343 }; 393 };
344 394
395 $DB_ENV->txn_checkpoint (0, 0, 0);
345 CFPlus::_exit 0; 396 CFPlus::_exit 0;
346 } 397 }
347 398
348 close $fh; 399 close $fh;
349 CFPlus::fh_nonblocking $FH, 1; 400 CFPlus::fh_nonblocking $FH, 1;
350 401
351 $CB{die} = sub { die shift }; 402 $CB{die} = sub { die shift };
352 403
353 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 404 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
354 405 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
355 sync_tick; 406 $SYNC->again unless $SYNC->is_active;
356} 407}
357 408
358sub stop { 409sub stop {
359 close $FH; 410 close $FH;
360} 411}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines