ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.30
Committed: Thu Dec 27 18:35:56 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-0_9963
Changes since 1.29: +18 -15 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 BDB::max_poll_time 0.03;
36
37 our $DB_ENV;
38 our $DB_STATE;
39 our %DB_TABLE;
40
41 sub open_db {
42 mkdir $DB_HOME, 0777;
43
44 $DB_ENV = db_env_create;
45
46 $DB_ENV->set_errfile (\*STDERR);
47 $DB_ENV->set_msgfile (\*STDERR);
48 $DB_ENV->set_verbose (-1, 1);
49
50 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC);
51 $DB_ENV->set_cachesize (0, 2048 * 1024, 0);
52
53 db_env_open $DB_ENV, $DB_HOME,
54 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
55 0666;
56
57 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
58
59 1
60 }
61
62 sub table($) {
63 $DB_TABLE{$_[0]} ||= do {
64 my ($table) = @_;
65
66 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
67
68 my $db = db_create $DB_ENV;
69 $db->set_flags (BDB::CHKSUM);
70
71 db_open $db, undef, $table, undef, BDB::BTREE,
72 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
73
74 $! and "unable to open/create database table $_[0]: ". BDB::strerror;
75
76 $db
77 }
78 }
79
80 #############################################################################
81
82 unless (eval { open_db }) {
83 warn "$@";#d#
84 eval { File::Path::rmtree $DB_HOME };
85 open_db;
86 }
87
88 our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
89
90 our $SYNC = EV::timer_ns 0, 60, sub {
91 $_[0]->stop;
92 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
93 };
94
95 our $tilemap;
96
97 sub exists($$$) {
98 my ($db, $key, $cb) = @_;
99
100 my $data;
101 db_get table $db, undef, $key, $data, 0, sub {
102 $cb->($! ? () : length $data);
103 };
104 }
105
106 sub get($$$) {
107 my ($db, $key, $cb) = @_;
108
109 my $data;
110 db_get table $db, undef, $key, $data, 0, sub {
111 $cb->($! ? () : $data);
112 };
113 }
114
115 sub put($$$$) {
116 my ($db, $key, $data, $cb) = @_;
117
118 db_put table $db, undef, $key, $data, 0, sub {
119 $cb->($!);
120 $SYNC->again unless $SYNC->is_active;
121 };
122 }
123
124 sub do_table {
125 my ($db, $cb) = @_;
126
127 $db = table $db;
128
129 my $cursor = $db->cursor;
130 my %kv;
131
132 for (;;) {
133 db_c_get $cursor, my $k, my $v, BDB::NEXT;
134 last if $!;
135 $kv{$k} = $v;
136 }
137
138 $cb->(\%kv);
139 }
140
141 sub do_get_tile_id {
142 my ($name, $cb) = @_;
143
144 my $table = table "facemap";
145 my $id;
146
147 db_get $table, undef, $name, $id, 0;
148 return $cb->($id) unless $!;
149
150 for (1..100) {
151 my $txn = $DB_ENV->txn_begin;
152 db_get $table, $txn, id => $id, 0;
153
154 $id = 64 if $id < 64;
155
156 ++$id;
157
158 db_put $table, $txn, id => $id, 0;
159 db_txn_finish $txn;
160
161 $SYNC->again unless $SYNC->is_active;
162
163 return $cb->($id) unless $!;
164
165 select undef, undef, undef, 0.01 * rand;
166 }
167
168 die "maximum number of transaction retries reached - database problems?";
169 }
170
171 sub get_tile_id_sync($) {
172 my ($name) = @_;
173
174 $tilemap->{$name} ||= do {
175 my $id;
176 do_get_tile_id $name, sub {
177 $id = $_[0];
178 };
179 BDB::flush;
180 $id
181 }
182 }
183
184 #############################################################################
185
186 sub path_of_res($) {
187 utf8::downgrade $_[0]; # bug in unpack "H*"
188 "$DB_HOME/res-data-" . unpack "H*", $_[0]
189 }
190
191 sub sync {
192 # for debugging
193 #DC::DB::Server::req (sync => sub { });
194 DC::DB::Server::sync ();
195 }
196
197 sub unlink($$) {
198 DC::DB::Server::req (unlink => @_);
199 }
200
201 sub read_file($$) {
202 DC::DB::Server::req (read_file => @_);
203 }
204
205 sub write_file($$$) {
206 DC::DB::Server::req (write_file => @_);
207 }
208
209 sub prefetch_file($$$) {
210 DC::DB::Server::req (prefetch_file => @_);
211 }
212
213 sub logprint($$$) {
214 DC::DB::Server::req (logprint => @_);
215 }
216
217 #############################################################################
218
219 # fetch the full face table first
220 unless ($tilemap) {
221 do_table facemap => sub {
222 $tilemap = $_[0];
223 delete $tilemap->{id};
224 my %maptile = reverse %$tilemap;#d#
225 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
226 $tilemap = { };#d#
227 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
228 }#d#
229 };
230 }
231
232 package DC::DB::Server;
233
234 use strict;
235
236 use EV ();
237 use Fcntl;
238
239 our %CB;
240 our $FH;
241 our $ID = "aaa0";
242 our ($fh_r_watcher, $fh_w_watcher);
243 our $sync_timer;
244 our $write_buf;
245 our $read_buf;
246
247 sub fh_write {
248 my $len = syswrite $FH, $write_buf;
249
250 substr $write_buf, 0, $len, "";
251
252 $fh_w_watcher->stop
253 unless length $write_buf;
254 }
255
256 sub fh_read {
257 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
258
259 die "FATAL: database process died\n"
260 if $status == 0 && defined $status;
261
262 while () {
263 return if 4 > length $read_buf;
264 my $len = unpack "N", $read_buf;
265
266 return if $len + 4 > length $read_buf;
267
268 substr $read_buf, 0, 4, "";
269 my $res = Storable::thaw substr $read_buf, 0, $len, "";
270
271 my ($id, @args) = @$res;
272 (delete $CB{$id})->(@args);
273 }
274 }
275
276 sub sync {
277 # biggest mess evarr
278 my $fds; (vec $fds, fileno $FH, 1) = 1;
279
280 while (1 < scalar keys %CB) {
281 my $r = $fds;
282 my $w = length $write_buf ? $fds : undef;
283 select $r, $w, undef, undef;
284
285 fh_write if vec $w, fileno $FH, 1;
286 fh_read if vec $r, fileno $FH, 1;
287 }
288 }
289
290 sub req {
291 my ($type, @args) = @_;
292 my $cb = pop @args;
293
294 my $id = ++$ID;
295 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
296 $CB{$id} = $cb;
297
298 $fh_w_watcher->start;
299 }
300
301 sub do_unlink {
302 unlink $_[0];
303 }
304
305 sub do_read_file {
306 my ($path) = @_;
307
308 utf8::downgrade $path;
309 open my $fh, "<:raw", $path
310 or return;
311 sysread $fh, my $buf, -s $fh;
312
313 $buf
314 }
315
316 sub do_write_file {
317 my ($path, $data) = @_;
318
319 utf8::downgrade $path;
320 utf8::downgrade $data;
321 open my $fh, ">:raw", $path
322 or return;
323 syswrite $fh, $data;
324 close $fh;
325
326 1
327 }
328
329 sub do_prefetch_file {
330 my ($path, $size) = @_;
331
332 utf8::downgrade $path;
333 open my $fh, "<:raw", $path
334 or return;
335 sysread $fh, my $buf, $size;
336
337 1
338 }
339
340 our %LOG_FH;
341
342 sub do_logprint {
343 my ($path, $line) = @_;
344
345 $LOG_FH{$path} ||= do {
346 open my $fh, ">>:utf8", $path
347 or warn "Couldn't open logfile $path: $!";
348
349 $fh->autoflush (1);
350
351 $fh
352 };
353
354 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
355
356 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
357 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
358
359 print { $LOG_FH{$path} } "$ts $line\n"
360 }
361
362 sub run {
363 ($FH, my $fh) = DC::socketpipe;
364
365 my $oldfh = select $FH; $| = 1; select $oldfh;
366 my $oldfh = select $fh; $| = 1; select $oldfh;
367
368 my $pid = fork;
369
370 if (defined $pid && !$pid) {
371 local $SIG{QUIT} = "IGNORE";
372 local $SIG{__DIE__};
373 local $SIG{__WARN__};
374 eval {
375 close $FH;
376
377 while () {
378 4 == read $fh, my $len, 4
379 or last;
380 $len = unpack "N", $len;
381 $len == read $fh, my $req, $len
382 or die "unexpected eof while reading request";
383
384 $req = Storable::thaw $req;
385
386 my ($id, $type, @args) = @$req;
387 my $cb = DC::DB::Server->can ("do_$type")
388 or die "$type: unknown database request type\n";
389 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
390 (syswrite $fh, $res) == length $res
391 or die "DB::write: $!";
392 }
393 };
394
395 my $error = $@;
396
397 eval {
398 Storable::store_fd [die => $error], $fh;
399 };
400
401 warn $error
402 if $error;
403
404 DC::_exit 0;
405 }
406
407 close $fh;
408 DC::fh_nonblocking $FH, 1;
409
410 $CB{die} = sub { die shift };
411
412 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
413 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
414 }
415
416 sub stop {
417 close $FH;
418 }
419
420 1;
421
422 =back
423
424 =head1 AUTHOR
425
426 Marc Lehmann <schmorp@schmorp.de>
427 http://home.schmorp.de/
428
429 =cut
430