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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines