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 = "$Crossfire::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 | |
33 | sub open_db { |
48 | sub try_open_db { |
34 | mkdir $DB_HOME, 0777; |
49 | mkdir $DB_HOME, 0777; |
35 | |
50 | |
36 | $DB_ENV = db_env_create; |
51 | $DB_ENV = db_env_create; |
37 | |
52 | |
38 | $DB_ENV->set_errfile (\*STDERR); |
53 | $DB_ENV->set_errfile (\*STDERR); |
… | |
… | |
69 | } |
84 | } |
70 | } |
85 | } |
71 | |
86 | |
72 | ############################################################################# |
87 | ############################################################################# |
73 | |
88 | |
74 | unless (eval { open_db }) { |
89 | our $WATCHER; |
75 | warn "$@";#d# |
90 | our $SYNC; |
76 | eval { File::Path::rmtree $DB_HOME }; |
|
|
77 | open_db; |
|
|
78 | } |
|
|
79 | |
|
|
80 | our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
81 | |
|
|
82 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
83 | $_[0]->stop; |
|
|
84 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
85 | }; |
|
|
86 | |
|
|
87 | our $tilemap; |
91 | our $facemap; |
88 | |
92 | |
89 | sub exists($$$) { |
93 | sub exists($$$) { |
90 | my ($db, $key, $cb) = @_; |
94 | my ($db, $key, $cb) = @_; |
91 | |
95 | |
92 | my $data; |
96 | my $data; |
… | |
… | |
104 | }; |
108 | }; |
105 | } |
109 | } |
106 | |
110 | |
107 | sub put($$$$) { |
111 | sub put($$$$) { |
108 | my ($db, $key, $data, $cb) = @_; |
112 | my ($db, $key, $data, $cb) = @_; |
|
|
113 | warn "put $key ",(length $data),"\n";#d# |
109 | |
114 | |
110 | db_put table $db, undef, $key, $data, 0, sub { |
115 | db_put table $db, undef, $key, $data, 0, sub { |
111 | $cb->($!); |
116 | $cb->($!); |
112 | $SYNC->again unless $SYNC->is_active; |
117 | $SYNC->again unless $SYNC->is_active; |
113 | }; |
118 | }; |
… | |
… | |
135 | |
140 | |
136 | my $table = table "facemap"; |
141 | my $table = table "facemap"; |
137 | my $id; |
142 | my $id; |
138 | |
143 | |
139 | db_get $table, undef, $name, $id, 0; |
144 | db_get $table, undef, $name, $id, 0; |
140 | return $cb->($id) unless $!; |
145 | $! or return $cb->($id); |
141 | |
146 | |
142 | for (1..100) { |
147 | for (1..100) { |
143 | my $txn = $DB_ENV->txn_begin; |
148 | my $txn = $DB_ENV->txn_begin; |
144 | db_get $table, $txn, id => $id, 0; |
149 | db_get $table, $txn, id => $id, 0; |
145 | |
150 | |
… | |
… | |
148 | ++$id; |
153 | ++$id; |
149 | |
154 | |
150 | db_put $table, $txn, id => $id, 0; |
155 | db_put $table, $txn, id => $id, 0; |
151 | db_txn_finish $txn; |
156 | db_txn_finish $txn; |
152 | |
157 | |
|
|
158 | unless ($!) { |
|
|
159 | db_put $table, undef, $name => $id; |
153 | $SYNC->again unless $SYNC->is_active; |
160 | $SYNC->again unless $SYNC->is_active; |
154 | |
|
|
155 | return $cb->($id) unless $!; |
161 | return $cb->($id); |
|
|
162 | } |
156 | |
163 | |
157 | select undef, undef, undef, 0.01 * rand; |
164 | select undef, undef, undef, 0.01 * rand; |
158 | } |
165 | } |
159 | |
166 | |
160 | die "maximum number of transaction retries reached - database problems?"; |
167 | die "maximum number of transaction retries reached - database problems?"; |
161 | } |
168 | } |
162 | |
169 | |
163 | sub get_tile_id_sync($) { |
170 | sub get_tile_id_sync($) { |
164 | my ($name) = @_; |
171 | my ($name) = @_; |
165 | |
172 | |
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 | $tilemap->{$name} ||= do { |
173 | $facemap->{$name} ||= do { |
181 | my $id; |
174 | my $id; |
182 | do_get_tile_id $name, sub { |
175 | do_get_tile_id $name, sub { |
183 | $id = $_[0]; |
176 | $id = $_[0]; |
184 | }; |
177 | }; |
185 | BDB::flush; |
178 | BDB::flush; |
… | |
… | |
194 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
187 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
195 | } |
188 | } |
196 | |
189 | |
197 | sub sync { |
190 | sub sync { |
198 | # for debugging |
191 | # for debugging |
199 | #CFPlus::DB::Server::req (sync => sub { }); |
192 | #DC::DB::Server::req (sync => sub { }); |
200 | CFPlus::DB::Server::sync (); |
193 | DC::DB::Server::sync (); |
201 | } |
194 | } |
202 | |
195 | |
203 | sub unlink($$) { |
196 | sub unlink($$) { |
204 | CFPlus::DB::Server::req (unlink => @_); |
197 | DC::DB::Server::req (unlink => @_); |
205 | } |
198 | } |
206 | |
199 | |
207 | sub read_file($$) { |
200 | sub read_file($$) { |
208 | CFPlus::DB::Server::req (read_file => @_); |
201 | DC::DB::Server::req (read_file => @_); |
209 | } |
202 | } |
210 | |
203 | |
211 | sub write_file($$$) { |
204 | sub write_file($$$) { |
212 | CFPlus::DB::Server::req (write_file => @_); |
205 | DC::DB::Server::req (write_file => @_); |
213 | } |
206 | } |
214 | |
207 | |
215 | sub prefetch_file($$$) { |
208 | sub prefetch_file($$$) { |
216 | CFPlus::DB::Server::req (prefetch_file => @_); |
209 | DC::DB::Server::req (prefetch_file => @_); |
217 | } |
210 | } |
218 | |
211 | |
219 | sub logprint($$$) { |
212 | sub logprint($$$) { |
220 | CFPlus::DB::Server::req (logprint => @_); |
213 | DC::DB::Server::req (logprint => @_); |
221 | } |
214 | } |
222 | |
215 | |
|
|
216 | ############################################################################# |
|
|
217 | |
223 | package CFPlus::DB::Server; |
218 | package DC::DB::Server; |
224 | |
219 | |
225 | use strict; |
220 | use strict; |
226 | |
221 | |
227 | use EV (); |
222 | use EV (); |
228 | use Fcntl; |
223 | use Fcntl; |
… | |
… | |
349 | |
344 | |
350 | print { $LOG_FH{$path} } "$ts $line\n" |
345 | print { $LOG_FH{$path} } "$ts $line\n" |
351 | } |
346 | } |
352 | |
347 | |
353 | sub run { |
348 | sub run { |
354 | ($FH, my $fh) = CFPlus::socketpipe; |
349 | ($FH, my $fh) = DC::socketpipe; |
355 | |
350 | |
356 | my $oldfh = select $FH; $| = 1; select $oldfh; |
351 | my $oldfh = select $FH; $| = 1; select $oldfh; |
357 | my $oldfh = select $fh; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
358 | |
353 | |
359 | my $pid = fork; |
354 | my $pid = fork; |
360 | |
355 | |
361 | if (defined $pid && !$pid) { |
356 | if (defined $pid && !$pid) { |
362 | local $SIG{QUIT}; |
357 | local $SIG{QUIT} = "IGNORE"; |
363 | local $SIG{__DIE__}; |
358 | local $SIG{__DIE__}; |
364 | local $SIG{__WARN__}; |
359 | local $SIG{__WARN__}; |
365 | eval { |
360 | eval { |
366 | close $FH; |
361 | close $FH; |
367 | |
362 | |
… | |
… | |
373 | or die "unexpected eof while reading request"; |
368 | or die "unexpected eof while reading request"; |
374 | |
369 | |
375 | $req = Storable::thaw $req; |
370 | $req = Storable::thaw $req; |
376 | |
371 | |
377 | my ($id, $type, @args) = @$req; |
372 | my ($id, $type, @args) = @$req; |
378 | my $cb = CFPlus::DB::Server->can ("do_$type") |
373 | my $cb = DC::DB::Server->can ("do_$type") |
379 | or die "$type: unknown database request type\n"; |
374 | or die "$type: unknown database request type\n"; |
380 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
375 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
381 | (syswrite $fh, $res) == length $res |
376 | (syswrite $fh, $res) == length $res |
382 | or die "DB::write: $!"; |
377 | or die "DB::write: $!"; |
383 | } |
378 | } |
… | |
… | |
390 | }; |
385 | }; |
391 | |
386 | |
392 | warn $error |
387 | warn $error |
393 | if $error; |
388 | if $error; |
394 | |
389 | |
395 | CFPlus::_exit 0; |
390 | DC::_exit 0; |
396 | } |
391 | } |
397 | |
392 | |
398 | close $fh; |
393 | close $fh; |
399 | CFPlus::fh_nonblocking $FH, 1; |
394 | DC::fh_nonblocking $FH, 1; |
400 | |
395 | |
401 | $CB{die} = sub { die shift }; |
396 | $CB{die} = sub { die shift }; |
402 | |
397 | |
403 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
398 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
404 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
399 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
… | |
… | |
406 | |
401 | |
407 | sub stop { |
402 | sub stop { |
408 | close $FH; |
403 | close $FH; |
409 | } |
404 | } |
410 | |
405 | |
|
|
406 | package DC::DB; |
|
|
407 | |
|
|
408 | sub open_db { |
|
|
409 | unless (eval { try_open_db }) { |
|
|
410 | warn "$@";#d# |
|
|
411 | eval { File::Path::rmtree $DB_HOME }; |
|
|
412 | try_open_db; |
|
|
413 | } |
|
|
414 | |
|
|
415 | # fetch the full face table first |
|
|
416 | unless ($facemap) { |
|
|
417 | do_table facemap => sub { |
|
|
418 | $facemap = $_[0]; |
|
|
419 | delete $facemap->{id}; |
|
|
420 | my %maptile = reverse %$facemap;#d# |
|
|
421 | if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d# |
|
|
422 | $facemap = { };#d# |
|
|
423 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
424 | }#d# |
|
|
425 | }; |
|
|
426 | } |
|
|
427 | |
|
|
428 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
429 | $SYNC = EV::timer_ns 0, 60, sub { |
|
|
430 | warn "SYNC\n";#d# |
|
|
431 | $_[0]->stop; |
|
|
432 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
433 | }; |
|
|
434 | } |
|
|
435 | |
|
|
436 | END { |
|
|
437 | %DB_TABLE = (); |
|
|
438 | undef $DB_ENV; |
|
|
439 | } |
|
|
440 | |
411 | 1; |
441 | 1; |
412 | |
442 | |
413 | =back |
443 | =back |
414 | |
444 | |
415 | =head1 AUTHOR |
445 | =head1 AUTHOR |