ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.2 by root, Fri Apr 6 08:09:43 2007 UTC vs.
Revision 1.9 by root, Thu Jul 12 17:56:51 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFPlus::DB - async. database access for cfplus 3CFPlus::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
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent (); 21use AnyEvent ();
22use Storable (); # finally 22use Storable ();
23use Config;
24
25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of($) {
30 "$DB_HOME/data-$_[0]"
31}
23 32
24sub sync { 33sub 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
39sub exists($$$) {
40 CFPlus::DB::Server::req (exists => @_);
41}
42
30sub get($$$) { 43sub get($$$) {
31 CFPlus::DB::Server::req (get => @_); 44 CFPlus::DB::Server::req (get => @_);
32} 45}
33 46
34sub put($$$$) { 47sub put($$$$) {
35 CFPlus::DB::Server::req (put => @_); 48 CFPlus::DB::Server::req (put => @_);
49}
50
51sub unlink($$) {
52 CFPlus::DB::Server::req (unlink => @_);
53}
54
55sub write_file($$$) {
56 CFPlus::DB::Server::req (write_file => @_);
36} 57}
37 58
38our $tilemap; 59our $tilemap;
39 60
40sub get_tile_id_sync($) { 61sub get_tile_id_sync($) {
59use strict; 80use strict;
60 81
61use Fcntl; 82use Fcntl;
62use BerkeleyDB; 83use BerkeleyDB;
63 84
64our $DB_HOME = "$Crossfire::VARDIR/cfplus";
65our $DB_ENV; 85our $DB_ENV;
66our $DB_STATE; 86our $DB_STATE;
67our %DB_TABLE; 87our %DB_TABLE;
68 88
69sub open_db { 89sub open_db {
100 -Flags => DB_CREATE | DB_UPGRADE, 120 -Flags => DB_CREATE | DB_UPGRADE,
101 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 121 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
102 } 122 }
103} 123}
104 124
105our $SYNC_INTERVAL = 6; 125our $SYNC_INTERVAL = 60;
106 126
107our %CB; 127our %CB;
108our $FH; 128our $FH;
109our $ID = "aaa0"; 129our $ID = "aaa0";
110our ($fh_r_watcher, $fh_w_watcher); 130our ($fh_r_watcher, $fh_w_watcher);
174sub do_sync { 194sub do_sync {
175 $DB_ENV->txn_checkpoint (0, 0, 0); 195 $DB_ENV->txn_checkpoint (0, 0, 0);
176 () 196 ()
177} 197}
178 198
199sub do_exists {
200 my ($db, $key) = @_;
201
202 utf8::downgrade $key;
203 my $data;
204 (table $db)->db_get ($key, $data) == 0
205 ? length $data
206 : ()
207}
208
179sub do_get { 209sub do_get {
180 my ($db, $key) = @_; 210 my ($db, $key) = @_;
181 211
212 utf8::downgrade $key;
182 my $data; 213 my $data;
183 (table $db)->db_get ($key, $data) == 0 214 (table $db)->db_get ($key, $data) == 0
184 ? $data 215 ? $data
185 : () 216 : ()
186} 217}
187 218
188sub do_put { 219sub do_put {
189 my ($db, $key, $data) = @_; 220 my ($db, $key, $data) = @_;
190 221
222 utf8::downgrade $key;
223 utf8::downgrade $data;
191 (table $db)->db_put ($key => $data) 224 (table $db)->db_put ($key => $data)
192} 225}
193 226
194sub do_table { 227sub do_table {
195 my ($db) = @_; 228 my ($db) = @_;
229 } 262 }
230 263
231 die "maximum number of transaction retries reached - database problems?"; 264 die "maximum number of transaction retries reached - database problems?";
232} 265}
233 266
267sub do_unlink {
268 unlink $_[0];
269}
270
271sub do_write_file {
272 my ($file, $data) = @_;
273
274 utf8::downgrade $file;
275 utf8::downgrade $data;
276 open my $fh, ">:raw", CFPlus::DB::path_of $file
277 or return;
278 print $fh $data;
279 close $fh;
280
281 1
282}
283
234sub run { 284sub run {
235 ($FH, my $fh) = CFPlus::socketpipe; 285 ($FH, my $fh) = CFPlus::socketpipe;
236 286
237 my $oldfh = select $FH; $| = 1; select $oldfh; 287 my $oldfh = select $FH; $| = 1; select $oldfh;
238 my $oldfh = select $fh; $| = 1; select $oldfh; 288 my $oldfh = select $fh; $| = 1; select $oldfh;
243 local $SIG{__DIE__}; 293 local $SIG{__DIE__};
244 eval { 294 eval {
245 close $FH; 295 close $FH;
246 296
247 unless (eval { open_db }) { 297 unless (eval { open_db }) {
248 File::Path::rmtree $DB_HOME; 298 eval { File::Path::rmtree $DB_HOME };
249 open_db; 299 open_db;
250 } 300 }
251 301
252 while () { 302 while () {
253 4 == read $fh, my $len, 4 303 4 == read $fh, my $len, 4
278 328
279 CFPlus::_exit 0; 329 CFPlus::_exit 0;
280 } 330 }
281 331
282 close $fh; 332 close $fh;
283 fcntl $FH, F_SETFL, O_NONBLOCK; 333 CFPlus::fh_nonblocking $FH, 1;
284 334
285 $CB{die} = sub { die shift }; 335 $CB{die} = sub { die shift };
286 336
287 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 337 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read);
288 338
289 sync_tick; 339 sync_tick;
340}
341
342sub stop {
343 close $FH;
290} 344}
291 345
2921; 3461;
293 347
294=back 348=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines