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