… | |
… | |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package DC::DB; |
15 | package DC::DB; |
16 | |
16 | |
17 | use strict; |
17 | use common::sense; |
18 | use utf8; |
|
|
19 | |
18 | |
20 | use File::Path (); |
19 | use File::Path (); |
21 | use Carp (); |
20 | use Carp (); |
22 | use Storable (); |
21 | use Storable (); |
|
|
22 | use AnyEvent::Util (); |
23 | use Config; |
23 | use Config; |
24 | use BDB; |
24 | use BDB; |
|
|
25 | use Fcntl (); |
25 | |
26 | |
26 | use DC; |
27 | use DC; |
27 | |
28 | |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
29 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
30 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
31 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
32 | |
|
|
33 | sub FIRST_TILE_ID () { 64 } |
31 | |
34 | |
32 | unless (-d $DB_HOME) { |
35 | unless (-d $DB_HOME) { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
36 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
37 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
38 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
… | |
… | |
44 | |
47 | |
45 | BDB::max_poll_time 0.03; |
48 | BDB::max_poll_time 0.03; |
46 | BDB::max_parallel 1; |
49 | BDB::max_parallel 1; |
47 | |
50 | |
48 | our $DB_ENV; |
51 | our $DB_ENV; |
|
|
52 | our $DB_ENV_FH; |
49 | our $DB_STATE; |
53 | our $DB_STATE; |
50 | our %DB_TABLE; |
54 | our %DB_TABLE; |
51 | our $TILE_SEQ; |
55 | our $TILE_SEQ; |
52 | |
56 | |
|
|
57 | sub all_databases { |
|
|
58 | opendir my $fh, $DB_HOME |
|
|
59 | or return; |
|
|
60 | |
|
|
61 | grep !/^(?:\.|log\.|_)/, readdir $fh |
|
|
62 | } |
|
|
63 | |
|
|
64 | sub try_verify_env($) { |
|
|
65 | my ($env) = @_; |
|
|
66 | |
|
|
67 | open my $lock, "+>$DB_HOME/__lock" |
|
|
68 | or die "__lock: $!"; |
|
|
69 | |
|
|
70 | flock $lock, &Fcntl::LOCK_EX |
|
|
71 | or die "flock: $!"; |
|
|
72 | |
|
|
73 | # we look at the __db.register env file that has been created by now |
|
|
74 | # and check for the number of registered processes - if there is |
|
|
75 | # only one, we verify all databases, otherwise we skip this |
|
|
76 | # we MUST NOT close the filehandle as longa swe keep the env open, as |
|
|
77 | # this destroys the record locks on it. |
|
|
78 | open $DB_ENV_FH, "<$DB_HOME/__db.register" |
|
|
79 | or die "__db.register: $!"; |
|
|
80 | |
|
|
81 | # __db.register contains one record per process, with X signifying |
|
|
82 | # empty records (of course, this is completely private to bdb...) |
|
|
83 | my $count = grep /^[^X]/, <$DB_ENV_FH>; |
|
|
84 | |
|
|
85 | if ($count == 1) { |
|
|
86 | # if any databases are corrupted, we simply delete all of them |
|
|
87 | |
|
|
88 | for (all_databases) { |
|
|
89 | my $dbh = db_create $env |
|
|
90 | or last; |
|
|
91 | |
|
|
92 | # a failed verify will panic the environment, which is fine with us |
|
|
93 | db_verify $dbh, "$DB_HOME/$_"; |
|
|
94 | |
|
|
95 | return if $!; # nuke database and recreate if verification failure |
|
|
96 | } |
|
|
97 | |
|
|
98 | } |
|
|
99 | |
|
|
100 | # close probably cleans those up, but we also want to run on windows, |
|
|
101 | # so better be safe. |
|
|
102 | flock $lock, &Fcntl::LOCK_UN |
|
|
103 | or die "funlock: $!"; |
|
|
104 | |
|
|
105 | 1 |
|
|
106 | } |
|
|
107 | |
53 | sub try_open_db { |
108 | sub try_open_db { |
54 | File::Path::mkpath [$DB_HOME]; |
109 | File::Path::mkpath [$DB_HOME]; |
|
|
110 | |
|
|
111 | undef $DB_ENV; |
|
|
112 | undef $DB_ENV_FH; |
55 | |
113 | |
56 | my $env = db_env_create; |
114 | my $env = db_env_create; |
57 | |
115 | |
58 | $env->set_errfile (\*STDERR); |
116 | $env->set_errfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
117 | $env->set_msgfile (\*STDERR); |
60 | $env->set_verbose (-1, 1); |
118 | $env->set_verbose (-1, 1); |
61 | |
119 | |
62 | $env->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
120 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
121 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
122 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
123 | |
|
|
124 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
125 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
126 | |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
127 | $env->set_cachesize (0, 2048 * 1024, 0); |
64 | |
128 | |
65 | db_env_open $env, $DB_HOME, |
129 | db_env_open $env, $DB_HOME, |
66 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
130 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
67 | 0666; |
131 | 0666; |
68 | |
132 | |
69 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
133 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
70 | |
134 | |
|
|
135 | # now we go through the registered processes, if there is only one, we verify all files |
|
|
136 | # to make sure windows didn'T corrupt them (as windows does....) |
|
|
137 | try_verify_env $env |
|
|
138 | or die "database environment failed verification"; |
|
|
139 | |
71 | $DB_ENV = $env; |
140 | $DB_ENV = $env; |
72 | |
141 | |
73 | 1 |
142 | 1 |
74 | } |
143 | } |
75 | |
144 | |
76 | sub table($) { |
145 | sub table($) { |
77 | $DB_TABLE{$_[0]} ||= do { |
146 | $DB_TABLE{$_[0]} ||= do { |
78 | my ($table) = @_; |
147 | my ($table) = @_; |
79 | |
148 | |
80 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
149 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
150 | |
|
|
151 | $DB_ENV#d# |
|
|
152 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
81 | |
153 | |
82 | my $db = db_create $DB_ENV; |
154 | my $db = db_create $DB_ENV; |
83 | $db->set_flags (BDB::CHKSUM); |
155 | $db->set_flags (BDB::CHKSUM); |
84 | |
156 | |
85 | db_open $db, undef, $table, undef, BDB::BTREE, |
157 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
150 | db_get $table, undef, $name => $id, 0; |
222 | db_get $table, undef, $name => $id, 0; |
151 | $! or return $cb->($id); |
223 | $! or return $cb->($id); |
152 | |
224 | |
153 | unless ($TILE_SEQ) { |
225 | unless ($TILE_SEQ) { |
154 | $TILE_SEQ = $table->sequence; |
226 | $TILE_SEQ = $table->sequence; |
155 | $TILE_SEQ->initial_value (64); |
227 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
156 | $TILE_SEQ->set_cachesize (0); |
228 | $TILE_SEQ->set_cachesize (0); |
157 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
229 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
158 | } |
230 | } |
159 | |
231 | |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
232 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
… | |
… | |
215 | |
287 | |
216 | ############################################################################# |
288 | ############################################################################# |
217 | |
289 | |
218 | package DC::DB::Server; |
290 | package DC::DB::Server; |
219 | |
291 | |
220 | use strict; |
292 | use common::sense; |
221 | |
293 | |
222 | use EV (); |
294 | use EV (); |
223 | use Fcntl; |
295 | use Fcntl; |
224 | |
296 | |
225 | our %CB; |
297 | our %CB; |
… | |
… | |
344 | |
416 | |
345 | print { $LOG_FH{$path} } "$ts $line\n" |
417 | print { $LOG_FH{$path} } "$ts $line\n" |
346 | } |
418 | } |
347 | |
419 | |
348 | sub run { |
420 | sub run { |
349 | ($FH, my $fh) = DC::socketpipe; |
421 | ($FH, my $fh) = AnyEvent::Util::portable_socketpair |
|
|
422 | or die "unable to create database socketpair: $!"; |
350 | |
423 | |
351 | my $oldfh = select $FH; $| = 1; select $oldfh; |
424 | my $oldfh = select $FH; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
425 | my $oldfh = select $fh; $| = 1; select $oldfh; |
353 | |
426 | |
354 | my $pid = fork; |
427 | my $pid = fork; |
… | |
… | |
404 | } |
477 | } |
405 | |
478 | |
406 | package DC::DB; |
479 | package DC::DB; |
407 | |
480 | |
408 | sub nuke_db { |
481 | sub nuke_db { |
|
|
482 | undef $DB_ENV; |
|
|
483 | undef $DB_ENV_FH; |
|
|
484 | |
409 | File::Path::mkpath [$DB_HOME]; |
485 | File::Path::mkpath [$DB_HOME]; |
410 | eval { File::Path::rmtree $DB_HOME }; |
486 | eval { File::Path::rmtree $DB_HOME }; |
411 | } |
487 | } |
412 | |
488 | |
413 | sub open_db { |
489 | sub open_db { |
… | |
… | |
436 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
512 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
437 | }; |
513 | }; |
438 | } |
514 | } |
439 | |
515 | |
440 | END { |
516 | END { |
|
|
517 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
518 | if $DB_ENV; |
|
|
519 | |
441 | undef $TILE_SEQ; |
520 | undef $TILE_SEQ; |
442 | %DB_TABLE = (); |
521 | %DB_TABLE = (); |
443 | undef $DB_ENV; |
522 | undef $DB_ENV; |
444 | } |
523 | } |
445 | |
524 | |