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.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;
23 24
24use CFPlus; 25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of($) {
30 "$DB_HOME/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 => @_);
38} 57}
39 58
40our $tilemap; 59our $tilemap;
41 60
42sub get_tile_id_sync($) { 61sub get_tile_id_sync($) {
61use strict; 80use strict;
62 81
63use Fcntl; 82use Fcntl;
64use BerkeleyDB; 83use BerkeleyDB;
65 84
66our $DB_HOME = "$Crossfire::VARDIR/cfplus";
67our $DB_ENV; 85our $DB_ENV;
68our $DB_STATE; 86our $DB_STATE;
69our %DB_TABLE; 87our %DB_TABLE;
70 88
71sub open_db { 89sub open_db {
102 -Flags => DB_CREATE | DB_UPGRADE, 120 -Flags => DB_CREATE | DB_UPGRADE,
103 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 121 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
104 } 122 }
105} 123}
106 124
107our $SYNC_INTERVAL = 6; 125our $SYNC_INTERVAL = 60;
108 126
109our %CB; 127our %CB;
110our $FH; 128our $FH;
111our $ID = "aaa0"; 129our $ID = "aaa0";
112our ($fh_r_watcher, $fh_w_watcher); 130our ($fh_r_watcher, $fh_w_watcher);
176sub do_sync { 194sub do_sync {
177 $DB_ENV->txn_checkpoint (0, 0, 0); 195 $DB_ENV->txn_checkpoint (0, 0, 0);
178 () 196 ()
179} 197}
180 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
181sub do_get { 209sub do_get {
182 my ($db, $key) = @_; 210 my ($db, $key) = @_;
183 211
212 utf8::downgrade $key;
184 my $data; 213 my $data;
185 (table $db)->db_get ($key, $data) == 0 214 (table $db)->db_get ($key, $data) == 0
186 ? $data 215 ? $data
187 : () 216 : ()
188} 217}
189 218
190sub do_put { 219sub do_put {
191 my ($db, $key, $data) = @_; 220 my ($db, $key, $data) = @_;
192 221
222 utf8::downgrade $key;
223 utf8::downgrade $data;
193 (table $db)->db_put ($key => $data) 224 (table $db)->db_put ($key => $data)
194} 225}
195 226
196sub do_table { 227sub do_table {
197 my ($db) = @_; 228 my ($db) = @_;
231 } 262 }
232 263
233 die "maximum number of transaction retries reached - database problems?"; 264 die "maximum number of transaction retries reached - database problems?";
234} 265}
235 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
236sub run { 284sub run {
237 ($FH, my $fh) = CFPlus::socketpipe; 285 ($FH, my $fh) = CFPlus::socketpipe;
238 286
239 my $oldfh = select $FH; $| = 1; select $oldfh; 287 my $oldfh = select $FH; $| = 1; select $oldfh;
240 my $oldfh = select $fh; $| = 1; select $oldfh; 288 my $oldfh = select $fh; $| = 1; select $oldfh;
245 local $SIG{__DIE__}; 293 local $SIG{__DIE__};
246 eval { 294 eval {
247 close $FH; 295 close $FH;
248 296
249 unless (eval { open_db }) { 297 unless (eval { open_db }) {
250 File::Path::rmtree $DB_HOME; 298 eval { File::Path::rmtree $DB_HOME };
251 open_db; 299 open_db;
252 } 300 }
253 301
254 while () { 302 while () {
255 4 == read $fh, my $len, 4 303 4 == read $fh, my $len, 4
289 $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);
290 338
291 sync_tick; 339 sync_tick;
292} 340}
293 341
342sub stop {
343 close $FH;
344}
345
2941; 3461;
295 347
296=back 348=back
297 349
298=head1 AUTHOR 350=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines