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.14 |
use Event (); |
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 |
|
|
our %CB; |
131 |
|
|
our $FH; |
132 |
|
|
our $ID = "aaa0"; |
133 |
|
|
our ($fh_r_watcher, $fh_w_watcher); |
134 |
|
|
our $sync_timer; |
135 |
|
|
our $write_buf; |
136 |
|
|
our $read_buf; |
137 |
|
|
|
138 |
root |
1.14 |
our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { |
139 |
|
|
CFPlus::DB::Server::req (sync => sub { }); |
140 |
|
|
$_[0]->w->stop; |
141 |
|
|
}); |
142 |
|
|
|
143 |
root |
1.1 |
sub fh_write { |
144 |
|
|
my $len = syswrite $FH, $write_buf; |
145 |
|
|
|
146 |
|
|
substr $write_buf, 0, $len, ""; |
147 |
|
|
|
148 |
root |
1.14 |
$fh_w_watcher->stop |
149 |
root |
1.1 |
unless length $write_buf; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
sub fh_read { |
153 |
|
|
my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
154 |
|
|
|
155 |
|
|
die "FATAL: database process died\n" |
156 |
|
|
if $status == 0 && defined $status; |
157 |
|
|
|
158 |
|
|
while () { |
159 |
|
|
return if 4 > length $read_buf; |
160 |
|
|
my $len = unpack "N", $read_buf; |
161 |
|
|
|
162 |
|
|
return if $len + 4 > length $read_buf; |
163 |
|
|
|
164 |
|
|
substr $read_buf, 0, 4, ""; |
165 |
|
|
my $res = Storable::thaw substr $read_buf, 0, $len, ""; |
166 |
|
|
|
167 |
|
|
my ($id, @args) = @$res; |
168 |
|
|
(delete $CB{$id})->(@args); |
169 |
|
|
} |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
sub sync { |
173 |
|
|
# biggest mess evarr |
174 |
|
|
my $fds; (vec $fds, fileno $FH, 1) = 1; |
175 |
|
|
|
176 |
|
|
while (1 < scalar keys %CB) { |
177 |
|
|
my $r = $fds; |
178 |
|
|
my $w = length $write_buf ? $fds : undef; |
179 |
|
|
select $r, $w, undef, undef; |
180 |
|
|
|
181 |
|
|
fh_write if vec $w, fileno $FH, 1; |
182 |
|
|
fh_read if vec $r, fileno $FH, 1; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
sub req { |
187 |
|
|
my ($type, @args) = @_; |
188 |
|
|
my $cb = pop @args; |
189 |
|
|
|
190 |
|
|
my $id = ++$ID; |
191 |
|
|
$write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
192 |
|
|
$CB{$id} = $cb; |
193 |
|
|
|
194 |
root |
1.14 |
$fh_w_watcher->start; |
195 |
|
|
$SYNC->start; |
196 |
root |
1.1 |
} |
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 |
root |
1.14 |
$fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
353 |
|
|
$fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
354 |
|
|
$SYNC->start; |
355 |
root |
1.1 |
} |
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 |
|
|
|