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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines