… | |
… | |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use Carp (); |
20 | use Carp (); |
21 | use Storable (); |
21 | use Storable (); |
22 | use Config; |
22 | use Config; |
23 | use Event (); |
|
|
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 | |
… | |
… | |
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($$$) { |
… | |
… | |
66 | } |
69 | } |
67 | |
70 | |
68 | our $tilemap; |
71 | our $tilemap; |
69 | |
72 | |
70 | sub get_tile_id_sync($) { |
73 | sub get_tile_id_sync($) { |
71 | my ($hash) = @_; |
74 | my ($name) = @_; |
72 | |
75 | |
73 | # fetch the full face table first |
76 | # fetch the full face table first |
74 | unless ($tilemap) { |
77 | unless ($tilemap) { |
75 | 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 | }); |
76 | sync; |
87 | sync; |
77 | } |
88 | } |
78 | |
89 | |
79 | $tilemap->{$hash} ||= do { |
90 | $tilemap->{$name} ||= do { |
80 | my $id; |
91 | my $id; |
81 | CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); |
92 | CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); |
82 | sync; |
93 | sync; |
83 | $id |
94 | $id |
84 | } |
95 | } |
85 | } |
96 | } |
86 | |
97 | |
87 | package CFPlus::DB::Server; |
98 | package CFPlus::DB::Server; |
88 | |
99 | |
89 | use strict; |
100 | use strict; |
90 | |
101 | |
|
|
102 | use EV (); |
91 | use Fcntl; |
103 | use Fcntl; |
92 | use BerkeleyDB; |
104 | use BerkeleyDB; |
93 | |
105 | |
94 | our $DB_ENV; |
106 | our $DB_ENV; |
95 | our $DB_STATE; |
107 | our $DB_STATE; |
… | |
… | |
106 | -Cachesize => 8_000_000, |
118 | -Cachesize => 8_000_000, |
107 | -ErrFile => "$DB_HOME/errorlog.txt", |
119 | -ErrFile => "$DB_HOME/errorlog.txt", |
108 | # -ErrPrefix => "DATABASE", |
120 | # -ErrPrefix => "DATABASE", |
109 | -Verbose => 1, |
121 | -Verbose => 1, |
110 | -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, |
111 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
123 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
112 | 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"; |
113 | |
125 | |
114 | 1 |
126 | 1 |
115 | } |
127 | } |
116 | |
128 | |
… | |
… | |
124 | -Env => $DB_ENV, |
136 | -Env => $DB_ENV, |
125 | -Filename => $table, |
137 | -Filename => $table, |
126 | # -Filename => "database", |
138 | # -Filename => "database", |
127 | # -Subname => $table, |
139 | # -Subname => $table, |
128 | -Property => DB_CHKSUM, |
140 | -Property => DB_CHKSUM, |
129 | -Flags => DB_CREATE | DB_UPGRADE, |
141 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
130 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
142 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
131 | } |
143 | } |
132 | } |
144 | } |
133 | |
145 | |
134 | our %CB; |
146 | our %CB; |
… | |
… | |
137 | our ($fh_r_watcher, $fh_w_watcher); |
149 | our ($fh_r_watcher, $fh_w_watcher); |
138 | our $sync_timer; |
150 | our $sync_timer; |
139 | our $write_buf; |
151 | our $write_buf; |
140 | our $read_buf; |
152 | our $read_buf; |
141 | |
153 | |
142 | our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { |
154 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
155 | $_[0]->stop; |
143 | CFPlus::DB::Server::req (sync => sub { }); |
156 | CFPlus::DB::Server::req (sync => sub { }); |
144 | $_[0]->w->stop; |
|
|
145 | }); |
157 | }; |
146 | |
158 | |
147 | sub fh_write { |
159 | sub fh_write { |
148 | my $len = syswrite $FH, $write_buf; |
160 | my $len = syswrite $FH, $write_buf; |
149 | |
161 | |
150 | substr $write_buf, 0, $len, ""; |
162 | substr $write_buf, 0, $len, ""; |
… | |
… | |
194 | my $id = ++$ID; |
206 | my $id = ++$ID; |
195 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
207 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
196 | $CB{$id} = $cb; |
208 | $CB{$id} = $cb; |
197 | |
209 | |
198 | $fh_w_watcher->start; |
210 | $fh_w_watcher->start; |
199 | $SYNC->start; |
211 | $SYNC->again unless $SYNC->is_active; |
200 | } |
212 | } |
201 | |
213 | |
202 | sub do_sync { |
214 | sub do_sync { |
203 | $DB_ENV->txn_checkpoint (0, 0, 0); |
215 | $DB_ENV->txn_checkpoint (0, 0, 0); |
204 | () |
216 | () |
… | |
… | |
244 | |
256 | |
245 | \%kv |
257 | \%kv |
246 | } |
258 | } |
247 | |
259 | |
248 | sub do_get_tile_id { |
260 | sub do_get_tile_id { |
249 | my ($hash) = @_; |
261 | my ($name) = @_; |
250 | |
262 | |
251 | my $id; |
263 | my $id; |
252 | my $table = table "facemap"; |
264 | my $table = table "facemap"; |
253 | |
265 | |
254 | return $id |
266 | return $id |
255 | if $table->db_get ($hash, $id) == 0; |
267 | if $table->db_get ($name, $id) == 0; |
256 | |
268 | |
257 | for (1..100) { |
269 | for (1..100) { |
258 | my $txn = $DB_ENV->txn_begin; |
270 | my $txn = $DB_ENV->txn_begin; |
259 | my $status = $table->db_get (id => $id); |
271 | my $status = $table->db_get (id => $id); |
260 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
272 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
261 | $id = ($id || 64) + 1; |
273 | $id = ($id || 64) + 1; |
262 | if ($table->db_put (id => $id) == 0 |
274 | if ($table->db_put (id => $id) == 0 |
263 | && $table->db_put ($hash => $id) == 0) { |
275 | && $table->db_put ($name => $id) == 0) { |
264 | $txn->txn_commit; |
276 | $txn->txn_commit; |
265 | |
277 | |
266 | return $id; |
278 | return $id; |
267 | } |
279 | } |
268 | } |
280 | } |
269 | $txn->txn_abort; |
281 | $txn->txn_abort; |
|
|
282 | select undef, undef, undef, 0.01 * rand; |
270 | } |
283 | } |
271 | |
284 | |
272 | die "maximum number of transaction retries reached - database problems?"; |
285 | die "maximum number of transaction retries reached - database problems?"; |
273 | } |
286 | } |
274 | |
287 | |
275 | sub do_unlink { |
288 | sub do_unlink { |
276 | unlink $_[0]; |
289 | unlink $_[0]; |
277 | } |
290 | } |
278 | |
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 | |
279 | sub do_write_file { |
303 | sub do_write_file { |
280 | my ($file, $data) = @_; |
304 | my ($path, $data) = @_; |
281 | |
305 | |
282 | utf8::downgrade $file; |
306 | utf8::downgrade $path; |
283 | utf8::downgrade $data; |
307 | utf8::downgrade $data; |
284 | open my $fh, ">:raw", $file |
308 | open my $fh, ">:raw", $path |
285 | or return; |
309 | or return; |
286 | print $fh $data; |
310 | syswrite $fh, $data; |
287 | close $fh; |
311 | close $fh; |
288 | |
312 | |
289 | 1 |
313 | 1 |
290 | } |
314 | } |
291 | |
315 | |
292 | sub do_prefetch_file { |
316 | sub do_prefetch_file { |
293 | my ($file, $size) = @_; |
317 | my ($path, $size) = @_; |
294 | |
318 | |
295 | utf8::downgrade $file; |
319 | utf8::downgrade $path; |
296 | open my $fh, "<:raw", $file |
320 | open my $fh, "<:raw", $path |
297 | or return; |
321 | or return; |
298 | sysread $fh, my $buf, $size; |
322 | sysread $fh, my $buf, $size; |
299 | |
323 | |
300 | 1 |
324 | 1 |
301 | } |
325 | } |
… | |
… | |
354 | my ($id, $type, @args) = @$req; |
378 | my ($id, $type, @args) = @$req; |
355 | my $cb = CFPlus::DB::Server->can ("do_$type") |
379 | my $cb = CFPlus::DB::Server->can ("do_$type") |
356 | or die "$type: unknown database request type\n"; |
380 | or die "$type: unknown database request type\n"; |
357 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
381 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
358 | (syswrite $fh, $res) == length $res |
382 | (syswrite $fh, $res) == length $res |
359 | or die; |
383 | or die "DB::write: $!"; |
360 | } |
384 | } |
361 | }; |
385 | }; |
362 | |
386 | |
363 | my $error = $@; |
387 | my $error = $@; |
364 | |
388 | |
365 | eval { |
389 | eval { |
|
|
390 | $DB_ENV->txn_checkpoint (0, 0, 0); |
|
|
391 | |
366 | undef %DB_TABLE; |
392 | undef %DB_TABLE; |
367 | undef $DB_ENV; |
393 | undef $DB_ENV; |
368 | |
394 | |
369 | Storable::store_fd [die => $error], $fh; |
395 | Storable::store_fd [die => $error], $fh; |
370 | }; |
396 | }; |
… | |
… | |
375 | close $fh; |
401 | close $fh; |
376 | CFPlus::fh_nonblocking $FH, 1; |
402 | CFPlus::fh_nonblocking $FH, 1; |
377 | |
403 | |
378 | $CB{die} = sub { die shift }; |
404 | $CB{die} = sub { die shift }; |
379 | |
405 | |
380 | $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
406 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
381 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
407 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
382 | $SYNC->start; |
408 | $SYNC->again unless $SYNC->is_active; |
383 | } |
409 | } |
384 | |
410 | |
385 | sub stop { |
411 | sub stop { |
386 | close $FH; |
412 | close $FH; |
387 | } |
413 | } |