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 $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
27 | our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
28 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
29 | |
29 | |
30 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { |
30 | if (!-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 | |
|
|
35 | BDB::max_poll_time 0.03; |
34 | |
36 | |
35 | our $DB_ENV; |
37 | our $DB_ENV; |
36 | our $DB_STATE; |
38 | our $DB_STATE; |
37 | our %DB_TABLE; |
39 | our %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 | |
169 | sub get_tile_id_sync($) { |
171 | sub 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 | CFPlus::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 | |
203 | sub sync { |
191 | sub sync { |
204 | # for debugging |
192 | # for debugging |
205 | #CFPlus::DB::Server::req (sync => sub { }); |
193 | #DC::DB::Server::req (sync => sub { }); |
206 | CFPlus::DB::Server::sync (); |
194 | DC::DB::Server::sync (); |
207 | } |
195 | } |
208 | |
196 | |
209 | sub unlink($$) { |
197 | sub unlink($$) { |
210 | CFPlus::DB::Server::req (unlink => @_); |
198 | DC::DB::Server::req (unlink => @_); |
211 | } |
199 | } |
212 | |
200 | |
213 | sub read_file($$) { |
201 | sub read_file($$) { |
214 | CFPlus::DB::Server::req (read_file => @_); |
202 | DC::DB::Server::req (read_file => @_); |
215 | } |
203 | } |
216 | |
204 | |
217 | sub write_file($$$) { |
205 | sub write_file($$$) { |
218 | CFPlus::DB::Server::req (write_file => @_); |
206 | DC::DB::Server::req (write_file => @_); |
219 | } |
207 | } |
220 | |
208 | |
221 | sub prefetch_file($$$) { |
209 | sub prefetch_file($$$) { |
222 | CFPlus::DB::Server::req (prefetch_file => @_); |
210 | DC::DB::Server::req (prefetch_file => @_); |
223 | } |
211 | } |
224 | |
212 | |
225 | sub logprint($$$) { |
213 | sub logprint($$$) { |
226 | CFPlus::DB::Server::req (logprint => @_); |
214 | DC::DB::Server::req (logprint => @_); |
227 | } |
215 | } |
228 | |
216 | |
|
|
217 | ############################################################################# |
|
|
218 | |
|
|
219 | # fetch the full face table first |
|
|
220 | unless ($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 | |
229 | package CFPlus::DB::Server; |
232 | package DC::DB::Server; |
230 | |
233 | |
231 | use strict; |
234 | use strict; |
232 | |
235 | |
233 | use EV (); |
236 | use EV (); |
234 | use Fcntl; |
237 | use Fcntl; |
… | |
… | |
355 | |
358 | |
356 | print { $LOG_FH{$path} } "$ts $line\n" |
359 | print { $LOG_FH{$path} } "$ts $line\n" |
357 | } |
360 | } |
358 | |
361 | |
359 | sub run { |
362 | sub run { |
360 | ($FH, my $fh) = CFPlus::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 = CFPlus::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 | CFPlus::_exit 0; |
404 | DC::_exit 0; |
402 | } |
405 | } |
403 | |
406 | |
404 | close $fh; |
407 | close $fh; |
405 | CFPlus::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; |