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

1=head1 NAME 1=head1 NAME
2 2
3CFPlus::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 CFPlus::DB; 7 use DC::DB;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package CFPlus::DB; 15package DC::DB;
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use Storable (); 21use Storable ();
22use Config; 22use Config;
23use BDB; 23use BDB;
24 24
25use CFPlus; 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 CFPlus::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;
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 187 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 188}
202 189
203sub sync { 190sub sync {
204 # for debugging 191 # for debugging
205 #CFPlus::DB::Server::req (sync => sub { }); 192 #DC::DB::Server::req (sync => sub { });
206 CFPlus::DB::Server::sync (); 193 DC::DB::Server::sync ();
207} 194}
208 195
209sub unlink($$) { 196sub unlink($$) {
210 CFPlus::DB::Server::req (unlink => @_); 197 DC::DB::Server::req (unlink => @_);
211} 198}
212 199
213sub read_file($$) { 200sub read_file($$) {
214 CFPlus::DB::Server::req (read_file => @_); 201 DC::DB::Server::req (read_file => @_);
215} 202}
216 203
217sub write_file($$$) { 204sub write_file($$$) {
218 CFPlus::DB::Server::req (write_file => @_); 205 DC::DB::Server::req (write_file => @_);
219} 206}
220 207
221sub prefetch_file($$$) { 208sub prefetch_file($$$) {
222 CFPlus::DB::Server::req (prefetch_file => @_); 209 DC::DB::Server::req (prefetch_file => @_);
223} 210}
224 211
225sub logprint($$$) { 212sub logprint($$$) {
226 CFPlus::DB::Server::req (logprint => @_); 213 DC::DB::Server::req (logprint => @_);
227} 214}
228 215
216#############################################################################
217
229package CFPlus::DB::Server; 218package DC::DB::Server;
230 219
231use strict; 220use strict;
232 221
233use EV (); 222use EV ();
234use Fcntl; 223use Fcntl;
355 344
356 print { $LOG_FH{$path} } "$ts $line\n" 345 print { $LOG_FH{$path} } "$ts $line\n"
357} 346}
358 347
359sub run { 348sub run {
360 ($FH, my $fh) = CFPlus::socketpipe; 349 ($FH, my $fh) = DC::socketpipe;
361 350
362 my $oldfh = select $FH; $| = 1; select $oldfh; 351 my $oldfh = select $FH; $| = 1; select $oldfh;
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
379 or die "unexpected eof while reading request"; 368 or die "unexpected eof while reading request";
380 369
381 $req = Storable::thaw $req; 370 $req = Storable::thaw $req;
382 371
383 my ($id, $type, @args) = @$req; 372 my ($id, $type, @args) = @$req;
384 my $cb = CFPlus::DB::Server->can ("do_$type") 373 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 374 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 375 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 376 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 377 or die "DB::write: $!";
389 } 378 }
396 }; 385 };
397 386
398 warn $error 387 warn $error
399 if $error; 388 if $error;
400 389
401 CFPlus::_exit 0; 390 DC::_exit 0;
402 } 391 }
403 392
404 close $fh; 393 close $fh;
405 CFPlus::fh_nonblocking $FH, 1; 394 DC::fh_nonblocking $FH, 1;
406 395
407 $CB{die} = sub { die shift }; 396 $CB{die} = sub { die shift };
408 397
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 398 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 399 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
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