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

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.29 DC::DB - async. database and filesystem access for cfplus
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.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 root 1.9
35 root 1.30 BDB::max_poll_time 0.03;
36    
37 root 1.24 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 root 1.9 }
79    
80 root 1.24 #############################################################################
81    
82     unless (eval { open_db }) {
83     warn "$@";#d#
84     eval { File::Path::rmtree $DB_HOME };
85     open_db;
86 root 1.1 }
87    
88 root 1.24 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 root 1.9 sub exists($$$) {
98 root 1.24 my ($db, $key, $cb) = @_;
99    
100     my $data;
101     db_get table $db, undef, $key, $data, 0, sub {
102     $cb->($! ? () : length $data);
103     };
104 root 1.9 }
105    
106 root 1.1 sub get($$$) {
107 root 1.24 my ($db, $key, $cb) = @_;
108    
109     my $data;
110     db_get table $db, undef, $key, $data, 0, sub {
111     $cb->($! ? () : $data);
112     };
113 root 1.1 }
114    
115     sub put($$$$) {
116 root 1.24 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 root 1.1 }
123    
124 root 1.24 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 root 1.9 }
140    
141 root 1.24 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 root 1.16
163 root 1.24 return $cb->($id) unless $!;
164 root 1.9
165 root 1.24 select undef, undef, undef, 0.01 * rand;
166     }
167 root 1.11
168 root 1.24 die "maximum number of transaction retries reached - database problems?";
169 root 1.15 }
170    
171 root 1.1 sub get_tile_id_sync($) {
172 root 1.20 my ($name) = @_;
173 root 1.1
174 root 1.20 $tilemap->{$name} ||= do {
175 root 1.1 my $id;
176 root 1.24 do_get_tile_id $name, sub {
177     $id = $_[0];
178     };
179     BDB::flush;
180 root 1.1 $id
181     }
182     }
183    
184 root 1.24 #############################################################################
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 root 1.29 #DC::DB::Server::req (sync => sub { });
194     DC::DB::Server::sync ();
195 root 1.24 }
196 root 1.1
197 root 1.24 sub unlink($$) {
198 root 1.29 DC::DB::Server::req (unlink => @_);
199 root 1.24 }
200 root 1.1
201 root 1.24 sub read_file($$) {
202 root 1.29 DC::DB::Server::req (read_file => @_);
203 root 1.24 }
204 root 1.1
205 root 1.24 sub write_file($$$) {
206 root 1.29 DC::DB::Server::req (write_file => @_);
207 root 1.24 }
208 root 1.1
209 root 1.24 sub prefetch_file($$$) {
210 root 1.29 DC::DB::Server::req (prefetch_file => @_);
211 root 1.24 }
212 root 1.1
213 root 1.24 sub logprint($$$) {
214 root 1.29 DC::DB::Server::req (logprint => @_);
215 root 1.1 }
216    
217 root 1.30 #############################################################################
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 root 1.29 package DC::DB::Server;
233 root 1.1
234 root 1.24 use strict;
235 root 1.1
236 root 1.24 use EV ();
237     use Fcntl;
238 root 1.1
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 root 1.14 $fh_w_watcher->stop
253 root 1.1 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 root 1.14 $fh_w_watcher->start;
299 root 1.1 }
300    
301 root 1.9 sub do_unlink {
302     unlink $_[0];
303     }
304    
305 root 1.16 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 root 1.9 sub do_write_file {
317 root 1.16 my ($path, $data) = @_;
318 root 1.9
319 root 1.16 utf8::downgrade $path;
320 root 1.9 utf8::downgrade $data;
321 root 1.16 open my $fh, ">:raw", $path
322 root 1.9 or return;
323 root 1.16 syswrite $fh, $data;
324 root 1.9 close $fh;
325    
326     1
327     }
328    
329 root 1.11 sub do_prefetch_file {
330 root 1.16 my ($path, $size) = @_;
331 root 1.11
332 root 1.16 utf8::downgrade $path;
333     open my $fh, "<:raw", $path
334 root 1.11 or return;
335     sysread $fh, my $buf, $size;
336    
337     1
338     }
339    
340 root 1.15 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 root 1.1 sub run {
363 root 1.29 ($FH, my $fh) = DC::socketpipe;
364 root 1.1
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 root 1.30 local $SIG{QUIT} = "IGNORE";
372 root 1.1 local $SIG{__DIE__};
373 root 1.15 local $SIG{__WARN__};
374 root 1.1 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 root 1.29 my $cb = DC::DB::Server->can ("do_$type")
388 root 1.1 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 root 1.21 or die "DB::write: $!";
392 root 1.1 }
393     };
394    
395     my $error = $@;
396    
397     eval {
398     Storable::store_fd [die => $error], $fh;
399     };
400    
401 root 1.25 warn $error
402     if $error;
403 root 1.24
404 root 1.29 DC::_exit 0;
405 root 1.1 }
406    
407     close $fh;
408 root 1.29 DC::fh_nonblocking $FH, 1;
409 root 1.1
410 root 1.2 $CB{die} = sub { die shift };
411 root 1.1
412 root 1.21 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
413     $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
414 root 1.1 }
415    
416 root 1.6 sub stop {
417     close $FH;
418     }
419    
420 root 1.1 1;
421    
422     =back
423    
424     =head1 AUTHOR
425    
426     Marc Lehmann <schmorp@schmorp.de>
427     http://home.schmorp.de/
428    
429     =cut
430