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