1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::DB - async. database access for cfplus |
3 | CFPlus::DB - async. database and filesystem access for cfplus |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFPlus::DB; |
7 | use CFPlus::DB; |
8 | |
8 | |
… | |
… | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use Carp (); |
20 | use Carp (); |
21 | use AnyEvent (); |
21 | use AnyEvent (); |
22 | use Storable (); # finally |
22 | use Storable (); |
|
|
23 | use Config; |
|
|
24 | |
|
|
25 | use CFPlus; |
|
|
26 | |
|
|
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
|
|
28 | |
|
|
29 | sub path_of_res($) { |
|
|
30 | utf8::downgrade $_[0]; # bug in unpack "H*" |
|
|
31 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
|
|
32 | } |
23 | |
33 | |
24 | sub sync { |
34 | sub sync { |
25 | # for debugging |
35 | # for debugging |
26 | #CFPlus::DB::Server::req (sync => sub { }); |
36 | #CFPlus::DB::Server::req (sync => sub { }); |
27 | CFPlus::DB::Server::sync (); |
37 | CFPlus::DB::Server::sync (); |
28 | } |
38 | } |
29 | |
39 | |
|
|
40 | sub exists($$$) { |
|
|
41 | CFPlus::DB::Server::req (exists => @_); |
|
|
42 | } |
|
|
43 | |
30 | sub get($$$) { |
44 | sub get($$$) { |
31 | CFPlus::DB::Server::req (get => @_); |
45 | CFPlus::DB::Server::req (get => @_); |
32 | } |
46 | } |
33 | |
47 | |
34 | sub put($$$$) { |
48 | sub put($$$$) { |
35 | CFPlus::DB::Server::req (put => @_); |
49 | CFPlus::DB::Server::req (put => @_); |
|
|
50 | } |
|
|
51 | |
|
|
52 | sub unlink($$) { |
|
|
53 | CFPlus::DB::Server::req (unlink => @_); |
|
|
54 | } |
|
|
55 | |
|
|
56 | sub write_file($$$) { |
|
|
57 | CFPlus::DB::Server::req (write_file => @_); |
|
|
58 | } |
|
|
59 | |
|
|
60 | sub prefetch_file($$$) { |
|
|
61 | CFPlus::DB::Server::req (prefetch_file => @_); |
36 | } |
62 | } |
37 | |
63 | |
38 | our $tilemap; |
64 | our $tilemap; |
39 | |
65 | |
40 | sub get_tile_id_sync($) { |
66 | sub get_tile_id_sync($) { |
… | |
… | |
59 | use strict; |
85 | use strict; |
60 | |
86 | |
61 | use Fcntl; |
87 | use Fcntl; |
62 | use BerkeleyDB; |
88 | use BerkeleyDB; |
63 | |
89 | |
64 | our $DB_HOME = "$Crossfire::VARDIR/cfplus"; |
|
|
65 | our $DB_ENV; |
90 | our $DB_ENV; |
66 | our $DB_STATE; |
91 | our $DB_STATE; |
67 | our %DB_TABLE; |
92 | our %DB_TABLE; |
68 | |
93 | |
69 | sub open_db { |
94 | sub open_db { |
… | |
… | |
100 | -Flags => DB_CREATE | DB_UPGRADE, |
125 | -Flags => DB_CREATE | DB_UPGRADE, |
101 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
126 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
102 | } |
127 | } |
103 | } |
128 | } |
104 | |
129 | |
105 | our $SYNC_INTERVAL = 6; |
130 | our $SYNC_INTERVAL = 60; |
106 | |
131 | |
107 | our %CB; |
132 | our %CB; |
108 | our $FH; |
133 | our $FH; |
109 | our $ID = "aaa0"; |
134 | our $ID = "aaa0"; |
110 | our ($fh_r_watcher, $fh_w_watcher); |
135 | our ($fh_r_watcher, $fh_w_watcher); |
… | |
… | |
174 | sub do_sync { |
199 | sub do_sync { |
175 | $DB_ENV->txn_checkpoint (0, 0, 0); |
200 | $DB_ENV->txn_checkpoint (0, 0, 0); |
176 | () |
201 | () |
177 | } |
202 | } |
178 | |
203 | |
|
|
204 | 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 | |
179 | sub do_get { |
214 | sub do_get { |
180 | my ($db, $key) = @_; |
215 | my ($db, $key) = @_; |
181 | |
216 | |
|
|
217 | utf8::downgrade $key; |
182 | my $data; |
218 | my $data; |
183 | (table $db)->db_get ($key, $data) == 0 |
219 | (table $db)->db_get ($key, $data) == 0 |
184 | ? $data |
220 | ? $data |
185 | : () |
221 | : () |
186 | } |
222 | } |
187 | |
223 | |
188 | sub do_put { |
224 | sub do_put { |
189 | my ($db, $key, $data) = @_; |
225 | my ($db, $key, $data) = @_; |
190 | |
226 | |
|
|
227 | utf8::downgrade $key; |
|
|
228 | utf8::downgrade $data; |
191 | (table $db)->db_put ($key => $data) |
229 | (table $db)->db_put ($key => $data) |
192 | } |
230 | } |
193 | |
231 | |
194 | sub do_table { |
232 | sub do_table { |
195 | my ($db) = @_; |
233 | my ($db) = @_; |
… | |
… | |
229 | } |
267 | } |
230 | |
268 | |
231 | die "maximum number of transaction retries reached - database problems?"; |
269 | die "maximum number of transaction retries reached - database problems?"; |
232 | } |
270 | } |
233 | |
271 | |
|
|
272 | 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 | open my $fh, ">:raw", $file |
|
|
282 | or return; |
|
|
283 | print $fh $data; |
|
|
284 | close $fh; |
|
|
285 | |
|
|
286 | 1 |
|
|
287 | } |
|
|
288 | |
|
|
289 | 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 | |
234 | sub run { |
300 | sub run { |
235 | ($FH, my $fh) = CFPlus::socketpipe; |
301 | ($FH, my $fh) = CFPlus::socketpipe; |
236 | |
302 | |
237 | my $oldfh = select $FH; $| = 1; select $oldfh; |
303 | my $oldfh = select $FH; $| = 1; select $oldfh; |
238 | my $oldfh = select $fh; $| = 1; select $oldfh; |
304 | my $oldfh = select $fh; $| = 1; select $oldfh; |
… | |
… | |
243 | local $SIG{__DIE__}; |
309 | local $SIG{__DIE__}; |
244 | eval { |
310 | eval { |
245 | close $FH; |
311 | close $FH; |
246 | |
312 | |
247 | unless (eval { open_db }) { |
313 | unless (eval { open_db }) { |
248 | File::Path::rmtree $DB_HOME; |
314 | eval { File::Path::rmtree $DB_HOME }; |
249 | open_db; |
315 | open_db; |
250 | } |
316 | } |
251 | |
317 | |
252 | while () { |
318 | while () { |
253 | 4 == read $fh, my $len, 4 |
319 | 4 == read $fh, my $len, 4 |
… | |
… | |
278 | |
344 | |
279 | CFPlus::_exit 0; |
345 | CFPlus::_exit 0; |
280 | } |
346 | } |
281 | |
347 | |
282 | close $fh; |
348 | close $fh; |
283 | fcntl $FH, F_SETFL, O_NONBLOCK; |
349 | CFPlus::fh_nonblocking $FH, 1; |
284 | |
350 | |
285 | $CB{die} = sub { die shift }; |
351 | $CB{die} = sub { die shift }; |
286 | |
352 | |
287 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
353 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
288 | |
354 | |
289 | sync_tick; |
355 | sync_tick; |
|
|
356 | } |
|
|
357 | |
|
|
358 | sub stop { |
|
|
359 | close $FH; |
290 | } |
360 | } |
291 | |
361 | |
292 | 1; |
362 | 1; |
293 | |
363 | |
294 | =back |
364 | =back |