… | |
… | |
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 (); |
23 | use AnyEvent::Util (); |
22 | use AnyEvent::Util (); |
… | |
… | |
48 | |
47 | |
49 | BDB::max_poll_time 0.03; |
48 | BDB::max_poll_time 0.03; |
50 | BDB::max_parallel 1; |
49 | BDB::max_parallel 1; |
51 | |
50 | |
52 | our $DB_ENV; |
51 | our $DB_ENV; |
|
|
52 | our $DB_ENV_FH; |
53 | our $DB_STATE; |
53 | our $DB_STATE; |
54 | our %DB_TABLE; |
54 | our %DB_TABLE; |
55 | our $TILE_SEQ; |
55 | our $TILE_SEQ; |
56 | |
56 | |
57 | sub all_databases { |
57 | sub all_databases { |
… | |
… | |
62 | } |
62 | } |
63 | |
63 | |
64 | sub try_verify_env($) { |
64 | sub try_verify_env($) { |
65 | my ($env) = @_; |
65 | my ($env) = @_; |
66 | |
66 | |
|
|
67 | open my $lock, "+>$DB_HOME/__lock" |
|
|
68 | or die "__lock: $!"; |
|
|
69 | |
|
|
70 | flock $lock, &Fcntl::LOCK_EX |
|
|
71 | or die "flock: $!"; |
|
|
72 | |
67 | # we lock the __db.register env file that has been created by now |
73 | # we look at the __db.register env file that has been created by now |
68 | # and check for the number of registered processes - if there is |
74 | # and check for the number of registered processes - if there is |
69 | # only one, we verify all databases, otherwise we skip this |
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. |
70 | open my $fh, "+<$DB_HOME/__db.register" |
78 | open $DB_ENV_FH, "<$DB_HOME/__db.register" |
71 | or die "__db.register: $!"; |
79 | or die "__db.register: $!"; |
72 | |
|
|
73 | open my $lock, "+>$DB_HOME/__lock" |
|
|
74 | or die "__lock: $!"; |
|
|
75 | |
|
|
76 | flock $lock, &Fcntl::LOCK_EX |
|
|
77 | or die "flock: $!"; |
|
|
78 | |
80 | |
79 | # __db.register contains one record per process, with X signifying |
81 | # __db.register contains one record per process, with X signifying |
80 | # empty records (of course, this is completely private to bdb...) |
82 | # empty records (of course, this is completely private to bdb...) |
81 | my $count = grep /^[^X]/, <$fh>; |
83 | my $count = grep /^[^X]/, <$DB_ENV_FH>; |
82 | |
84 | |
83 | if ($count == 1) { |
85 | if ($count == 1) { |
84 | # if any databases are corrupted, we simply delete all of them |
86 | # if any databases are corrupted, we simply delete all of them |
85 | |
87 | |
86 | for (all_databases) { |
88 | for (all_databases) { |
… | |
… | |
103 | 1 |
105 | 1 |
104 | } |
106 | } |
105 | |
107 | |
106 | sub try_open_db { |
108 | sub try_open_db { |
107 | File::Path::mkpath [$DB_HOME]; |
109 | File::Path::mkpath [$DB_HOME]; |
|
|
110 | |
|
|
111 | undef $DB_ENV; |
|
|
112 | undef $DB_ENV_FH; |
108 | |
113 | |
109 | my $env = db_env_create; |
114 | my $env = db_env_create; |
110 | |
115 | |
111 | $env->set_errfile (\*STDERR); |
116 | $env->set_errfile (\*STDERR); |
112 | $env->set_msgfile (\*STDERR); |
117 | $env->set_msgfile (\*STDERR); |
… | |
… | |
282 | |
287 | |
283 | ############################################################################# |
288 | ############################################################################# |
284 | |
289 | |
285 | package DC::DB::Server; |
290 | package DC::DB::Server; |
286 | |
291 | |
287 | use strict; |
292 | use common::sense; |
288 | |
293 | |
289 | use EV (); |
294 | use EV (); |
290 | use Fcntl; |
295 | use Fcntl; |
291 | |
296 | |
292 | our %CB; |
297 | our %CB; |
… | |
… | |
472 | } |
477 | } |
473 | |
478 | |
474 | package DC::DB; |
479 | package DC::DB; |
475 | |
480 | |
476 | sub nuke_db { |
481 | sub nuke_db { |
|
|
482 | undef $DB_ENV; |
|
|
483 | undef $DB_ENV_FH; |
|
|
484 | |
477 | File::Path::mkpath [$DB_HOME]; |
485 | File::Path::mkpath [$DB_HOME]; |
478 | eval { File::Path::rmtree $DB_HOME }; |
486 | eval { File::Path::rmtree $DB_HOME }; |
479 | } |
487 | } |
480 | |
488 | |
481 | sub open_db { |
489 | sub open_db { |