1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | DC::DB - async. database and filesystem access for cfplus |
3 | DC::DB - async. database and filesystem access for deliantra |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use DC::DB; |
7 | use DC::DB; |
8 | |
8 | |
… | |
… | |
22 | use Config; |
22 | use Config; |
23 | use BDB; |
23 | use BDB; |
24 | |
24 | |
25 | use DC; |
25 | use DC; |
26 | |
26 | |
27 | our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
27 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
|
|
28 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
29 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
29 | |
30 | |
|
|
31 | if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { |
|
|
32 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
|
|
33 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
|
|
34 | } |
|
|
35 | |
30 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { |
36 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { |
31 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
37 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
32 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$DBDIR to $DB_HOME\n"; |
38 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
33 | } |
39 | } |
|
|
40 | |
|
|
41 | BDB::max_poll_time 0.03; |
|
|
42 | BDB::max_parallel 1; |
34 | |
43 | |
35 | our $DB_ENV; |
44 | our $DB_ENV; |
36 | our $DB_STATE; |
45 | our $DB_STATE; |
37 | our %DB_TABLE; |
46 | our %DB_TABLE; |
38 | |
47 | |
39 | sub open_db { |
48 | sub try_open_db { |
40 | mkdir $DB_HOME, 0777; |
49 | mkdir $DB_HOME, 0777; |
41 | |
50 | |
42 | $DB_ENV = db_env_create; |
51 | $DB_ENV = db_env_create; |
43 | |
52 | |
44 | $DB_ENV->set_errfile (\*STDERR); |
53 | $DB_ENV->set_errfile (\*STDERR); |
… | |
… | |
75 | } |
84 | } |
76 | } |
85 | } |
77 | |
86 | |
78 | ############################################################################# |
87 | ############################################################################# |
79 | |
88 | |
80 | unless (eval { open_db }) { |
89 | our $WATCHER; |
81 | warn "$@";#d# |
90 | our $SYNC; |
82 | eval { File::Path::rmtree $DB_HOME }; |
|
|
83 | open_db; |
|
|
84 | } |
|
|
85 | |
|
|
86 | our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
87 | |
|
|
88 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
89 | $_[0]->stop; |
|
|
90 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
91 | }; |
|
|
92 | |
|
|
93 | our $tilemap; |
91 | our $facemap; |
94 | |
92 | |
95 | sub exists($$$) { |
93 | sub exists($$$) { |
96 | my ($db, $key, $cb) = @_; |
94 | my ($db, $key, $cb) = @_; |
97 | |
95 | |
98 | my $data; |
96 | my $data; |
… | |
… | |
110 | }; |
108 | }; |
111 | } |
109 | } |
112 | |
110 | |
113 | sub put($$$$) { |
111 | sub put($$$$) { |
114 | my ($db, $key, $data, $cb) = @_; |
112 | my ($db, $key, $data, $cb) = @_; |
|
|
113 | warn "put $key ",(length $data),"\n";#d# |
115 | |
114 | |
116 | db_put table $db, undef, $key, $data, 0, sub { |
115 | db_put table $db, undef, $key, $data, 0, sub { |
117 | $cb->($!); |
116 | $cb->($!); |
118 | $SYNC->again unless $SYNC->is_active; |
117 | $SYNC->again unless $SYNC->is_active; |
119 | }; |
118 | }; |
… | |
… | |
141 | |
140 | |
142 | my $table = table "facemap"; |
141 | my $table = table "facemap"; |
143 | my $id; |
142 | my $id; |
144 | |
143 | |
145 | db_get $table, undef, $name, $id, 0; |
144 | db_get $table, undef, $name, $id, 0; |
146 | return $cb->($id) unless $!; |
145 | $! or return $cb->($id); |
147 | |
146 | |
148 | for (1..100) { |
147 | for (1..100) { |
149 | my $txn = $DB_ENV->txn_begin; |
148 | my $txn = $DB_ENV->txn_begin; |
150 | db_get $table, $txn, id => $id, 0; |
149 | db_get $table, $txn, id => $id, 0; |
151 | |
150 | |
… | |
… | |
154 | ++$id; |
153 | ++$id; |
155 | |
154 | |
156 | db_put $table, $txn, id => $id, 0; |
155 | db_put $table, $txn, id => $id, 0; |
157 | db_txn_finish $txn; |
156 | db_txn_finish $txn; |
158 | |
157 | |
|
|
158 | unless ($!) { |
|
|
159 | db_put $table, undef, $name => $id; |
159 | $SYNC->again unless $SYNC->is_active; |
160 | $SYNC->again unless $SYNC->is_active; |
160 | |
|
|
161 | return $cb->($id) unless $!; |
161 | return $cb->($id); |
|
|
162 | } |
162 | |
163 | |
163 | select undef, undef, undef, 0.01 * rand; |
164 | select undef, undef, undef, 0.01 * rand; |
164 | } |
165 | } |
165 | |
166 | |
166 | die "maximum number of transaction retries reached - database problems?"; |
167 | die "maximum number of transaction retries reached - database problems?"; |
167 | } |
168 | } |
168 | |
169 | |
169 | sub get_tile_id_sync($) { |
170 | sub get_tile_id_sync($) { |
170 | my ($name) = @_; |
171 | my ($name) = @_; |
171 | |
172 | |
172 | # fetch the full face table first |
|
|
173 | unless ($tilemap) { |
|
|
174 | do_table facemap => sub { |
|
|
175 | $tilemap = $_[0]; |
|
|
176 | delete $tilemap->{id}; |
|
|
177 | my %maptile = reverse %$tilemap;#d# |
|
|
178 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
|
|
179 | $tilemap = { };#d# |
|
|
180 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
181 | }#d# |
|
|
182 | }; |
|
|
183 | BDB::flush; |
|
|
184 | } |
|
|
185 | |
|
|
186 | $tilemap->{$name} ||= do { |
173 | $facemap->{$name} ||= do { |
187 | my $id; |
174 | my $id; |
188 | do_get_tile_id $name, sub { |
175 | do_get_tile_id $name, sub { |
189 | $id = $_[0]; |
176 | $id = $_[0]; |
190 | }; |
177 | }; |
191 | BDB::flush; |
178 | BDB::flush; |
… | |
… | |
223 | } |
210 | } |
224 | |
211 | |
225 | sub logprint($$$) { |
212 | sub logprint($$$) { |
226 | DC::DB::Server::req (logprint => @_); |
213 | DC::DB::Server::req (logprint => @_); |
227 | } |
214 | } |
|
|
215 | |
|
|
216 | ############################################################################# |
228 | |
217 | |
229 | package DC::DB::Server; |
218 | package DC::DB::Server; |
230 | |
219 | |
231 | use strict; |
220 | use strict; |
232 | |
221 | |
… | |
… | |
363 | my $oldfh = select $fh; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
364 | |
353 | |
365 | my $pid = fork; |
354 | my $pid = fork; |
366 | |
355 | |
367 | if (defined $pid && !$pid) { |
356 | if (defined $pid && !$pid) { |
368 | local $SIG{QUIT}; |
357 | local $SIG{QUIT} = "IGNORE"; |
369 | local $SIG{__DIE__}; |
358 | local $SIG{__DIE__}; |
370 | local $SIG{__WARN__}; |
359 | local $SIG{__WARN__}; |
371 | eval { |
360 | eval { |
372 | close $FH; |
361 | close $FH; |
373 | |
362 | |
… | |
… | |
412 | |
401 | |
413 | sub stop { |
402 | sub stop { |
414 | close $FH; |
403 | close $FH; |
415 | } |
404 | } |
416 | |
405 | |
|
|
406 | package DC::DB; |
|
|
407 | |
|
|
408 | sub open_db { |
|
|
409 | unless (eval { try_open_db }) { |
|
|
410 | warn "$@";#d# |
|
|
411 | eval { File::Path::rmtree $DB_HOME }; |
|
|
412 | try_open_db; |
|
|
413 | } |
|
|
414 | |
|
|
415 | # fetch the full face table first |
|
|
416 | unless ($facemap) { |
|
|
417 | do_table facemap => sub { |
|
|
418 | $facemap = $_[0]; |
|
|
419 | delete $facemap->{id}; |
|
|
420 | my %maptile = reverse %$facemap;#d# |
|
|
421 | if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d# |
|
|
422 | $facemap = { };#d# |
|
|
423 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
424 | }#d# |
|
|
425 | }; |
|
|
426 | } |
|
|
427 | |
|
|
428 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
429 | $SYNC = EV::timer_ns 0, 60, sub { |
|
|
430 | warn "SYNC\n";#d# |
|
|
431 | $_[0]->stop; |
|
|
432 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
433 | }; |
|
|
434 | } |
|
|
435 | |
|
|
436 | END { |
|
|
437 | %DB_TABLE = (); |
|
|
438 | undef $DB_ENV; |
|
|
439 | } |
|
|
440 | |
417 | 1; |
441 | 1; |
418 | |
442 | |
419 | =back |
443 | =back |
420 | |
444 | |
421 | =head1 AUTHOR |
445 | =head1 AUTHOR |