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.35 by root, Sun Mar 30 06:05:52 2008 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 ();
22use Config; 23use Config;
23use BDB; 24use BDB;
24 25
26 27
27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 28our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; 29our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}";
29our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 30our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
30 31
32unless (-d $DB_HOME) {
31if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { 33 if (-d "$Deliantra::VARDIR/$ODBDIR") {
32 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; 34 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
33 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; 35 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") { 36 } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") {
37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; 38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n";
39 } else {
40 File::Path::mkpath [$DB_HOME]
41 or die "unable to create database directory $DB_HOME: $!";
42 }
39} 43}
40 44
41BDB::max_poll_time 0.03; 45BDB::max_poll_time 0.03;
42BDB::max_parallel 1; 46BDB::max_parallel 1;
43 47
44our $DB_ENV; 48our $DB_ENV;
45our $DB_STATE; 49our $DB_STATE;
46our %DB_TABLE; 50our %DB_TABLE;
51our $TILE_SEQ;
47 52
48sub try_open_db { 53sub try_open_db {
49 mkdir $DB_HOME, 0777; 54 File::Path::mkpath [$DB_HOME];
50 55
51 $DB_ENV = db_env_create; 56 $DB_ENV = db_env_create;
52 57
53 $DB_ENV->set_errfile (\*STDERR); 58 $DB_ENV->set_errfile (\*STDERR);
54 $DB_ENV->set_msgfile (\*STDERR); 59 $DB_ENV->set_msgfile (\*STDERR);
86 91
87############################################################################# 92#############################################################################
88 93
89our $WATCHER; 94our $WATCHER;
90our $SYNC; 95our $SYNC;
91our $tilemap; 96our $facemap;
92 97
93sub exists($$$) { 98sub exists($$$) {
94 my ($db, $key, $cb) = @_; 99 my ($db, $key, $cb) = @_;
95 100
96 my $data; 101 my $data;
138 my ($name, $cb) = @_; 143 my ($name, $cb) = @_;
139 144
140 my $table = table "facemap"; 145 my $table = table "facemap";
141 my $id; 146 my $id;
142 147
143 db_get $table, undef, $name, $id, 0; 148 db_get $table, undef, $name => $id, 0;
144 return $cb->($id) unless $!; 149 $! or return $cb->($id);
145 150
146 for (1..100) { 151 unless ($TILE_SEQ) {
147 my $txn = $DB_ENV->txn_begin; 152 $TILE_SEQ = $table->sequence;
148 db_get $table, $txn, id => $id, 0; 153 $TILE_SEQ->initial_value (64);
154 $TILE_SEQ->set_cachesize (0);
155 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
156 }
149 157
150 $id = 64 if $id < 64; 158 db_sequence_get $TILE_SEQ, undef, 1, my $id;
151 159
152 ++$id; 160 die "unable to allocate tile id: $!"
153 161 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 } 162
163 db_put $table, undef, $name => $id, 0;
164 $cb->($id);
163 165
164 die "maximum number of transaction retries reached - database problems?";
165} 166}
166 167
167sub get_tile_id_sync($) { 168sub get_tile_id_sync($) {
168 my ($name) = @_; 169 my ($name) = @_;
169 170
170 $tilemap->{$name} ||= do { 171 $facemap->{$name} ||= do {
171 my $id; 172 my $id;
172 do_get_tile_id $name, sub { 173 do_get_tile_id $name, sub {
173 $id = $_[0]; 174 $id = $_[0];
174 }; 175 };
175 BDB::flush; 176 BDB::flush;
400 close $FH; 401 close $FH;
401} 402}
402 403
403package DC::DB; 404package DC::DB;
404 405
406sub nuke_db {
407 File::Path::mkpath [$DB_HOME];
408 eval { File::Path::rmtree $DB_HOME };
409}
410
405sub open_db { 411sub open_db {
406 unless (eval { try_open_db }) { 412 unless (eval { try_open_db }) {
407 warn "$@";#d# 413 warn "$@";#d#
408 eval { File::Path::rmtree $DB_HOME }; 414 eval { nuke_db };
409 try_open_db; 415 try_open_db;
410 } 416 }
411 417
412 # fetch the full face table first 418 # fetch the full face table first
413 unless ($tilemap) { 419 unless ($facemap) {
414 do_table facemap => sub { 420 do_table facemap => sub {
415 $tilemap = $_[0]; 421 $facemap = $_[0];
416 delete $tilemap->{id}; 422 delete $facemap->{id};
417 my %maptile = reverse %$tilemap;#d# 423 my %maptile = reverse %$facemap;#d#
418 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# 424 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
419 $tilemap = { };#d# 425 $facemap = { };#d#
420 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# 426 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
421 }#d# 427 }#d#
422 }; 428 };
423 } 429 }
424 430
428 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 434 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
429 }; 435 };
430} 436}
431 437
432END { 438END {
439 undef $TILE_SEQ;
433 %DB_TABLE = (); 440 %DB_TABLE = ();
434 undef $DB_ENV; 441 undef $DB_ENV;
435} 442}
436 443
4371; 4441;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines