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.17 by root, Fri Aug 10 04:02:13 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 Storable ();
22use Config;
21use AnyEvent (); 23use Event ();
22use Storable (); # finally 24
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}
23 33
24sub sync { 34sub sync {
25 # for debugging 35 # for debugging
26 #CFPlus::DB::Server::req (sync => sub { }); 36 #CFPlus::DB::Server::req (sync => sub { });
27 CFPlus::DB::Server::sync (); 37 CFPlus::DB::Server::sync ();
28} 38}
29 39
40sub exists($$$) {
41 CFPlus::DB::Server::req (exists => @_);
42}
43
30sub get($$$) { 44sub get($$$) {
31 CFPlus::DB::Server::req (get => @_); 45 CFPlus::DB::Server::req (get => @_);
32} 46}
33 47
34sub put($$$$) { 48sub put($$$$) {
35 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 => @_);
36} 70}
37 71
38our $tilemap; 72our $tilemap;
39 73
40sub get_tile_id_sync($) { 74sub get_tile_id_sync($) {
59use strict; 93use strict;
60 94
61use Fcntl; 95use Fcntl;
62use BerkeleyDB; 96use BerkeleyDB;
63 97
64our $DB_HOME = "$Crossfire::VARDIR/cfplus";
65our $DB_ENV; 98our $DB_ENV;
66our $DB_STATE; 99our $DB_STATE;
67our %DB_TABLE; 100our %DB_TABLE;
68 101
69sub open_db { 102sub open_db {
100 -Flags => DB_CREATE | DB_UPGRADE, 133 -Flags => DB_CREATE | DB_UPGRADE,
101 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 134 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
102 } 135 }
103} 136}
104 137
105our $SYNC_INTERVAL = 6;
106
107our %CB; 138our %CB;
108our $FH; 139our $FH;
109our $ID = "aaa0"; 140our $ID = "aaa0";
110our ($fh_r_watcher, $fh_w_watcher); 141our ($fh_r_watcher, $fh_w_watcher);
111our $sync_timer; 142our $sync_timer;
112our $write_buf; 143our $write_buf;
113our $read_buf; 144our $read_buf;
114 145
146our $SYNC = Event->idle (min => 10, max => 60, parked => 1, cb => sub {
147 CFPlus::DB::Server::req (sync => sub { });
148 $_[0]->w->stop;
149});
150
115sub fh_write { 151sub fh_write {
116 my $len = syswrite $FH, $write_buf; 152 my $len = syswrite $FH, $write_buf;
117 153
118 substr $write_buf, 0, $len, ""; 154 substr $write_buf, 0, $len, "";
119 155
120 undef $fh_w_watcher 156 $fh_w_watcher->stop
121 unless length $write_buf; 157 unless length $write_buf;
122} 158}
123 159
124sub fh_read { 160sub fh_read {
125 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 161 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
161 197
162 my $id = ++$ID; 198 my $id = ++$ID;
163 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 199 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
164 $CB{$id} = $cb; 200 $CB{$id} = $cb;
165 201
166 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 202 $fh_w_watcher->start;
167} 203 $SYNC->start;
168
169sub sync_tick {
170 req "sync", sub { };
171 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
172} 204}
173 205
174sub do_sync { 206sub do_sync {
175 $DB_ENV->txn_checkpoint (0, 0, 0); 207 $DB_ENV->txn_checkpoint (0, 0, 0);
176 () 208 ()
177} 209}
178 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 : ()
219}
220
179sub do_get { 221sub do_get {
180 my ($db, $key) = @_; 222 my ($db, $key) = @_;
181 223
224 utf8::downgrade $key;
182 my $data; 225 my $data;
183 (table $db)->db_get ($key, $data) == 0 226 (table $db)->db_get ($key, $data) == 0
184 ? $data 227 ? $data
185 : () 228 : ()
186} 229}
187 230
188sub do_put { 231sub do_put {
189 my ($db, $key, $data) = @_; 232 my ($db, $key, $data) = @_;
190 233
234 utf8::downgrade $key;
235 utf8::downgrade $data;
191 (table $db)->db_put ($key => $data) 236 (table $db)->db_put ($key => $data)
192} 237}
193 238
194sub do_table { 239sub do_table {
195 my ($db) = @_; 240 my ($db) = @_;
229 } 274 }
230 275
231 die "maximum number of transaction retries reached - database problems?"; 276 die "maximum number of transaction retries reached - database problems?";
232} 277}
233 278
279sub do_unlink {
280 unlink $_[0];
281}
282
283sub do_read_file {
284 my ($path) = @_;
285
286 utf8::downgrade $path;
287 open my $fh, "<:raw", $path
288 or return;
289 sysread $fh, my $buf, -s $fh;
290
291 $buf
292}
293
294sub do_write_file {
295 my ($path, $data) = @_;
296
297 utf8::downgrade $path;
298 utf8::downgrade $data;
299 open my $fh, ">:raw", $path
300 or return;
301 syswrite $fh, $data;
302 close $fh;
303
304 1
305}
306
307sub do_prefetch_file {
308 my ($path, $size) = @_;
309
310 utf8::downgrade $path;
311 open my $fh, "<:raw", $path
312 or return;
313 sysread $fh, my $buf, $size;
314
315 1
316}
317
318our %LOG_FH;
319
320sub do_logprint {
321 my ($path, $line) = @_;
322
323 $LOG_FH{$path} ||= do {
324 open my $fh, ">>:utf8", $path
325 or warn "Couldn't open logfile $path: $!";
326
327 $fh->autoflush (1);
328
329 $fh
330 };
331
332 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
333
334 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
335 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
336
337 print { $LOG_FH{$path} } "$ts $line\n"
338}
339
234sub run { 340sub run {
235 ($FH, my $fh) = CFPlus::socketpipe; 341 ($FH, my $fh) = CFPlus::socketpipe;
236 342
237 my $oldfh = select $FH; $| = 1; select $oldfh; 343 my $oldfh = select $FH; $| = 1; select $oldfh;
238 my $oldfh = select $fh; $| = 1; select $oldfh; 344 my $oldfh = select $fh; $| = 1; select $oldfh;
239 345
240 my $pid = fork; 346 my $pid = fork;
241 347
242 if (defined $pid && !$pid) { 348 if (defined $pid && !$pid) {
349 local $SIG{QUIT};
243 local $SIG{__DIE__}; 350 local $SIG{__DIE__};
351 local $SIG{__WARN__};
244 eval { 352 eval {
245 close $FH; 353 close $FH;
246 354
247 unless (eval { open_db }) { 355 unless (eval { open_db }) {
248 File::Path::rmtree $DB_HOME; 356 eval { File::Path::rmtree $DB_HOME };
249 open_db; 357 open_db;
250 } 358 }
251 359
252 while () { 360 while () {
253 4 == read $fh, my $len, 4 361 4 == read $fh, my $len, 4
278 386
279 CFPlus::_exit 0; 387 CFPlus::_exit 0;
280 } 388 }
281 389
282 close $fh; 390 close $fh;
283 fcntl $FH, F_SETFL, O_NONBLOCK; 391 CFPlus::fh_nonblocking $FH, 1;
284 392
285 $CB{die} = sub { die shift }; 393 $CB{die} = sub { die shift };
286 394
287 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); 395 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
396 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
397 $SYNC->start;
398}
288 399
289 sync_tick; 400sub stop {
401 close $FH;
290} 402}
291 403
2921; 4041;
293 405
294=back 406=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines