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.34 by root, Sun Mar 30 04:59:42 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;
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 my $env = db_env_create;
52 57
53 $DB_ENV->set_errfile (\*STDERR); 58 $env->set_errfile (\*STDERR);
54 $DB_ENV->set_msgfile (\*STDERR); 59 $env->set_msgfile (\*STDERR);
55 $DB_ENV->set_verbose (-1, 1); 60 $env->set_verbose (-1, 1);
56 61
57 $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);
58 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 63 $env->set_cachesize (0, 2048 * 1024, 0);
59 64
60 db_env_open $DB_ENV, $DB_HOME, 65 db_env_open $env, $DB_HOME,
61 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,
62 0666; 67 0666;
63 68
64 $! 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;
65 72
66 1 73 1
67} 74}
68 75
69sub table($) { 76sub table($) {
108 }; 115 };
109} 116}
110 117
111sub put($$$$) { 118sub put($$$$) {
112 my ($db, $key, $data, $cb) = @_; 119 my ($db, $key, $data, $cb) = @_;
113 warn "put $key ",(length $data),"\n";#d#
114 120
115 db_put table $db, undef, $key, $data, 0, sub { 121 db_put table $db, undef, $key, $data, 0, sub {
116 $cb->($!); 122 $cb->($!);
117 $SYNC->again unless $SYNC->is_active; 123 $SYNC->again unless $SYNC->is_active;
118 }; 124 };
139 my ($name, $cb) = @_; 145 my ($name, $cb) = @_;
140 146
141 my $table = table "facemap"; 147 my $table = table "facemap";
142 my $id; 148 my $id;
143 149
144 db_get $table, undef, $name, $id, 0; 150 db_get $table, undef, $name => $id, 0;
145 $! or return $cb->($id); 151 $! or return $cb->($id);
146 152
147 for (1..100) { 153 unless ($TILE_SEQ) {
148 my $txn = $DB_ENV->txn_begin; 154 $TILE_SEQ = $table->sequence;
149 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 }
150 159
151 $id = 64 if $id < 64; 160 db_sequence_get $TILE_SEQ, undef, 1, my $id;
152 161
153 ++$id; 162 die "unable to allocate tile id: $!"
154 163 if $!;
155 db_put $table, $txn, id => $id, 0; 164
156 db_txn_finish $txn;
157
158 unless ($!) {
159 db_put $table, undef, $name => $id; 165 db_put $table, undef, $name => $id, 0;
160 $SYNC->again unless $SYNC->is_active; 166 $cb->($id);
161 return $cb->($id);
162 }
163 167
164 select undef, undef, undef, 0.01 * rand;
165 }
166
167 die "maximum number of transaction retries reached - database problems?";
168} 168}
169 169
170sub get_tile_id_sync($) { 170sub get_tile_id_sync($) {
171 my ($name) = @_; 171 my ($name) = @_;
172 172
403 close $FH; 403 close $FH;
404} 404}
405 405
406package DC::DB; 406package DC::DB;
407 407
408sub nuke_db {
409 File::Path::mkpath [$DB_HOME];
410 eval { File::Path::rmtree $DB_HOME };
411}
412
408sub open_db { 413sub open_db {
409 unless (eval { try_open_db }) { 414 unless (eval { try_open_db }) {
410 warn "$@";#d# 415 warn "$@";#d#
411 eval { File::Path::rmtree $DB_HOME }; 416 eval { nuke_db };
412 try_open_db; 417 try_open_db;
413 } 418 }
414 419
415 # fetch the full face table first 420 # fetch the full face table first
416 unless ($facemap) { 421 unless ($facemap) {
425 }; 430 };
426 } 431 }
427 432
428 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; 433 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
429 $SYNC = EV::timer_ns 0, 60, sub { 434 $SYNC = EV::timer_ns 0, 60, sub {
430 warn "SYNC\n";#d#
431 $_[0]->stop; 435 $_[0]->stop;
432 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 436 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
433 }; 437 };
434} 438}
435 439
436END { 440END {
441 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
442 if $DB_ENV;
443
444 undef $TILE_SEQ;
437 %DB_TABLE = (); 445 %DB_TABLE = ();
438 undef $DB_ENV; 446 undef $DB_ENV;
439} 447}
440 448
4411; 4491;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines