… | |
… | |
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 (); |
23 | use Config; |
22 | use Config; |
24 | |
23 | |
25 | use CFPlus; |
24 | use CFPlus; |
26 | |
25 | |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
26 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
28 | |
27 | |
29 | sub path_of($) { |
28 | sub path_of_res($) { |
30 | "$DB_HOME/data-$_[0]" |
29 | utf8::downgrade $_[0]; # bug in unpack "H*" |
|
|
30 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
31 | } |
31 | } |
32 | |
32 | |
33 | sub sync { |
33 | sub sync { |
34 | # for debugging |
34 | # for debugging |
35 | #CFPlus::DB::Server::req (sync => sub { }); |
35 | #CFPlus::DB::Server::req (sync => sub { }); |
… | |
… | |
50 | |
50 | |
51 | sub unlink($$) { |
51 | sub unlink($$) { |
52 | CFPlus::DB::Server::req (unlink => @_); |
52 | CFPlus::DB::Server::req (unlink => @_); |
53 | } |
53 | } |
54 | |
54 | |
|
|
55 | sub read_file($$) { |
|
|
56 | CFPlus::DB::Server::req (read_file => @_); |
|
|
57 | } |
|
|
58 | |
55 | sub write_file($$$) { |
59 | sub write_file($$$) { |
56 | CFPlus::DB::Server::req (write_file => @_); |
60 | CFPlus::DB::Server::req (write_file => @_); |
57 | } |
61 | } |
58 | |
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 | |
59 | our $tilemap; |
71 | our $tilemap; |
60 | |
72 | |
61 | sub get_tile_id_sync($) { |
73 | sub get_tile_id_sync($) { |
62 | my ($hash) = @_; |
74 | my ($name) = @_; |
63 | |
75 | |
64 | # fetch the full face table first |
76 | # fetch the full face table first |
65 | unless ($tilemap) { |
77 | unless ($tilemap) { |
66 | 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 | }); |
67 | sync; |
87 | sync; |
68 | } |
88 | } |
69 | |
89 | |
70 | $tilemap->{$hash} ||= do { |
90 | $tilemap->{$name} ||= do { |
71 | my $id; |
91 | my $id; |
72 | CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); |
92 | CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); |
73 | sync; |
93 | sync; |
74 | $id |
94 | $id |
75 | } |
95 | } |
76 | } |
96 | } |
77 | |
97 | |
… | |
… | |
97 | -Cachesize => 8_000_000, |
117 | -Cachesize => 8_000_000, |
98 | -ErrFile => "$DB_HOME/errorlog.txt", |
118 | -ErrFile => "$DB_HOME/errorlog.txt", |
99 | # -ErrPrefix => "DATABASE", |
119 | # -ErrPrefix => "DATABASE", |
100 | -Verbose => 1, |
120 | -Verbose => 1, |
101 | -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, |
102 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
122 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
103 | 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"; |
104 | |
124 | |
105 | 1 |
125 | 1 |
106 | } |
126 | } |
107 | |
127 | |
… | |
… | |
115 | -Env => $DB_ENV, |
135 | -Env => $DB_ENV, |
116 | -Filename => $table, |
136 | -Filename => $table, |
117 | # -Filename => "database", |
137 | # -Filename => "database", |
118 | # -Subname => $table, |
138 | # -Subname => $table, |
119 | -Property => DB_CHKSUM, |
139 | -Property => DB_CHKSUM, |
120 | -Flags => DB_CREATE | DB_UPGRADE, |
140 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
121 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
141 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
122 | } |
142 | } |
123 | } |
143 | } |
124 | |
|
|
125 | our $SYNC_INTERVAL = 60; |
|
|
126 | |
144 | |
127 | our %CB; |
145 | our %CB; |
128 | our $FH; |
146 | our $FH; |
129 | our $ID = "aaa0"; |
147 | our $ID = "aaa0"; |
130 | our ($fh_r_watcher, $fh_w_watcher); |
148 | our ($fh_r_watcher, $fh_w_watcher); |
131 | our $sync_timer; |
149 | our $sync_timer; |
132 | our $write_buf; |
150 | our $write_buf; |
133 | our $read_buf; |
151 | our $read_buf; |
134 | |
152 | |
|
|
153 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
154 | $_[0]->stop; |
|
|
155 | CFPlus::DB::Server::req (sync => sub { }); |
|
|
156 | }; |
|
|
157 | |
135 | sub fh_write { |
158 | sub fh_write { |
136 | my $len = syswrite $FH, $write_buf; |
159 | my $len = syswrite $FH, $write_buf; |
137 | |
160 | |
138 | substr $write_buf, 0, $len, ""; |
161 | substr $write_buf, 0, $len, ""; |
139 | |
162 | |
140 | undef $fh_w_watcher |
163 | $fh_w_watcher->stop |
141 | unless length $write_buf; |
164 | unless length $write_buf; |
142 | } |
165 | } |
143 | |
166 | |
144 | sub fh_read { |
167 | sub fh_read { |
145 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
168 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
181 | |
204 | |
182 | my $id = ++$ID; |
205 | my $id = ++$ID; |
183 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
206 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
184 | $CB{$id} = $cb; |
207 | $CB{$id} = $cb; |
185 | |
208 | |
186 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
209 | $fh_w_watcher->start; |
187 | } |
210 | $SYNC->again unless $SYNC->is_active; |
188 | |
|
|
189 | sub sync_tick { |
|
|
190 | req "sync", sub { }; |
|
|
191 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
192 | } |
211 | } |
193 | |
212 | |
194 | sub do_sync { |
213 | sub do_sync { |
195 | $DB_ENV->txn_checkpoint (0, 0, 0); |
214 | $DB_ENV->txn_checkpoint (0, 0, 0); |
196 | () |
215 | () |
… | |
… | |
236 | |
255 | |
237 | \%kv |
256 | \%kv |
238 | } |
257 | } |
239 | |
258 | |
240 | sub do_get_tile_id { |
259 | sub do_get_tile_id { |
241 | my ($hash) = @_; |
260 | my ($name) = @_; |
242 | |
261 | |
243 | my $id; |
262 | my $id; |
244 | my $table = table "facemap"; |
263 | my $table = table "facemap"; |
245 | |
264 | |
246 | return $id |
265 | return $id |
247 | if $table->db_get ($hash, $id) == 0; |
266 | if $table->db_get ($name, $id) == 0; |
248 | |
267 | |
249 | for (1..100) { |
268 | for (1..100) { |
250 | my $txn = $DB_ENV->txn_begin; |
269 | my $txn = $DB_ENV->txn_begin; |
251 | my $status = $table->db_get (id => $id); |
270 | my $status = $table->db_get (id => $id); |
252 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
271 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
253 | $id = ($id || 64) + 1; |
272 | $id = ($id || 64) + 1; |
254 | if ($table->db_put (id => $id) == 0 |
273 | if ($table->db_put (id => $id) == 0 |
255 | && $table->db_put ($hash => $id) == 0) { |
274 | && $table->db_put ($name => $id) == 0) { |
256 | $txn->txn_commit; |
275 | $txn->txn_commit; |
257 | |
276 | |
258 | return $id; |
277 | return $id; |
259 | } |
278 | } |
260 | } |
279 | } |
261 | $txn->txn_abort; |
280 | $txn->txn_abort; |
|
|
281 | select undef, undef, undef, 0.01 * rand; |
262 | } |
282 | } |
263 | |
283 | |
264 | die "maximum number of transaction retries reached - database problems?"; |
284 | die "maximum number of transaction retries reached - database problems?"; |
265 | } |
285 | } |
266 | |
286 | |
267 | sub do_unlink { |
287 | sub do_unlink { |
268 | unlink $_[0]; |
288 | unlink $_[0]; |
269 | } |
289 | } |
270 | |
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 | |
271 | sub do_write_file { |
302 | sub do_write_file { |
272 | my ($file, $data) = @_; |
303 | my ($path, $data) = @_; |
273 | |
304 | |
274 | utf8::downgrade $file; |
305 | utf8::downgrade $path; |
275 | utf8::downgrade $data; |
306 | utf8::downgrade $data; |
276 | open my $fh, ">:raw", CFPlus::DB::path_of $file |
307 | open my $fh, ">:raw", $path |
277 | or return; |
308 | or return; |
278 | print $fh $data; |
309 | syswrite $fh, $data; |
279 | close $fh; |
310 | close $fh; |
280 | |
311 | |
281 | 1 |
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" |
282 | } |
346 | } |
283 | |
347 | |
284 | sub run { |
348 | sub run { |
285 | ($FH, my $fh) = CFPlus::socketpipe; |
349 | ($FH, my $fh) = CFPlus::socketpipe; |
286 | |
350 | |
… | |
… | |
288 | my $oldfh = select $fh; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
289 | |
353 | |
290 | my $pid = fork; |
354 | my $pid = fork; |
291 | |
355 | |
292 | if (defined $pid && !$pid) { |
356 | if (defined $pid && !$pid) { |
|
|
357 | local $SIG{QUIT}; |
293 | local $SIG{__DIE__}; |
358 | local $SIG{__DIE__}; |
|
|
359 | local $SIG{__WARN__}; |
294 | eval { |
360 | eval { |
295 | close $FH; |
361 | close $FH; |
296 | |
362 | |
297 | unless (eval { open_db }) { |
363 | unless (eval { open_db }) { |
298 | eval { File::Path::rmtree $DB_HOME }; |
364 | eval { File::Path::rmtree $DB_HOME }; |
… | |
… | |
311 | my ($id, $type, @args) = @$req; |
377 | my ($id, $type, @args) = @$req; |
312 | my $cb = CFPlus::DB::Server->can ("do_$type") |
378 | my $cb = CFPlus::DB::Server->can ("do_$type") |
313 | or die "$type: unknown database request type\n"; |
379 | or die "$type: unknown database request type\n"; |
314 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
380 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
315 | (syswrite $fh, $res) == length $res |
381 | (syswrite $fh, $res) == length $res |
316 | or die; |
382 | or die "DB::write: $!"; |
317 | } |
383 | } |
318 | }; |
384 | }; |
319 | |
385 | |
320 | my $error = $@; |
386 | my $error = $@; |
321 | |
387 | |
… | |
… | |
324 | undef $DB_ENV; |
390 | undef $DB_ENV; |
325 | |
391 | |
326 | Storable::store_fd [die => $error], $fh; |
392 | Storable::store_fd [die => $error], $fh; |
327 | }; |
393 | }; |
328 | |
394 | |
|
|
395 | $DB_ENV->txn_checkpoint (0, 0, 0); |
329 | CFPlus::_exit 0; |
396 | CFPlus::_exit 0; |
330 | } |
397 | } |
331 | |
398 | |
332 | close $fh; |
399 | close $fh; |
333 | CFPlus::fh_nonblocking $FH, 1; |
400 | CFPlus::fh_nonblocking $FH, 1; |
334 | |
401 | |
335 | $CB{die} = sub { die shift }; |
402 | $CB{die} = sub { die shift }; |
336 | |
403 | |
337 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
404 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
338 | |
405 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
339 | sync_tick; |
406 | $SYNC->again unless $SYNC->is_active; |
340 | } |
407 | } |
341 | |
408 | |
342 | sub stop { |
409 | sub stop { |
343 | close $FH; |
410 | close $FH; |
344 | } |
411 | } |