1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | dc::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 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 | |
15 | package dc::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 dc; |
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") { |
… | |
… | |
175 | $tilemap = $_[0]; |
175 | $tilemap = $_[0]; |
176 | delete $tilemap->{id}; |
176 | delete $tilemap->{id}; |
177 | my %maptile = reverse %$tilemap;#d# |
177 | my %maptile = reverse %$tilemap;#d# |
178 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
178 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
179 | $tilemap = { };#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# |
180 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
181 | }#d# |
181 | }#d# |
182 | }; |
182 | }; |
183 | BDB::flush; |
183 | BDB::flush; |
184 | } |
184 | } |
185 | |
185 | |
… | |
… | |
200 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
200 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
201 | } |
201 | } |
202 | |
202 | |
203 | sub sync { |
203 | sub sync { |
204 | # for debugging |
204 | # for debugging |
205 | #dc::DB::Server::req (sync => sub { }); |
205 | #DC::DB::Server::req (sync => sub { }); |
206 | dc::DB::Server::sync (); |
206 | DC::DB::Server::sync (); |
207 | } |
207 | } |
208 | |
208 | |
209 | sub unlink($$) { |
209 | sub unlink($$) { |
210 | dc::DB::Server::req (unlink => @_); |
210 | DC::DB::Server::req (unlink => @_); |
211 | } |
211 | } |
212 | |
212 | |
213 | sub read_file($$) { |
213 | sub read_file($$) { |
214 | dc::DB::Server::req (read_file => @_); |
214 | DC::DB::Server::req (read_file => @_); |
215 | } |
215 | } |
216 | |
216 | |
217 | sub write_file($$$) { |
217 | sub write_file($$$) { |
218 | dc::DB::Server::req (write_file => @_); |
218 | DC::DB::Server::req (write_file => @_); |
219 | } |
219 | } |
220 | |
220 | |
221 | sub prefetch_file($$$) { |
221 | sub prefetch_file($$$) { |
222 | dc::DB::Server::req (prefetch_file => @_); |
222 | DC::DB::Server::req (prefetch_file => @_); |
223 | } |
223 | } |
224 | |
224 | |
225 | sub logprint($$$) { |
225 | sub logprint($$$) { |
226 | dc::DB::Server::req (logprint => @_); |
226 | DC::DB::Server::req (logprint => @_); |
227 | } |
227 | } |
228 | |
228 | |
229 | package dc::DB::Server; |
229 | package DC::DB::Server; |
230 | |
230 | |
231 | use strict; |
231 | use strict; |
232 | |
232 | |
233 | use EV (); |
233 | use EV (); |
234 | use Fcntl; |
234 | use Fcntl; |
… | |
… | |
355 | |
355 | |
356 | print { $LOG_FH{$path} } "$ts $line\n" |
356 | print { $LOG_FH{$path} } "$ts $line\n" |
357 | } |
357 | } |
358 | |
358 | |
359 | sub run { |
359 | sub run { |
360 | ($FH, my $fh) = dc::socketpipe; |
360 | ($FH, my $fh) = DC::socketpipe; |
361 | |
361 | |
362 | my $oldfh = select $FH; $| = 1; select $oldfh; |
362 | my $oldfh = select $FH; $| = 1; select $oldfh; |
363 | my $oldfh = select $fh; $| = 1; select $oldfh; |
363 | my $oldfh = select $fh; $| = 1; select $oldfh; |
364 | |
364 | |
365 | my $pid = fork; |
365 | my $pid = fork; |
… | |
… | |
379 | or die "unexpected eof while reading request"; |
379 | or die "unexpected eof while reading request"; |
380 | |
380 | |
381 | $req = Storable::thaw $req; |
381 | $req = Storable::thaw $req; |
382 | |
382 | |
383 | my ($id, $type, @args) = @$req; |
383 | my ($id, $type, @args) = @$req; |
384 | my $cb = dc::DB::Server->can ("do_$type") |
384 | my $cb = DC::DB::Server->can ("do_$type") |
385 | or die "$type: unknown database request type\n"; |
385 | or die "$type: unknown database request type\n"; |
386 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
386 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
387 | (syswrite $fh, $res) == length $res |
387 | (syswrite $fh, $res) == length $res |
388 | or die "DB::write: $!"; |
388 | or die "DB::write: $!"; |
389 | } |
389 | } |
… | |
… | |
396 | }; |
396 | }; |
397 | |
397 | |
398 | warn $error |
398 | warn $error |
399 | if $error; |
399 | if $error; |
400 | |
400 | |
401 | dc::_exit 0; |
401 | DC::_exit 0; |
402 | } |
402 | } |
403 | |
403 | |
404 | close $fh; |
404 | close $fh; |
405 | dc::fh_nonblocking $FH, 1; |
405 | DC::fh_nonblocking $FH, 1; |
406 | |
406 | |
407 | $CB{die} = sub { die shift }; |
407 | $CB{die} = sub { die shift }; |
408 | |
408 | |
409 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
409 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
410 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
410 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |