ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.28
Committed: Wed Dec 26 20:46:39 2007 UTC (16 years, 4 months ago) by root
Branch: MAIN
Changes since 1.27: +17 -17 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 dc::DB - async. database and filesystem access for cfplus
4
5 =head1 SYNOPSIS
6
7 use dc::DB;
8
9 =head1 DESCRIPTION
10
11 =over 4
12
13 =cut
14
15 package dc::DB;
16
17 use strict;
18 use utf8;
19
20 use Carp ();
21 use Storable ();
22 use Config;
23 use BDB;
24
25 use dc;
26
27 our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}";
28 our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29
30 if (!-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
35 our $DB_ENV;
36 our $DB_STATE;
37 our %DB_TABLE;
38
39 sub 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
60 sub 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
80 unless (eval { open_db }) {
81 warn "$@";#d#
82 eval { File::Path::rmtree $DB_HOME };
83 open_db;
84 }
85
86 our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
87
88 our $SYNC = EV::timer_ns 0, 60, sub {
89 $_[0]->stop;
90 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
91 };
92
93 our $tilemap;
94
95 sub 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
104 sub 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
113 sub 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
122 sub 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
139 sub 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
169 sub 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 #############################################################################
197
198 sub path_of_res($) {
199 utf8::downgrade $_[0]; # bug in unpack "H*"
200 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201 }
202
203 sub sync {
204 # for debugging
205 #dc::DB::Server::req (sync => sub { });
206 dc::DB::Server::sync ();
207 }
208
209 sub unlink($$) {
210 dc::DB::Server::req (unlink => @_);
211 }
212
213 sub read_file($$) {
214 dc::DB::Server::req (read_file => @_);
215 }
216
217 sub write_file($$$) {
218 dc::DB::Server::req (write_file => @_);
219 }
220
221 sub prefetch_file($$$) {
222 dc::DB::Server::req (prefetch_file => @_);
223 }
224
225 sub logprint($$$) {
226 dc::DB::Server::req (logprint => @_);
227 }
228
229 package dc::DB::Server;
230
231 use strict;
232
233 use EV ();
234 use Fcntl;
235
236 our %CB;
237 our $FH;
238 our $ID = "aaa0";
239 our ($fh_r_watcher, $fh_w_watcher);
240 our $sync_timer;
241 our $write_buf;
242 our $read_buf;
243
244 sub fh_write {
245 my $len = syswrite $FH, $write_buf;
246
247 substr $write_buf, 0, $len, "";
248
249 $fh_w_watcher->stop
250 unless length $write_buf;
251 }
252
253 sub fh_read {
254 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
255
256 die "FATAL: database process died\n"
257 if $status == 0 && defined $status;
258
259 while () {
260 return if 4 > length $read_buf;
261 my $len = unpack "N", $read_buf;
262
263 return if $len + 4 > length $read_buf;
264
265 substr $read_buf, 0, 4, "";
266 my $res = Storable::thaw substr $read_buf, 0, $len, "";
267
268 my ($id, @args) = @$res;
269 (delete $CB{$id})->(@args);
270 }
271 }
272
273 sub sync {
274 # biggest mess evarr
275 my $fds; (vec $fds, fileno $FH, 1) = 1;
276
277 while (1 < scalar keys %CB) {
278 my $r = $fds;
279 my $w = length $write_buf ? $fds : undef;
280 select $r, $w, undef, undef;
281
282 fh_write if vec $w, fileno $FH, 1;
283 fh_read if vec $r, fileno $FH, 1;
284 }
285 }
286
287 sub req {
288 my ($type, @args) = @_;
289 my $cb = pop @args;
290
291 my $id = ++$ID;
292 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
293 $CB{$id} = $cb;
294
295 $fh_w_watcher->start;
296 }
297
298 sub do_unlink {
299 unlink $_[0];
300 }
301
302 sub 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
313 sub do_write_file {
314 my ($path, $data) = @_;
315
316 utf8::downgrade $path;
317 utf8::downgrade $data;
318 open my $fh, ">:raw", $path
319 or return;
320 syswrite $fh, $data;
321 close $fh;
322
323 1
324 }
325
326 sub do_prefetch_file {
327 my ($path, $size) = @_;
328
329 utf8::downgrade $path;
330 open my $fh, "<:raw", $path
331 or return;
332 sysread $fh, my $buf, $size;
333
334 1
335 }
336
337 our %LOG_FH;
338
339 sub do_logprint {
340 my ($path, $line) = @_;
341
342 $LOG_FH{$path} ||= do {
343 open my $fh, ">>:utf8", $path
344 or warn "Couldn't open logfile $path: $!";
345
346 $fh->autoflush (1);
347
348 $fh
349 };
350
351 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
352
353 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
354 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
355
356 print { $LOG_FH{$path} } "$ts $line\n"
357 }
358
359 sub run {
360 ($FH, my $fh) = dc::socketpipe;
361
362 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh;
364
365 my $pid = fork;
366
367 if (defined $pid && !$pid) {
368 local $SIG{QUIT};
369 local $SIG{__DIE__};
370 local $SIG{__WARN__};
371 eval {
372 close $FH;
373
374 while () {
375 4 == read $fh, my $len, 4
376 or last;
377 $len = unpack "N", $len;
378 $len == read $fh, my $req, $len
379 or die "unexpected eof while reading request";
380
381 $req = Storable::thaw $req;
382
383 my ($id, $type, @args) = @$req;
384 my $cb = dc::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!";
389 }
390 };
391
392 my $error = $@;
393
394 eval {
395 Storable::store_fd [die => $error], $fh;
396 };
397
398 warn $error
399 if $error;
400
401 dc::_exit 0;
402 }
403
404 close $fh;
405 dc::fh_nonblocking $FH, 1;
406
407 $CB{die} = sub { die shift };
408
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
411 }
412
413 sub stop {
414 close $FH;
415 }
416
417 1;
418
419 =back
420
421 =head1 AUTHOR
422
423 Marc Lehmann <schmorp@schmorp.de>
424 http://home.schmorp.de/
425
426 =cut
427