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.3 by root, Fri Apr 6 21:53:56 2007 UTC vs.
Revision 1.11 by root, Sat Jul 14 12:05:53 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;
23 24
24use CFPlus; 25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of_res($) {
30 "$DB_HOME/res-data-$_[0]"
31}
25 32
26sub sync { 33sub sync {
27 # for debugging 34 # for debugging
28 #CFPlus::DB::Server::req (sync => sub { }); 35 #CFPlus::DB::Server::req (sync => sub { });
29 CFPlus::DB::Server::sync (); 36 CFPlus::DB::Server::sync ();
30} 37}
31 38
39sub exists($$$) {
40 CFPlus::DB::Server::req (exists => @_);
41}
42
32sub get($$$) { 43sub get($$$) {
33 CFPlus::DB::Server::req (get => @_); 44 CFPlus::DB::Server::req (get => @_);
34} 45}
35 46
36sub put($$$$) { 47sub put($$$$) {
37 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 => @_);
57}
58
59sub prefetch_file($$$) {
60 CFPlus::DB::Server::req (prefetch_file => @_);
38} 61}
39 62
40our $tilemap; 63our $tilemap;
41 64
42sub get_tile_id_sync($) { 65sub get_tile_id_sync($) {
61use strict; 84use strict;
62 85
63use Fcntl; 86use Fcntl;
64use BerkeleyDB; 87use BerkeleyDB;
65 88
66our $DB_HOME = "$Crossfire::VARDIR/cfplus";
67our $DB_ENV; 89our $DB_ENV;
68our $DB_STATE; 90our $DB_STATE;
69our %DB_TABLE; 91our %DB_TABLE;
70 92
71sub open_db { 93sub open_db {
102 -Flags => DB_CREATE | DB_UPGRADE, 124 -Flags => DB_CREATE | DB_UPGRADE,
103 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 125 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
104 } 126 }
105} 127}
106 128
107our $SYNC_INTERVAL = 6; 129our $SYNC_INTERVAL = 60;
108 130
109our %CB; 131our %CB;
110our $FH; 132our $FH;
111our $ID = "aaa0"; 133our $ID = "aaa0";
112our ($fh_r_watcher, $fh_w_watcher); 134our ($fh_r_watcher, $fh_w_watcher);
176sub do_sync { 198sub do_sync {
177 $DB_ENV->txn_checkpoint (0, 0, 0); 199 $DB_ENV->txn_checkpoint (0, 0, 0);
178 () 200 ()
179} 201}
180 202
203sub 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
181sub do_get { 213sub do_get {
182 my ($db, $key) = @_; 214 my ($db, $key) = @_;
183 215
216 utf8::downgrade $key;
184 my $data; 217 my $data;
185 (table $db)->db_get ($key, $data) == 0 218 (table $db)->db_get ($key, $data) == 0
186 ? $data 219 ? $data
187 : () 220 : ()
188} 221}
189 222
190sub do_put { 223sub do_put {
191 my ($db, $key, $data) = @_; 224 my ($db, $key, $data) = @_;
192 225
226 utf8::downgrade $key;
227 utf8::downgrade $data;
193 (table $db)->db_put ($key => $data) 228 (table $db)->db_put ($key => $data)
194} 229}
195 230
196sub do_table { 231sub do_table {
197 my ($db) = @_; 232 my ($db) = @_;
231 } 266 }
232 267
233 die "maximum number of transaction retries reached - database problems?"; 268 die "maximum number of transaction retries reached - database problems?";
234} 269}
235 270
271sub do_unlink {
272 unlink $_[0];
273}
274
275sub 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
288sub 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
236sub run { 299sub run {
237 ($FH, my $fh) = CFPlus::socketpipe; 300 ($FH, my $fh) = CFPlus::socketpipe;
238 301
239 my $oldfh = select $FH; $| = 1; select $oldfh; 302 my $oldfh = select $FH; $| = 1; select $oldfh;
240 my $oldfh = select $fh; $| = 1; select $oldfh; 303 my $oldfh = select $fh; $| = 1; select $oldfh;
245 local $SIG{__DIE__}; 308 local $SIG{__DIE__};
246 eval { 309 eval {
247 close $FH; 310 close $FH;
248 311
249 unless (eval { open_db }) { 312 unless (eval { open_db }) {
250 File::Path::rmtree $DB_HOME; 313 eval { File::Path::rmtree $DB_HOME };
251 open_db; 314 open_db;
252 } 315 }
253 316
254 while () { 317 while () {
255 4 == read $fh, my $len, 4 318 4 == read $fh, my $len, 4
289 $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);
290 353
291 sync_tick; 354 sync_tick;
292} 355}
293 356
357sub stop {
358 close $FH;
359}
360
2941; 3611;
295 362
296=back 363=back
297 364
298=head1 AUTHOR 365=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines