ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.7
Committed: Thu Jun 28 05:34:59 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
include berkeleydb version in env dir name

File Contents

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