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.15 by root, Mon Aug 6 02:11:45 2007 UTC vs.
Revision 1.28 by root, Wed Dec 26 20:46:39 2007 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 cfplus
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 Event (); 23use BDB;
24 24
25use CFPlus; 25use dc;
26 26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; 27our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
28our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29
30if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") {
31 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
32 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$DBDIR to $DB_HOME\n";
33}
34
35our $DB_ENV;
36our $DB_STATE;
37our %DB_TABLE;
38
39sub open_db {
40 mkdir $DB_HOME, 0777;
41
42 $DB_ENV = db_env_create;
43
44 $DB_ENV->set_errfile (\*STDERR);
45 $DB_ENV->set_msgfile (\*STDERR);
46 $DB_ENV->set_verbose (-1, 1);
47
48 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC);
49 $DB_ENV->set_cachesize (0, 2048 * 1024, 0);
50
51 db_env_open $DB_ENV, $DB_HOME,
52 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
53 0666;
54
55 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
56
57 1
58}
59
60sub table($) {
61 $DB_TABLE{$_[0]} ||= do {
62 my ($table) = @_;
63
64 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
65
66 my $db = db_create $DB_ENV;
67 $db->set_flags (BDB::CHKSUM);
68
69 db_open $db, undef, $table, undef, BDB::BTREE,
70 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
71
72 $! and "unable to open/create database table $_[0]: ". BDB::strerror;
73
74 $db
75 }
76}
77
78#############################################################################
79
80unless (eval { open_db }) {
81 warn "$@";#d#
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;
94
95sub exists($$$) {
96 my ($db, $key, $cb) = @_;
97
98 my $data;
99 db_get table $db, undef, $key, $data, 0, sub {
100 $cb->($! ? () : length $data);
101 };
102}
103
104sub get($$$) {
105 my ($db, $key, $cb) = @_;
106
107 my $data;
108 db_get table $db, undef, $key, $data, 0, sub {
109 $cb->($! ? () : $data);
110 };
111}
112
113sub put($$$$) {
114 my ($db, $key, $data, $cb) = @_;
115
116 db_put table $db, undef, $key, $data, 0, sub {
117 $cb->($!);
118 $SYNC->again unless $SYNC->is_active;
119 };
120}
121
122sub do_table {
123 my ($db, $cb) = @_;
124
125 $db = table $db;
126
127 my $cursor = $db->cursor;
128 my %kv;
129
130 for (;;) {
131 db_c_get $cursor, my $k, my $v, BDB::NEXT;
132 last if $!;
133 $kv{$k} = $v;
134 }
135
136 $cb->(\%kv);
137}
138
139sub do_get_tile_id {
140 my ($name, $cb) = @_;
141
142 my $table = table "facemap";
143 my $id;
144
145 db_get $table, undef, $name, $id, 0;
146 return $cb->($id) unless $!;
147
148 for (1..100) {
149 my $txn = $DB_ENV->txn_begin;
150 db_get $table, $txn, id => $id, 0;
151
152 $id = 64 if $id < 64;
153
154 ++$id;
155
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 }
165
166 die "maximum number of transaction retries reached - database problems?";
167}
168
169sub get_tile_id_sync($) {
170 my ($name) = @_;
171
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 {
187 my $id;
188 do_get_tile_id $name, sub {
189 $id = $_[0];
190 };
191 BDB::flush;
192 $id
193 }
194}
195
196#############################################################################
28 197
29sub path_of_res($) { 198sub path_of_res($) {
30 utf8::downgrade $_[0]; # bug in unpack "H*" 199 utf8::downgrade $_[0]; # bug in unpack "H*"
31 "$DB_HOME/res-data-" . unpack "H*", $_[0] 200 "$DB_HOME/res-data-" . unpack "H*", $_[0]
32} 201}
33 202
34sub sync { 203sub sync {
35 # for debugging 204 # for debugging
36 #CFPlus::DB::Server::req (sync => sub { }); 205 #dc::DB::Server::req (sync => sub { });
37 CFPlus::DB::Server::sync (); 206 dc::DB::Server::sync ();
38}
39
40sub exists($$$) {
41 CFPlus::DB::Server::req (exists => @_);
42}
43
44sub get($$$) {
45 CFPlus::DB::Server::req (get => @_);
46}
47
48sub put($$$$) {
49 CFPlus::DB::Server::req (put => @_);
50} 207}
51 208
52sub unlink($$) { 209sub unlink($$) {
53 CFPlus::DB::Server::req (unlink => @_); 210 dc::DB::Server::req (unlink => @_);
211}
212
213sub read_file($$) {
214 dc::DB::Server::req (read_file => @_);
54} 215}
55 216
56sub write_file($$$) { 217sub write_file($$$) {
57 CFPlus::DB::Server::req (write_file => @_); 218 dc::DB::Server::req (write_file => @_);
58} 219}
59 220
60sub prefetch_file($$$) { 221sub prefetch_file($$$) {
61 CFPlus::DB::Server::req (prefetch_file => @_); 222 dc::DB::Server::req (prefetch_file => @_);
62} 223}
63 224
64sub logprint($$$) { 225sub logprint($$$) {
65 CFPlus::DB::Server::req (logprint => @_); 226 dc::DB::Server::req (logprint => @_);
66} 227}
67 228
68our $tilemap;
69
70sub get_tile_id_sync($) {
71 my ($hash) = @_;
72
73 # fetch the full face table first
74 unless ($tilemap) {
75 CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] });
76 sync;
77 }
78
79 $tilemap->{$hash} ||= do {
80 my $id;
81 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] });
82 sync;
83 $id
84 }
85}
86
87package CFPlus::DB::Server; 229package dc::DB::Server;
88 230
89use strict; 231use strict;
90 232
233use EV ();
91use Fcntl; 234use Fcntl;
92use BerkeleyDB;
93
94our $DB_ENV;
95our $DB_STATE;
96our %DB_TABLE;
97
98sub open_db {
99 mkdir $DB_HOME, 0777;
100 my $recover = $BerkeleyDB::db_version >= 4.4
101 ? eval "DB_REGISTER | DB_RECOVER"
102 : 0;
103
104 $DB_ENV = new BerkeleyDB::Env
105 -Home => $DB_HOME,
106 -Cachesize => 8_000_000,
107 -ErrFile => "$DB_HOME/errorlog.txt",
108# -ErrPrefix => "DATABASE",
109 -Verbose => 1,
110 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
111 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
112 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
113
114 1
115}
116
117sub table($) {
118 $DB_TABLE{$_[0]} ||= do {
119 my ($table) = @_;
120
121 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
122
123 new BerkeleyDB::Btree
124 -Env => $DB_ENV,
125 -Filename => $table,
126# -Filename => "database",
127# -Subname => $table,
128 -Property => DB_CHKSUM,
129 -Flags => DB_CREATE | DB_UPGRADE,
130 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
131 }
132}
133 235
134our %CB; 236our %CB;
135our $FH; 237our $FH;
136our $ID = "aaa0"; 238our $ID = "aaa0";
137our ($fh_r_watcher, $fh_w_watcher); 239our ($fh_r_watcher, $fh_w_watcher);
138our $sync_timer; 240our $sync_timer;
139our $write_buf; 241our $write_buf;
140our $read_buf; 242our $read_buf;
141 243
142our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub {
143 CFPlus::DB::Server::req (sync => sub { });
144 $_[0]->w->stop;
145});
146
147sub fh_write { 244sub fh_write {
148 my $len = syswrite $FH, $write_buf; 245 my $len = syswrite $FH, $write_buf;
149 246
150 substr $write_buf, 0, $len, ""; 247 substr $write_buf, 0, $len, "";
151 248
194 my $id = ++$ID; 291 my $id = ++$ID;
195 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 292 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
196 $CB{$id} = $cb; 293 $CB{$id} = $cb;
197 294
198 $fh_w_watcher->start; 295 $fh_w_watcher->start;
199 $SYNC->start;
200}
201
202sub do_sync {
203 $DB_ENV->txn_checkpoint (0, 0, 0);
204 ()
205}
206
207sub do_exists {
208 my ($db, $key) = @_;
209
210 utf8::downgrade $key;
211 my $data;
212 (table $db)->db_get ($key, $data) == 0
213 ? length $data
214 : ()
215}
216
217sub do_get {
218 my ($db, $key) = @_;
219
220 utf8::downgrade $key;
221 my $data;
222 (table $db)->db_get ($key, $data) == 0
223 ? $data
224 : ()
225}
226
227sub do_put {
228 my ($db, $key, $data) = @_;
229
230 utf8::downgrade $key;
231 utf8::downgrade $data;
232 (table $db)->db_put ($key => $data)
233}
234
235sub do_table {
236 my ($db) = @_;
237
238 $db = table $db;
239
240 my $cursor = $db->db_cursor;
241 my %kv;
242 my ($k, $v);
243 $kv{$k} = $v while $cursor->c_get ($k, $v, BerkeleyDB::DB_NEXT) == 0;
244
245 \%kv
246}
247
248sub do_get_tile_id {
249 my ($hash) = @_;
250
251 my $id;
252 my $table = table "facemap";
253
254 return $id
255 if $table->db_get ($hash, $id) == 0;
256
257 for (1..100) {
258 my $txn = $DB_ENV->txn_begin;
259 my $status = $table->db_get (id => $id);
260 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
261 $id = ($id || 64) + 1;
262 if ($table->db_put (id => $id) == 0
263 && $table->db_put ($hash => $id) == 0) {
264 $txn->txn_commit;
265
266 return $id;
267 }
268 }
269 $txn->txn_abort;
270 }
271
272 die "maximum number of transaction retries reached - database problems?";
273} 296}
274 297
275sub do_unlink { 298sub do_unlink {
276 unlink $_[0]; 299 unlink $_[0];
277} 300}
278 301
302sub do_read_file {
303 my ($path) = @_;
304
305 utf8::downgrade $path;
306 open my $fh, "<:raw", $path
307 or return;
308 sysread $fh, my $buf, -s $fh;
309
310 $buf
311}
312
279sub do_write_file { 313sub do_write_file {
280 my ($file, $data) = @_; 314 my ($path, $data) = @_;
281 315
282 utf8::downgrade $file; 316 utf8::downgrade $path;
283 utf8::downgrade $data; 317 utf8::downgrade $data;
284 open my $fh, ">:raw", $file 318 open my $fh, ">:raw", $path
285 or return; 319 or return;
286 print $fh $data; 320 syswrite $fh, $data;
287 close $fh; 321 close $fh;
288 322
289 1 323 1
290} 324}
291 325
292sub do_prefetch_file { 326sub do_prefetch_file {
293 my ($file, $size) = @_; 327 my ($path, $size) = @_;
294 328
295 utf8::downgrade $file; 329 utf8::downgrade $path;
296 open my $fh, "<:raw", $file 330 open my $fh, "<:raw", $path
297 or return; 331 or return;
298 sysread $fh, my $buf, $size; 332 sysread $fh, my $buf, $size;
299 333
300 1 334 1
301} 335}
321 355
322 print { $LOG_FH{$path} } "$ts $line\n" 356 print { $LOG_FH{$path} } "$ts $line\n"
323} 357}
324 358
325sub run { 359sub run {
326 ($FH, my $fh) = CFPlus::socketpipe; 360 ($FH, my $fh) = dc::socketpipe;
327 361
328 my $oldfh = select $FH; $| = 1; select $oldfh; 362 my $oldfh = select $FH; $| = 1; select $oldfh;
329 my $oldfh = select $fh; $| = 1; select $oldfh; 363 my $oldfh = select $fh; $| = 1; select $oldfh;
330 364
331 my $pid = fork; 365 my $pid = fork;
335 local $SIG{__DIE__}; 369 local $SIG{__DIE__};
336 local $SIG{__WARN__}; 370 local $SIG{__WARN__};
337 eval { 371 eval {
338 close $FH; 372 close $FH;
339 373
340 unless (eval { open_db }) {
341 eval { File::Path::rmtree $DB_HOME };
342 open_db;
343 }
344
345 while () { 374 while () {
346 4 == read $fh, my $len, 4 375 4 == read $fh, my $len, 4
347 or last; 376 or last;
348 $len = unpack "N", $len; 377 $len = unpack "N", $len;
349 $len == read $fh, my $req, $len 378 $len == read $fh, my $req, $len
350 or die "unexpected eof while reading request"; 379 or die "unexpected eof while reading request";
351 380
352 $req = Storable::thaw $req; 381 $req = Storable::thaw $req;
353 382
354 my ($id, $type, @args) = @$req; 383 my ($id, $type, @args) = @$req;
355 my $cb = CFPlus::DB::Server->can ("do_$type") 384 my $cb = dc::DB::Server->can ("do_$type")
356 or die "$type: unknown database request type\n"; 385 or die "$type: unknown database request type\n";
357 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
358 (syswrite $fh, $res) == length $res 387 (syswrite $fh, $res) == length $res
359 or die; 388 or die "DB::write: $!";
360 } 389 }
361 }; 390 };
362 391
363 my $error = $@; 392 my $error = $@;
364 393
365 eval { 394 eval {
366 undef %DB_TABLE;
367 undef $DB_ENV;
368
369 Storable::store_fd [die => $error], $fh; 395 Storable::store_fd [die => $error], $fh;
370 }; 396 };
371 397
398 warn $error
399 if $error;
400
372 CFPlus::_exit 0; 401 dc::_exit 0;
373 } 402 }
374 403
375 close $fh; 404 close $fh;
376 CFPlus::fh_nonblocking $FH, 1; 405 dc::fh_nonblocking $FH, 1;
377 406
378 $CB{die} = sub { die shift }; 407 $CB{die} = sub { die shift };
379 408
380 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); 409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
381 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); 410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
382 $SYNC->start;
383} 411}
384 412
385sub stop { 413sub stop {
386 close $FH; 414 close $FH;
387} 415}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines