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