… | |
… | |
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 | |
137 | |
130 | our %CB; |
138 | our %CB; |
… | |
… | |
133 | our ($fh_r_watcher, $fh_w_watcher); |
141 | our ($fh_r_watcher, $fh_w_watcher); |
134 | our $sync_timer; |
142 | our $sync_timer; |
135 | our $write_buf; |
143 | our $write_buf; |
136 | our $read_buf; |
144 | our $read_buf; |
137 | |
145 | |
138 | our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { |
146 | our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { |
139 | CFPlus::DB::Server::req (sync => sub { }); |
147 | CFPlus::DB::Server::req (sync => sub { }); |
140 | $_[0]->w->stop; |
148 | $_[0]->w->stop; |
141 | }); |
149 | }); |
142 | |
150 | |
143 | sub fh_write { |
151 | sub fh_write { |
… | |
… | |
261 | |
269 | |
262 | return $id; |
270 | return $id; |
263 | } |
271 | } |
264 | } |
272 | } |
265 | $txn->txn_abort; |
273 | $txn->txn_abort; |
|
|
274 | select undef, undef, undef, 0.01 * rand; |
266 | } |
275 | } |
267 | |
276 | |
268 | die "maximum number of transaction retries reached - database problems?"; |
277 | die "maximum number of transaction retries reached - database problems?"; |
269 | } |
278 | } |
270 | |
279 | |
271 | sub do_unlink { |
280 | sub do_unlink { |
272 | unlink $_[0]; |
281 | unlink $_[0]; |
273 | } |
282 | } |
274 | |
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 | |
275 | sub do_write_file { |
295 | sub do_write_file { |
276 | my ($file, $data) = @_; |
296 | my ($path, $data) = @_; |
277 | |
297 | |
278 | utf8::downgrade $file; |
298 | utf8::downgrade $path; |
279 | utf8::downgrade $data; |
299 | utf8::downgrade $data; |
280 | open my $fh, ">:raw", $file |
300 | open my $fh, ">:raw", $path |
281 | or return; |
301 | or return; |
282 | print $fh $data; |
302 | syswrite $fh, $data; |
283 | close $fh; |
303 | close $fh; |
284 | |
304 | |
285 | 1 |
305 | 1 |
286 | } |
306 | } |
287 | |
307 | |
288 | sub do_prefetch_file { |
308 | sub do_prefetch_file { |
289 | my ($file, $size) = @_; |
309 | my ($path, $size) = @_; |
290 | |
310 | |
291 | utf8::downgrade $file; |
311 | utf8::downgrade $path; |
292 | open my $fh, "<:raw", $file |
312 | open my $fh, "<:raw", $path |
293 | or return; |
313 | or return; |
294 | sysread $fh, my $buf, $size; |
314 | sysread $fh, my $buf, $size; |
295 | |
315 | |
296 | 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" |
297 | } |
339 | } |
298 | |
340 | |
299 | sub run { |
341 | sub run { |
300 | ($FH, my $fh) = CFPlus::socketpipe; |
342 | ($FH, my $fh) = CFPlus::socketpipe; |
301 | |
343 | |
… | |
… | |
303 | my $oldfh = select $fh; $| = 1; select $oldfh; |
345 | my $oldfh = select $fh; $| = 1; select $oldfh; |
304 | |
346 | |
305 | my $pid = fork; |
347 | my $pid = fork; |
306 | |
348 | |
307 | if (defined $pid && !$pid) { |
349 | if (defined $pid && !$pid) { |
|
|
350 | local $SIG{QUIT}; |
308 | local $SIG{__DIE__}; |
351 | local $SIG{__DIE__}; |
|
|
352 | local $SIG{__WARN__}; |
309 | eval { |
353 | eval { |
310 | close $FH; |
354 | close $FH; |
311 | |
355 | |
312 | unless (eval { open_db }) { |
356 | unless (eval { open_db }) { |
313 | eval { File::Path::rmtree $DB_HOME }; |
357 | eval { File::Path::rmtree $DB_HOME }; |