--- deliantra/Deliantra-Client/DC/DB.pm 2007/07/29 19:32:29 1.14 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/08/20 16:16:06 1.19 @@ -53,6 +53,10 @@ CFPlus::DB::Server::req (unlink => @_); } +sub read_file($$) { + CFPlus::DB::Server::req (read_file => @_); +} + sub write_file($$$) { CFPlus::DB::Server::req (write_file => @_); } @@ -61,6 +65,10 @@ CFPlus::DB::Server::req (prefetch_file => @_); } +sub logprint($$$) { + CFPlus::DB::Server::req (logprint => @_); +} + our $tilemap; sub get_tile_id_sync($) { @@ -104,7 +112,7 @@ # -ErrPrefix => "DATABASE", -Verbose => 1, -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, - -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, + -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC, or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; 1 @@ -122,7 +130,7 @@ # -Filename => "database", # -Subname => $table, -Property => DB_CHKSUM, - -Flags => DB_CREATE | DB_UPGRADE, + -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE, or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" } } @@ -135,7 +143,7 @@ our $write_buf; our $read_buf; -our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { +our $SYNC = Event->idle (min => 120, max => 180, parked => 1, cb => sub { CFPlus::DB::Server::req (sync => sub { }); $_[0]->w->stop; }); @@ -263,6 +271,7 @@ } } $txn->txn_abort; + select undef, undef, undef, 0.01 * rand; } die "maximum number of transaction retries reached - database problems?"; @@ -272,30 +281,63 @@ unlink $_[0]; } +sub do_read_file { + my ($path) = @_; + + utf8::downgrade $path; + open my $fh, "<:raw", $path + or return; + sysread $fh, my $buf, -s $fh; + + $buf +} + sub do_write_file { - my ($file, $data) = @_; + my ($path, $data) = @_; - utf8::downgrade $file; + utf8::downgrade $path; utf8::downgrade $data; - open my $fh, ">:raw", $file + open my $fh, ">:raw", $path or return; - print $fh $data; + syswrite $fh, $data; close $fh; 1 } sub do_prefetch_file { - my ($file, $size) = @_; + my ($path, $size) = @_; - utf8::downgrade $file; - open my $fh, "<:raw", $file + utf8::downgrade $path; + open my $fh, "<:raw", $path or return; sysread $fh, my $buf, $size; 1 } +our %LOG_FH; + +sub do_logprint { + my ($path, $line) = @_; + + $LOG_FH{$path} ||= do { + open my $fh, ">>:utf8", $path + or warn "Couldn't open logfile $path: $!"; + + $fh->autoflush (1); + + $fh + }; + + my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; + + my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", + $year + 1900, $mon + 1, $mday, $hour, $min, $sec; + + print { $LOG_FH{$path} } "$ts $line\n" +} + sub run { ($FH, my $fh) = CFPlus::socketpipe; @@ -305,7 +347,9 @@ my $pid = fork; if (defined $pid && !$pid) { + local $SIG{QUIT}; local $SIG{__DIE__}; + local $SIG{__WARN__}; eval { close $FH;