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.6 by root, Mon Apr 16 21:06:37 2007 UTC vs.
Revision 1.15 by root, Mon Aug 6 02:11:45 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
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent ();
22use Storable (); 21use Storable ();
22use Config;
23use Event ();
23 24
24use CFPlus; 25use CFPlus;
26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29sub path_of_res($) {
30 utf8::downgrade $_[0]; # bug in unpack "H*"
31 "$DB_HOME/res-data-" . unpack "H*", $_[0]
32}
25 33
26sub sync { 34sub sync {
27 # for debugging 35 # for debugging
28 #CFPlus::DB::Server::req (sync => sub { }); 36 #CFPlus::DB::Server::req (sync => sub { });
29 CFPlus::DB::Server::sync (); 37 CFPlus::DB::Server::sync ();
30} 38}
31 39
40sub exists($$$) {
41 CFPlus::DB::Server::req (exists => @_);
42}
43
32sub get($$$) { 44sub get($$$) {
33 CFPlus::DB::Server::req (get => @_); 45 CFPlus::DB::Server::req (get => @_);
34} 46}
35 47
36sub put($$$$) { 48sub put($$$$) {
37 CFPlus::DB::Server::req (put => @_); 49 CFPlus::DB::Server::req (put => @_);
50}
51
52sub unlink($$) {
53 CFPlus::DB::Server::req (unlink => @_);
54}
55
56sub write_file($$$) {
57 CFPlus::DB::Server::req (write_file => @_);
58}
59
60sub prefetch_file($$$) {
61 CFPlus::DB::Server::req (prefetch_file => @_);
62}
63
64sub logprint($$$) {
65 CFPlus::DB::Server::req (logprint => @_);
38} 66}
39 67
40our $tilemap; 68our $tilemap;
41 69
42sub get_tile_id_sync($) { 70sub get_tile_id_sync($) {
61use strict; 89use strict;
62 90
63use Fcntl; 91use Fcntl;
64use BerkeleyDB; 92use BerkeleyDB;
65 93
66our $DB_HOME = "$Crossfire::VARDIR/cfplus";
67our $DB_ENV; 94our $DB_ENV;
68our $DB_STATE; 95our $DB_STATE;
69our %DB_TABLE; 96our %DB_TABLE;
70 97
71sub open_db { 98sub open_db {
102 -Flags => DB_CREATE | DB_UPGRADE, 129 -Flags => DB_CREATE | DB_UPGRADE,
103 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 130 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
104 } 131 }
105} 132}
106 133
107our $SYNC_INTERVAL = 60;
108
109our %CB; 134our %CB;
110our $FH; 135our $FH;
111our $ID = "aaa0"; 136our $ID = "aaa0";
112our ($fh_r_watcher, $fh_w_watcher); 137our ($fh_r_watcher, $fh_w_watcher);
113our $sync_timer; 138our $sync_timer;
114our $write_buf; 139our $write_buf;
115our $read_buf; 140our $read_buf;
116 141
142our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub {
143 CFPlus::DB::Server::req (sync => sub { });
144 $_[0]->w->stop;
145});
146
117sub fh_write { 147sub fh_write {
118 my $len = syswrite $FH, $write_buf; 148 my $len = syswrite $FH, $write_buf;
119 149
120 substr $write_buf, 0, $len, ""; 150 substr $write_buf, 0, $len, "";
121 151
122 undef $fh_w_watcher 152 $fh_w_watcher->stop
123 unless length $write_buf; 153 unless length $write_buf;
124} 154}
125 155
126sub fh_read { 156sub fh_read {
127 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 157 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
163 193
164 my $id = ++$ID; 194 my $id = ++$ID;
165 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 195 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
166 $CB{$id} = $cb; 196 $CB{$id} = $cb;
167 197
168 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 198 $fh_w_watcher->start;
169} 199 $SYNC->start;
170
171sub sync_tick {
172 req "sync", sub { };
173 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
174} 200}
175 201
176sub do_sync { 202sub do_sync {
177 $DB_ENV->txn_checkpoint (0, 0, 0); 203 $DB_ENV->txn_checkpoint (0, 0, 0);
178 () 204 ()
205}
206
207sub do_exists {
208 my ($db, $key) = @_;
209
210 utf8::downgrade $key;
211 my $data;
212 (table $db)->db_get ($key, $data) == 0
213 ? length $data
214 : ()
179} 215}
180 216
181sub do_get { 217sub do_get {
182 my ($db, $key) = @_; 218 my ($db, $key) = @_;
183 219
234 } 270 }
235 271
236 die "maximum number of transaction retries reached - database problems?"; 272 die "maximum number of transaction retries reached - database problems?";
237} 273}
238 274
275sub do_unlink {
276 unlink $_[0];
277}
278
279sub do_write_file {
280 my ($file, $data) = @_;
281
282 utf8::downgrade $file;
283 utf8::downgrade $data;
284 open my $fh, ">:raw", $file
285 or return;
286 print $fh $data;
287 close $fh;
288
289 1
290}
291
292sub do_prefetch_file {
293 my ($file, $size) = @_;
294
295 utf8::downgrade $file;
296 open my $fh, "<:raw", $file
297 or return;
298 sysread $fh, my $buf, $size;
299
300 1
301}
302
303our %LOG_FH;
304
305sub do_logprint {
306 my ($path, $line) = @_;
307
308 $LOG_FH{$path} ||= do {
309 open my $fh, ">>:utf8", $path
310 or warn "Couldn't open logfile $path: $!";
311
312 $fh->autoflush (1);
313
314 $fh
315 };
316
317 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
318
319 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
320 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
321
322 print { $LOG_FH{$path} } "$ts $line\n"
323}
324
239sub run { 325sub run {
240 ($FH, my $fh) = CFPlus::socketpipe; 326 ($FH, my $fh) = CFPlus::socketpipe;
241 327
242 my $oldfh = select $FH; $| = 1; select $oldfh; 328 my $oldfh = select $FH; $| = 1; select $oldfh;
243 my $oldfh = select $fh; $| = 1; select $oldfh; 329 my $oldfh = select $fh; $| = 1; select $oldfh;
244 330
245 my $pid = fork; 331 my $pid = fork;
246 332
247 if (defined $pid && !$pid) { 333 if (defined $pid && !$pid) {
334 local $SIG{QUIT};
248 local $SIG{__DIE__}; 335 local $SIG{__DIE__};
336 local $SIG{__WARN__};
249 eval { 337 eval {
250 close $FH; 338 close $FH;
251 339
252 unless (eval { open_db }) { 340 unless (eval { open_db }) {
253 eval { File::Path::rmtree $DB_HOME }; 341 eval { File::Path::rmtree $DB_HOME };
287 close $fh; 375 close $fh;
288 CFPlus::fh_nonblocking $FH, 1; 376 CFPlus::fh_nonblocking $FH, 1;
289 377
290 $CB{die} = sub { die shift }; 378 $CB{die} = sub { die shift };
291 379
292 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 380 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
293 381 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
294 sync_tick; 382 $SYNC->start;
295} 383}
296 384
297sub stop { 385sub stop {
298 close $FH; 386 close $FH;
299} 387}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines