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.31 by root, Thu Jan 10 23:02:19 2008 UTC vs.
Revision 1.38 by root, Fri Jul 18 22:40:51 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
25use DC; 26use DC;
26 27
27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 28our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; 29our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$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;
46BDB::max_parallel 1;
42 47
43our $DB_ENV; 48our $DB_ENV;
44our $DB_STATE; 49our $DB_STATE;
45our %DB_TABLE; 50our %DB_TABLE;
51our $TILE_SEQ;
46 52
47sub open_db { 53sub try_open_db {
48 mkdir $DB_HOME, 0777; 54 File::Path::mkpath [$DB_HOME];
49 55
50 $DB_ENV = db_env_create; 56 my $env = db_env_create;
51 57
52 $DB_ENV->set_errfile (\*STDERR); 58 $env->set_errfile (\*STDERR);
53 $DB_ENV->set_msgfile (\*STDERR); 59 $env->set_msgfile (\*STDERR);
54 $DB_ENV->set_verbose (-1, 1); 60 $env->set_verbose (-1, 1);
55 61
56 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); 62 $env->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC);
57 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 63 $env->set_cachesize (0, 2048 * 1024, 0);
58 64
59 db_env_open $DB_ENV, $DB_HOME, 65 db_env_open $env, $DB_HOME,
60 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 66 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
61 0666; 67 0666;
62 68
63 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 69 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
70
71 $DB_ENV = $env;
64 72
65 1 73 1
66} 74}
67 75
68sub table($) { 76sub table($) {
83 } 91 }
84} 92}
85 93
86############################################################################# 94#############################################################################
87 95
88unless (eval { open_db }) { 96our $WATCHER;
89 warn "$@";#d# 97our $SYNC;
90 eval { File::Path::rmtree $DB_HOME };
91 open_db;
92}
93
94our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
95
96our $SYNC = EV::timer_ns 0, 60, sub {
97 $_[0]->stop;
98 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
99};
100
101our $tilemap; 98our $facemap;
102 99
103sub exists($$$) { 100sub exists($$$) {
104 my ($db, $key, $cb) = @_; 101 my ($db, $key, $cb) = @_;
105 102
106 my $data; 103 my $data;
148 my ($name, $cb) = @_; 145 my ($name, $cb) = @_;
149 146
150 my $table = table "facemap"; 147 my $table = table "facemap";
151 my $id; 148 my $id;
152 149
153 db_get $table, undef, $name, $id, 0; 150 db_get $table, undef, $name => $id, 0;
154 return $cb->($id) unless $!; 151 $! or return $cb->($id);
155 152
156 for (1..100) { 153 unless ($TILE_SEQ) {
157 my $txn = $DB_ENV->txn_begin; 154 $TILE_SEQ = $table->sequence;
158 db_get $table, $txn, id => $id, 0; 155 $TILE_SEQ->initial_value (64);
156 $TILE_SEQ->set_cachesize (0);
157 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
158 }
159 159
160 $id = 64 if $id < 64; 160 db_sequence_get $TILE_SEQ, undef, 1, my $id;
161 161
162 ++$id; 162 die "unable to allocate tile id: $!"
163 163 if $!;
164 db_put $table, $txn, id => $id, 0;
165 db_txn_finish $txn;
166
167 $SYNC->again unless $SYNC->is_active;
168
169 return $cb->($id) unless $!;
170
171 select undef, undef, undef, 0.01 * rand;
172 } 164
165 db_put $table, undef, $name => $id, 0;
166 $cb->($id);
173 167
174 die "maximum number of transaction retries reached - database problems?";
175} 168}
176 169
177sub get_tile_id_sync($) { 170sub get_tile_id_sync($) {
178 my ($name) = @_; 171 my ($name) = @_;
179 172
180 $tilemap->{$name} ||= do { 173 $facemap->{$name} ||= do {
181 my $id; 174 my $id;
182 do_get_tile_id $name, sub { 175 do_get_tile_id $name, sub {
183 $id = $_[0]; 176 $id = $_[0];
184 }; 177 };
185 BDB::flush; 178 BDB::flush;
219sub logprint($$$) { 212sub logprint($$$) {
220 DC::DB::Server::req (logprint => @_); 213 DC::DB::Server::req (logprint => @_);
221} 214}
222 215
223############################################################################# 216#############################################################################
224
225# fetch the full face table first
226unless ($tilemap) {
227 do_table facemap => sub {
228 $tilemap = $_[0];
229 delete $tilemap->{id};
230 my %maptile = reverse %$tilemap;#d#
231 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
232 $tilemap = { };#d#
233 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
234 }#d#
235 };
236}
237 217
238package DC::DB::Server; 218package DC::DB::Server;
239 219
240use strict; 220use strict;
241 221
421 401
422sub stop { 402sub stop {
423 close $FH; 403 close $FH;
424} 404}
425 405
406package DC::DB;
407
408sub nuke_db {
409 File::Path::mkpath [$DB_HOME];
410 eval { File::Path::rmtree $DB_HOME };
411}
412
413sub open_db {
414 unless (eval { try_open_db }) {
415 warn "$@";#d#
416 eval { nuke_db };
417 try_open_db;
418 }
419
420 # fetch the full face table first
421 unless ($facemap) {
422 do_table facemap => sub {
423 $facemap = $_[0];
424 delete $facemap->{id};
425 my %maptile = reverse %$facemap;#d#
426 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
427 $facemap = { };#d#
428 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
429 }#d#
430 };
431 }
432
433 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
434 $SYNC = EV::timer_ns 0, 60, sub {
435 $_[0]->stop;
436 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
437 };
438}
439
440END {
441 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
442 if $DB_ENV;
443
444 undef $TILE_SEQ;
445 %DB_TABLE = ();
446 undef $DB_ENV;
447}
448
4261; 4491;
427 450
428=back 451=back
429 452
430=head1 AUTHOR 453=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines