… | |
… | |
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 (); |
|
|
23 | use AnyEvent::Util (); |
22 | use Config; |
24 | use Config; |
23 | use BDB; |
25 | use BDB; |
|
|
26 | use Fcntl (); |
24 | |
27 | |
25 | use DC; |
28 | use DC; |
26 | |
29 | |
27 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
30 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
28 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
31 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
32 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
30 | |
33 | |
|
|
34 | sub FIRST_TILE_ID () { 64 } |
|
|
35 | |
|
|
36 | unless (-d $DB_HOME) { |
31 | if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { |
37 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
32 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
38 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
33 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
39 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
34 | } |
|
|
35 | |
|
|
36 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { |
40 | } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") { |
37 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
41 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
38 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
42 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
|
|
43 | } else { |
|
|
44 | File::Path::mkpath [$DB_HOME] |
|
|
45 | or die "unable to create database directory $DB_HOME: $!"; |
|
|
46 | } |
39 | } |
47 | } |
40 | |
48 | |
41 | BDB::max_poll_time 0.03; |
49 | BDB::max_poll_time 0.03; |
42 | BDB::max_parallel 1; |
50 | BDB::max_parallel 1; |
43 | |
51 | |
44 | our $DB_ENV; |
52 | our $DB_ENV; |
|
|
53 | our $DB_ENV_FH; |
45 | our $DB_STATE; |
54 | our $DB_STATE; |
46 | our %DB_TABLE; |
55 | our %DB_TABLE; |
|
|
56 | our $TILE_SEQ; |
|
|
57 | |
|
|
58 | sub all_databases { |
|
|
59 | opendir my $fh, $DB_HOME |
|
|
60 | or return; |
|
|
61 | |
|
|
62 | grep !/^(?:\.|log\.|_)/, readdir $fh |
|
|
63 | } |
|
|
64 | |
|
|
65 | sub try_verify_env($) { |
|
|
66 | my ($env) = @_; |
|
|
67 | |
|
|
68 | open my $lock, "+>$DB_HOME/__lock" |
|
|
69 | or die "__lock: $!"; |
|
|
70 | |
|
|
71 | flock $lock, &Fcntl::LOCK_EX |
|
|
72 | or die "flock: $!"; |
|
|
73 | |
|
|
74 | # we look at the __db.register env file that has been created by now |
|
|
75 | # and check for the number of registered processes - if there is |
|
|
76 | # only one, we verify all databases, otherwise we skip this |
|
|
77 | # we MUST NOT close the filehandle as longa swe keep the env open, as |
|
|
78 | # this destroys the record locks on it. |
|
|
79 | open $DB_ENV_FH, "<$DB_HOME/__db.register" |
|
|
80 | or die "__db.register: $!"; |
|
|
81 | |
|
|
82 | # __db.register contains one record per process, with X signifying |
|
|
83 | # empty records (of course, this is completely private to bdb...) |
|
|
84 | my $count = grep /^[^X]/, <$DB_ENV_FH>; |
|
|
85 | |
|
|
86 | if ($count == 1) { |
|
|
87 | # if any databases are corrupted, we simply delete all of them |
|
|
88 | |
|
|
89 | for (all_databases) { |
|
|
90 | my $dbh = db_create $env |
|
|
91 | or last; |
|
|
92 | |
|
|
93 | # a failed verify will panic the environment, which is fine with us |
|
|
94 | db_verify $dbh, "$DB_HOME/$_"; |
|
|
95 | |
|
|
96 | return if $!; # nuke database and recreate if verification failure |
|
|
97 | } |
|
|
98 | |
|
|
99 | } |
|
|
100 | |
|
|
101 | # close probably cleans those up, but we also want to run on windows, |
|
|
102 | # so better be safe. |
|
|
103 | flock $lock, &Fcntl::LOCK_UN |
|
|
104 | or die "funlock: $!"; |
|
|
105 | |
|
|
106 | 1 |
|
|
107 | } |
47 | |
108 | |
48 | sub try_open_db { |
109 | sub try_open_db { |
49 | mkdir $DB_HOME, 0777; |
110 | File::Path::mkpath [$DB_HOME]; |
50 | |
111 | |
|
|
112 | undef $DB_ENV; |
|
|
113 | undef $DB_ENV_FH; |
|
|
114 | |
51 | $DB_ENV = db_env_create; |
115 | my $env = db_env_create; |
52 | |
116 | |
53 | $DB_ENV->set_errfile (\*STDERR); |
117 | $env->set_errfile (\*STDERR); |
54 | $DB_ENV->set_msgfile (\*STDERR); |
118 | $env->set_msgfile (\*STDERR); |
55 | $DB_ENV->set_verbose (-1, 1); |
119 | $env->set_verbose (-1, 1); |
56 | |
120 | |
57 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
121 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
122 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
123 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
124 | |
|
|
125 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
126 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
127 | |
58 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
128 | $env->set_cachesize (0, 2048 * 1024, 0); |
59 | |
129 | |
60 | db_env_open $DB_ENV, $DB_HOME, |
130 | db_env_open $env, $DB_HOME, |
61 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
131 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
62 | 0666; |
132 | 0666; |
63 | |
133 | |
64 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
134 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
|
|
135 | |
|
|
136 | # now we go through the registered processes, if there is only one, we verify all files |
|
|
137 | # to make sure windows didn'T corrupt them (as windows does....) |
|
|
138 | try_verify_env $env |
|
|
139 | or die "database environment failed verification"; |
|
|
140 | |
|
|
141 | $DB_ENV = $env; |
65 | |
142 | |
66 | 1 |
143 | 1 |
67 | } |
144 | } |
68 | |
145 | |
69 | sub table($) { |
146 | sub table($) { |
70 | $DB_TABLE{$_[0]} ||= do { |
147 | $DB_TABLE{$_[0]} ||= do { |
71 | my ($table) = @_; |
148 | my ($table) = @_; |
72 | |
149 | |
73 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
150 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
74 | |
151 | |
|
|
152 | $DB_ENV#d# |
|
|
153 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
|
|
154 | |
75 | my $db = db_create $DB_ENV; |
155 | my $db = db_create $DB_ENV; |
76 | $db->set_flags (BDB::CHKSUM); |
156 | $db->set_flags (BDB::CHKSUM); |
77 | |
157 | |
78 | db_open $db, undef, $table, undef, BDB::BTREE, |
158 | db_open $db, undef, $table, undef, BDB::BTREE, |
79 | BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; |
159 | BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; |
… | |
… | |
86 | |
166 | |
87 | ############################################################################# |
167 | ############################################################################# |
88 | |
168 | |
89 | our $WATCHER; |
169 | our $WATCHER; |
90 | our $SYNC; |
170 | our $SYNC; |
91 | our $tilemap; |
171 | our $facemap; |
92 | |
172 | |
93 | sub exists($$$) { |
173 | sub exists($$$) { |
94 | my ($db, $key, $cb) = @_; |
174 | my ($db, $key, $cb) = @_; |
95 | |
175 | |
96 | my $data; |
176 | my $data; |
… | |
… | |
138 | my ($name, $cb) = @_; |
218 | my ($name, $cb) = @_; |
139 | |
219 | |
140 | my $table = table "facemap"; |
220 | my $table = table "facemap"; |
141 | my $id; |
221 | my $id; |
142 | |
222 | |
143 | db_get $table, undef, $name, $id, 0; |
223 | db_get $table, undef, $name => $id, 0; |
144 | return $cb->($id) unless $!; |
224 | $! or return $cb->($id); |
145 | |
225 | |
146 | for (1..100) { |
226 | unless ($TILE_SEQ) { |
147 | my $txn = $DB_ENV->txn_begin; |
227 | $TILE_SEQ = $table->sequence; |
148 | db_get $table, $txn, id => $id, 0; |
228 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
|
|
229 | $TILE_SEQ->set_cachesize (0); |
|
|
230 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
|
|
231 | } |
149 | |
232 | |
150 | $id = 64 if $id < 64; |
233 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
151 | |
234 | |
152 | ++$id; |
235 | die "unable to allocate tile id: $!" |
153 | |
236 | if $!; |
154 | db_put $table, $txn, id => $id, 0; |
|
|
155 | db_txn_finish $txn; |
|
|
156 | |
|
|
157 | $SYNC->again unless $SYNC->is_active; |
|
|
158 | |
|
|
159 | return $cb->($id) unless $!; |
|
|
160 | |
|
|
161 | select undef, undef, undef, 0.01 * rand; |
|
|
162 | } |
237 | |
|
|
238 | db_put $table, undef, $name => $id, 0; |
|
|
239 | $cb->($id); |
163 | |
240 | |
164 | die "maximum number of transaction retries reached - database problems?"; |
|
|
165 | } |
241 | } |
166 | |
242 | |
167 | sub get_tile_id_sync($) { |
243 | sub get_tile_id_sync($) { |
168 | my ($name) = @_; |
244 | my ($name) = @_; |
169 | |
245 | |
170 | $tilemap->{$name} ||= do { |
246 | $facemap->{$name} ||= do { |
171 | my $id; |
247 | my $id; |
172 | do_get_tile_id $name, sub { |
248 | do_get_tile_id $name, sub { |
173 | $id = $_[0]; |
249 | $id = $_[0]; |
174 | }; |
250 | }; |
175 | BDB::flush; |
251 | BDB::flush; |
… | |
… | |
341 | |
417 | |
342 | print { $LOG_FH{$path} } "$ts $line\n" |
418 | print { $LOG_FH{$path} } "$ts $line\n" |
343 | } |
419 | } |
344 | |
420 | |
345 | sub run { |
421 | sub run { |
346 | ($FH, my $fh) = DC::socketpipe; |
422 | ($FH, my $fh) = AnyEvent::Util::portable_socketpair |
|
|
423 | or die "unable to create database socketpair: $!"; |
347 | |
424 | |
348 | my $oldfh = select $FH; $| = 1; select $oldfh; |
425 | my $oldfh = select $FH; $| = 1; select $oldfh; |
349 | my $oldfh = select $fh; $| = 1; select $oldfh; |
426 | my $oldfh = select $fh; $| = 1; select $oldfh; |
350 | |
427 | |
351 | my $pid = fork; |
428 | my $pid = fork; |
… | |
… | |
400 | close $FH; |
477 | close $FH; |
401 | } |
478 | } |
402 | |
479 | |
403 | package DC::DB; |
480 | package DC::DB; |
404 | |
481 | |
|
|
482 | sub nuke_db { |
|
|
483 | undef $DB_ENV; |
|
|
484 | undef $DB_ENV_FH; |
|
|
485 | |
|
|
486 | File::Path::mkpath [$DB_HOME]; |
|
|
487 | eval { File::Path::rmtree $DB_HOME }; |
|
|
488 | } |
|
|
489 | |
405 | sub open_db { |
490 | sub open_db { |
406 | unless (eval { try_open_db }) { |
491 | unless (eval { try_open_db }) { |
407 | warn "$@";#d# |
492 | warn "$@";#d# |
408 | eval { File::Path::rmtree $DB_HOME }; |
493 | eval { nuke_db }; |
409 | try_open_db; |
494 | try_open_db; |
410 | } |
495 | } |
411 | |
496 | |
412 | # fetch the full face table first |
497 | # fetch the full face table first |
413 | unless ($tilemap) { |
498 | unless ($facemap) { |
414 | do_table facemap => sub { |
499 | do_table facemap => sub { |
415 | $tilemap = $_[0]; |
500 | $facemap = $_[0]; |
416 | delete $tilemap->{id}; |
501 | delete $facemap->{id}; |
417 | my %maptile = reverse %$tilemap;#d# |
502 | my %maptile = reverse %$facemap;#d# |
418 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
503 | if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d# |
419 | $tilemap = { };#d# |
504 | $facemap = { };#d# |
420 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
505 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
421 | }#d# |
506 | }#d# |
422 | }; |
507 | }; |
423 | } |
508 | } |
424 | |
509 | |
… | |
… | |
428 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
513 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
429 | }; |
514 | }; |
430 | } |
515 | } |
431 | |
516 | |
432 | END { |
517 | END { |
|
|
518 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
519 | if $DB_ENV; |
|
|
520 | |
|
|
521 | undef $TILE_SEQ; |
433 | %DB_TABLE = (); |
522 | %DB_TABLE = (); |
434 | undef $DB_ENV; |
523 | undef $DB_ENV; |
435 | } |
524 | } |
436 | |
525 | |
437 | 1; |
526 | 1; |