ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.11
Committed: Sat Jul 14 12:05:53 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.10: +17 -2 lines
Log Message:
greatly enhance and improve music selection algorithm and reduce database load

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     use AnyEvent ();
22 root 1.5 use Storable ();
23 root 1.9 use Config;
24 root 1.1
25 root 1.3 use CFPlus;
26    
27 root 1.9 our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28    
29 root 1.11 sub path_of_res($) {
30 root 1.10 "$DB_HOME/res-data-$_[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     sub write_file($$$) {
56     CFPlus::DB::Server::req (write_file => @_);
57     }
58    
59 root 1.11 sub prefetch_file($$$) {
60     CFPlus::DB::Server::req (prefetch_file => @_);
61     }
62    
63 root 1.1 our $tilemap;
64    
65     sub get_tile_id_sync($) {
66     my ($hash) = @_;
67    
68     # fetch the full face table first
69     unless ($tilemap) {
70     CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] });
71     sync;
72     }
73    
74     $tilemap->{$hash} ||= do {
75     my $id;
76     CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] });
77     sync;
78     $id
79     }
80     }
81    
82     package CFPlus::DB::Server;
83    
84     use strict;
85    
86     use Fcntl;
87     use BerkeleyDB;
88    
89     our $DB_ENV;
90     our $DB_STATE;
91     our %DB_TABLE;
92    
93     sub open_db {
94     mkdir $DB_HOME, 0777;
95     my $recover = $BerkeleyDB::db_version >= 4.4
96     ? eval "DB_REGISTER | DB_RECOVER"
97     : 0;
98    
99     $DB_ENV = new BerkeleyDB::Env
100     -Home => $DB_HOME,
101     -Cachesize => 8_000_000,
102     -ErrFile => "$DB_HOME/errorlog.txt",
103     # -ErrPrefix => "DATABASE",
104     -Verbose => 1,
105     -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
106     -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
107     or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
108    
109     1
110     }
111    
112     sub table($) {
113     $DB_TABLE{$_[0]} ||= do {
114     my ($table) = @_;
115    
116     $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
117    
118     new BerkeleyDB::Btree
119     -Env => $DB_ENV,
120     -Filename => $table,
121     # -Filename => "database",
122     # -Subname => $table,
123     -Property => DB_CHKSUM,
124     -Flags => DB_CREATE | DB_UPGRADE,
125     or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
126     }
127     }
128    
129 root 1.4 our $SYNC_INTERVAL = 60;
130 root 1.1
131     our %CB;
132     our $FH;
133     our $ID = "aaa0";
134     our ($fh_r_watcher, $fh_w_watcher);
135     our $sync_timer;
136     our $write_buf;
137     our $read_buf;
138    
139     sub fh_write {
140     my $len = syswrite $FH, $write_buf;
141    
142     substr $write_buf, 0, $len, "";
143    
144     undef $fh_w_watcher
145     unless length $write_buf;
146     }
147    
148     sub fh_read {
149     my $status = sysread $FH, $read_buf, 16384, length $read_buf;
150    
151     die "FATAL: database process died\n"
152     if $status == 0 && defined $status;
153    
154     while () {
155     return if 4 > length $read_buf;
156     my $len = unpack "N", $read_buf;
157    
158     return if $len + 4 > length $read_buf;
159    
160     substr $read_buf, 0, 4, "";
161     my $res = Storable::thaw substr $read_buf, 0, $len, "";
162    
163     my ($id, @args) = @$res;
164     (delete $CB{$id})->(@args);
165     }
166     }
167    
168     sub sync {
169     # biggest mess evarr
170     my $fds; (vec $fds, fileno $FH, 1) = 1;
171    
172     while (1 < scalar keys %CB) {
173     my $r = $fds;
174     my $w = length $write_buf ? $fds : undef;
175     select $r, $w, undef, undef;
176    
177     fh_write if vec $w, fileno $FH, 1;
178     fh_read if vec $r, fileno $FH, 1;
179     }
180     }
181    
182     sub req {
183     my ($type, @args) = @_;
184     my $cb = pop @args;
185    
186     my $id = ++$ID;
187     $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
188     $CB{$id} = $cb;
189    
190     $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write);
191     }
192    
193     sub sync_tick {
194     req "sync", sub { };
195     $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
196     }
197    
198     sub do_sync {
199     $DB_ENV->txn_checkpoint (0, 0, 0);
200     ()
201     }
202    
203 root 1.9 sub do_exists {
204     my ($db, $key) = @_;
205    
206     utf8::downgrade $key;
207     my $data;
208     (table $db)->db_get ($key, $data) == 0
209     ? length $data
210     : ()
211     }
212    
213 root 1.1 sub do_get {
214     my ($db, $key) = @_;
215    
216 root 1.5 utf8::downgrade $key;
217 root 1.1 my $data;
218     (table $db)->db_get ($key, $data) == 0
219     ? $data
220     : ()
221     }
222    
223     sub do_put {
224     my ($db, $key, $data) = @_;
225    
226 root 1.5 utf8::downgrade $key;
227     utf8::downgrade $data;
228 root 1.1 (table $db)->db_put ($key => $data)
229     }
230    
231     sub do_table {
232     my ($db) = @_;
233    
234     $db = table $db;
235    
236     my $cursor = $db->db_cursor;
237     my %kv;
238     my ($k, $v);
239     $kv{$k} = $v while $cursor->c_get ($k, $v, BerkeleyDB::DB_NEXT) == 0;
240    
241     \%kv
242     }
243    
244     sub do_get_tile_id {
245     my ($hash) = @_;
246    
247     my $id;
248     my $table = table "facemap";
249    
250     return $id
251     if $table->db_get ($hash, $id) == 0;
252    
253     for (1..100) {
254     my $txn = $DB_ENV->txn_begin;
255     my $status = $table->db_get (id => $id);
256     if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
257     $id = ($id || 64) + 1;
258     if ($table->db_put (id => $id) == 0
259     && $table->db_put ($hash => $id) == 0) {
260     $txn->txn_commit;
261    
262     return $id;
263     }
264     }
265     $txn->txn_abort;
266     }
267    
268     die "maximum number of transaction retries reached - database problems?";
269     }
270    
271 root 1.9 sub do_unlink {
272     unlink $_[0];
273     }
274    
275     sub do_write_file {
276     my ($file, $data) = @_;
277    
278     utf8::downgrade $file;
279     utf8::downgrade $data;
280 root 1.11 open my $fh, ">:raw", $file
281 root 1.9 or return;
282     print $fh $data;
283     close $fh;
284    
285     1
286     }
287    
288 root 1.11 sub do_prefetch_file {
289     my ($file, $size) = @_;
290    
291     utf8::downgrade $file;
292     open my $fh, "<:raw", $file
293     or return;
294     sysread $fh, my $buf, $size;
295    
296     1
297     }
298    
299 root 1.1 sub run {
300     ($FH, my $fh) = CFPlus::socketpipe;
301    
302     my $oldfh = select $FH; $| = 1; select $oldfh;
303     my $oldfh = select $fh; $| = 1; select $oldfh;
304    
305     my $pid = fork;
306    
307     if (defined $pid && !$pid) {
308     local $SIG{__DIE__};
309     eval {
310     close $FH;
311    
312     unless (eval { open_db }) {
313 root 1.6 eval { File::Path::rmtree $DB_HOME };
314 root 1.1 open_db;
315     }
316    
317     while () {
318     4 == read $fh, my $len, 4
319     or last;
320     $len = unpack "N", $len;
321     $len == read $fh, my $req, $len
322     or die "unexpected eof while reading request";
323    
324     $req = Storable::thaw $req;
325    
326     my ($id, $type, @args) = @$req;
327     my $cb = CFPlus::DB::Server->can ("do_$type")
328     or die "$type: unknown database request type\n";
329     my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
330     (syswrite $fh, $res) == length $res
331     or die;
332     }
333     };
334    
335     my $error = $@;
336    
337     eval {
338     undef %DB_TABLE;
339     undef $DB_ENV;
340    
341     Storable::store_fd [die => $error], $fh;
342     };
343    
344     CFPlus::_exit 0;
345     }
346    
347     close $fh;
348 root 1.3 CFPlus::fh_nonblocking $FH, 1;
349 root 1.1
350 root 1.2 $CB{die} = sub { die shift };
351 root 1.1
352     $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read);
353    
354     sync_tick;
355     }
356    
357 root 1.6 sub stop {
358     close $FH;
359     }
360    
361 root 1.1 1;
362    
363     =back
364    
365     =head1 AUTHOR
366    
367     Marc Lehmann <schmorp@schmorp.de>
368     http://home.schmorp.de/
369    
370     =cut
371