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