ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.10
Committed: Fri Jul 13 05:31:31 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.9: +1 -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 AnyEvent ();
22 use Storable ();
23 use Config;
24
25 use CFPlus;
26
27 our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}";
28
29 sub path_of($) {
30 "$DB_HOME/res-data-$_[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 write_file($$$) {
56 CFPlus::DB::Server::req (write_file => @_);
57 }
58
59 our $tilemap;
60
61 sub get_tile_id_sync($) {
62 my ($hash) = @_;
63
64 # fetch the full face table first
65 unless ($tilemap) {
66 CFPlus::DB::Server::req (table => facemap => sub { $tilemap = $_[0] });
67 sync;
68 }
69
70 $tilemap->{$hash} ||= do {
71 my $id;
72 CFPlus::DB::Server::req (get_tile_id => $hash, sub { $id = $_[0] });
73 sync;
74 $id
75 }
76 }
77
78 package CFPlus::DB::Server;
79
80 use strict;
81
82 use Fcntl;
83 use BerkeleyDB;
84
85 our $DB_ENV;
86 our $DB_STATE;
87 our %DB_TABLE;
88
89 sub open_db {
90 mkdir $DB_HOME, 0777;
91 my $recover = $BerkeleyDB::db_version >= 4.4
92 ? eval "DB_REGISTER | DB_RECOVER"
93 : 0;
94
95 $DB_ENV = new BerkeleyDB::Env
96 -Home => $DB_HOME,
97 -Cachesize => 8_000_000,
98 -ErrFile => "$DB_HOME/errorlog.txt",
99 # -ErrPrefix => "DATABASE",
100 -Verbose => 1,
101 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
102 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
103 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
104
105 1
106 }
107
108 sub table($) {
109 $DB_TABLE{$_[0]} ||= do {
110 my ($table) = @_;
111
112 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
113
114 new BerkeleyDB::Btree
115 -Env => $DB_ENV,
116 -Filename => $table,
117 # -Filename => "database",
118 # -Subname => $table,
119 -Property => DB_CHKSUM,
120 -Flags => DB_CREATE | DB_UPGRADE,
121 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
122 }
123 }
124
125 our $SYNC_INTERVAL = 60;
126
127 our %CB;
128 our $FH;
129 our $ID = "aaa0";
130 our ($fh_r_watcher, $fh_w_watcher);
131 our $sync_timer;
132 our $write_buf;
133 our $read_buf;
134
135 sub fh_write {
136 my $len = syswrite $FH, $write_buf;
137
138 substr $write_buf, 0, $len, "";
139
140 undef $fh_w_watcher
141 unless length $write_buf;
142 }
143
144 sub fh_read {
145 my $status = sysread $FH, $read_buf, 16384, length $read_buf;
146
147 die "FATAL: database process died\n"
148 if $status == 0 && defined $status;
149
150 while () {
151 return if 4 > length $read_buf;
152 my $len = unpack "N", $read_buf;
153
154 return if $len + 4 > length $read_buf;
155
156 substr $read_buf, 0, 4, "";
157 my $res = Storable::thaw substr $read_buf, 0, $len, "";
158
159 my ($id, @args) = @$res;
160 (delete $CB{$id})->(@args);
161 }
162 }
163
164 sub sync {
165 # biggest mess evarr
166 my $fds; (vec $fds, fileno $FH, 1) = 1;
167
168 while (1 < scalar keys %CB) {
169 my $r = $fds;
170 my $w = length $write_buf ? $fds : undef;
171 select $r, $w, undef, undef;
172
173 fh_write if vec $w, fileno $FH, 1;
174 fh_read if vec $r, fileno $FH, 1;
175 }
176 }
177
178 sub req {
179 my ($type, @args) = @_;
180 my $cb = pop @args;
181
182 my $id = ++$ID;
183 $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args];
184 $CB{$id} = $cb;
185
186 $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write);
187 }
188
189 sub sync_tick {
190 req "sync", sub { };
191 $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick);
192 }
193
194 sub do_sync {
195 $DB_ENV->txn_checkpoint (0, 0, 0);
196 ()
197 }
198
199 sub do_exists {
200 my ($db, $key) = @_;
201
202 utf8::downgrade $key;
203 my $data;
204 (table $db)->db_get ($key, $data) == 0
205 ? length $data
206 : ()
207 }
208
209 sub do_get {
210 my ($db, $key) = @_;
211
212 utf8::downgrade $key;
213 my $data;
214 (table $db)->db_get ($key, $data) == 0
215 ? $data
216 : ()
217 }
218
219 sub do_put {
220 my ($db, $key, $data) = @_;
221
222 utf8::downgrade $key;
223 utf8::downgrade $data;
224 (table $db)->db_put ($key => $data)
225 }
226
227 sub do_table {
228 my ($db) = @_;
229
230 $db = table $db;
231
232 my $cursor = $db->db_cursor;
233 my %kv;
234 my ($k, $v);
235 $kv{$k} = $v while $cursor->c_get ($k, $v, BerkeleyDB::DB_NEXT) == 0;
236
237 \%kv
238 }
239
240 sub do_get_tile_id {
241 my ($hash) = @_;
242
243 my $id;
244 my $table = table "facemap";
245
246 return $id
247 if $table->db_get ($hash, $id) == 0;
248
249 for (1..100) {
250 my $txn = $DB_ENV->txn_begin;
251 my $status = $table->db_get (id => $id);
252 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
253 $id = ($id || 64) + 1;
254 if ($table->db_put (id => $id) == 0
255 && $table->db_put ($hash => $id) == 0) {
256 $txn->txn_commit;
257
258 return $id;
259 }
260 }
261 $txn->txn_abort;
262 }
263
264 die "maximum number of transaction retries reached - database problems?";
265 }
266
267 sub do_unlink {
268 unlink $_[0];
269 }
270
271 sub do_write_file {
272 my ($file, $data) = @_;
273
274 utf8::downgrade $file;
275 utf8::downgrade $data;
276 open my $fh, ">:raw", CFPlus::DB::path_of $file
277 or return;
278 print $fh $data;
279 close $fh;
280
281 1
282 }
283
284 sub run {
285 ($FH, my $fh) = CFPlus::socketpipe;
286
287 my $oldfh = select $FH; $| = 1; select $oldfh;
288 my $oldfh = select $fh; $| = 1; select $oldfh;
289
290 my $pid = fork;
291
292 if (defined $pid && !$pid) {
293 local $SIG{__DIE__};
294 eval {
295 close $FH;
296
297 unless (eval { open_db }) {
298 eval { File::Path::rmtree $DB_HOME };
299 open_db;
300 }
301
302 while () {
303 4 == read $fh, my $len, 4
304 or last;
305 $len = unpack "N", $len;
306 $len == read $fh, my $req, $len
307 or die "unexpected eof while reading request";
308
309 $req = Storable::thaw $req;
310
311 my ($id, $type, @args) = @$req;
312 my $cb = CFPlus::DB::Server->can ("do_$type")
313 or die "$type: unknown database request type\n";
314 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
315 (syswrite $fh, $res) == length $res
316 or die;
317 }
318 };
319
320 my $error = $@;
321
322 eval {
323 undef %DB_TABLE;
324 undef $DB_ENV;
325
326 Storable::store_fd [die => $error], $fh;
327 };
328
329 CFPlus::_exit 0;
330 }
331
332 close $fh;
333 CFPlus::fh_nonblocking $FH, 1;
334
335 $CB{die} = sub { die shift };
336
337 $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read);
338
339 sync_tick;
340 }
341
342 sub stop {
343 close $FH;
344 }
345
346 1;
347
348 =back
349
350 =head1 AUTHOR
351
352 Marc Lehmann <schmorp@schmorp.de>
353 http://home.schmorp.de/
354
355 =cut
356