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.32 by root, Sat Jan 19 04:49:37 2008 UTC vs.
Revision 1.34 by root, Sun Mar 30 04:59:42 2008 UTC

43 43
44our $DB_ENV; 44our $DB_ENV;
45our $DB_STATE; 45our $DB_STATE;
46our %DB_TABLE; 46our %DB_TABLE;
47 47
48sub open_db { 48sub try_open_db {
49 mkdir $DB_HOME, 0777; 49 mkdir $DB_HOME, 0777;
50 50
51 $DB_ENV = db_env_create; 51 $DB_ENV = db_env_create;
52 52
53 $DB_ENV->set_errfile (\*STDERR); 53 $DB_ENV->set_errfile (\*STDERR);
84 } 84 }
85} 85}
86 86
87############################################################################# 87#############################################################################
88 88
89unless (eval { open_db }) { 89our $WATCHER;
90 warn "$@";#d# 90our $SYNC;
91 eval { File::Path::rmtree $DB_HOME };
92 open_db;
93}
94
95our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
96
97our $SYNC = EV::timer_ns 0, 60, sub {
98 $_[0]->stop;
99 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
100};
101
102our $tilemap; 91our $facemap;
103 92
104sub exists($$$) { 93sub exists($$$) {
105 my ($db, $key, $cb) = @_; 94 my ($db, $key, $cb) = @_;
106 95
107 my $data; 96 my $data;
119 }; 108 };
120} 109}
121 110
122sub put($$$$) { 111sub put($$$$) {
123 my ($db, $key, $data, $cb) = @_; 112 my ($db, $key, $data, $cb) = @_;
113 warn "put $key ",(length $data),"\n";#d#
124 114
125 db_put table $db, undef, $key, $data, 0, sub { 115 db_put table $db, undef, $key, $data, 0, sub {
126 $cb->($!); 116 $cb->($!);
127 $SYNC->again unless $SYNC->is_active; 117 $SYNC->again unless $SYNC->is_active;
128 }; 118 };
150 140
151 my $table = table "facemap"; 141 my $table = table "facemap";
152 my $id; 142 my $id;
153 143
154 db_get $table, undef, $name, $id, 0; 144 db_get $table, undef, $name, $id, 0;
155 return $cb->($id) unless $!; 145 $! or return $cb->($id);
156 146
157 for (1..100) { 147 for (1..100) {
158 my $txn = $DB_ENV->txn_begin; 148 my $txn = $DB_ENV->txn_begin;
159 db_get $table, $txn, id => $id, 0; 149 db_get $table, $txn, id => $id, 0;
160 150
163 ++$id; 153 ++$id;
164 154
165 db_put $table, $txn, id => $id, 0; 155 db_put $table, $txn, id => $id, 0;
166 db_txn_finish $txn; 156 db_txn_finish $txn;
167 157
158 unless ($!) {
159 db_put $table, undef, $name => $id;
168 $SYNC->again unless $SYNC->is_active; 160 $SYNC->again unless $SYNC->is_active;
169
170 return $cb->($id) unless $!; 161 return $cb->($id);
162 }
171 163
172 select undef, undef, undef, 0.01 * rand; 164 select undef, undef, undef, 0.01 * rand;
173 } 165 }
174 166
175 die "maximum number of transaction retries reached - database problems?"; 167 die "maximum number of transaction retries reached - database problems?";
176} 168}
177 169
178sub get_tile_id_sync($) { 170sub get_tile_id_sync($) {
179 my ($name) = @_; 171 my ($name) = @_;
180 172
181 $tilemap->{$name} ||= do { 173 $facemap->{$name} ||= do {
182 my $id; 174 my $id;
183 do_get_tile_id $name, sub { 175 do_get_tile_id $name, sub {
184 $id = $_[0]; 176 $id = $_[0];
185 }; 177 };
186 BDB::flush; 178 BDB::flush;
220sub logprint($$$) { 212sub logprint($$$) {
221 DC::DB::Server::req (logprint => @_); 213 DC::DB::Server::req (logprint => @_);
222} 214}
223 215
224############################################################################# 216#############################################################################
225
226# fetch the full face table first
227unless ($tilemap) {
228 do_table facemap => sub {
229 $tilemap = $_[0];
230 delete $tilemap->{id};
231 my %maptile = reverse %$tilemap;#d#
232 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
233 $tilemap = { };#d#
234 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
235 }#d#
236 };
237}
238 217
239package DC::DB::Server; 218package DC::DB::Server;
240 219
241use strict; 220use strict;
242 221
422 401
423sub stop { 402sub stop {
424 close $FH; 403 close $FH;
425} 404}
426 405
406package DC::DB;
407
408sub open_db {
409 unless (eval { try_open_db }) {
410 warn "$@";#d#
411 eval { File::Path::rmtree $DB_HOME };
412 try_open_db;
413 }
414
415 # fetch the full face table first
416 unless ($facemap) {
417 do_table facemap => sub {
418 $facemap = $_[0];
419 delete $facemap->{id};
420 my %maptile = reverse %$facemap;#d#
421 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
422 $facemap = { };#d#
423 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
424 }#d#
425 };
426 }
427
428 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
429 $SYNC = EV::timer_ns 0, 60, sub {
430 warn "SYNC\n";#d#
431 $_[0]->stop;
432 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
433 };
434}
435
436END {
437 %DB_TABLE = ();
438 undef $DB_ENV;
439}
440
4271; 4411;
428 442
429=back 443=back
430 444
431=head1 AUTHOR 445=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines