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.34 by root, Sun Mar 30 04:59:42 2008 UTC

86 86
87############################################################################# 87#############################################################################
88 88
89our $WATCHER; 89our $WATCHER;
90our $SYNC; 90our $SYNC;
91our $tilemap; 91our $facemap;
92 92
93sub exists($$$) { 93sub exists($$$) {
94 my ($db, $key, $cb) = @_; 94 my ($db, $key, $cb) = @_;
95 95
96 my $data; 96 my $data;
108 }; 108 };
109} 109}
110 110
111sub put($$$$) { 111sub put($$$$) {
112 my ($db, $key, $data, $cb) = @_; 112 my ($db, $key, $data, $cb) = @_;
113 warn "put $key ",(length $data),"\n";#d#
113 114
114 db_put table $db, undef, $key, $data, 0, sub { 115 db_put table $db, undef, $key, $data, 0, sub {
115 $cb->($!); 116 $cb->($!);
116 $SYNC->again unless $SYNC->is_active; 117 $SYNC->again unless $SYNC->is_active;
117 }; 118 };
139 140
140 my $table = table "facemap"; 141 my $table = table "facemap";
141 my $id; 142 my $id;
142 143
143 db_get $table, undef, $name, $id, 0; 144 db_get $table, undef, $name, $id, 0;
144 return $cb->($id) unless $!; 145 $! or return $cb->($id);
145 146
146 for (1..100) { 147 for (1..100) {
147 my $txn = $DB_ENV->txn_begin; 148 my $txn = $DB_ENV->txn_begin;
148 db_get $table, $txn, id => $id, 0; 149 db_get $table, $txn, id => $id, 0;
149 150
152 ++$id; 153 ++$id;
153 154
154 db_put $table, $txn, id => $id, 0; 155 db_put $table, $txn, id => $id, 0;
155 db_txn_finish $txn; 156 db_txn_finish $txn;
156 157
158 unless ($!) {
159 db_put $table, undef, $name => $id;
157 $SYNC->again unless $SYNC->is_active; 160 $SYNC->again unless $SYNC->is_active;
158
159 return $cb->($id) unless $!; 161 return $cb->($id);
162 }
160 163
161 select undef, undef, undef, 0.01 * rand; 164 select undef, undef, undef, 0.01 * rand;
162 } 165 }
163 166
164 die "maximum number of transaction retries reached - database problems?"; 167 die "maximum number of transaction retries reached - database problems?";
165} 168}
166 169
167sub get_tile_id_sync($) { 170sub get_tile_id_sync($) {
168 my ($name) = @_; 171 my ($name) = @_;
169 172
170 $tilemap->{$name} ||= do { 173 $facemap->{$name} ||= do {
171 my $id; 174 my $id;
172 do_get_tile_id $name, sub { 175 do_get_tile_id $name, sub {
173 $id = $_[0]; 176 $id = $_[0];
174 }; 177 };
175 BDB::flush; 178 BDB::flush;
408 eval { File::Path::rmtree $DB_HOME }; 411 eval { File::Path::rmtree $DB_HOME };
409 try_open_db; 412 try_open_db;
410 } 413 }
411 414
412 # fetch the full face table first 415 # fetch the full face table first
413 unless ($tilemap) { 416 unless ($facemap) {
414 do_table facemap => sub { 417 do_table facemap => sub {
415 $tilemap = $_[0]; 418 $facemap = $_[0];
416 delete $tilemap->{id}; 419 delete $facemap->{id};
417 my %maptile = reverse %$tilemap;#d# 420 my %maptile = reverse %$facemap;#d#
418 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# 421 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
419 $tilemap = { };#d# 422 $facemap = { };#d#
420 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# 423 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
421 }#d# 424 }#d#
422 }; 425 };
423 } 426 }
424 427
425 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; 428 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
426 $SYNC = EV::timer_ns 0, 60, sub { 429 $SYNC = EV::timer_ns 0, 60, sub {
430 warn "SYNC\n";#d#
427 $_[0]->stop; 431 $_[0]->stop;
428 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 432 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
429 }; 433 };
430} 434}
431 435

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines