… | |
… | |
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 | |
… | |
… | |
51 | |
51 | |
52 | sub unlink($$) { |
52 | sub unlink($$) { |
53 | CFPlus::DB::Server::req (unlink => @_); |
53 | CFPlus::DB::Server::req (unlink => @_); |
54 | } |
54 | } |
55 | |
55 | |
|
|
56 | sub read_file($$) { |
|
|
57 | CFPlus::DB::Server::req (read_file => @_); |
|
|
58 | } |
|
|
59 | |
56 | sub write_file($$$) { |
60 | sub write_file($$$) { |
57 | CFPlus::DB::Server::req (write_file => @_); |
61 | CFPlus::DB::Server::req (write_file => @_); |
58 | } |
62 | } |
59 | |
63 | |
60 | sub prefetch_file($$$) { |
64 | sub prefetch_file($$$) { |
61 | CFPlus::DB::Server::req (prefetch_file => @_); |
65 | CFPlus::DB::Server::req (prefetch_file => @_); |
|
|
66 | } |
|
|
67 | |
|
|
68 | sub logprint($$$) { |
|
|
69 | CFPlus::DB::Server::req (logprint => @_); |
62 | } |
70 | } |
63 | |
71 | |
64 | our $tilemap; |
72 | our $tilemap; |
65 | |
73 | |
66 | sub get_tile_id_sync($) { |
74 | sub get_tile_id_sync($) { |
… | |
… | |
102 | -Cachesize => 8_000_000, |
110 | -Cachesize => 8_000_000, |
103 | -ErrFile => "$DB_HOME/errorlog.txt", |
111 | -ErrFile => "$DB_HOME/errorlog.txt", |
104 | # -ErrPrefix => "DATABASE", |
112 | # -ErrPrefix => "DATABASE", |
105 | -Verbose => 1, |
113 | -Verbose => 1, |
106 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
114 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
107 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
115 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
108 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
116 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
109 | |
117 | |
110 | 1 |
118 | 1 |
111 | } |
119 | } |
112 | |
120 | |
… | |
… | |
120 | -Env => $DB_ENV, |
128 | -Env => $DB_ENV, |
121 | -Filename => $table, |
129 | -Filename => $table, |
122 | # -Filename => "database", |
130 | # -Filename => "database", |
123 | # -Subname => $table, |
131 | # -Subname => $table, |
124 | -Property => DB_CHKSUM, |
132 | -Property => DB_CHKSUM, |
125 | -Flags => DB_CREATE | DB_UPGRADE, |
133 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
126 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
134 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
127 | } |
135 | } |
128 | } |
136 | } |
129 | |
|
|
130 | our $SYNC_INTERVAL = 60; |
|
|
131 | |
137 | |
132 | our %CB; |
138 | our %CB; |
133 | our $FH; |
139 | our $FH; |
134 | our $ID = "aaa0"; |
140 | our $ID = "aaa0"; |
135 | our ($fh_r_watcher, $fh_w_watcher); |
141 | our ($fh_r_watcher, $fh_w_watcher); |
136 | our $sync_timer; |
142 | our $sync_timer; |
137 | our $write_buf; |
143 | our $write_buf; |
138 | our $read_buf; |
144 | our $read_buf; |
139 | |
145 | |
|
|
146 | our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { |
|
|
147 | CFPlus::DB::Server::req (sync => sub { }); |
|
|
148 | $_[0]->w->stop; |
|
|
149 | }); |
|
|
150 | |
140 | sub fh_write { |
151 | sub fh_write { |
141 | my $len = syswrite $FH, $write_buf; |
152 | my $len = syswrite $FH, $write_buf; |
142 | |
153 | |
143 | substr $write_buf, 0, $len, ""; |
154 | substr $write_buf, 0, $len, ""; |
144 | |
155 | |
145 | undef $fh_w_watcher |
156 | $fh_w_watcher->stop |
146 | unless length $write_buf; |
157 | unless length $write_buf; |
147 | } |
158 | } |
148 | |
159 | |
149 | sub fh_read { |
160 | sub fh_read { |
150 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
161 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
186 | |
197 | |
187 | my $id = ++$ID; |
198 | my $id = ++$ID; |
188 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
199 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
189 | $CB{$id} = $cb; |
200 | $CB{$id} = $cb; |
190 | |
201 | |
191 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
202 | $fh_w_watcher->start; |
192 | } |
203 | $SYNC->start; |
193 | |
|
|
194 | sub sync_tick { |
|
|
195 | req "sync", sub { }; |
|
|
196 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
197 | } |
204 | } |
198 | |
205 | |
199 | sub do_sync { |
206 | sub do_sync { |
200 | $DB_ENV->txn_checkpoint (0, 0, 0); |
207 | $DB_ENV->txn_checkpoint (0, 0, 0); |
201 | () |
208 | () |
… | |
… | |
262 | |
269 | |
263 | return $id; |
270 | return $id; |
264 | } |
271 | } |
265 | } |
272 | } |
266 | $txn->txn_abort; |
273 | $txn->txn_abort; |
|
|
274 | select undef, undef, undef, 0.01 * rand; |
267 | } |
275 | } |
268 | |
276 | |
269 | die "maximum number of transaction retries reached - database problems?"; |
277 | die "maximum number of transaction retries reached - database problems?"; |
270 | } |
278 | } |
271 | |
279 | |
272 | sub do_unlink { |
280 | sub do_unlink { |
273 | unlink $_[0]; |
281 | unlink $_[0]; |
274 | } |
282 | } |
275 | |
283 | |
|
|
284 | sub do_read_file { |
|
|
285 | my ($path) = @_; |
|
|
286 | |
|
|
287 | utf8::downgrade $path; |
|
|
288 | open my $fh, "<:raw", $path |
|
|
289 | or return; |
|
|
290 | sysread $fh, my $buf, -s $fh; |
|
|
291 | |
|
|
292 | $buf |
|
|
293 | } |
|
|
294 | |
276 | sub do_write_file { |
295 | sub do_write_file { |
277 | my ($file, $data) = @_; |
296 | my ($path, $data) = @_; |
278 | |
297 | |
279 | utf8::downgrade $file; |
298 | utf8::downgrade $path; |
280 | utf8::downgrade $data; |
299 | utf8::downgrade $data; |
281 | open my $fh, ">:raw", $file |
300 | open my $fh, ">:raw", $path |
282 | or return; |
301 | or return; |
283 | print $fh $data; |
302 | syswrite $fh, $data; |
284 | close $fh; |
303 | close $fh; |
285 | |
304 | |
286 | 1 |
305 | 1 |
287 | } |
306 | } |
288 | |
307 | |
289 | sub do_prefetch_file { |
308 | sub do_prefetch_file { |
290 | my ($file, $size) = @_; |
309 | my ($path, $size) = @_; |
291 | |
310 | |
292 | utf8::downgrade $file; |
311 | utf8::downgrade $path; |
293 | open my $fh, "<:raw", $file |
312 | open my $fh, "<:raw", $path |
294 | or return; |
313 | or return; |
295 | sysread $fh, my $buf, $size; |
314 | sysread $fh, my $buf, $size; |
296 | |
315 | |
297 | 1 |
316 | 1 |
|
|
317 | } |
|
|
318 | |
|
|
319 | our %LOG_FH; |
|
|
320 | |
|
|
321 | sub do_logprint { |
|
|
322 | my ($path, $line) = @_; |
|
|
323 | |
|
|
324 | $LOG_FH{$path} ||= do { |
|
|
325 | open my $fh, ">>:utf8", $path |
|
|
326 | or warn "Couldn't open logfile $path: $!"; |
|
|
327 | |
|
|
328 | $fh->autoflush (1); |
|
|
329 | |
|
|
330 | $fh |
|
|
331 | }; |
|
|
332 | |
|
|
333 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
|
|
334 | |
|
|
335 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
336 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
337 | |
|
|
338 | print { $LOG_FH{$path} } "$ts $line\n" |
298 | } |
339 | } |
299 | |
340 | |
300 | sub run { |
341 | sub run { |
301 | ($FH, my $fh) = CFPlus::socketpipe; |
342 | ($FH, my $fh) = CFPlus::socketpipe; |
302 | |
343 | |
… | |
… | |
304 | my $oldfh = select $fh; $| = 1; select $oldfh; |
345 | my $oldfh = select $fh; $| = 1; select $oldfh; |
305 | |
346 | |
306 | my $pid = fork; |
347 | my $pid = fork; |
307 | |
348 | |
308 | if (defined $pid && !$pid) { |
349 | if (defined $pid && !$pid) { |
|
|
350 | local $SIG{QUIT}; |
309 | local $SIG{__DIE__}; |
351 | local $SIG{__DIE__}; |
|
|
352 | local $SIG{__WARN__}; |
310 | eval { |
353 | eval { |
311 | close $FH; |
354 | close $FH; |
312 | |
355 | |
313 | unless (eval { open_db }) { |
356 | unless (eval { open_db }) { |
314 | eval { File::Path::rmtree $DB_HOME }; |
357 | eval { File::Path::rmtree $DB_HOME }; |
… | |
… | |
348 | close $fh; |
391 | close $fh; |
349 | CFPlus::fh_nonblocking $FH, 1; |
392 | CFPlus::fh_nonblocking $FH, 1; |
350 | |
393 | |
351 | $CB{die} = sub { die shift }; |
394 | $CB{die} = sub { die shift }; |
352 | |
395 | |
353 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
396 | $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
354 | |
397 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
355 | sync_tick; |
398 | $SYNC->start; |
356 | } |
399 | } |
357 | |
400 | |
358 | sub stop { |
401 | sub stop { |
359 | close $FH; |
402 | close $FH; |
360 | } |
403 | } |