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