ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.43 by root, Fri Jan 9 22:38:17 2009 UTC vs.
Revision 1.44 by root, Sat Jan 10 20:56:34 2009 UTC

48 48
49BDB::max_poll_time 0.03; 49BDB::max_poll_time 0.03;
50BDB::max_parallel 1; 50BDB::max_parallel 1;
51 51
52our $DB_ENV; 52our $DB_ENV;
53our $DB_ENV_FH;
53our $DB_STATE; 54our $DB_STATE;
54our %DB_TABLE; 55our %DB_TABLE;
55our $TILE_SEQ; 56our $TILE_SEQ;
56 57
57sub all_databases { 58sub all_databases {
62} 63}
63 64
64sub try_verify_env($) { 65sub 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
106sub try_open_db { 109sub 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
474package DC::DB; 480package DC::DB;
475 481
476sub nuke_db { 482sub 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
481sub open_db { 490sub open_db {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines