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.29 by root, Wed Dec 26 21:03:21 2007 UTC vs.
Revision 1.34 by root, Sun Mar 30 04:59:42 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3DC::DB - async. database and filesystem access for cfplus 3DC::DB - async. database and filesystem access for deliantra
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use DC::DB; 7 use DC::DB;
8 8
22use Config; 22use Config;
23use BDB; 23use BDB;
24 24
25use DC; 25use DC;
26 26
27our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}";
28our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 29our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29 30
31if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") {
32 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
33 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
34}
35
30if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { 36if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") {
31 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
32 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$DBDIR to $DB_HOME\n"; 38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n";
33} 39}
40
41BDB::max_poll_time 0.03;
42BDB::max_parallel 1;
34 43
35our $DB_ENV; 44our $DB_ENV;
36our $DB_STATE; 45our $DB_STATE;
37our %DB_TABLE; 46our %DB_TABLE;
38 47
39sub open_db { 48sub try_open_db {
40 mkdir $DB_HOME, 0777; 49 mkdir $DB_HOME, 0777;
41 50
42 $DB_ENV = db_env_create; 51 $DB_ENV = db_env_create;
43 52
44 $DB_ENV->set_errfile (\*STDERR); 53 $DB_ENV->set_errfile (\*STDERR);
75 } 84 }
76} 85}
77 86
78############################################################################# 87#############################################################################
79 88
80unless (eval { open_db }) { 89our $WATCHER;
81 warn "$@";#d# 90our $SYNC;
82 eval { File::Path::rmtree $DB_HOME };
83 open_db;
84}
85
86our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
87
88our $SYNC = EV::timer_ns 0, 60, sub {
89 $_[0]->stop;
90 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
91};
92
93our $tilemap; 91our $facemap;
94 92
95sub exists($$$) { 93sub exists($$$) {
96 my ($db, $key, $cb) = @_; 94 my ($db, $key, $cb) = @_;
97 95
98 my $data; 96 my $data;
110 }; 108 };
111} 109}
112 110
113sub put($$$$) { 111sub put($$$$) {
114 my ($db, $key, $data, $cb) = @_; 112 my ($db, $key, $data, $cb) = @_;
113 warn "put $key ",(length $data),"\n";#d#
115 114
116 db_put table $db, undef, $key, $data, 0, sub { 115 db_put table $db, undef, $key, $data, 0, sub {
117 $cb->($!); 116 $cb->($!);
118 $SYNC->again unless $SYNC->is_active; 117 $SYNC->again unless $SYNC->is_active;
119 }; 118 };
141 140
142 my $table = table "facemap"; 141 my $table = table "facemap";
143 my $id; 142 my $id;
144 143
145 db_get $table, undef, $name, $id, 0; 144 db_get $table, undef, $name, $id, 0;
146 return $cb->($id) unless $!; 145 $! or return $cb->($id);
147 146
148 for (1..100) { 147 for (1..100) {
149 my $txn = $DB_ENV->txn_begin; 148 my $txn = $DB_ENV->txn_begin;
150 db_get $table, $txn, id => $id, 0; 149 db_get $table, $txn, id => $id, 0;
151 150
154 ++$id; 153 ++$id;
155 154
156 db_put $table, $txn, id => $id, 0; 155 db_put $table, $txn, id => $id, 0;
157 db_txn_finish $txn; 156 db_txn_finish $txn;
158 157
158 unless ($!) {
159 db_put $table, undef, $name => $id;
159 $SYNC->again unless $SYNC->is_active; 160 $SYNC->again unless $SYNC->is_active;
160
161 return $cb->($id) unless $!; 161 return $cb->($id);
162 }
162 163
163 select undef, undef, undef, 0.01 * rand; 164 select undef, undef, undef, 0.01 * rand;
164 } 165 }
165 166
166 die "maximum number of transaction retries reached - database problems?"; 167 die "maximum number of transaction retries reached - database problems?";
167} 168}
168 169
169sub get_tile_id_sync($) { 170sub get_tile_id_sync($) {
170 my ($name) = @_; 171 my ($name) = @_;
171 172
172 # fetch the full face table first
173 unless ($tilemap) {
174 do_table facemap => sub {
175 $tilemap = $_[0];
176 delete $tilemap->{id};
177 my %maptile = reverse %$tilemap;#d#
178 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
179 $tilemap = { };#d#
180 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
181 }#d#
182 };
183 BDB::flush;
184 }
185
186 $tilemap->{$name} ||= do { 173 $facemap->{$name} ||= do {
187 my $id; 174 my $id;
188 do_get_tile_id $name, sub { 175 do_get_tile_id $name, sub {
189 $id = $_[0]; 176 $id = $_[0];
190 }; 177 };
191 BDB::flush; 178 BDB::flush;
223} 210}
224 211
225sub logprint($$$) { 212sub logprint($$$) {
226 DC::DB::Server::req (logprint => @_); 213 DC::DB::Server::req (logprint => @_);
227} 214}
215
216#############################################################################
228 217
229package DC::DB::Server; 218package DC::DB::Server;
230 219
231use strict; 220use strict;
232 221
363 my $oldfh = select $fh; $| = 1; select $oldfh; 352 my $oldfh = select $fh; $| = 1; select $oldfh;
364 353
365 my $pid = fork; 354 my $pid = fork;
366 355
367 if (defined $pid && !$pid) { 356 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 357 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 358 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 359 local $SIG{__WARN__};
371 eval { 360 eval {
372 close $FH; 361 close $FH;
373 362
412 401
413sub stop { 402sub stop {
414 close $FH; 403 close $FH;
415} 404}
416 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
4171; 4411;
418 442
419=back 443=back
420 444
421=head1 AUTHOR 445=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines