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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines