ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.31
Committed: Thu Jan 10 23:02:19 2008 UTC (16 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-0_9964
Changes since 1.30: +10 -4 lines
Log Message:
*** empty log message ***

File Contents

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