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 | |
… | |
… | |
15 | package DC::DB; |
15 | package DC::DB; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
|
|
20 | use File::Path (); |
20 | use Carp (); |
21 | use Carp (); |
21 | use Storable (); |
22 | use Storable (); |
22 | use Config; |
23 | use Config; |
23 | use BDB; |
24 | use BDB; |
24 | |
25 | |
25 | use DC; |
26 | use DC; |
26 | |
27 | |
27 | our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
|
|
29 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
28 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
29 | |
31 | |
30 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { |
32 | unless (-d $DB_HOME) { |
|
|
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
|
|
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
|
|
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
|
|
36 | } elsif (-d "$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"; |
|
|
39 | } else { |
|
|
40 | File::Path::mkpath [$DB_HOME] |
|
|
41 | or die "unable to create database directory $DB_HOME: $!"; |
|
|
42 | } |
33 | } |
43 | } |
34 | |
44 | |
35 | BDB::max_poll_time 0.03; |
45 | BDB::max_poll_time 0.03; |
|
|
46 | BDB::max_parallel 1; |
36 | |
47 | |
37 | our $DB_ENV; |
48 | our $DB_ENV; |
38 | our $DB_STATE; |
49 | our $DB_STATE; |
39 | our %DB_TABLE; |
50 | our %DB_TABLE; |
|
|
51 | our $TILE_SEQ; |
40 | |
52 | |
41 | sub open_db { |
53 | sub try_open_db { |
42 | mkdir $DB_HOME, 0777; |
54 | File::Path::mkpath [$DB_HOME]; |
43 | |
55 | |
44 | $DB_ENV = db_env_create; |
56 | my $env = db_env_create; |
45 | |
57 | |
46 | $DB_ENV->set_errfile (\*STDERR); |
58 | $env->set_errfile (\*STDERR); |
47 | $DB_ENV->set_msgfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
48 | $DB_ENV->set_verbose (-1, 1); |
60 | $env->set_verbose (-1, 1); |
49 | |
61 | |
50 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
62 | $env->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
51 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
52 | |
64 | |
53 | db_env_open $DB_ENV, $DB_HOME, |
65 | db_env_open $env, $DB_HOME, |
54 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
66 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
55 | 0666; |
67 | 0666; |
56 | |
68 | |
57 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
69 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
|
|
70 | |
|
|
71 | $DB_ENV = $env; |
58 | |
72 | |
59 | 1 |
73 | 1 |
60 | } |
74 | } |
61 | |
75 | |
62 | sub table($) { |
76 | sub table($) { |
… | |
… | |
77 | } |
91 | } |
78 | } |
92 | } |
79 | |
93 | |
80 | ############################################################################# |
94 | ############################################################################# |
81 | |
95 | |
82 | unless (eval { open_db }) { |
96 | our $WATCHER; |
83 | warn "$@";#d# |
97 | our $SYNC; |
84 | eval { File::Path::rmtree $DB_HOME }; |
|
|
85 | open_db; |
|
|
86 | } |
|
|
87 | |
|
|
88 | our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
89 | |
|
|
90 | our $SYNC = EV::timer_ns 0, 60, sub { |
|
|
91 | $_[0]->stop; |
|
|
92 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
93 | }; |
|
|
94 | |
|
|
95 | our $tilemap; |
98 | our $facemap; |
96 | |
99 | |
97 | sub exists($$$) { |
100 | sub exists($$$) { |
98 | my ($db, $key, $cb) = @_; |
101 | my ($db, $key, $cb) = @_; |
99 | |
102 | |
100 | my $data; |
103 | my $data; |
… | |
… | |
142 | my ($name, $cb) = @_; |
145 | my ($name, $cb) = @_; |
143 | |
146 | |
144 | my $table = table "facemap"; |
147 | my $table = table "facemap"; |
145 | my $id; |
148 | my $id; |
146 | |
149 | |
147 | db_get $table, undef, $name, $id, 0; |
150 | db_get $table, undef, $name => $id, 0; |
148 | return $cb->($id) unless $!; |
151 | $! or return $cb->($id); |
149 | |
152 | |
150 | for (1..100) { |
153 | unless ($TILE_SEQ) { |
151 | my $txn = $DB_ENV->txn_begin; |
154 | $TILE_SEQ = $table->sequence; |
152 | db_get $table, $txn, id => $id, 0; |
155 | $TILE_SEQ->initial_value (64); |
|
|
156 | $TILE_SEQ->set_cachesize (0); |
|
|
157 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
|
|
158 | } |
153 | |
159 | |
154 | $id = 64 if $id < 64; |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
155 | |
161 | |
156 | ++$id; |
162 | die "unable to allocate tile id: $!" |
157 | |
163 | if $!; |
158 | db_put $table, $txn, id => $id, 0; |
|
|
159 | db_txn_finish $txn; |
|
|
160 | |
|
|
161 | $SYNC->again unless $SYNC->is_active; |
|
|
162 | |
|
|
163 | return $cb->($id) unless $!; |
|
|
164 | |
|
|
165 | select undef, undef, undef, 0.01 * rand; |
|
|
166 | } |
164 | |
|
|
165 | db_put $table, undef, $name => $id, 0; |
|
|
166 | $cb->($id); |
167 | |
167 | |
168 | die "maximum number of transaction retries reached - database problems?"; |
|
|
169 | } |
168 | } |
170 | |
169 | |
171 | sub get_tile_id_sync($) { |
170 | sub get_tile_id_sync($) { |
172 | my ($name) = @_; |
171 | my ($name) = @_; |
173 | |
172 | |
174 | $tilemap->{$name} ||= do { |
173 | $facemap->{$name} ||= do { |
175 | my $id; |
174 | my $id; |
176 | do_get_tile_id $name, sub { |
175 | do_get_tile_id $name, sub { |
177 | $id = $_[0]; |
176 | $id = $_[0]; |
178 | }; |
177 | }; |
179 | BDB::flush; |
178 | BDB::flush; |
… | |
… | |
213 | sub logprint($$$) { |
212 | sub logprint($$$) { |
214 | DC::DB::Server::req (logprint => @_); |
213 | DC::DB::Server::req (logprint => @_); |
215 | } |
214 | } |
216 | |
215 | |
217 | ############################################################################# |
216 | ############################################################################# |
218 | |
|
|
219 | # fetch the full face table first |
|
|
220 | unless ($tilemap) { |
|
|
221 | do_table facemap => sub { |
|
|
222 | $tilemap = $_[0]; |
|
|
223 | delete $tilemap->{id}; |
|
|
224 | my %maptile = reverse %$tilemap;#d# |
|
|
225 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
|
|
226 | $tilemap = { };#d# |
|
|
227 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
228 | }#d# |
|
|
229 | }; |
|
|
230 | } |
|
|
231 | |
217 | |
232 | package DC::DB::Server; |
218 | package DC::DB::Server; |
233 | |
219 | |
234 | use strict; |
220 | use strict; |
235 | |
221 | |
… | |
… | |
415 | |
401 | |
416 | sub stop { |
402 | sub stop { |
417 | close $FH; |
403 | close $FH; |
418 | } |
404 | } |
419 | |
405 | |
|
|
406 | package DC::DB; |
|
|
407 | |
|
|
408 | sub nuke_db { |
|
|
409 | File::Path::mkpath [$DB_HOME]; |
|
|
410 | eval { File::Path::rmtree $DB_HOME }; |
|
|
411 | } |
|
|
412 | |
|
|
413 | sub open_db { |
|
|
414 | unless (eval { try_open_db }) { |
|
|
415 | warn "$@";#d# |
|
|
416 | eval { nuke_db }; |
|
|
417 | try_open_db; |
|
|
418 | } |
|
|
419 | |
|
|
420 | # fetch the full face table first |
|
|
421 | unless ($facemap) { |
|
|
422 | do_table facemap => sub { |
|
|
423 | $facemap = $_[0]; |
|
|
424 | delete $facemap->{id}; |
|
|
425 | my %maptile = reverse %$facemap;#d# |
|
|
426 | if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d# |
|
|
427 | $facemap = { };#d# |
|
|
428 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
|
|
429 | }#d# |
|
|
430 | }; |
|
|
431 | } |
|
|
432 | |
|
|
433 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
434 | $SYNC = EV::timer_ns 0, 60, sub { |
|
|
435 | $_[0]->stop; |
|
|
436 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
437 | }; |
|
|
438 | } |
|
|
439 | |
|
|
440 | END { |
|
|
441 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
442 | if $DB_ENV; |
|
|
443 | |
|
|
444 | undef $TILE_SEQ; |
|
|
445 | %DB_TABLE = (); |
|
|
446 | undef $DB_ENV; |
|
|
447 | } |
|
|
448 | |
420 | 1; |
449 | 1; |
421 | |
450 | |
422 | =back |
451 | =back |
423 | |
452 | |
424 | =head1 AUTHOR |
453 | =head1 AUTHOR |