… | |
… | |
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 | |
… | |
… | |
110 | -Cachesize => 8_000_000, |
117 | -Cachesize => 8_000_000, |
111 | -ErrFile => "$DB_HOME/errorlog.txt", |
118 | -ErrFile => "$DB_HOME/errorlog.txt", |
112 | # -ErrPrefix => "DATABASE", |
119 | # -ErrPrefix => "DATABASE", |
113 | -Verbose => 1, |
120 | -Verbose => 1, |
114 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
121 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
115 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
122 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, |
116 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
123 | or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; |
117 | |
124 | |
118 | 1 |
125 | 1 |
119 | } |
126 | } |
120 | |
127 | |
… | |
… | |
128 | -Env => $DB_ENV, |
135 | -Env => $DB_ENV, |
129 | -Filename => $table, |
136 | -Filename => $table, |
130 | # -Filename => "database", |
137 | # -Filename => "database", |
131 | # -Subname => $table, |
138 | # -Subname => $table, |
132 | -Property => DB_CHKSUM, |
139 | -Property => DB_CHKSUM, |
133 | -Flags => DB_CREATE | DB_UPGRADE, |
140 | -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, |
134 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
141 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
135 | } |
142 | } |
136 | } |
143 | } |
137 | |
144 | |
138 | our %CB; |
145 | our %CB; |
… | |
… | |
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 => 10, max => 60, 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 | } |
273 | $txn->txn_abort; |
280 | $txn->txn_abort; |
|
|
281 | select undef, undef, undef, 0.01 * rand; |
274 | } |
282 | } |
275 | |
283 | |
276 | die "maximum number of transaction retries reached - database problems?"; |
284 | die "maximum number of transaction retries reached - database problems?"; |
277 | } |
285 | } |
278 | |
286 | |
… | |
… | |
369 | my ($id, $type, @args) = @$req; |
377 | my ($id, $type, @args) = @$req; |
370 | my $cb = CFPlus::DB::Server->can ("do_$type") |
378 | my $cb = CFPlus::DB::Server->can ("do_$type") |
371 | or die "$type: unknown database request type\n"; |
379 | or die "$type: unknown database request type\n"; |
372 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
380 | my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
373 | (syswrite $fh, $res) == length $res |
381 | (syswrite $fh, $res) == length $res |
374 | or die; |
382 | or die "DB::write: $!"; |
375 | } |
383 | } |
376 | }; |
384 | }; |
377 | |
385 | |
378 | my $error = $@; |
386 | my $error = $@; |
379 | |
387 | |
… | |
… | |
382 | undef $DB_ENV; |
390 | undef $DB_ENV; |
383 | |
391 | |
384 | Storable::store_fd [die => $error], $fh; |
392 | Storable::store_fd [die => $error], $fh; |
385 | }; |
393 | }; |
386 | |
394 | |
|
|
395 | $DB_ENV->txn_checkpoint (0, 0, 0); |
387 | CFPlus::_exit 0; |
396 | CFPlus::_exit 0; |
388 | } |
397 | } |
389 | |
398 | |
390 | close $fh; |
399 | close $fh; |
391 | CFPlus::fh_nonblocking $FH, 1; |
400 | CFPlus::fh_nonblocking $FH, 1; |
392 | |
401 | |
393 | $CB{die} = sub { die shift }; |
402 | $CB{die} = sub { die shift }; |
394 | |
403 | |
395 | $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; |
396 | $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; |
397 | $SYNC->start; |
406 | $SYNC->again unless $SYNC->is_active; |
398 | } |
407 | } |
399 | |
408 | |
400 | sub stop { |
409 | sub stop { |
401 | close $FH; |
410 | close $FH; |
402 | } |
411 | } |