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.25 by root, Tue Dec 11 03:38:36 2007 UTC vs.
Revision 1.31 by root, Thu Jan 10 23:02:19 2008 UTC

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