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