ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.2
Committed: Fri Apr 6 08:09:43 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.1: +1 -3 lines
Log Message:
- optimise mapcache tile loading quite a bit to avoid
  loading the same tile again and again.
- only request mapcache tiles serially, to avoid starving
  foreground requests.

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