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.45 by root, Sat Apr 3 02:58:24 2010 UTC

12 12
13=cut 13=cut
14 14
15package DC::DB; 15package DC::DB;
16 16
17use strict; 17use common::sense;
18use utf8;
19 18
20use File::Path (); 19use File::Path ();
21use Carp (); 20use Carp ();
22use Storable (); 21use Storable ();
23use AnyEvent::Util (); 22use AnyEvent::Util ();
48 47
49BDB::max_poll_time 0.03; 48BDB::max_poll_time 0.03;
50BDB::max_parallel 1; 49BDB::max_parallel 1;
51 50
52our $DB_ENV; 51our $DB_ENV;
52our $DB_ENV_FH;
53our $DB_STATE; 53our $DB_STATE;
54our %DB_TABLE; 54our %DB_TABLE;
55our $TILE_SEQ; 55our $TILE_SEQ;
56 56
57sub all_databases { 57sub all_databases {
62} 62}
63 63
64sub try_verify_env($) { 64sub 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
106sub try_open_db { 108sub 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
285package DC::DB::Server; 290package DC::DB::Server;
286 291
287use strict; 292use common::sense;
288 293
289use EV (); 294use EV ();
290use Fcntl; 295use Fcntl;
291 296
292our %CB; 297our %CB;
472} 477}
473 478
474package DC::DB; 479package DC::DB;
475 480
476sub nuke_db { 481sub 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
481sub open_db { 489sub open_db {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines