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.28 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.35 by root, Sun Mar 30 06:05:52 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
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
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 $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 28our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
29our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}";
28our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 30our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29 31
30if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { 32unless (-d $DB_HOME) {
33 if (-d "$Deliantra::VARDIR/$ODBDIR") {
34 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
35 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
36 } elsif (-d "$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";
39 } else {
40 File::Path::mkpath [$DB_HOME]
41 or die "unable to create database directory $DB_HOME: $!";
42 }
33} 43}
44
45BDB::max_poll_time 0.03;
46BDB::max_parallel 1;
34 47
35our $DB_ENV; 48our $DB_ENV;
36our $DB_STATE; 49our $DB_STATE;
37our %DB_TABLE; 50our %DB_TABLE;
51our $TILE_SEQ;
38 52
39sub open_db { 53sub try_open_db {
40 mkdir $DB_HOME, 0777; 54 File::Path::mkpath [$DB_HOME];
41 55
42 $DB_ENV = db_env_create; 56 $DB_ENV = db_env_create;
43 57
44 $DB_ENV->set_errfile (\*STDERR); 58 $DB_ENV->set_errfile (\*STDERR);
45 $DB_ENV->set_msgfile (\*STDERR); 59 $DB_ENV->set_msgfile (\*STDERR);
75 } 89 }
76} 90}
77 91
78############################################################################# 92#############################################################################
79 93
80unless (eval { open_db }) { 94our $WATCHER;
81 warn "$@";#d# 95our $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; 96our $facemap;
94 97
95sub exists($$$) { 98sub exists($$$) {
96 my ($db, $key, $cb) = @_; 99 my ($db, $key, $cb) = @_;
97 100
98 my $data; 101 my $data;
140 my ($name, $cb) = @_; 143 my ($name, $cb) = @_;
141 144
142 my $table = table "facemap"; 145 my $table = table "facemap";
143 my $id; 146 my $id;
144 147
145 db_get $table, undef, $name, $id, 0; 148 db_get $table, undef, $name => $id, 0;
146 return $cb->($id) unless $!; 149 $! or return $cb->($id);
147 150
148 for (1..100) { 151 unless ($TILE_SEQ) {
149 my $txn = $DB_ENV->txn_begin; 152 $TILE_SEQ = $table->sequence;
150 db_get $table, $txn, id => $id, 0; 153 $TILE_SEQ->initial_value (64);
154 $TILE_SEQ->set_cachesize (0);
155 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
156 }
151 157
152 $id = 64 if $id < 64; 158 db_sequence_get $TILE_SEQ, undef, 1, my $id;
153 159
154 ++$id; 160 die "unable to allocate tile id: $!"
155 161 if $!;
156 db_put $table, $txn, id => $id, 0;
157 db_txn_finish $txn;
158
159 $SYNC->again unless $SYNC->is_active;
160
161 return $cb->($id) unless $!;
162
163 select undef, undef, undef, 0.01 * rand;
164 } 162
163 db_put $table, undef, $name => $id, 0;
164 $cb->($id);
165 165
166 die "maximum number of transaction retries reached - database problems?";
167} 166}
168 167
169sub get_tile_id_sync($) { 168sub get_tile_id_sync($) {
170 my ($name) = @_; 169 my ($name) = @_;
171 170
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 { 171 $facemap->{$name} ||= do {
187 my $id; 172 my $id;
188 do_get_tile_id $name, sub { 173 do_get_tile_id $name, sub {
189 $id = $_[0]; 174 $id = $_[0];
190 }; 175 };
191 BDB::flush; 176 BDB::flush;
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 185 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 186}
202 187
203sub sync { 188sub sync {
204 # for debugging 189 # for debugging
205 #dc::DB::Server::req (sync => sub { }); 190 #DC::DB::Server::req (sync => sub { });
206 dc::DB::Server::sync (); 191 DC::DB::Server::sync ();
207} 192}
208 193
209sub unlink($$) { 194sub unlink($$) {
210 dc::DB::Server::req (unlink => @_); 195 DC::DB::Server::req (unlink => @_);
211} 196}
212 197
213sub read_file($$) { 198sub read_file($$) {
214 dc::DB::Server::req (read_file => @_); 199 DC::DB::Server::req (read_file => @_);
215} 200}
216 201
217sub write_file($$$) { 202sub write_file($$$) {
218 dc::DB::Server::req (write_file => @_); 203 DC::DB::Server::req (write_file => @_);
219} 204}
220 205
221sub prefetch_file($$$) { 206sub prefetch_file($$$) {
222 dc::DB::Server::req (prefetch_file => @_); 207 DC::DB::Server::req (prefetch_file => @_);
223} 208}
224 209
225sub logprint($$$) { 210sub logprint($$$) {
226 dc::DB::Server::req (logprint => @_); 211 DC::DB::Server::req (logprint => @_);
227} 212}
228 213
214#############################################################################
215
229package dc::DB::Server; 216package DC::DB::Server;
230 217
231use strict; 218use strict;
232 219
233use EV (); 220use EV ();
234use Fcntl; 221use Fcntl;
355 342
356 print { $LOG_FH{$path} } "$ts $line\n" 343 print { $LOG_FH{$path} } "$ts $line\n"
357} 344}
358 345
359sub run { 346sub run {
360 ($FH, my $fh) = dc::socketpipe; 347 ($FH, my $fh) = DC::socketpipe;
361 348
362 my $oldfh = select $FH; $| = 1; select $oldfh; 349 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 350 my $oldfh = select $fh; $| = 1; select $oldfh;
364 351
365 my $pid = fork; 352 my $pid = fork;
366 353
367 if (defined $pid && !$pid) { 354 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 355 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 356 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 357 local $SIG{__WARN__};
371 eval { 358 eval {
372 close $FH; 359 close $FH;
373 360
379 or die "unexpected eof while reading request"; 366 or die "unexpected eof while reading request";
380 367
381 $req = Storable::thaw $req; 368 $req = Storable::thaw $req;
382 369
383 my ($id, $type, @args) = @$req; 370 my ($id, $type, @args) = @$req;
384 my $cb = dc::DB::Server->can ("do_$type") 371 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 372 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 373 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 374 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 375 or die "DB::write: $!";
389 } 376 }
396 }; 383 };
397 384
398 warn $error 385 warn $error
399 if $error; 386 if $error;
400 387
401 dc::_exit 0; 388 DC::_exit 0;
402 } 389 }
403 390
404 close $fh; 391 close $fh;
405 dc::fh_nonblocking $FH, 1; 392 DC::fh_nonblocking $FH, 1;
406 393
407 $CB{die} = sub { die shift }; 394 $CB{die} = sub { die shift };
408 395
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 396 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 397 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
412 399
413sub stop { 400sub stop {
414 close $FH; 401 close $FH;
415} 402}
416 403
404package DC::DB;
405
406sub nuke_db {
407 File::Path::mkpath [$DB_HOME];
408 eval { File::Path::rmtree $DB_HOME };
409}
410
411sub open_db {
412 unless (eval { try_open_db }) {
413 warn "$@";#d#
414 eval { nuke_db };
415 try_open_db;
416 }
417
418 # fetch the full face table first
419 unless ($facemap) {
420 do_table facemap => sub {
421 $facemap = $_[0];
422 delete $facemap->{id};
423 my %maptile = reverse %$facemap;#d#
424 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
425 $facemap = { };#d#
426 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
427 }#d#
428 };
429 }
430
431 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
432 $SYNC = EV::timer_ns 0, 60, sub {
433 $_[0]->stop;
434 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
435 };
436}
437
438END {
439 undef $TILE_SEQ;
440 %DB_TABLE = ();
441 undef $DB_ENV;
442}
443
4171; 4441;
418 445
419=back 446=back
420 447
421=head1 AUTHOR 448=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines