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.32 by root, Sat Jan 19 04:49:37 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;
42BDB::max_parallel 1;
34 43
35our $DB_ENV; 44our $DB_ENV;
36our $DB_STATE; 45our $DB_STATE;
37our %DB_TABLE; 46our %DB_TABLE;
38 47
166 die "maximum number of transaction retries reached - database problems?"; 175 die "maximum number of transaction retries reached - database problems?";
167} 176}
168 177
169sub get_tile_id_sync($) { 178sub get_tile_id_sync($) {
170 my ($name) = @_; 179 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 180
186 $tilemap->{$name} ||= do { 181 $tilemap->{$name} ||= do {
187 my $id; 182 my $id;
188 do_get_tile_id $name, sub { 183 do_get_tile_id $name, sub {
189 $id = $_[0]; 184 $id = $_[0];
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 195 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 196}
202 197
203sub sync { 198sub sync {
204 # for debugging 199 # for debugging
205 #dc::DB::Server::req (sync => sub { }); 200 #DC::DB::Server::req (sync => sub { });
206 dc::DB::Server::sync (); 201 DC::DB::Server::sync ();
207} 202}
208 203
209sub unlink($$) { 204sub unlink($$) {
210 dc::DB::Server::req (unlink => @_); 205 DC::DB::Server::req (unlink => @_);
211} 206}
212 207
213sub read_file($$) { 208sub read_file($$) {
214 dc::DB::Server::req (read_file => @_); 209 DC::DB::Server::req (read_file => @_);
215} 210}
216 211
217sub write_file($$$) { 212sub write_file($$$) {
218 dc::DB::Server::req (write_file => @_); 213 DC::DB::Server::req (write_file => @_);
219} 214}
220 215
221sub prefetch_file($$$) { 216sub prefetch_file($$$) {
222 dc::DB::Server::req (prefetch_file => @_); 217 DC::DB::Server::req (prefetch_file => @_);
223} 218}
224 219
225sub logprint($$$) { 220sub logprint($$$) {
226 dc::DB::Server::req (logprint => @_); 221 DC::DB::Server::req (logprint => @_);
227} 222}
228 223
224#############################################################################
225
226# fetch the full face table first
227unless ($tilemap) {
228 do_table facemap => sub {
229 $tilemap = $_[0];
230 delete $tilemap->{id};
231 my %maptile = reverse %$tilemap;#d#
232 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
233 $tilemap = { };#d#
234 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
235 }#d#
236 };
237}
238
229package dc::DB::Server; 239package DC::DB::Server;
230 240
231use strict; 241use strict;
232 242
233use EV (); 243use EV ();
234use Fcntl; 244use Fcntl;
355 365
356 print { $LOG_FH{$path} } "$ts $line\n" 366 print { $LOG_FH{$path} } "$ts $line\n"
357} 367}
358 368
359sub run { 369sub run {
360 ($FH, my $fh) = dc::socketpipe; 370 ($FH, my $fh) = DC::socketpipe;
361 371
362 my $oldfh = select $FH; $| = 1; select $oldfh; 372 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 373 my $oldfh = select $fh; $| = 1; select $oldfh;
364 374
365 my $pid = fork; 375 my $pid = fork;
366 376
367 if (defined $pid && !$pid) { 377 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 378 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 379 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 380 local $SIG{__WARN__};
371 eval { 381 eval {
372 close $FH; 382 close $FH;
373 383
379 or die "unexpected eof while reading request"; 389 or die "unexpected eof while reading request";
380 390
381 $req = Storable::thaw $req; 391 $req = Storable::thaw $req;
382 392
383 my ($id, $type, @args) = @$req; 393 my ($id, $type, @args) = @$req;
384 my $cb = dc::DB::Server->can ("do_$type") 394 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 395 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 396 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 397 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 398 or die "DB::write: $!";
389 } 399 }
396 }; 406 };
397 407
398 warn $error 408 warn $error
399 if $error; 409 if $error;
400 410
401 dc::_exit 0; 411 DC::_exit 0;
402 } 412 }
403 413
404 close $fh; 414 close $fh;
405 dc::fh_nonblocking $FH, 1; 415 DC::fh_nonblocking $FH, 1;
406 416
407 $CB{die} = sub { die shift }; 417 $CB{die} = sub { die shift };
408 418
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 419 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 420 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines