… | |
… | |
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; |
|
|
23 | use Event (); |
24 | |
24 | |
25 | use CFPlus; |
25 | use CFPlus; |
26 | |
26 | |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
28 | |
28 | |
29 | sub path_of_res($) { |
29 | sub path_of_res($) { |
|
|
30 | utf8::downgrade $_[0]; # bug in unpack "H*" |
30 | "$DB_HOME/res-data-$_[0]" |
31 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
31 | } |
32 | } |
32 | |
33 | |
33 | sub sync { |
34 | sub sync { |
34 | # for debugging |
35 | # for debugging |
35 | #CFPlus::DB::Server::req (sync => sub { }); |
36 | #CFPlus::DB::Server::req (sync => sub { }); |
… | |
… | |
50 | |
51 | |
51 | sub unlink($$) { |
52 | sub unlink($$) { |
52 | CFPlus::DB::Server::req (unlink => @_); |
53 | CFPlus::DB::Server::req (unlink => @_); |
53 | } |
54 | } |
54 | |
55 | |
|
|
56 | sub read_file($$) { |
|
|
57 | CFPlus::DB::Server::req (read_file => @_); |
|
|
58 | } |
|
|
59 | |
55 | sub write_file($$$) { |
60 | sub write_file($$$) { |
56 | CFPlus::DB::Server::req (write_file => @_); |
61 | CFPlus::DB::Server::req (write_file => @_); |
57 | } |
62 | } |
58 | |
63 | |
59 | sub prefetch_file($$$) { |
64 | sub prefetch_file($$$) { |
60 | CFPlus::DB::Server::req (prefetch_file => @_); |
65 | CFPlus::DB::Server::req (prefetch_file => @_); |
61 | } |
66 | } |
62 | |
67 | |
|
|
68 | sub logprint($$$) { |
|
|
69 | CFPlus::DB::Server::req (logprint => @_); |
|
|
70 | } |
|
|
71 | |
63 | our $tilemap; |
72 | our $tilemap; |
64 | |
73 | |
65 | sub get_tile_id_sync($) { |
74 | sub get_tile_id_sync($) { |
66 | my ($hash) = @_; |
75 | my ($name) = @_; |
67 | |
76 | |
68 | # fetch the full face table first |
77 | # fetch the full face table first |
69 | unless ($tilemap) { |
78 | unless ($tilemap) { |
70 | 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 | }); |
71 | sync; |
88 | sync; |
72 | } |
89 | } |
73 | |
90 | |
74 | $tilemap->{$hash} ||= do { |
91 | $tilemap->{$name} ||= do { |
75 | my $id; |
92 | my $id; |
76 | CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); |
93 | CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); |
77 | sync; |
94 | sync; |
78 | $id |
95 | $id |
79 | } |
96 | } |
80 | } |
97 | } |
81 | |
98 | |
… | |
… | |
101 | -Cachesize => 8_000_000, |
118 | -Cachesize => 8_000_000, |
102 | -ErrFile => "$DB_HOME/errorlog.txt", |
119 | -ErrFile => "$DB_HOME/errorlog.txt", |
103 | # -ErrPrefix => "DATABASE", |
120 | # -ErrPrefix => "DATABASE", |
104 | -Verbose => 1, |
121 | -Verbose => 1, |
105 | -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, |
106 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
123 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
107 | 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"; |
108 | |
125 | |
109 | 1 |
126 | 1 |
110 | } |
127 | } |
111 | |
128 | |
… | |
… | |
119 | -Env => $DB_ENV, |
136 | -Env => $DB_ENV, |
120 | -Filename => $table, |
137 | -Filename => $table, |
121 | # -Filename => "database", |
138 | # -Filename => "database", |
122 | # -Subname => $table, |
139 | # -Subname => $table, |
123 | -Property => DB_CHKSUM, |
140 | -Property => DB_CHKSUM, |
124 | -Flags => DB_CREATE | DB_UPGRADE, |
141 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
125 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
142 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
126 | } |
143 | } |
127 | } |
144 | } |
128 | |
|
|
129 | our $SYNC_INTERVAL = 60; |
|
|
130 | |
145 | |
131 | our %CB; |
146 | our %CB; |
132 | our $FH; |
147 | our $FH; |
133 | our $ID = "aaa0"; |
148 | our $ID = "aaa0"; |
134 | our ($fh_r_watcher, $fh_w_watcher); |
149 | our ($fh_r_watcher, $fh_w_watcher); |
135 | our $sync_timer; |
150 | our $sync_timer; |
136 | our $write_buf; |
151 | our $write_buf; |
137 | our $read_buf; |
152 | our $read_buf; |
138 | |
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 | |
139 | sub fh_write { |
159 | sub fh_write { |
140 | my $len = syswrite $FH, $write_buf; |
160 | my $len = syswrite $FH, $write_buf; |
141 | |
161 | |
142 | substr $write_buf, 0, $len, ""; |
162 | substr $write_buf, 0, $len, ""; |
143 | |
163 | |
144 | undef $fh_w_watcher |
164 | $fh_w_watcher->stop |
145 | unless length $write_buf; |
165 | unless length $write_buf; |
146 | } |
166 | } |
147 | |
167 | |
148 | sub fh_read { |
168 | sub fh_read { |
149 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
169 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
185 | |
205 | |
186 | my $id = ++$ID; |
206 | my $id = ++$ID; |
187 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
207 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
188 | $CB{$id} = $cb; |
208 | $CB{$id} = $cb; |
189 | |
209 | |
190 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
210 | $fh_w_watcher->start; |
191 | } |
211 | $SYNC->start; |
192 | |
|
|
193 | sub sync_tick { |
|
|
194 | req "sync", sub { }; |
|
|
195 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
196 | } |
212 | } |
197 | |
213 | |
198 | sub do_sync { |
214 | sub do_sync { |
199 | $DB_ENV->txn_checkpoint (0, 0, 0); |
215 | $DB_ENV->txn_checkpoint (0, 0, 0); |
200 | () |
216 | () |
… | |
… | |
240 | |
256 | |
241 | \%kv |
257 | \%kv |
242 | } |
258 | } |
243 | |
259 | |
244 | sub do_get_tile_id { |
260 | sub do_get_tile_id { |
245 | my ($hash) = @_; |
261 | my ($name) = @_; |
246 | |
262 | |
247 | my $id; |
263 | my $id; |
248 | my $table = table "facemap"; |
264 | my $table = table "facemap"; |
249 | |
265 | |
250 | return $id |
266 | return $id |
251 | if $table->db_get ($hash, $id) == 0; |
267 | if $table->db_get ($name, $id) == 0; |
252 | |
268 | |
253 | for (1..100) { |
269 | for (1..100) { |
254 | my $txn = $DB_ENV->txn_begin; |
270 | my $txn = $DB_ENV->txn_begin; |
255 | my $status = $table->db_get (id => $id); |
271 | my $status = $table->db_get (id => $id); |
256 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
272 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
257 | $id = ($id || 64) + 1; |
273 | $id = ($id || 64) + 1; |
258 | if ($table->db_put (id => $id) == 0 |
274 | if ($table->db_put (id => $id) == 0 |
259 | && $table->db_put ($hash => $id) == 0) { |
275 | && $table->db_put ($name => $id) == 0) { |
260 | $txn->txn_commit; |
276 | $txn->txn_commit; |
261 | |
277 | |
262 | return $id; |
278 | return $id; |
263 | } |
279 | } |
264 | } |
280 | } |
265 | $txn->txn_abort; |
281 | $txn->txn_abort; |
|
|
282 | select undef, undef, undef, 0.01 * rand; |
266 | } |
283 | } |
267 | |
284 | |
268 | die "maximum number of transaction retries reached - database problems?"; |
285 | die "maximum number of transaction retries reached - database problems?"; |
269 | } |
286 | } |
270 | |
287 | |
271 | sub do_unlink { |
288 | sub do_unlink { |
272 | unlink $_[0]; |
289 | unlink $_[0]; |
273 | } |
290 | } |
274 | |
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 | |
275 | sub do_write_file { |
303 | sub do_write_file { |
276 | my ($file, $data) = @_; |
304 | my ($path, $data) = @_; |
277 | |
305 | |
278 | utf8::downgrade $file; |
306 | utf8::downgrade $path; |
279 | utf8::downgrade $data; |
307 | utf8::downgrade $data; |
280 | open my $fh, ">:raw", $file |
308 | open my $fh, ">:raw", $path |
281 | or return; |
309 | or return; |
282 | print $fh $data; |
310 | syswrite $fh, $data; |
283 | close $fh; |
311 | close $fh; |
284 | |
312 | |
285 | 1 |
313 | 1 |
286 | } |
314 | } |
287 | |
315 | |
288 | sub do_prefetch_file { |
316 | sub do_prefetch_file { |
289 | my ($file, $size) = @_; |
317 | my ($path, $size) = @_; |
290 | |
318 | |
291 | utf8::downgrade $file; |
319 | utf8::downgrade $path; |
292 | open my $fh, "<:raw", $file |
320 | open my $fh, "<:raw", $path |
293 | or return; |
321 | or return; |
294 | sysread $fh, my $buf, $size; |
322 | sysread $fh, my $buf, $size; |
295 | |
323 | |
296 | 1 |
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" |
297 | } |
347 | } |
298 | |
348 | |
299 | sub run { |
349 | sub run { |
300 | ($FH, my $fh) = CFPlus::socketpipe; |
350 | ($FH, my $fh) = CFPlus::socketpipe; |
301 | |
351 | |
… | |
… | |
303 | my $oldfh = select $fh; $| = 1; select $oldfh; |
353 | my $oldfh = select $fh; $| = 1; select $oldfh; |
304 | |
354 | |
305 | my $pid = fork; |
355 | my $pid = fork; |
306 | |
356 | |
307 | if (defined $pid && !$pid) { |
357 | if (defined $pid && !$pid) { |
|
|
358 | local $SIG{QUIT}; |
308 | local $SIG{__DIE__}; |
359 | local $SIG{__DIE__}; |
|
|
360 | local $SIG{__WARN__}; |
309 | eval { |
361 | eval { |
310 | close $FH; |
362 | close $FH; |
311 | |
363 | |
312 | unless (eval { open_db }) { |
364 | unless (eval { open_db }) { |
313 | eval { File::Path::rmtree $DB_HOME }; |
365 | eval { File::Path::rmtree $DB_HOME }; |
… | |
… | |
347 | close $fh; |
399 | close $fh; |
348 | CFPlus::fh_nonblocking $FH, 1; |
400 | CFPlus::fh_nonblocking $FH, 1; |
349 | |
401 | |
350 | $CB{die} = sub { die shift }; |
402 | $CB{die} = sub { die shift }; |
351 | |
403 | |
352 | $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); |
353 | |
405 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
354 | sync_tick; |
406 | $SYNC->start; |
355 | } |
407 | } |
356 | |
408 | |
357 | sub stop { |
409 | sub stop { |
358 | close $FH; |
410 | close $FH; |
359 | } |
411 | } |