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.1 by root, Fri Apr 6 07:45:34 2007 UTC vs.
Revision 1.20 by root, Tue Aug 28 20:07:18 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFPlus::DB - async. database access for cfplus 3CFPlus::DB - async. database and filesystem access for cfplus
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFPlus::DB; 7 use CFPlus::DB;
8 8
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use Storable ();
22use Config;
21use AnyEvent (); 23use Event ();
22use Storable (); # finally 24
25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of_res($) {
30 utf8::downgrade $_[0]; # bug in unpack "H*"
31 "$DB_HOME/res-data-" . unpack "H*", $_[0]
32}
23 33
24sub sync { 34sub sync {
25 # for debugging 35 # for debugging
26 #CFPlus::DB::Server::req (sync => sub { }); 36 #CFPlus::DB::Server::req (sync => sub { });
27 CFPlus::DB::Server::sync (); 37 CFPlus::DB::Server::sync ();
28} 38}
29 39
40sub exists($$$) {
41 CFPlus::DB::Server::req (exists => @_);
42}
43
30sub get($$$) { 44sub get($$$) {
31 CFPlus::DB::Server::req (get => @_); 45 CFPlus::DB::Server::req (get => @_);
32} 46}
33 47
34sub put($$$$) { 48sub put($$$$) {
35 CFPlus::DB::Server::req (put => @_); 49 CFPlus::DB::Server::req (put => @_);
36} 50}
37 51
52sub unlink($$) {
53 CFPlus::DB::Server::req (unlink => @_);
54}
55
56sub read_file($$) {
57 CFPlus::DB::Server::req (read_file => @_);
58}
59
60sub write_file($$$) {
61 CFPlus::DB::Server::req (write_file => @_);
62}
63
64sub prefetch_file($$$) {
65 CFPlus::DB::Server::req (prefetch_file => @_);
66}
67
68sub logprint($$$) {
69 CFPlus::DB::Server::req (logprint => @_);
70}
71
38our $tilemap; 72our $tilemap;
39 73
40sub get_tile_id_sync($) { 74sub get_tile_id_sync($) {
41 my ($hash) = @_; 75 my ($name) = @_;
42 76
43 # fetch the full face table first 77 # fetch the full face table first
44 unless ($tilemap) { 78 unless ($tilemap) {
45 CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] }); 79 CFPlus::DB::Server::req (table => facemap => sub {
80 $tilemap = $_[0];
81 delete $tilemap->{id};
82 my %maptile = reverse %$tilemap;#d#
83 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
84 $tilemap = { };#d#
85 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
86 }#d#
87 });
46 sync; 88 sync;
47 } 89 }
48 90
49 $tilemap->{$hash} ||= do { 91 $tilemap->{$name} ||= do {
50 my $id; 92 my $id;
51 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] }); 93 CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] });
52 sync; 94 sync;
53 $id 95 $id
54 } 96 }
55} 97}
56 98
59use strict; 101use strict;
60 102
61use Fcntl; 103use Fcntl;
62use BerkeleyDB; 104use BerkeleyDB;
63 105
64our $DB_HOME = "$Crossfire::VARDIR/cfplus";
65our $DB_ENV; 106our $DB_ENV;
66our $DB_STATE; 107our $DB_STATE;
67our %DB_TABLE; 108our %DB_TABLE;
68 109
69sub open_db { 110sub open_db {
77 -Cachesize => 8_000_000, 118 -Cachesize => 8_000_000,
78 -ErrFile => "$DB_HOME/errorlog.txt", 119 -ErrFile => "$DB_HOME/errorlog.txt",
79# -ErrPrefix => "DATABASE", 120# -ErrPrefix => "DATABASE",
80 -Verbose => 1, 121 -Verbose => 1,
81 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 122 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
82 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 123 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
83 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; 124 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
84 125
85 1 126 1
86} 127}
87 128
95 -Env => $DB_ENV, 136 -Env => $DB_ENV,
96 -Filename => $table, 137 -Filename => $table,
97# -Filename => "database", 138# -Filename => "database",
98# -Subname => $table, 139# -Subname => $table,
99 -Property => DB_CHKSUM, 140 -Property => DB_CHKSUM,
100 -Flags => DB_CREATE | DB_UPGRADE, 141 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
101 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 142 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
102 } 143 }
103} 144}
104
105our $SYNC_INTERVAL = 6;
106 145
107our %CB; 146our %CB;
108our $FH; 147our $FH;
109our $ID = "aaa0"; 148our $ID = "aaa0";
110our ($fh_r_watcher, $fh_w_watcher); 149our ($fh_r_watcher, $fh_w_watcher);
111our $sync_timer; 150our $sync_timer;
112our $write_buf; 151our $write_buf;
113our $read_buf; 152our $read_buf;
114 153
154our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub {
155 CFPlus::DB::Server::req (sync => sub { });
156 $_[0]->w->stop;
157});
158
115sub fh_write { 159sub fh_write {
116 my $len = syswrite $FH, $write_buf; 160 my $len = syswrite $FH, $write_buf;
117 161
118 substr $write_buf, 0, $len, ""; 162 substr $write_buf, 0, $len, "";
119 163
120 undef $fh_w_watcher 164 $fh_w_watcher->stop
121 unless length $write_buf; 165 unless length $write_buf;
122} 166}
123 167
124sub fh_read { 168sub fh_read {
125 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 169 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
161 205
162 my $id = ++$ID; 206 my $id = ++$ID;
163 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 207 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
164 $CB{$id} = $cb; 208 $CB{$id} = $cb;
165 209
166 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 210 $fh_w_watcher->start;
167} 211 $SYNC->start;
168
169sub sync_tick {
170 req "sync", sub { };
171 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
172} 212}
173 213
174sub do_sync { 214sub do_sync {
175 $DB_ENV->txn_checkpoint (0, 0, 0); 215 $DB_ENV->txn_checkpoint (0, 0, 0);
176 () 216 ()
177} 217}
178 218
219sub do_exists {
220 my ($db, $key) = @_;
221
222 utf8::downgrade $key;
223 my $data;
224 (table $db)->db_get ($key, $data) == 0
225 ? length $data
226 : ()
227}
228
179sub do_get { 229sub do_get {
180 my ($db, $key) = @_; 230 my ($db, $key) = @_;
181 231
232 utf8::downgrade $key;
182 my $data; 233 my $data;
183 (table $db)->db_get ($key, $data) == 0 234 (table $db)->db_get ($key, $data) == 0
184 ? $data 235 ? $data
185 : () 236 : ()
186} 237}
187 238
188sub do_put { 239sub do_put {
189 my ($db, $key, $data) = @_; 240 my ($db, $key, $data) = @_;
190 241
242 utf8::downgrade $key;
243 utf8::downgrade $data;
191 (table $db)->db_put ($key => $data) 244 (table $db)->db_put ($key => $data)
192} 245}
193 246
194sub do_table { 247sub do_table {
195 my ($db) = @_; 248 my ($db) = @_;
203 256
204 \%kv 257 \%kv
205} 258}
206 259
207sub do_get_tile_id { 260sub do_get_tile_id {
208 my ($hash) = @_; 261 my ($name) = @_;
209 262
210 my $id; 263 my $id;
211 my $table = table "facemap"; 264 my $table = table "facemap";
212 265
213 return $id 266 return $id
214 if $table->db_get ($hash, $id) == 0; 267 if $table->db_get ($name, $id) == 0;
215 268
216 for (1..100) { 269 for (1..100) {
217 my $txn = $DB_ENV->txn_begin; 270 my $txn = $DB_ENV->txn_begin;
218 my $status = $table->db_get (id => $id); 271 my $status = $table->db_get (id => $id);
219 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 272 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
220 $id = ($id || 64) + 1; 273 $id = ($id || 64) + 1;
221 if ($table->db_put (id => $id) == 0 274 if ($table->db_put (id => $id) == 0
222 && $table->db_put ($hash => $id) == 0) { 275 && $table->db_put ($name => $id) == 0) {
223 $txn->txn_commit; 276 $txn->txn_commit;
224 277
225 return $id; 278 return $id;
226 } 279 }
227 } 280 }
228 $txn->txn_abort; 281 $txn->txn_abort;
282 select undef, undef, undef, 0.01 * rand;
229 } 283 }
230 284
231 die "maximum number of transaction retries reached - database problems?"; 285 die "maximum number of transaction retries reached - database problems?";
286}
287
288sub do_unlink {
289 unlink $_[0];
290}
291
292sub do_read_file {
293 my ($path) = @_;
294
295 utf8::downgrade $path;
296 open my $fh, "<:raw", $path
297 or return;
298 sysread $fh, my $buf, -s $fh;
299
300 $buf
301}
302
303sub do_write_file {
304 my ($path, $data) = @_;
305
306 utf8::downgrade $path;
307 utf8::downgrade $data;
308 open my $fh, ">:raw", $path
309 or return;
310 syswrite $fh, $data;
311 close $fh;
312
313 1
314}
315
316sub do_prefetch_file {
317 my ($path, $size) = @_;
318
319 utf8::downgrade $path;
320 open my $fh, "<:raw", $path
321 or return;
322 sysread $fh, my $buf, $size;
323
324 1
325}
326
327our %LOG_FH;
328
329sub do_logprint {
330 my ($path, $line) = @_;
331
332 $LOG_FH{$path} ||= do {
333 open my $fh, ">>:utf8", $path
334 or warn "Couldn't open logfile $path: $!";
335
336 $fh->autoflush (1);
337
338 $fh
339 };
340
341 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
342
343 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
344 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
345
346 print { $LOG_FH{$path} } "$ts $line\n"
232} 347}
233 348
234sub run { 349sub run {
235 ($FH, my $fh) = CFPlus::socketpipe; 350 ($FH, my $fh) = CFPlus::socketpipe;
236 351
238 my $oldfh = select $fh; $| = 1; select $oldfh; 353 my $oldfh = select $fh; $| = 1; select $oldfh;
239 354
240 my $pid = fork; 355 my $pid = fork;
241 356
242 if (defined $pid && !$pid) { 357 if (defined $pid && !$pid) {
358 local $SIG{QUIT};
243 local $SIG{__DIE__}; 359 local $SIG{__DIE__};
360 local $SIG{__WARN__};
244 eval { 361 eval {
245 close $FH; 362 close $FH;
246 363
247 unless (eval { open_db }) { 364 unless (eval { open_db }) {
248 File::Path::rmtree $DB_HOME; 365 eval { File::Path::rmtree $DB_HOME };
249 open_db; 366 open_db;
250 } 367 }
251 368
252 while () { 369 while () {
253 4 == read $fh, my $len, 4 370 4 == read $fh, my $len, 4
278 395
279 CFPlus::_exit 0; 396 CFPlus::_exit 0;
280 } 397 }
281 398
282 close $fh; 399 close $fh;
283 fcntl $FH, F_SETFL, O_NONBLOCK; 400 CFPlus::fh_nonblocking $FH, 1;
284 401
285 $CB{die} = sub { 402 $CB{die} = sub { die shift };
286 die shift;
287 };
288 403
289 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 404 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
405 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
406 $SYNC->start;
407}
290 408
291 sync_tick; 409sub stop {
410 close $FH;
292} 411}
293 412
2941; 4131;
295 414
296=back 415=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines