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.41 by root, Wed Dec 24 04:09:27 2008 UTC vs.
Revision 1.44 by root, Sat Jan 10 20:56:34 2009 UTC

18use utf8; 18use utf8;
19 19
20use File::Path (); 20use File::Path ();
21use Carp (); 21use Carp ();
22use Storable (); 22use Storable ();
23use AnyEvent::Util ();
23use Config; 24use Config;
24use BDB; 25use BDB;
26use Fcntl ();
25 27
26use DC; 28use DC;
27 29
28our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 30our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
29our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 31our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
46 48
47BDB::max_poll_time 0.03; 49BDB::max_poll_time 0.03;
48BDB::max_parallel 1; 50BDB::max_parallel 1;
49 51
50our $DB_ENV; 52our $DB_ENV;
53our $DB_ENV_FH;
51our $DB_STATE; 54our $DB_STATE;
52our %DB_TABLE; 55our %DB_TABLE;
53our $TILE_SEQ; 56our $TILE_SEQ;
54 57
58sub all_databases {
59 opendir my $fh, $DB_HOME
60 or return;
61
62 grep !/^(?:\.|log\.|_)/, readdir $fh
63}
64
65sub 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
55sub try_open_db { 109sub try_open_db {
56 File::Path::mkpath [$DB_HOME]; 110 File::Path::mkpath [$DB_HOME];
111
112 undef $DB_ENV;
113 undef $DB_ENV_FH;
57 114
58 my $env = db_env_create; 115 my $env = db_env_create;
59 116
60 $env->set_errfile (\*STDERR); 117 $env->set_errfile (\*STDERR);
61 $env->set_msgfile (\*STDERR); 118 $env->set_msgfile (\*STDERR);
73 db_env_open $env, $DB_HOME, 130 db_env_open $env, $DB_HOME,
74 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,
75 0666; 132 0666;
76 133
77 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 134 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
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";
78 140
79 $DB_ENV = $env; 141 $DB_ENV = $env;
80 142
81 1 143 1
82} 144}
355 417
356 print { $LOG_FH{$path} } "$ts $line\n" 418 print { $LOG_FH{$path} } "$ts $line\n"
357} 419}
358 420
359sub run { 421sub run {
360 ($FH, my $fh) = DC::socketpipe; 422 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
423 or die "unable to create database socketpair: $!";
361 424
362 my $oldfh = select $FH; $| = 1; select $oldfh; 425 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 426 my $oldfh = select $fh; $| = 1; select $oldfh;
364 427
365 my $pid = fork; 428 my $pid = fork;
415} 478}
416 479
417package DC::DB; 480package DC::DB;
418 481
419sub nuke_db { 482sub nuke_db {
483 undef $DB_ENV;
484 undef $DB_ENV_FH;
485
420 File::Path::mkpath [$DB_HOME]; 486 File::Path::mkpath [$DB_HOME];
421 eval { File::Path::rmtree $DB_HOME }; 487 eval { File::Path::rmtree $DB_HOME };
422} 488}
423 489
424sub open_db { 490sub open_db {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines