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