1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::DB - async. database and filesystem access for cfplus |
3 | DC::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 | |
15 | package CFPlus::DB; |
15 | package DC::DB; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use Carp (); |
20 | use Carp (); |
21 | use Storable (); |
21 | use Storable (); |
22 | use Config; |
22 | use Config; |
23 | use BDB; |
23 | use BDB; |
24 | |
24 | |
25 | use CFPlus; |
25 | use DC; |
26 | |
26 | |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-" . BDB::VERSION . "-$Config{archname}"; |
27 | our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
|
|
28 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
29 | |
|
|
30 | if (!-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 | |
29 | our $DB_ENV; |
35 | our $DB_ENV; |
30 | our $DB_STATE; |
36 | our $DB_STATE; |
31 | our %DB_TABLE; |
37 | our %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 | |
197 | sub sync { |
203 | sub 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 | |
203 | sub unlink($$) { |
209 | sub unlink($$) { |
204 | CFPlus::DB::Server::req (unlink => @_); |
210 | DC::DB::Server::req (unlink => @_); |
205 | } |
211 | } |
206 | |
212 | |
207 | sub read_file($$) { |
213 | sub read_file($$) { |
208 | CFPlus::DB::Server::req (read_file => @_); |
214 | DC::DB::Server::req (read_file => @_); |
209 | } |
215 | } |
210 | |
216 | |
211 | sub write_file($$$) { |
217 | sub write_file($$$) { |
212 | CFPlus::DB::Server::req (write_file => @_); |
218 | DC::DB::Server::req (write_file => @_); |
213 | } |
219 | } |
214 | |
220 | |
215 | sub prefetch_file($$$) { |
221 | sub prefetch_file($$$) { |
216 | CFPlus::DB::Server::req (prefetch_file => @_); |
222 | DC::DB::Server::req (prefetch_file => @_); |
217 | } |
223 | } |
218 | |
224 | |
219 | sub logprint($$$) { |
225 | sub logprint($$$) { |
220 | CFPlus::DB::Server::req (logprint => @_); |
226 | DC::DB::Server::req (logprint => @_); |
221 | } |
227 | } |
222 | |
228 | |
223 | package CFPlus::DB::Server; |
229 | package DC::DB::Server; |
224 | |
230 | |
225 | use strict; |
231 | use strict; |
226 | |
232 | |
227 | use EV (); |
233 | use EV (); |
228 | use Fcntl; |
234 | use Fcntl; |
… | |
… | |
349 | |
355 | |
350 | print { $LOG_FH{$path} } "$ts $line\n" |
356 | print { $LOG_FH{$path} } "$ts $line\n" |
351 | } |
357 | } |
352 | |
358 | |
353 | sub run { |
359 | sub 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 | } |
… | |
… | |
387 | |
393 | |
388 | eval { |
394 | eval { |
389 | Storable::store_fd [die => $error], $fh; |
395 | Storable::store_fd [die => $error], $fh; |
390 | }; |
396 | }; |
391 | |
397 | |
392 | warn $error; |
398 | warn $error |
|
|
399 | if $error; |
393 | |
400 | |
394 | CFPlus::_exit 0; |
401 | DC::_exit 0; |
395 | } |
402 | } |
396 | |
403 | |
397 | close $fh; |
404 | close $fh; |
398 | CFPlus::fh_nonblocking $FH, 1; |
405 | DC::fh_nonblocking $FH, 1; |
399 | |
406 | |
400 | $CB{die} = sub { die shift }; |
407 | $CB{die} = sub { die shift }; |
401 | |
408 | |
402 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
409 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
403 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
410 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |