1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::DB - async. database access for cfplus |
3 | CFPlus::DB - async. database and filesystem access for cfplus |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFPlus::DB; |
7 | use CFPlus::DB; |
8 | |
8 | |
… | |
… | |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use Carp (); |
20 | use Carp (); |
21 | use AnyEvent (); |
|
|
22 | use Storable (); # finally |
21 | use Storable (); |
|
|
22 | use Config; |
|
|
23 | |
|
|
24 | use CFPlus; |
|
|
25 | |
|
|
26 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
|
|
27 | |
|
|
28 | sub path_of_res($) { |
|
|
29 | utf8::downgrade $_[0]; # bug in unpack "H*" |
|
|
30 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
|
|
31 | } |
23 | |
32 | |
24 | sub sync { |
33 | sub sync { |
25 | # for debugging |
34 | # for debugging |
26 | #CFPlus::DB::Server::req (sync => sub { }); |
35 | #CFPlus::DB::Server::req (sync => sub { }); |
27 | CFPlus::DB::Server::sync (); |
36 | CFPlus::DB::Server::sync (); |
28 | } |
37 | } |
29 | |
38 | |
|
|
39 | sub exists($$$) { |
|
|
40 | CFPlus::DB::Server::req (exists => @_); |
|
|
41 | } |
|
|
42 | |
30 | sub get($$$) { |
43 | sub get($$$) { |
31 | CFPlus::DB::Server::req (get => @_); |
44 | CFPlus::DB::Server::req (get => @_); |
32 | } |
45 | } |
33 | |
46 | |
34 | sub put($$$$) { |
47 | sub put($$$$) { |
35 | CFPlus::DB::Server::req (put => @_); |
48 | CFPlus::DB::Server::req (put => @_); |
36 | } |
49 | } |
37 | |
50 | |
|
|
51 | sub unlink($$) { |
|
|
52 | CFPlus::DB::Server::req (unlink => @_); |
|
|
53 | } |
|
|
54 | |
|
|
55 | sub read_file($$) { |
|
|
56 | CFPlus::DB::Server::req (read_file => @_); |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub write_file($$$) { |
|
|
60 | CFPlus::DB::Server::req (write_file => @_); |
|
|
61 | } |
|
|
62 | |
|
|
63 | sub prefetch_file($$$) { |
|
|
64 | CFPlus::DB::Server::req (prefetch_file => @_); |
|
|
65 | } |
|
|
66 | |
|
|
67 | sub logprint($$$) { |
|
|
68 | CFPlus::DB::Server::req (logprint => @_); |
|
|
69 | } |
|
|
70 | |
38 | our $tilemap; |
71 | our $tilemap; |
39 | |
72 | |
40 | sub get_tile_id_sync($) { |
73 | sub get_tile_id_sync($) { |
41 | my ($hash) = @_; |
74 | my ($name) = @_; |
42 | |
75 | |
43 | # fetch the full face table first |
76 | # fetch the full face table first |
44 | unless ($tilemap) { |
77 | unless ($tilemap) { |
45 | CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] }); |
78 | CFPlus::DB::Server::req (table => facemap => sub { |
|
|
79 | $tilemap = $_[0]; |
|
|
80 | delete $tilemap->{id}; |
|
|
81 | my %maptile = reverse %$tilemap;#d# |
|
|
82 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
|
|
83 | $tilemap = { };#d# |
|
|
84 | CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
85 | }#d# |
|
|
86 | }); |
46 | sync; |
87 | sync; |
47 | } |
88 | } |
48 | |
89 | |
49 | $tilemap->{$hash} ||= do { |
90 | $tilemap->{$name} ||= do { |
50 | my $id; |
91 | my $id; |
51 | CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); |
92 | CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); |
52 | sync; |
93 | sync; |
53 | $id |
94 | $id |
54 | } |
95 | } |
55 | } |
96 | } |
56 | |
97 | |
… | |
… | |
59 | use strict; |
100 | use strict; |
60 | |
101 | |
61 | use Fcntl; |
102 | use Fcntl; |
62 | use BerkeleyDB; |
103 | use BerkeleyDB; |
63 | |
104 | |
64 | our $DB_HOME = "$Crossfire::VARDIR/cfplus"; |
|
|
65 | our $DB_ENV; |
105 | our $DB_ENV; |
66 | our $DB_STATE; |
106 | our $DB_STATE; |
67 | our %DB_TABLE; |
107 | our %DB_TABLE; |
68 | |
108 | |
69 | sub open_db { |
109 | sub open_db { |
… | |
… | |
77 | -Cachesize => 8_000_000, |
117 | -Cachesize => 8_000_000, |
78 | -ErrFile => "$DB_HOME/errorlog.txt", |
118 | -ErrFile => "$DB_HOME/errorlog.txt", |
79 | # -ErrPrefix => "DATABASE", |
119 | # -ErrPrefix => "DATABASE", |
80 | -Verbose => 1, |
120 | -Verbose => 1, |
81 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
121 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
82 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
122 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
83 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
123 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
84 | |
124 | |
85 | 1 |
125 | 1 |
86 | } |
126 | } |
87 | |
127 | |
… | |
… | |
95 | -Env => $DB_ENV, |
135 | -Env => $DB_ENV, |
96 | -Filename => $table, |
136 | -Filename => $table, |
97 | # -Filename => "database", |
137 | # -Filename => "database", |
98 | # -Subname => $table, |
138 | # -Subname => $table, |
99 | -Property => DB_CHKSUM, |
139 | -Property => DB_CHKSUM, |
100 | -Flags => DB_CREATE | DB_UPGRADE, |
140 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
101 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
141 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
102 | } |
142 | } |
103 | } |
143 | } |
104 | |
|
|
105 | our $SYNC_INTERVAL = 6; |
|
|
106 | |
144 | |
107 | our %CB; |
145 | our %CB; |
108 | our $FH; |
146 | our $FH; |
109 | our $ID = "aaa0"; |
147 | our $ID = "aaa0"; |
110 | our ($fh_r_watcher, $fh_w_watcher); |
148 | our ($fh_r_watcher, $fh_w_watcher); |
111 | our $sync_timer; |
149 | our $sync_timer; |
112 | our $write_buf; |
150 | our $write_buf; |
113 | our $read_buf; |
151 | our $read_buf; |
114 | |
152 | |
|
|
153 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
154 | $_[0]->stop; |
|
|
155 | CFPlus::DB::Server::req (sync => sub { }); |
|
|
156 | }; |
|
|
157 | |
115 | sub fh_write { |
158 | sub fh_write { |
116 | my $len = syswrite $FH, $write_buf; |
159 | my $len = syswrite $FH, $write_buf; |
117 | |
160 | |
118 | substr $write_buf, 0, $len, ""; |
161 | substr $write_buf, 0, $len, ""; |
119 | |
162 | |
120 | undef $fh_w_watcher |
163 | $fh_w_watcher->stop |
121 | unless length $write_buf; |
164 | unless length $write_buf; |
122 | } |
165 | } |
123 | |
166 | |
124 | sub fh_read { |
167 | sub fh_read { |
125 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
168 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
161 | |
204 | |
162 | my $id = ++$ID; |
205 | my $id = ++$ID; |
163 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
206 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
164 | $CB{$id} = $cb; |
207 | $CB{$id} = $cb; |
165 | |
208 | |
166 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
209 | $fh_w_watcher->start; |
167 | } |
210 | $SYNC->again unless $SYNC->is_active; |
168 | |
|
|
169 | sub sync_tick { |
|
|
170 | req "sync", sub { }; |
|
|
171 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
172 | } |
211 | } |
173 | |
212 | |
174 | sub do_sync { |
213 | sub do_sync { |
175 | $DB_ENV->txn_checkpoint (0, 0, 0); |
214 | $DB_ENV->txn_checkpoint (0, 0, 0); |
176 | () |
215 | () |
177 | } |
216 | } |
178 | |
217 | |
|
|
218 | sub do_exists { |
|
|
219 | my ($db, $key) = @_; |
|
|
220 | |
|
|
221 | utf8::downgrade $key; |
|
|
222 | my $data; |
|
|
223 | (table $db)->db_get ($key, $data) == 0 |
|
|
224 | ? length $data |
|
|
225 | : () |
|
|
226 | } |
|
|
227 | |
179 | sub do_get { |
228 | sub do_get { |
180 | my ($db, $key) = @_; |
229 | my ($db, $key) = @_; |
181 | |
230 | |
|
|
231 | utf8::downgrade $key; |
182 | my $data; |
232 | my $data; |
183 | (table $db)->db_get ($key, $data) == 0 |
233 | (table $db)->db_get ($key, $data) == 0 |
184 | ? $data |
234 | ? $data |
185 | : () |
235 | : () |
186 | } |
236 | } |
187 | |
237 | |
188 | sub do_put { |
238 | sub do_put { |
189 | my ($db, $key, $data) = @_; |
239 | my ($db, $key, $data) = @_; |
190 | |
240 | |
|
|
241 | utf8::downgrade $key; |
|
|
242 | utf8::downgrade $data; |
191 | (table $db)->db_put ($key => $data) |
243 | (table $db)->db_put ($key => $data) |
192 | } |
244 | } |
193 | |
245 | |
194 | sub do_table { |
246 | sub do_table { |
195 | my ($db) = @_; |
247 | my ($db) = @_; |
… | |
… | |
203 | |
255 | |
204 | \%kv |
256 | \%kv |
205 | } |
257 | } |
206 | |
258 | |
207 | sub do_get_tile_id { |
259 | sub do_get_tile_id { |
208 | my ($hash) = @_; |
260 | my ($name) = @_; |
209 | |
261 | |
210 | my $id; |
262 | my $id; |
211 | my $table = table "facemap"; |
263 | my $table = table "facemap"; |
212 | |
264 | |
213 | return $id |
265 | return $id |
214 | if $table->db_get ($hash, $id) == 0; |
266 | if $table->db_get ($name, $id) == 0; |
215 | |
267 | |
216 | for (1..100) { |
268 | for (1..100) { |
217 | my $txn = $DB_ENV->txn_begin; |
269 | my $txn = $DB_ENV->txn_begin; |
218 | my $status = $table->db_get (id => $id); |
270 | my $status = $table->db_get (id => $id); |
219 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
271 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
220 | $id = ($id || 64) + 1; |
272 | $id = ($id || 64) + 1; |
221 | if ($table->db_put (id => $id) == 0 |
273 | if ($table->db_put (id => $id) == 0 |
222 | && $table->db_put ($hash => $id) == 0) { |
274 | && $table->db_put ($name => $id) == 0) { |
223 | $txn->txn_commit; |
275 | $txn->txn_commit; |
224 | |
276 | |
225 | return $id; |
277 | return $id; |
226 | } |
278 | } |
227 | } |
279 | } |
228 | $txn->txn_abort; |
280 | $txn->txn_abort; |
|
|
281 | select undef, undef, undef, 0.01 * rand; |
229 | } |
282 | } |
230 | |
283 | |
231 | die "maximum number of transaction retries reached - database problems?"; |
284 | die "maximum number of transaction retries reached - database problems?"; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub do_unlink { |
|
|
288 | unlink $_[0]; |
|
|
289 | } |
|
|
290 | |
|
|
291 | sub do_read_file { |
|
|
292 | my ($path) = @_; |
|
|
293 | |
|
|
294 | utf8::downgrade $path; |
|
|
295 | open my $fh, "<:raw", $path |
|
|
296 | or return; |
|
|
297 | sysread $fh, my $buf, -s $fh; |
|
|
298 | |
|
|
299 | $buf |
|
|
300 | } |
|
|
301 | |
|
|
302 | sub do_write_file { |
|
|
303 | my ($path, $data) = @_; |
|
|
304 | |
|
|
305 | utf8::downgrade $path; |
|
|
306 | utf8::downgrade $data; |
|
|
307 | open my $fh, ">:raw", $path |
|
|
308 | or return; |
|
|
309 | syswrite $fh, $data; |
|
|
310 | close $fh; |
|
|
311 | |
|
|
312 | 1 |
|
|
313 | } |
|
|
314 | |
|
|
315 | sub do_prefetch_file { |
|
|
316 | my ($path, $size) = @_; |
|
|
317 | |
|
|
318 | utf8::downgrade $path; |
|
|
319 | open my $fh, "<:raw", $path |
|
|
320 | or return; |
|
|
321 | sysread $fh, my $buf, $size; |
|
|
322 | |
|
|
323 | 1 |
|
|
324 | } |
|
|
325 | |
|
|
326 | our %LOG_FH; |
|
|
327 | |
|
|
328 | sub do_logprint { |
|
|
329 | my ($path, $line) = @_; |
|
|
330 | |
|
|
331 | $LOG_FH{$path} ||= do { |
|
|
332 | open my $fh, ">>:utf8", $path |
|
|
333 | or warn "Couldn't open logfile $path: $!"; |
|
|
334 | |
|
|
335 | $fh->autoflush (1); |
|
|
336 | |
|
|
337 | $fh |
|
|
338 | }; |
|
|
339 | |
|
|
340 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
|
|
341 | |
|
|
342 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
343 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
344 | |
|
|
345 | print { $LOG_FH{$path} } "$ts $line\n" |
232 | } |
346 | } |
233 | |
347 | |
234 | sub run { |
348 | sub run { |
235 | ($FH, my $fh) = CFPlus::socketpipe; |
349 | ($FH, my $fh) = CFPlus::socketpipe; |
236 | |
350 | |
… | |
… | |
238 | my $oldfh = select $fh; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
239 | |
353 | |
240 | my $pid = fork; |
354 | my $pid = fork; |
241 | |
355 | |
242 | if (defined $pid && !$pid) { |
356 | if (defined $pid && !$pid) { |
|
|
357 | local $SIG{QUIT}; |
243 | local $SIG{__DIE__}; |
358 | local $SIG{__DIE__}; |
|
|
359 | local $SIG{__WARN__}; |
244 | eval { |
360 | eval { |
245 | close $FH; |
361 | close $FH; |
246 | |
362 | |
247 | unless (eval { open_db }) { |
363 | unless (eval { open_db }) { |
248 | File::Path::rmtree $DB_HOME; |
364 | eval { File::Path::rmtree $DB_HOME }; |
249 | open_db; |
365 | open_db; |
250 | } |
366 | } |
251 | |
367 | |
252 | while () { |
368 | while () { |
253 | 4 == read $fh, my $len, 4 |
369 | 4 == read $fh, my $len, 4 |
… | |
… | |
261 | my ($id, $type, @args) = @$req; |
377 | my ($id, $type, @args) = @$req; |
262 | my $cb = CFPlus::DB::Server->can ("do_$type") |
378 | my $cb = CFPlus::DB::Server->can ("do_$type") |
263 | or die "$type: unknown database request type\n"; |
379 | or die "$type: unknown database request type\n"; |
264 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
380 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
265 | (syswrite $fh, $res) == length $res |
381 | (syswrite $fh, $res) == length $res |
266 | or die; |
382 | or die "DB::write: $!"; |
267 | } |
383 | } |
268 | }; |
384 | }; |
269 | |
385 | |
270 | my $error = $@; |
386 | my $error = $@; |
271 | |
387 | |
272 | eval { |
388 | eval { |
|
|
389 | $DB_ENV->txn_checkpoint (0, 0, 0); |
|
|
390 | |
273 | undef %DB_TABLE; |
391 | undef %DB_TABLE; |
274 | undef $DB_ENV; |
392 | undef $DB_ENV; |
275 | |
393 | |
276 | Storable::store_fd [die => $error], $fh; |
394 | Storable::store_fd [die => $error], $fh; |
277 | }; |
395 | }; |
278 | |
396 | |
279 | CFPlus::_exit 0; |
397 | CFPlus::_exit 0; |
280 | } |
398 | } |
281 | |
399 | |
282 | close $fh; |
400 | close $fh; |
283 | fcntl $FH, F_SETFL, O_NONBLOCK; |
401 | CFPlus::fh_nonblocking $FH, 1; |
284 | |
402 | |
285 | $CB{die} = sub { die shift }; |
403 | $CB{die} = sub { die shift }; |
286 | |
404 | |
287 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
405 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
|
|
406 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
|
|
407 | $SYNC->again unless $SYNC->is_active; |
|
|
408 | } |
288 | |
409 | |
289 | sync_tick; |
410 | sub stop { |
|
|
411 | close $FH; |
290 | } |
412 | } |
291 | |
413 | |
292 | 1; |
414 | 1; |
293 | |
415 | |
294 | =back |
416 | =back |