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 | } |
|
|
44 | |
|
|
45 | BDB::max_poll_time 0.03; |
|
|
46 | BDB::max_parallel 1; |
34 | |
47 | |
35 | our $DB_ENV; |
48 | our $DB_ENV; |
36 | our $DB_STATE; |
49 | our $DB_STATE; |
37 | our %DB_TABLE; |
50 | our %DB_TABLE; |
|
|
51 | our $TILE_SEQ; |
38 | |
52 | |
39 | sub open_db { |
53 | sub try_open_db { |
40 | mkdir $DB_HOME, 0777; |
54 | File::Path::mkpath [$DB_HOME]; |
41 | |
55 | |
42 | $DB_ENV = db_env_create; |
56 | my $env = db_env_create; |
43 | |
57 | |
44 | $DB_ENV->set_errfile (\*STDERR); |
58 | $env->set_errfile (\*STDERR); |
45 | $DB_ENV->set_msgfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
46 | $DB_ENV->set_verbose (-1, 1); |
60 | $env->set_verbose (-1, 1); |
47 | |
61 | |
48 | $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); |
49 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
50 | |
64 | |
51 | db_env_open $DB_ENV, $DB_HOME, |
65 | db_env_open $env, $DB_HOME, |
52 | 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, |
53 | 0666; |
67 | 0666; |
54 | |
68 | |
55 | $! 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; |
56 | |
72 | |
57 | 1 |
73 | 1 |
58 | } |
74 | } |
59 | |
75 | |
60 | sub table($) { |
76 | sub table($) { |
… | |
… | |
75 | } |
91 | } |
76 | } |
92 | } |
77 | |
93 | |
78 | ############################################################################# |
94 | ############################################################################# |
79 | |
95 | |
80 | unless (eval { open_db }) { |
96 | our $WATCHER; |
81 | warn "$@";#d# |
97 | 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; |
98 | our $facemap; |
94 | |
99 | |
95 | sub exists($$$) { |
100 | sub exists($$$) { |
96 | my ($db, $key, $cb) = @_; |
101 | my ($db, $key, $cb) = @_; |
97 | |
102 | |
98 | my $data; |
103 | my $data; |
… | |
… | |
140 | my ($name, $cb) = @_; |
145 | my ($name, $cb) = @_; |
141 | |
146 | |
142 | my $table = table "facemap"; |
147 | my $table = table "facemap"; |
143 | my $id; |
148 | my $id; |
144 | |
149 | |
145 | db_get $table, undef, $name, $id, 0; |
150 | db_get $table, undef, $name => $id, 0; |
146 | return $cb->($id) unless $!; |
151 | $! or return $cb->($id); |
147 | |
152 | |
148 | for (1..100) { |
153 | unless ($TILE_SEQ) { |
149 | my $txn = $DB_ENV->txn_begin; |
154 | $TILE_SEQ = $table->sequence; |
150 | 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 | } |
151 | |
159 | |
152 | $id = 64 if $id < 64; |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
153 | |
161 | |
154 | ++$id; |
162 | die "unable to allocate tile id: $!" |
155 | |
163 | if $!; |
156 | db_put $table, $txn, id => $id, 0; |
|
|
157 | db_txn_finish $txn; |
|
|
158 | |
|
|
159 | $SYNC->again unless $SYNC->is_active; |
|
|
160 | |
|
|
161 | return $cb->($id) unless $!; |
|
|
162 | |
|
|
163 | select undef, undef, undef, 0.01 * rand; |
|
|
164 | } |
164 | |
|
|
165 | db_put $table, undef, $name => $id, 0; |
|
|
166 | $cb->($id); |
165 | |
167 | |
166 | 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 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 | |
417 | 1; |
449 | 1; |
418 | |
450 | |
419 | =back |
451 | =back |
420 | |
452 | |
421 | =head1 AUTHOR |
453 | =head1 AUTHOR |