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.19 by root, Mon Aug 20 16:16:06 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 read_file($$) {
57 CFPlus::DB::Server::req (read_file => @_);
58}
59
60sub write_file($$$) {
61 CFPlus::DB::Server::req (write_file => @_);
62}
63
64sub prefetch_file($$$) {
65 CFPlus::DB::Server::req (prefetch_file => @_);
66}
67
68sub logprint($$$) {
69 CFPlus::DB::Server::req (logprint => @_);
38} 70}
39 71
40our $tilemap; 72our $tilemap;
41 73
42sub get_tile_id_sync($) { 74sub get_tile_id_sync($) {
61use strict; 93use strict;
62 94
63use Fcntl; 95use Fcntl;
64use BerkeleyDB; 96use BerkeleyDB;
65 97
66our $DB_HOME = "$Crossfire::VARDIR/cfplus";
67our $DB_ENV; 98our $DB_ENV;
68our $DB_STATE; 99our $DB_STATE;
69our %DB_TABLE; 100our %DB_TABLE;
70 101
71sub open_db { 102sub open_db {
79 -Cachesize => 8_000_000, 110 -Cachesize => 8_000_000,
80 -ErrFile => "$DB_HOME/errorlog.txt", 111 -ErrFile => "$DB_HOME/errorlog.txt",
81# -ErrPrefix => "DATABASE", 112# -ErrPrefix => "DATABASE",
82 -Verbose => 1, 113 -Verbose => 1,
83 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 114 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
84 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 115 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
85 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; 116 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
86 117
87 1 118 1
88} 119}
89 120
97 -Env => $DB_ENV, 128 -Env => $DB_ENV,
98 -Filename => $table, 129 -Filename => $table,
99# -Filename => "database", 130# -Filename => "database",
100# -Subname => $table, 131# -Subname => $table,
101 -Property => DB_CHKSUM, 132 -Property => DB_CHKSUM,
102 -Flags => DB_CREATE | DB_UPGRADE, 133 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
103 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 134 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
104 } 135 }
105} 136}
106
107our $SYNC_INTERVAL = 60;
108 137
109our %CB; 138our %CB;
110our $FH; 139our $FH;
111our $ID = "aaa0"; 140our $ID = "aaa0";
112our ($fh_r_watcher, $fh_w_watcher); 141our ($fh_r_watcher, $fh_w_watcher);
113our $sync_timer; 142our $sync_timer;
114our $write_buf; 143our $write_buf;
115our $read_buf; 144our $read_buf;
116 145
146our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub {
147 CFPlus::DB::Server::req (sync => sub { });
148 $_[0]->w->stop;
149});
150
117sub fh_write { 151sub fh_write {
118 my $len = syswrite $FH, $write_buf; 152 my $len = syswrite $FH, $write_buf;
119 153
120 substr $write_buf, 0, $len, ""; 154 substr $write_buf, 0, $len, "";
121 155
122 undef $fh_w_watcher 156 $fh_w_watcher->stop
123 unless length $write_buf; 157 unless length $write_buf;
124} 158}
125 159
126sub fh_read { 160sub fh_read {
127 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 161 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
163 197
164 my $id = ++$ID; 198 my $id = ++$ID;
165 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 199 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
166 $CB{$id} = $cb; 200 $CB{$id} = $cb;
167 201
168 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 202 $fh_w_watcher->start;
169} 203 $SYNC->start;
170
171sub sync_tick {
172 req "sync", sub { };
173 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
174} 204}
175 205
176sub do_sync { 206sub do_sync {
177 $DB_ENV->txn_checkpoint (0, 0, 0); 207 $DB_ENV->txn_checkpoint (0, 0, 0);
178 () 208 ()
209}
210
211sub do_exists {
212 my ($db, $key) = @_;
213
214 utf8::downgrade $key;
215 my $data;
216 (table $db)->db_get ($key, $data) == 0
217 ? length $data
218 : ()
179} 219}
180 220
181sub do_get { 221sub do_get {
182 my ($db, $key) = @_; 222 my ($db, $key) = @_;
183 223
229 269
230 return $id; 270 return $id;
231 } 271 }
232 } 272 }
233 $txn->txn_abort; 273 $txn->txn_abort;
274 select undef, undef, undef, 0.01 * rand;
234 } 275 }
235 276
236 die "maximum number of transaction retries reached - database problems?"; 277 die "maximum number of transaction retries reached - database problems?";
278}
279
280sub do_unlink {
281 unlink $_[0];
282}
283
284sub do_read_file {
285 my ($path) = @_;
286
287 utf8::downgrade $path;
288 open my $fh, "<:raw", $path
289 or return;
290 sysread $fh, my $buf, -s $fh;
291
292 $buf
293}
294
295sub do_write_file {
296 my ($path, $data) = @_;
297
298 utf8::downgrade $path;
299 utf8::downgrade $data;
300 open my $fh, ">:raw", $path
301 or return;
302 syswrite $fh, $data;
303 close $fh;
304
305 1
306}
307
308sub do_prefetch_file {
309 my ($path, $size) = @_;
310
311 utf8::downgrade $path;
312 open my $fh, "<:raw", $path
313 or return;
314 sysread $fh, my $buf, $size;
315
316 1
317}
318
319our %LOG_FH;
320
321sub do_logprint {
322 my ($path, $line) = @_;
323
324 $LOG_FH{$path} ||= do {
325 open my $fh, ">>:utf8", $path
326 or warn "Couldn't open logfile $path: $!";
327
328 $fh->autoflush (1);
329
330 $fh
331 };
332
333 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
334
335 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
336 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
337
338 print { $LOG_FH{$path} } "$ts $line\n"
237} 339}
238 340
239sub run { 341sub run {
240 ($FH, my $fh) = CFPlus::socketpipe; 342 ($FH, my $fh) = CFPlus::socketpipe;
241 343
243 my $oldfh = select $fh; $| = 1; select $oldfh; 345 my $oldfh = select $fh; $| = 1; select $oldfh;
244 346
245 my $pid = fork; 347 my $pid = fork;
246 348
247 if (defined $pid && !$pid) { 349 if (defined $pid && !$pid) {
350 local $SIG{QUIT};
248 local $SIG{__DIE__}; 351 local $SIG{__DIE__};
352 local $SIG{__WARN__};
249 eval { 353 eval {
250 close $FH; 354 close $FH;
251 355
252 unless (eval { open_db }) { 356 unless (eval { open_db }) {
253 eval { File::Path::rmtree $DB_HOME }; 357 eval { File::Path::rmtree $DB_HOME };
287 close $fh; 391 close $fh;
288 CFPlus::fh_nonblocking $FH, 1; 392 CFPlus::fh_nonblocking $FH, 1;
289 393
290 $CB{die} = sub { die shift }; 394 $CB{die} = sub { die shift };
291 395
292 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 396 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
293 397 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
294 sync_tick; 398 $SYNC->start;
295} 399}
296 400
297sub stop { 401sub stop {
298 close $FH; 402 close $FH;
299} 403}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines