ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.22
Committed: Tue Nov 13 02:47:30 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
Changes since 1.21: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 CFPlus::DB - async. database and filesystem access for cfplus
4
5 =head1 SYNOPSIS
6
7 use CFPlus::DB;
8
9 =head1 DESCRIPTION
10
11 =over 4
12
13 =cut
14
15 package CFPlus::DB;
16
17 use strict;
18 use utf8;
19
20 use Carp ();
21 use Storable ();
22 use Config;
23
24 use CFPlus;
25
26 our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
27
28 sub path_of_res($) {
29 utf8::downgrade $_[0]; # bug in unpack "H*"
30 "$DB_HOME/res-data-" . unpack "H*", $_[0]
31 }
32
33 sub sync {
34 # for debugging
35 #CFPlus::DB::Server::req (sync => sub { });
36 CFPlus::DB::Server::sync ();
37 }
38
39 sub exists($$$) {
40 CFPlus::DB::Server::req (exists => @_);
41 }
42
43 sub get($$$) {
44 CFPlus::DB::Server::req (get => @_);
45 }
46
47 sub put($$$$) {
48 CFPlus::DB::Server::req (put => @_);
49 }
50
51 sub unlink($$) {
52 CFPlus::DB::Server::req (unlink => @_);
53 }
54
55 sub read_file($$) {
56 CFPlus::DB::Server::req (read_file => @_);
57 }
58
59 sub write_file($$$) {
60 CFPlus::DB::Server::req (write_file => @_);
61 }
62
63 sub prefetch_file($$$) {
64 CFPlus::DB::Server::req (prefetch_file => @_);
65 }
66
67 sub logprint($$$) {
68 CFPlus::DB::Server::req (logprint => @_);
69 }
70
71 our $tilemap;
72
73 sub get_tile_id_sync($) {
74 my ($name) = @_;
75
76 # fetch the full face table first
77 unless ($tilemap) {
78 CFPlus::DB::Server::req (table => facemap => sub {
79 $tilemap = $_[0];
80 delete $tilemap->{id};
81 my %maptile = reverse %$tilemap;#d#
82 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
83 $tilemap = { };#d#
84 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
85 }#d#
86 });
87 sync;
88 }
89
90 $tilemap->{$name} ||= do {
91 my $id;
92 CFPlus::DB::Server::req (get_tile_id => $name, sub { $id = $_[0] });
93 sync;
94 $id
95 }
96 }
97
98 package CFPlus::DB::Server;
99
100 use strict;
101
102 use Fcntl;
103 use BerkeleyDB;
104
105 our $DB_ENV;
106 our $DB_STATE;
107 our %DB_TABLE;
108
109 sub open_db {
110 mkdir $DB_HOME, 0777;
111 my $recover = $BerkeleyDB::db_version >= 4.4
112 ? eval "DB_REGISTER | DB_RECOVER"
113 : 0;
114
115 $DB_ENV = new BerkeleyDB::Env
116 -Home => $DB_HOME,
117 -Cachesize => 8_000_000,
118 -ErrFile => "$DB_HOME/errorlog.txt",
119 # -ErrPrefix => "DATABASE",
120 -Verbose => 1,
121 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
122 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE | DB_TXN_WRITE_NOSYNC,
123 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
124
125 1
126 }
127
128 sub table($) {
129 $DB_TABLE{$_[0]} ||= do {
130 my ($table) = @_;
131
132 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
133
134 new BerkeleyDB::Btree
135 -Env => $DB_ENV,
136 -Filename => $table,
137 # -Filename => "database",
138 # -Subname => $table,
139 -Property => DB_CHKSUM,
140 -Flags => DB_AUTO_COMMIT | DB_CREATE | DB_UPGRADE,
141 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
142 }
143 }
144
145 our %CB;
146 our $FH;
147 our $ID = "aaa0";
148 our ($fh_r_watcher, $fh_w_watcher);
149 our $sync_timer;
150 our $write_buf;
151 our $read_buf;
152
153 our $SYNC = EV::timer_ns 0, 60, sub {
154 $_[0]->stop;
155 CFPlus::DB::Server::req (sync => sub { });
156 };
157
158 sub fh_write {
159 my $len = syswrite $FH, $write_buf;
160
161 substr $write_buf, 0, $len, "";
162
163 $fh_w_watcher->stop
164 unless length $write_buf;
165 }
166
167 sub fh_read {
168 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
169
170 die "FATAL: database process died\n"
171 if $status == 0 && defined $status;
172
173 while () {
174 return if 4 > length $read_buf;
175 my $len = unpack "N", $read_buf;
176
177 return if $len + 4 > length $read_buf;
178
179 substr $read_buf, 0, 4, "";
180 my $res = Storable::thaw substr $read_buf, 0, $len, "";
181
182 my ($id, @args) = @$res;
183 (delete $CB{$id})->(@args);
184 }
185 }
186
187 sub sync {
188 # biggest mess evarr
189 my $fds; (vec $fds, fileno $FH, 1) = 1;
190
191 while (1 < scalar keys %CB) {
192 my $r = $fds;
193 my $w = length $write_buf ? $fds : undef;
194 select $r, $w, undef, undef;
195
196 fh_write if vec $w, fileno $FH, 1;
197 fh_read if vec $r, fileno $FH, 1;
198 }
199 }
200
201 sub req {
202 my ($type, @args) = @_;
203 my $cb = pop @args;
204
205 my $id = ++$ID;
206 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
207 $CB{$id} = $cb;
208
209 $fh_w_watcher->start;
210 $SYNC->again unless $SYNC->is_active;
211 }
212
213 sub do_sync {
214 $DB_ENV->txn_checkpoint (0, 0, 0);
215 ()
216 }
217
218 sub do_exists {
219 my ($db, $key) = @_;
220
221 utf8::downgrade $key;
222 my $data;
223 (table $db)->db_get ($key, $data) == 0
224 ? length $data
225 : ()
226 }
227
228 sub do_get {
229 my ($db, $key) = @_;
230
231 utf8::downgrade $key;
232 my $data;
233 (table $db)->db_get ($key, $data) == 0
234 ? $data
235 : ()
236 }
237
238 sub do_put {
239 my ($db, $key, $data) = @_;
240
241 utf8::downgrade $key;
242 utf8::downgrade $data;
243 (table $db)->db_put ($key => $data)
244 }
245
246 sub do_table {
247 my ($db) = @_;
248
249 $db = table $db;
250
251 my $cursor = $db->db_cursor;
252 my %kv;
253 my ($k, $v);
254 $kv{$k} = $v while $cursor->c_get ($k, $v, BerkeleyDB::DB_NEXT) == 0;
255
256 \%kv
257 }
258
259 sub do_get_tile_id {
260 my ($name) = @_;
261
262 my $id;
263 my $table = table "facemap";
264
265 return $id
266 if $table->db_get ($name, $id) == 0;
267
268 for (1..100) {
269 my $txn = $DB_ENV->txn_begin;
270 my $status = $table->db_get (id => $id);
271 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
272 $id = ($id || 64) + 1;
273 if ($table->db_put (id => $id) == 0
274 && $table->db_put ($name => $id) == 0) {
275 $txn->txn_commit;
276
277 return $id;
278 }
279 }
280 $txn->txn_abort;
281 select undef, undef, undef, 0.01 * rand;
282 }
283
284 die "maximum number of transaction retries reached - database problems?";
285 }
286
287 sub do_unlink {
288 unlink $_[0];
289 }
290
291 sub do_read_file {
292 my ($path) = @_;
293
294 utf8::downgrade $path;
295 open my $fh, "<:raw", $path
296 or return;
297 sysread $fh, my $buf, -s $fh;
298
299 $buf
300 }
301
302 sub do_write_file {
303 my ($path, $data) = @_;
304
305 utf8::downgrade $path;
306 utf8::downgrade $data;
307 open my $fh, ">:raw", $path
308 or return;
309 syswrite $fh, $data;
310 close $fh;
311
312 1
313 }
314
315 sub do_prefetch_file {
316 my ($path, $size) = @_;
317
318 utf8::downgrade $path;
319 open my $fh, "<:raw", $path
320 or return;
321 sysread $fh, my $buf, $size;
322
323 1
324 }
325
326 our %LOG_FH;
327
328 sub do_logprint {
329 my ($path, $line) = @_;
330
331 $LOG_FH{$path} ||= do {
332 open my $fh, ">>:utf8", $path
333 or warn "Couldn't open logfile $path: $!";
334
335 $fh->autoflush (1);
336
337 $fh
338 };
339
340 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
341
342 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
343 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
344
345 print { $LOG_FH{$path} } "$ts $line\n"
346 }
347
348 sub run {
349 ($FH, my $fh) = CFPlus::socketpipe;
350
351 my $oldfh = select $FH; $| = 1; select $oldfh;
352 my $oldfh = select $fh; $| = 1; select $oldfh;
353
354 my $pid = fork;
355
356 if (defined $pid && !$pid) {
357 local $SIG{QUIT};
358 local $SIG{__DIE__};
359 local $SIG{__WARN__};
360 eval {
361 close $FH;
362
363 unless (eval { open_db }) {
364 eval { File::Path::rmtree $DB_HOME };
365 open_db;
366 }
367
368 while () {
369 4 == read $fh, my $len, 4
370 or last;
371 $len = unpack "N", $len;
372 $len == read $fh, my $req, $len
373 or die "unexpected eof while reading request";
374
375 $req = Storable::thaw $req;
376
377 my ($id, $type, @args) = @$req;
378 my $cb = CFPlus::DB::Server->can ("do_$type")
379 or die "$type: unknown database request type\n";
380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
381 (syswrite $fh, $res) == length $res
382 or die "DB::write: $!";
383 }
384 };
385
386 my $error = $@;
387
388 eval {
389 $DB_ENV->txn_checkpoint (0, 0, 0);
390
391 undef %DB_TABLE;
392 undef $DB_ENV;
393
394 Storable::store_fd [die => $error], $fh;
395 };
396
397 CFPlus::_exit 0;
398 }
399
400 close $fh;
401 CFPlus::fh_nonblocking $FH, 1;
402
403 $CB{die} = sub { die shift };
404
405 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
406 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
407 $SYNC->again unless $SYNC->is_active;
408 }
409
410 sub stop {
411 close $FH;
412 }
413
414 1;
415
416 =back
417
418 =head1 AUTHOR
419
420 Marc Lehmann <schmorp@schmorp.de>
421 http://home.schmorp.de/
422
423 =cut
424