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.8 by root, Thu Jul 5 10:15:17 2007 UTC

17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent (); 21use AnyEvent ();
22use Storable (); # finally 22use Storable ();
23
24use CFPlus;
23 25
24sub sync { 26sub sync {
25 # for debugging 27 # for debugging
26 #CFPlus::DB::Server::req (sync => sub { }); 28 #CFPlus::DB::Server::req (sync => sub { });
27 CFPlus::DB::Server::sync (); 29 CFPlus::DB::Server::sync ();
58 60
59use strict; 61use strict;
60 62
61use Fcntl; 63use Fcntl;
62use BerkeleyDB; 64use BerkeleyDB;
65use Config;
63 66
64our $DB_HOME = "$Crossfire::VARDIR/cfplus"; 67our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
65our $DB_ENV; 68our $DB_ENV;
66our $DB_STATE; 69our $DB_STATE;
67our %DB_TABLE; 70our %DB_TABLE;
68 71
69sub open_db { 72sub open_db {
100 -Flags => DB_CREATE | DB_UPGRADE, 103 -Flags => DB_CREATE | DB_UPGRADE,
101 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 104 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
102 } 105 }
103} 106}
104 107
105our $SYNC_INTERVAL = 6; 108our $SYNC_INTERVAL = 60;
106 109
107our %CB; 110our %CB;
108our $FH; 111our $FH;
109our $ID = "aaa0"; 112our $ID = "aaa0";
110our ($fh_r_watcher, $fh_w_watcher); 113our ($fh_r_watcher, $fh_w_watcher);
177} 180}
178 181
179sub do_get { 182sub do_get {
180 my ($db, $key) = @_; 183 my ($db, $key) = @_;
181 184
185 utf8::downgrade $key;
182 my $data; 186 my $data;
183 (table $db)->db_get ($key, $data) == 0 187 (table $db)->db_get ($key, $data) == 0
184 ? $data 188 ? $data
185 : () 189 : ()
186} 190}
187 191
188sub do_put { 192sub do_put {
189 my ($db, $key, $data) = @_; 193 my ($db, $key, $data) = @_;
190 194
195 utf8::downgrade $key;
196 utf8::downgrade $data;
191 (table $db)->db_put ($key => $data) 197 (table $db)->db_put ($key => $data)
192} 198}
193 199
194sub do_table { 200sub do_table {
195 my ($db) = @_; 201 my ($db) = @_;
243 local $SIG{__DIE__}; 249 local $SIG{__DIE__};
244 eval { 250 eval {
245 close $FH; 251 close $FH;
246 252
247 unless (eval { open_db }) { 253 unless (eval { open_db }) {
248 File::Path::rmtree $DB_HOME; 254 eval { File::Path::rmtree $DB_HOME };
249 open_db; 255 open_db;
250 } 256 }
251 257
252 while () { 258 while () {
253 4 == read $fh, my $len, 4 259 4 == read $fh, my $len, 4
278 284
279 CFPlus::_exit 0; 285 CFPlus::_exit 0;
280 } 286 }
281 287
282 close $fh; 288 close $fh;
283 fcntl $FH, F_SETFL, O_NONBLOCK; 289 CFPlus::fh_nonblocking $FH, 1;
284 290
285 $CB{die} = sub { die shift }; 291 $CB{die} = sub { die shift };
286 292
287 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 293 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read);
288 294
289 sync_tick; 295 sync_tick;
296}
297
298sub stop {
299 close $FH;
290} 300}
291 301
2921; 3021;
293 303
294=back 304=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines