… | |
… | |
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 | |
… | |
… | |
70 | } |
69 | } |
71 | |
70 | |
72 | our $tilemap; |
71 | our $tilemap; |
73 | |
72 | |
74 | sub get_tile_id_sync($) { |
73 | sub get_tile_id_sync($) { |
75 | my ($hash) = @_; |
74 | my ($name) = @_; |
76 | |
75 | |
77 | # fetch the full face table first |
76 | # fetch the full face table first |
78 | unless ($tilemap) { |
77 | unless ($tilemap) { |
79 | 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 | }); |
80 | sync; |
87 | sync; |
81 | } |
88 | } |
82 | |
89 | |
83 | $tilemap->{$hash} ||= do { |
90 | $tilemap->{$name} ||= do { |
84 | my $id; |
91 | my $id; |
85 | CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); |
92 | CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] }); |
86 | sync; |
93 | sync; |
87 | $id |
94 | $id |
88 | } |
95 | } |
89 | } |
96 | } |
90 | |
97 | |
… | |
… | |
141 | our ($fh_r_watcher, $fh_w_watcher); |
148 | our ($fh_r_watcher, $fh_w_watcher); |
142 | our $sync_timer; |
149 | our $sync_timer; |
143 | our $write_buf; |
150 | our $write_buf; |
144 | our $read_buf; |
151 | our $read_buf; |
145 | |
152 | |
146 | our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { |
153 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
154 | $_[0]->stop; |
147 | CFPlus::DB::Server::req (sync => sub { }); |
155 | CFPlus::DB::Server::req (sync => sub { }); |
148 | $_[0]->w->stop; |
|
|
149 | }); |
156 | }; |
150 | |
157 | |
151 | sub fh_write { |
158 | sub fh_write { |
152 | my $len = syswrite $FH, $write_buf; |
159 | my $len = syswrite $FH, $write_buf; |
153 | |
160 | |
154 | substr $write_buf, 0, $len, ""; |
161 | substr $write_buf, 0, $len, ""; |
… | |
… | |
198 | my $id = ++$ID; |
205 | my $id = ++$ID; |
199 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
206 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
200 | $CB{$id} = $cb; |
207 | $CB{$id} = $cb; |
201 | |
208 | |
202 | $fh_w_watcher->start; |
209 | $fh_w_watcher->start; |
203 | $SYNC->start; |
210 | $SYNC->again unless $SYNC->is_active; |
204 | } |
211 | } |
205 | |
212 | |
206 | sub do_sync { |
213 | sub do_sync { |
207 | $DB_ENV->txn_checkpoint (0, 0, 0); |
214 | $DB_ENV->txn_checkpoint (0, 0, 0); |
208 | () |
215 | () |
… | |
… | |
248 | |
255 | |
249 | \%kv |
256 | \%kv |
250 | } |
257 | } |
251 | |
258 | |
252 | sub do_get_tile_id { |
259 | sub do_get_tile_id { |
253 | my ($hash) = @_; |
260 | my ($name) = @_; |
254 | |
261 | |
255 | my $id; |
262 | my $id; |
256 | my $table = table "facemap"; |
263 | my $table = table "facemap"; |
257 | |
264 | |
258 | return $id |
265 | return $id |
259 | if $table->db_get ($hash, $id) == 0; |
266 | if $table->db_get ($name, $id) == 0; |
260 | |
267 | |
261 | for (1..100) { |
268 | for (1..100) { |
262 | my $txn = $DB_ENV->txn_begin; |
269 | my $txn = $DB_ENV->txn_begin; |
263 | my $status = $table->db_get (id => $id); |
270 | my $status = $table->db_get (id => $id); |
264 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
271 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
265 | $id = ($id || 64) + 1; |
272 | $id = ($id || 64) + 1; |
266 | if ($table->db_put (id => $id) == 0 |
273 | if ($table->db_put (id => $id) == 0 |
267 | && $table->db_put ($hash => $id) == 0) { |
274 | && $table->db_put ($name => $id) == 0) { |
268 | $txn->txn_commit; |
275 | $txn->txn_commit; |
269 | |
276 | |
270 | return $id; |
277 | return $id; |
271 | } |
278 | } |
272 | } |
279 | } |
… | |
… | |
370 | my ($id, $type, @args) = @$req; |
377 | my ($id, $type, @args) = @$req; |
371 | my $cb = CFPlus::DB::Server->can ("do_$type") |
378 | my $cb = CFPlus::DB::Server->can ("do_$type") |
372 | or die "$type: unknown database request type\n"; |
379 | or die "$type: unknown database request type\n"; |
373 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
380 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
374 | (syswrite $fh, $res) == length $res |
381 | (syswrite $fh, $res) == length $res |
375 | or die; |
382 | or die "DB::write: $!"; |
376 | } |
383 | } |
377 | }; |
384 | }; |
378 | |
385 | |
379 | my $error = $@; |
386 | my $error = $@; |
380 | |
387 | |
… | |
… | |
383 | undef $DB_ENV; |
390 | undef $DB_ENV; |
384 | |
391 | |
385 | Storable::store_fd [die => $error], $fh; |
392 | Storable::store_fd [die => $error], $fh; |
386 | }; |
393 | }; |
387 | |
394 | |
|
|
395 | $DB_ENV->txn_checkpoint (0, 0, 0); |
388 | CFPlus::_exit 0; |
396 | CFPlus::_exit 0; |
389 | } |
397 | } |
390 | |
398 | |
391 | close $fh; |
399 | close $fh; |
392 | CFPlus::fh_nonblocking $FH, 1; |
400 | CFPlus::fh_nonblocking $FH, 1; |
393 | |
401 | |
394 | $CB{die} = sub { die shift }; |
402 | $CB{die} = sub { die shift }; |
395 | |
403 | |
396 | $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
404 | $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
397 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
405 | $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
398 | $SYNC->start; |
406 | $SYNC->again unless $SYNC->is_active; |
399 | } |
407 | } |
400 | |
408 | |
401 | sub stop { |
409 | sub stop { |
402 | close $FH; |
410 | close $FH; |
403 | } |
411 | } |