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.29 by root, Wed Dec 26 21:03:21 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 = "$Crossfire::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}
28 34
29our $DB_ENV; 35our $DB_ENV;
30our $DB_STATE; 36our $DB_STATE;
31our %DB_TABLE; 37our %DB_TABLE;
32 38
169 $tilemap = $_[0]; 175 $tilemap = $_[0];
170 delete $tilemap->{id}; 176 delete $tilemap->{id};
171 my %maptile = reverse %$tilemap;#d# 177 my %maptile = reverse %$tilemap;#d#
172 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# 178 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
173 $tilemap = { };#d# 179 $tilemap = { };#d#
174 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# 180 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
175 }#d# 181 }#d#
176 }; 182 };
177 BDB::flush; 183 BDB::flush;
178 } 184 }
179 185
194 "$DB_HOME/res-data-" . unpack "H*", $_[0] 200 "$DB_HOME/res-data-" . unpack "H*", $_[0]
195} 201}
196 202
197sub sync { 203sub sync {
198 # for debugging 204 # for debugging
199 #CFPlus::DB::Server::req (sync => sub { }); 205 #DC::DB::Server::req (sync => sub { });
200 CFPlus::DB::Server::sync (); 206 DC::DB::Server::sync ();
201} 207}
202 208
203sub unlink($$) { 209sub unlink($$) {
204 CFPlus::DB::Server::req (unlink => @_); 210 DC::DB::Server::req (unlink => @_);
205} 211}
206 212
207sub read_file($$) { 213sub read_file($$) {
208 CFPlus::DB::Server::req (read_file => @_); 214 DC::DB::Server::req (read_file => @_);
209} 215}
210 216
211sub write_file($$$) { 217sub write_file($$$) {
212 CFPlus::DB::Server::req (write_file => @_); 218 DC::DB::Server::req (write_file => @_);
213} 219}
214 220
215sub prefetch_file($$$) { 221sub prefetch_file($$$) {
216 CFPlus::DB::Server::req (prefetch_file => @_); 222 DC::DB::Server::req (prefetch_file => @_);
217} 223}
218 224
219sub logprint($$$) { 225sub logprint($$$) {
220 CFPlus::DB::Server::req (logprint => @_); 226 DC::DB::Server::req (logprint => @_);
221} 227}
222 228
223package CFPlus::DB::Server; 229package DC::DB::Server;
224 230
225use strict; 231use strict;
226 232
227use EV (); 233use EV ();
228use Fcntl; 234use Fcntl;
349 355
350 print { $LOG_FH{$path} } "$ts $line\n" 356 print { $LOG_FH{$path} } "$ts $line\n"
351} 357}
352 358
353sub run { 359sub run {
354 ($FH, my $fh) = CFPlus::socketpipe; 360 ($FH, my $fh) = DC::socketpipe;
355 361
356 my $oldfh = select $FH; $| = 1; select $oldfh; 362 my $oldfh = select $FH; $| = 1; select $oldfh;
357 my $oldfh = select $fh; $| = 1; select $oldfh; 363 my $oldfh = select $fh; $| = 1; select $oldfh;
358 364
359 my $pid = fork; 365 my $pid = fork;
373 or die "unexpected eof while reading request"; 379 or die "unexpected eof while reading request";
374 380
375 $req = Storable::thaw $req; 381 $req = Storable::thaw $req;
376 382
377 my ($id, $type, @args) = @$req; 383 my ($id, $type, @args) = @$req;
378 my $cb = CFPlus::DB::Server->can ("do_$type") 384 my $cb = DC::DB::Server->can ("do_$type")
379 or die "$type: unknown database request type\n"; 385 or die "$type: unknown database request type\n";
380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
381 (syswrite $fh, $res) == length $res 387 (syswrite $fh, $res) == length $res
382 or die "DB::write: $!"; 388 or die "DB::write: $!";
383 } 389 }
390 }; 396 };
391 397
392 warn $error 398 warn $error
393 if $error; 399 if $error;
394 400
395 CFPlus::_exit 0; 401 DC::_exit 0;
396 } 402 }
397 403
398 close $fh; 404 close $fh;
399 CFPlus::fh_nonblocking $FH, 1; 405 DC::fh_nonblocking $FH, 1;
400 406
401 $CB{die} = sub { die shift }; 407 $CB{die} = sub { die shift };
402 408
403 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
404 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines