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