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.28 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.31 by root, Thu Jan 10 23:02:19 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3dc::DB - async. database and filesystem access for cfplus 3DC::DB - async. database and filesystem access for deliantra
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use dc::DB; 7 use DC::DB;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package dc::DB; 15package DC::DB;
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use Storable (); 21use Storable ();
22use Config; 22use Config;
23use BDB; 23use BDB;
24 24
25use dc; 25use DC;
26 26
27our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}";
28our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 29our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29 30
31if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") {
32 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
33 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
34}
35
30if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { 36if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") {
31 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
32 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$DBDIR to $DB_HOME\n"; 38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n";
33} 39}
40
41BDB::max_poll_time 0.03;
34 42
35our $DB_ENV; 43our $DB_ENV;
36our $DB_STATE; 44our $DB_STATE;
37our %DB_TABLE; 45our %DB_TABLE;
38 46
166 die "maximum number of transaction retries reached - database problems?"; 174 die "maximum number of transaction retries reached - database problems?";
167} 175}
168 176
169sub get_tile_id_sync($) { 177sub get_tile_id_sync($) {
170 my ($name) = @_; 178 my ($name) = @_;
171
172 # fetch the full face table first
173 unless ($tilemap) {
174 do_table facemap => sub {
175 $tilemap = $_[0];
176 delete $tilemap->{id};
177 my %maptile = reverse %$tilemap;#d#
178 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
179 $tilemap = { };#d#
180 dc::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
181 }#d#
182 };
183 BDB::flush;
184 }
185 179
186 $tilemap->{$name} ||= do { 180 $tilemap->{$name} ||= do {
187 my $id; 181 my $id;
188 do_get_tile_id $name, sub { 182 do_get_tile_id $name, sub {
189 $id = $_[0]; 183 $id = $_[0];
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 194 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 195}
202 196
203sub sync { 197sub sync {
204 # for debugging 198 # for debugging
205 #dc::DB::Server::req (sync => sub { }); 199 #DC::DB::Server::req (sync => sub { });
206 dc::DB::Server::sync (); 200 DC::DB::Server::sync ();
207} 201}
208 202
209sub unlink($$) { 203sub unlink($$) {
210 dc::DB::Server::req (unlink => @_); 204 DC::DB::Server::req (unlink => @_);
211} 205}
212 206
213sub read_file($$) { 207sub read_file($$) {
214 dc::DB::Server::req (read_file => @_); 208 DC::DB::Server::req (read_file => @_);
215} 209}
216 210
217sub write_file($$$) { 211sub write_file($$$) {
218 dc::DB::Server::req (write_file => @_); 212 DC::DB::Server::req (write_file => @_);
219} 213}
220 214
221sub prefetch_file($$$) { 215sub prefetch_file($$$) {
222 dc::DB::Server::req (prefetch_file => @_); 216 DC::DB::Server::req (prefetch_file => @_);
223} 217}
224 218
225sub logprint($$$) { 219sub logprint($$$) {
226 dc::DB::Server::req (logprint => @_); 220 DC::DB::Server::req (logprint => @_);
227} 221}
228 222
223#############################################################################
224
225# fetch the full face table first
226unless ($tilemap) {
227 do_table facemap => sub {
228 $tilemap = $_[0];
229 delete $tilemap->{id};
230 my %maptile = reverse %$tilemap;#d#
231 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
232 $tilemap = { };#d#
233 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
234 }#d#
235 };
236}
237
229package dc::DB::Server; 238package DC::DB::Server;
230 239
231use strict; 240use strict;
232 241
233use EV (); 242use EV ();
234use Fcntl; 243use Fcntl;
355 364
356 print { $LOG_FH{$path} } "$ts $line\n" 365 print { $LOG_FH{$path} } "$ts $line\n"
357} 366}
358 367
359sub run { 368sub run {
360 ($FH, my $fh) = dc::socketpipe; 369 ($FH, my $fh) = DC::socketpipe;
361 370
362 my $oldfh = select $FH; $| = 1; select $oldfh; 371 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 372 my $oldfh = select $fh; $| = 1; select $oldfh;
364 373
365 my $pid = fork; 374 my $pid = fork;
366 375
367 if (defined $pid && !$pid) { 376 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 377 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 378 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 379 local $SIG{__WARN__};
371 eval { 380 eval {
372 close $FH; 381 close $FH;
373 382
379 or die "unexpected eof while reading request"; 388 or die "unexpected eof while reading request";
380 389
381 $req = Storable::thaw $req; 390 $req = Storable::thaw $req;
382 391
383 my ($id, $type, @args) = @$req; 392 my ($id, $type, @args) = @$req;
384 my $cb = dc::DB::Server->can ("do_$type") 393 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 394 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 395 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 396 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 397 or die "DB::write: $!";
389 } 398 }
396 }; 405 };
397 406
398 warn $error 407 warn $error
399 if $error; 408 if $error;
400 409
401 dc::_exit 0; 410 DC::_exit 0;
402 } 411 }
403 412
404 close $fh; 413 close $fh;
405 dc::fh_nonblocking $FH, 1; 414 DC::fh_nonblocking $FH, 1;
406 415
407 $CB{die} = sub { die shift }; 416 $CB{die} = sub { die shift };
408 417
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 418 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 419 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines