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.33 by root, Tue Mar 25 21:11:51 2008 UTC vs.
Revision 1.43 by root, Fri Jan 9 22:38:17 2009 UTC

15package DC::DB; 15package DC::DB;
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use File::Path ();
20use Carp (); 21use Carp ();
21use Storable (); 22use Storable ();
23use AnyEvent::Util ();
22use Config; 24use Config;
23use BDB; 25use BDB;
26use Fcntl ();
24 27
25use DC; 28use DC;
26 29
27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 30our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; 31our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
29our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 32our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
30 33
34sub FIRST_TILE_ID () { 64 }
35
36unless (-d $DB_HOME) {
31if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { 37 if (-d "$Deliantra::VARDIR/$ODBDIR") {
32 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; 38 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
33 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; 39 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
34}
35
36if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { 40 } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") {
37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 41 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; 42 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n";
43 } else {
44 File::Path::mkpath [$DB_HOME]
45 or die "unable to create database directory $DB_HOME: $!";
46 }
39} 47}
40 48
41BDB::max_poll_time 0.03; 49BDB::max_poll_time 0.03;
42BDB::max_parallel 1; 50BDB::max_parallel 1;
43 51
44our $DB_ENV; 52our $DB_ENV;
45our $DB_STATE; 53our $DB_STATE;
46our %DB_TABLE; 54our %DB_TABLE;
55our $TILE_SEQ;
56
57sub all_databases {
58 opendir my $fh, $DB_HOME
59 or return;
60
61 grep !/^(?:\.|log\.|_)/, readdir $fh
62}
63
64sub try_verify_env($) {
65 my ($env) = @_;
66
67 # we lock the __db.register env file that has been created by now
68 # and check for the number of registered processes - if there is
69 # only one, we verify all databases, otherwise we skip this
70 open my $fh, "+<$DB_HOME/__db.register"
71 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
79 # __db.register contains one record per process, with X signifying
80 # empty records (of course, this is completely private to bdb...)
81 my $count = grep /^[^X]/, <$fh>;
82
83 if ($count == 1) {
84 # if any databases are corrupted, we simply delete all of them
85
86 for (all_databases) {
87 my $dbh = db_create $env
88 or last;
89
90 # a failed verify will panic the environment, which is fine with us
91 db_verify $dbh, "$DB_HOME/$_";
92
93 return if $!; # nuke database and recreate if verification failure
94 }
95
96 }
97
98 # close probably cleans those up, but we also want to run on windows,
99 # so better be safe.
100 flock $lock, &Fcntl::LOCK_UN
101 or die "funlock: $!";
102
103 1
104}
47 105
48sub try_open_db { 106sub try_open_db {
49 mkdir $DB_HOME, 0777; 107 File::Path::mkpath [$DB_HOME];
50 108
51 $DB_ENV = db_env_create; 109 my $env = db_env_create;
52 110
53 $DB_ENV->set_errfile (\*STDERR); 111 $env->set_errfile (\*STDERR);
54 $DB_ENV->set_msgfile (\*STDERR); 112 $env->set_msgfile (\*STDERR);
55 $DB_ENV->set_verbose (-1, 1); 113 $env->set_verbose (-1, 1);
56 114
57 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); 115 $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
116 $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
117 $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
118
119 $env->set_timeout (3, BDB::SET_TXN_TIMEOUT);
120 $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT);
121
58 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 122 $env->set_cachesize (0, 2048 * 1024, 0);
59 123
60 db_env_open $DB_ENV, $DB_HOME, 124 db_env_open $env, $DB_HOME,
61 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 125 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
62 0666; 126 0666;
63 127
64 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 128 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
129
130 # now we go through the registered processes, if there is only one, we verify all files
131 # to make sure windows didn'T corrupt them (as windows does....)
132 try_verify_env $env
133 or die "database environment failed verification";
134
135 $DB_ENV = $env;
65 136
66 1 137 1
67} 138}
68 139
69sub table($) { 140sub table($) {
70 $DB_TABLE{$_[0]} ||= do { 141 $DB_TABLE{$_[0]} ||= do {
71 my ($table) = @_; 142 my ($table) = @_;
72 143
73 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 144 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
74 145
146 $DB_ENV#d#
147 or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
148
75 my $db = db_create $DB_ENV; 149 my $db = db_create $DB_ENV;
76 $db->set_flags (BDB::CHKSUM); 150 $db->set_flags (BDB::CHKSUM);
77 151
78 db_open $db, undef, $table, undef, BDB::BTREE, 152 db_open $db, undef, $table, undef, BDB::BTREE,
79 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; 153 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
86 160
87############################################################################# 161#############################################################################
88 162
89our $WATCHER; 163our $WATCHER;
90our $SYNC; 164our $SYNC;
91our $tilemap; 165our $facemap;
92 166
93sub exists($$$) { 167sub exists($$$) {
94 my ($db, $key, $cb) = @_; 168 my ($db, $key, $cb) = @_;
95 169
96 my $data; 170 my $data;
138 my ($name, $cb) = @_; 212 my ($name, $cb) = @_;
139 213
140 my $table = table "facemap"; 214 my $table = table "facemap";
141 my $id; 215 my $id;
142 216
143 db_get $table, undef, $name, $id, 0; 217 db_get $table, undef, $name => $id, 0;
144 return $cb->($id) unless $!; 218 $! or return $cb->($id);
145 219
146 for (1..100) { 220 unless ($TILE_SEQ) {
147 my $txn = $DB_ENV->txn_begin; 221 $TILE_SEQ = $table->sequence;
148 db_get $table, $txn, id => $id, 0; 222 $TILE_SEQ->initial_value (FIRST_TILE_ID);
223 $TILE_SEQ->set_cachesize (0);
224 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
225 }
149 226
150 $id = 64 if $id < 64; 227 db_sequence_get $TILE_SEQ, undef, 1, my $id;
151 228
152 ++$id; 229 die "unable to allocate tile id: $!"
153 230 if $!;
154 db_put $table, $txn, id => $id, 0;
155 db_txn_finish $txn;
156
157 $SYNC->again unless $SYNC->is_active;
158
159 return $cb->($id) unless $!;
160
161 select undef, undef, undef, 0.01 * rand;
162 } 231
232 db_put $table, undef, $name => $id, 0;
233 $cb->($id);
163 234
164 die "maximum number of transaction retries reached - database problems?";
165} 235}
166 236
167sub get_tile_id_sync($) { 237sub get_tile_id_sync($) {
168 my ($name) = @_; 238 my ($name) = @_;
169 239
170 $tilemap->{$name} ||= do { 240 $facemap->{$name} ||= do {
171 my $id; 241 my $id;
172 do_get_tile_id $name, sub { 242 do_get_tile_id $name, sub {
173 $id = $_[0]; 243 $id = $_[0];
174 }; 244 };
175 BDB::flush; 245 BDB::flush;
341 411
342 print { $LOG_FH{$path} } "$ts $line\n" 412 print { $LOG_FH{$path} } "$ts $line\n"
343} 413}
344 414
345sub run { 415sub run {
346 ($FH, my $fh) = DC::socketpipe; 416 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
417 or die "unable to create database socketpair: $!";
347 418
348 my $oldfh = select $FH; $| = 1; select $oldfh; 419 my $oldfh = select $FH; $| = 1; select $oldfh;
349 my $oldfh = select $fh; $| = 1; select $oldfh; 420 my $oldfh = select $fh; $| = 1; select $oldfh;
350 421
351 my $pid = fork; 422 my $pid = fork;
400 close $FH; 471 close $FH;
401} 472}
402 473
403package DC::DB; 474package DC::DB;
404 475
476sub nuke_db {
477 File::Path::mkpath [$DB_HOME];
478 eval { File::Path::rmtree $DB_HOME };
479}
480
405sub open_db { 481sub open_db {
406 unless (eval { try_open_db }) { 482 unless (eval { try_open_db }) {
407 warn "$@";#d# 483 warn "$@";#d#
408 eval { File::Path::rmtree $DB_HOME }; 484 eval { nuke_db };
409 try_open_db; 485 try_open_db;
410 } 486 }
411 487
412 # fetch the full face table first 488 # fetch the full face table first
413 unless ($tilemap) { 489 unless ($facemap) {
414 do_table facemap => sub { 490 do_table facemap => sub {
415 $tilemap = $_[0]; 491 $facemap = $_[0];
416 delete $tilemap->{id}; 492 delete $facemap->{id};
417 my %maptile = reverse %$tilemap;#d# 493 my %maptile = reverse %$facemap;#d#
418 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# 494 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
419 $tilemap = { };#d# 495 $facemap = { };#d#
420 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# 496 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
421 }#d# 497 }#d#
422 }; 498 };
423 } 499 }
424 500
428 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 504 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
429 }; 505 };
430} 506}
431 507
432END { 508END {
509 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
510 if $DB_ENV;
511
512 undef $TILE_SEQ;
433 %DB_TABLE = (); 513 %DB_TABLE = ();
434 undef $DB_ENV; 514 undef $DB_ENV;
435} 515}
436 516
4371; 5171;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines