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

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.9 CFPlus::DB - async. database and filesystem access for cfplus
4 root 1.1
5     =head1 SYNOPSIS
6    
7     use CFPlus::DB;
8    
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15     package CFPlus::DB;
16    
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.3 use CFPlus;
26    
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.24 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 root 1.9 }
77    
78 root 1.24 #############################################################################
79    
80     unless (eval { open_db }) {
81     warn "$@";#d#
82     eval { File::Path::rmtree $DB_HOME };
83     open_db;
84 root 1.1 }
85    
86 root 1.24 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 root 1.9 sub exists($$$) {
96 root 1.24 my ($db, $key, $cb) = @_;
97    
98     my $data;
99     db_get table $db, undef, $key, $data, 0, sub {
100     $cb->($! ? () : length $data);
101     };
102 root 1.9 }
103    
104 root 1.1 sub get($$$) {
105 root 1.24 my ($db, $key, $cb) = @_;
106    
107     my $data;
108     db_get table $db, undef, $key, $data, 0, sub {
109     $cb->($! ? () : $data);
110     };
111 root 1.1 }
112    
113     sub put($$$$) {
114 root 1.24 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 root 1.1 }
121    
122 root 1.24 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 root 1.9 }
138    
139 root 1.24 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 root 1.16
161 root 1.24 return $cb->($id) unless $!;
162 root 1.9
163 root 1.24 select undef, undef, undef, 0.01 * rand;
164     }
165 root 1.11
166 root 1.24 die "maximum number of transaction retries reached - database problems?";
167 root 1.15 }
168    
169 root 1.1 sub get_tile_id_sync($) {
170 root 1.20 my ($name) = @_;
171 root 1.1
172     # fetch the full face table first
173     unless ($tilemap) {
174 root 1.24 do_table facemap => sub {
175 root 1.20 $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     CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
181     }#d#
182 root 1.24 };
183     BDB::flush;
184 root 1.1 }
185    
186 root 1.20 $tilemap->{$name} ||= do {
187 root 1.1 my $id;
188 root 1.24 do_get_tile_id $name, sub {
189     $id = $_[0];
190     };
191     BDB::flush;
192 root 1.1 $id
193     }
194     }
195    
196 root 1.24 #############################################################################
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     #CFPlus::DB::Server::req (sync => sub { });
206     CFPlus::DB::Server::sync ();
207     }
208 root 1.1
209 root 1.24 sub unlink($$) {
210     CFPlus::DB::Server::req (unlink => @_);
211     }
212 root 1.1
213 root 1.24 sub read_file($$) {
214     CFPlus::DB::Server::req (read_file => @_);
215     }
216 root 1.1
217 root 1.24 sub write_file($$$) {
218     CFPlus::DB::Server::req (write_file => @_);
219     }
220 root 1.1
221 root 1.24 sub prefetch_file($$$) {
222     CFPlus::DB::Server::req (prefetch_file => @_);
223     }
224 root 1.1
225 root 1.24 sub logprint($$$) {
226     CFPlus::DB::Server::req (logprint => @_);
227 root 1.1 }
228    
229 root 1.24 package CFPlus::DB::Server;
230 root 1.1
231 root 1.24 use strict;
232 root 1.1
233 root 1.24 use EV ();
234     use Fcntl;
235 root 1.1
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 root 1.14 $fh_w_watcher->stop
250 root 1.1 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 root 1.14 $fh_w_watcher->start;
296 root 1.1 }
297    
298 root 1.9 sub do_unlink {
299     unlink $_[0];
300     }
301    
302 root 1.16 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 root 1.9 sub do_write_file {
314 root 1.16 my ($path, $data) = @_;
315 root 1.9
316 root 1.16 utf8::downgrade $path;
317 root 1.9 utf8::downgrade $data;
318 root 1.16 open my $fh, ">:raw", $path
319 root 1.9 or return;
320 root 1.16 syswrite $fh, $data;
321 root 1.9 close $fh;
322    
323     1
324     }
325    
326 root 1.11 sub do_prefetch_file {
327 root 1.16 my ($path, $size) = @_;
328 root 1.11
329 root 1.16 utf8::downgrade $path;
330     open my $fh, "<:raw", $path
331 root 1.11 or return;
332     sysread $fh, my $buf, $size;
333    
334     1
335     }
336    
337 root 1.15 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 root 1.1 sub run {
360     ($FH, my $fh) = CFPlus::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 root 1.15 local $SIG{QUIT};
369 root 1.1 local $SIG{__DIE__};
370 root 1.15 local $SIG{__WARN__};
371 root 1.1 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 = CFPlus::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 root 1.21 or die "DB::write: $!";
389 root 1.1 }
390     };
391    
392     my $error = $@;
393    
394     eval {
395     Storable::store_fd [die => $error], $fh;
396     };
397    
398 root 1.25 warn $error
399     if $error;
400 root 1.24
401 root 1.1 CFPlus::_exit 0;
402     }
403    
404     close $fh;
405 root 1.3 CFPlus::fh_nonblocking $FH, 1;
406 root 1.1
407 root 1.2 $CB{die} = sub { die shift };
408 root 1.1
409 root 1.21 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410     $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
411 root 1.1 }
412    
413 root 1.6 sub stop {
414     close $FH;
415     }
416    
417 root 1.1 1;
418    
419     =back
420    
421     =head1 AUTHOR
422    
423     Marc Lehmann <schmorp@schmorp.de>
424     http://home.schmorp.de/
425    
426     =cut
427