ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.8
Committed: Thu Jul 5 10:15:17 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.7: +2 -1 lines
Log Message:
include archname in database home dir

File Contents

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