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.13 by root, Sun Jul 29 04:14:45 2007 UTC vs.
Revision 1.14 by root, Sun Jul 29 19:32:29 2007 UTC

16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use Carp (); 20use Carp ();
21use AnyEvent ();
22use Storable (); 21use Storable ();
23use Config; 22use Config;
23use Event ();
24 24
25use CFPlus; 25use CFPlus;
26 26
27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; 27our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28 28
125 -Flags => DB_CREATE | DB_UPGRADE, 125 -Flags => DB_CREATE | DB_UPGRADE,
126 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 126 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
127 } 127 }
128} 128}
129 129
130our $SYNC_INTERVAL = 60;
131
132our %CB; 130our %CB;
133our $FH; 131our $FH;
134our $ID = "aaa0"; 132our $ID = "aaa0";
135our ($fh_r_watcher, $fh_w_watcher); 133our ($fh_r_watcher, $fh_w_watcher);
136our $sync_timer; 134our $sync_timer;
137our $write_buf; 135our $write_buf;
138our $read_buf; 136our $read_buf;
139 137
138our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub {
139 CFPlus::DB::Server::req (sync => sub { });
140 $_[0]->w->stop;
141});
142
140sub fh_write { 143sub fh_write {
141 my $len = syswrite $FH, $write_buf; 144 my $len = syswrite $FH, $write_buf;
142 145
143 substr $write_buf, 0, $len, ""; 146 substr $write_buf, 0, $len, "";
144 147
145 undef $fh_w_watcher 148 $fh_w_watcher->stop
146 unless length $write_buf; 149 unless length $write_buf;
147} 150}
148 151
149sub fh_read { 152sub fh_read {
150 my $status = sysread $FH, $read_buf, 16384, length $read_buf; 153 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
186 189
187 my $id = ++$ID; 190 my $id = ++$ID;
188 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; 191 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
189 $CB{$id} = $cb; 192 $CB{$id} = $cb;
190 193
191 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); 194 $fh_w_watcher->start;
192} 195 $SYNC->start;
193
194sub sync_tick {
195 req "sync", sub { };
196 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
197} 196}
198 197
199sub do_sync { 198sub do_sync {
200 $DB_ENV->txn_checkpoint (0, 0, 0); 199 $DB_ENV->txn_checkpoint (0, 0, 0);
201 () 200 ()
348 close $fh; 347 close $fh;
349 CFPlus::fh_nonblocking $FH, 1; 348 CFPlus::fh_nonblocking $FH, 1;
350 349
351 $CB{die} = sub { die shift }; 350 $CB{die} = sub { die shift };
352 351
353 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', nice => 1, cb => \&fh_read); 352 $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read);
354 353 $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write);
355 sync_tick; 354 $SYNC->start;
356} 355}
357 356
358sub stop { 357sub stop {
359 close $FH; 358 close $FH;
360} 359}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines