… | |
… | |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use File::Path (); |
20 | use File::Path (); |
21 | use Carp (); |
21 | use Carp (); |
22 | use Storable (); |
22 | use Storable (); |
|
|
23 | use AnyEvent::Util (); |
23 | use Config; |
24 | use Config; |
24 | use BDB; |
25 | use BDB; |
|
|
26 | use Fcntl (); |
25 | |
27 | |
26 | use DC; |
28 | use DC; |
27 | |
29 | |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
30 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
31 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
32 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
33 | |
|
|
34 | sub FIRST_TILE_ID () { 64 } |
31 | |
35 | |
32 | unless (-d $DB_HOME) { |
36 | unless (-d $DB_HOME) { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
37 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
38 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
35 | 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"; |
… | |
… | |
44 | |
48 | |
45 | BDB::max_poll_time 0.03; |
49 | BDB::max_poll_time 0.03; |
46 | BDB::max_parallel 1; |
50 | BDB::max_parallel 1; |
47 | |
51 | |
48 | our $DB_ENV; |
52 | our $DB_ENV; |
|
|
53 | our $DB_ENV_FH; |
49 | our $DB_STATE; |
54 | our $DB_STATE; |
50 | our %DB_TABLE; |
55 | our %DB_TABLE; |
51 | our $TILE_SEQ; |
56 | our $TILE_SEQ; |
52 | |
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 | } |
|
|
108 | |
53 | sub try_open_db { |
109 | sub try_open_db { |
54 | File::Path::mkpath [$DB_HOME]; |
110 | File::Path::mkpath [$DB_HOME]; |
|
|
111 | |
|
|
112 | undef $DB_ENV; |
|
|
113 | undef $DB_ENV_FH; |
55 | |
114 | |
56 | my $env = db_env_create; |
115 | my $env = db_env_create; |
57 | |
116 | |
58 | $env->set_errfile (\*STDERR); |
117 | $env->set_errfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
118 | $env->set_msgfile (\*STDERR); |
60 | $env->set_verbose (-1, 1); |
119 | $env->set_verbose (-1, 1); |
61 | |
120 | |
62 | $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 | |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
128 | $env->set_cachesize (0, 2048 * 1024, 0); |
64 | |
129 | |
65 | db_env_open $env, $DB_HOME, |
130 | db_env_open $env, $DB_HOME, |
66 | 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, |
67 | 0666; |
132 | 0666; |
68 | |
133 | |
69 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
134 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
70 | |
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 | |
71 | $DB_ENV = $env; |
141 | $DB_ENV = $env; |
72 | |
142 | |
73 | 1 |
143 | 1 |
74 | } |
144 | } |
75 | |
145 | |
76 | sub table($) { |
146 | sub table($) { |
77 | $DB_TABLE{$_[0]} ||= do { |
147 | $DB_TABLE{$_[0]} ||= do { |
78 | my ($table) = @_; |
148 | my ($table) = @_; |
79 | |
149 | |
80 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
150 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
151 | |
|
|
152 | $DB_ENV#d# |
|
|
153 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
81 | |
154 | |
82 | my $db = db_create $DB_ENV; |
155 | my $db = db_create $DB_ENV; |
83 | $db->set_flags (BDB::CHKSUM); |
156 | $db->set_flags (BDB::CHKSUM); |
84 | |
157 | |
85 | db_open $db, undef, $table, undef, BDB::BTREE, |
158 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
150 | db_get $table, undef, $name => $id, 0; |
223 | db_get $table, undef, $name => $id, 0; |
151 | $! or return $cb->($id); |
224 | $! or return $cb->($id); |
152 | |
225 | |
153 | unless ($TILE_SEQ) { |
226 | unless ($TILE_SEQ) { |
154 | $TILE_SEQ = $table->sequence; |
227 | $TILE_SEQ = $table->sequence; |
155 | $TILE_SEQ->initial_value (64); |
228 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
156 | $TILE_SEQ->set_cachesize (0); |
229 | $TILE_SEQ->set_cachesize (0); |
157 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
230 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
158 | } |
231 | } |
159 | |
232 | |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
233 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
… | |
… | |
344 | |
417 | |
345 | print { $LOG_FH{$path} } "$ts $line\n" |
418 | print { $LOG_FH{$path} } "$ts $line\n" |
346 | } |
419 | } |
347 | |
420 | |
348 | sub run { |
421 | sub run { |
349 | ($FH, my $fh) = DC::socketpipe; |
422 | ($FH, my $fh) = AnyEvent::Util::portable_socketpair |
|
|
423 | or die "unable to create database socketpair: $!"; |
350 | |
424 | |
351 | my $oldfh = select $FH; $| = 1; select $oldfh; |
425 | my $oldfh = select $FH; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
426 | my $oldfh = select $fh; $| = 1; select $oldfh; |
353 | |
427 | |
354 | my $pid = fork; |
428 | my $pid = fork; |
… | |
… | |
404 | } |
478 | } |
405 | |
479 | |
406 | package DC::DB; |
480 | package DC::DB; |
407 | |
481 | |
408 | sub nuke_db { |
482 | sub nuke_db { |
|
|
483 | undef $DB_ENV; |
|
|
484 | undef $DB_ENV_FH; |
|
|
485 | |
409 | File::Path::mkpath [$DB_HOME]; |
486 | File::Path::mkpath [$DB_HOME]; |
410 | eval { File::Path::rmtree $DB_HOME }; |
487 | eval { File::Path::rmtree $DB_HOME }; |
411 | } |
488 | } |
412 | |
489 | |
413 | sub open_db { |
490 | sub open_db { |
… | |
… | |
436 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
513 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
437 | }; |
514 | }; |
438 | } |
515 | } |
439 | |
516 | |
440 | END { |
517 | END { |
|
|
518 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
519 | if $DB_ENV; |
|
|
520 | |
441 | undef $TILE_SEQ; |
521 | undef $TILE_SEQ; |
442 | %DB_TABLE = (); |
522 | %DB_TABLE = (); |
443 | undef $DB_ENV; |
523 | undef $DB_ENV; |
444 | } |
524 | } |
445 | |
525 | |