1 |
root |
1.3 |
NAME |
2 |
|
|
BDB - Asynchronous Berkeley DB access |
3 |
|
|
|
4 |
|
|
SYNOPSIS |
5 |
|
|
use BDB; |
6 |
|
|
|
7 |
root |
1.4 |
my $env = db_env_create; |
8 |
|
|
|
9 |
|
|
mkdir "bdtest", 0700; |
10 |
|
|
db_env_open |
11 |
|
|
$env, |
12 |
|
|
"bdtest", |
13 |
|
|
BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL |
14 |
|
|
| BDB::INIT_TXN | BDB::RECOVER | BDB::USE_ENVIRON | BDB::CREATE, |
15 |
|
|
0600; |
16 |
|
|
|
17 |
|
|
$env->set_flags (BDB::AUTO_COMMIT | BDB::TXN_NOSYNC, 1); |
18 |
|
|
|
19 |
|
|
my $db = db_create $env; |
20 |
|
|
db_open $db, undef, "table", undef, BDB::BTREE, BDB::AUTO_COMMIT | BDB::CREATE |
21 |
|
|
| BDB::READ_UNCOMMITTED, 0600; |
22 |
|
|
db_put $db, undef, "key", "data", 0, sub { |
23 |
|
|
db_del $db, undef, "key"; |
24 |
|
|
}; |
25 |
|
|
db_sync $db; |
26 |
|
|
|
27 |
root |
1.6 |
# when you also use Coro, management is easy: |
28 |
|
|
use Coro::BDB; |
29 |
|
|
|
30 |
root |
1.9 |
# automatic event loop intergration with AnyEvent: |
31 |
|
|
use AnyEvent::BDB; |
32 |
root |
1.4 |
|
33 |
|
|
# automatic result processing with EV: |
34 |
|
|
my $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
35 |
|
|
|
36 |
|
|
# with Glib: |
37 |
|
|
add_watch Glib::IO BDB::poll_fileno, |
38 |
|
|
in => sub { BDB::poll_cb; 1 }; |
39 |
|
|
|
40 |
|
|
# or simply flush manually |
41 |
|
|
BDB::flush; |
42 |
|
|
|
43 |
root |
1.3 |
DESCRIPTION |
44 |
|
|
See the BerkeleyDB documentation |
45 |
|
|
(<http://www.oracle.com/technology/documentation/berkeley-db/db/index.ht |
46 |
|
|
ml>). The BDB API is very similar to the C API (the translation has been |
47 |
|
|
very faithful). |
48 |
|
|
|
49 |
|
|
See also the example sections in the document below and possibly the eg/ |
50 |
|
|
subdirectory of the BDB distribution. Last not least see the IO::AIO |
51 |
|
|
documentation, as that module uses almost the same asynchronous request |
52 |
|
|
model as this module. |
53 |
|
|
|
54 |
|
|
I know this is woefully inadequate documentation. Send a patch! |
55 |
|
|
|
56 |
|
|
REQUEST ANATOMY AND LIFETIME |
57 |
|
|
Every request method creates a request. which is a C data structure not |
58 |
|
|
directly visible to Perl. |
59 |
|
|
|
60 |
|
|
During their existance, bdb requests travel through the following |
61 |
|
|
states, in order: |
62 |
|
|
|
63 |
|
|
ready |
64 |
|
|
Immediately after a request is created it is put into the ready |
65 |
|
|
state, waiting for a thread to execute it. |
66 |
|
|
|
67 |
|
|
execute |
68 |
|
|
A thread has accepted the request for processing and is currently |
69 |
|
|
executing it (e.g. blocking in read). |
70 |
|
|
|
71 |
|
|
pending |
72 |
|
|
The request has been executed and is waiting for result processing. |
73 |
|
|
|
74 |
|
|
While request submission and execution is fully asynchronous, result |
75 |
|
|
processing is not and relies on the perl interpreter calling |
76 |
|
|
"poll_cb" (or another function with the same effect). |
77 |
|
|
|
78 |
|
|
result |
79 |
|
|
The request results are processed synchronously by "poll_cb". |
80 |
|
|
|
81 |
|
|
The "poll_cb" function will process all outstanding aio requests by |
82 |
|
|
calling their callbacks, freeing memory associated with them and |
83 |
|
|
managing any groups they are contained in. |
84 |
|
|
|
85 |
|
|
done |
86 |
|
|
Request has reached the end of its lifetime and holds no resources |
87 |
|
|
anymore (except possibly for the Perl object, but its connection to |
88 |
|
|
the actual aio request is severed and calling its methods will |
89 |
|
|
either do nothing or result in a runtime error). |
90 |
|
|
|
91 |
root |
1.8 |
WIN32 FILENAMES/DATABASE NAME MESS |
92 |
|
|
Perl on Win32 supports only ASCII filenames (the reason is that it |
93 |
|
|
abuses an internal flag to store wether a filename is Unicode or ANSI, |
94 |
|
|
but that flag is used for somethign else in the perl core, so there is |
95 |
|
|
no way to detect wether a filename is ANSI or Unicode-encoded). The BDB |
96 |
|
|
module tries to work around this issue by assuming that the filename is |
97 |
|
|
an ANSI filename and BDB was built for unicode support. |
98 |
root |
1.7 |
|
99 |
root |
1.3 |
BERKELEYDB FUNCTIONS |
100 |
|
|
All of these are functions. The create functions simply return a new |
101 |
root |
1.7 |
object and never block. All the remaining functions take an optional |
102 |
root |
1.5 |
callback as last argument. If it is missing, then the function will be |
103 |
|
|
executed synchronously. In both cases, $! will reflect the return value |
104 |
|
|
of the function. |
105 |
root |
1.3 |
|
106 |
|
|
BDB functions that cannot block (mostly functions that manipulate |
107 |
|
|
settings) are method calls on the relevant objects, so the rule of thumb |
108 |
root |
1.7 |
is: if it's a method, it's not blocking, if it's a function, it takes a |
109 |
root |
1.3 |
callback as last argument. |
110 |
|
|
|
111 |
root |
1.10 |
In the following, $int signifies an integer return value, "bdb_filename" |
112 |
|
|
is a "filename" (octets on unix, madness on windows), "U32" is an |
113 |
|
|
unsigned 32 bit integer, "int" is some integer, "NV" is a floating point |
114 |
|
|
value. |
115 |
root |
1.3 |
|
116 |
root |
1.11 |
Most "SV *" types are generic perl scalars (for input and output of data |
117 |
|
|
values). |
118 |
root |
1.3 |
|
119 |
|
|
The various "DB_ENV" etc. arguments are handles return by |
120 |
|
|
"db_env_create", "db_create", "txn_begin" and so on. If they have an |
121 |
|
|
appended "_ornull" this means they are optional and you can pass "undef" |
122 |
|
|
for them, resulting a NULL pointer on the C level. |
123 |
|
|
|
124 |
root |
1.11 |
The "SV *callback" is the optional callback function to call when the |
125 |
|
|
request is completed. This last callback argument is special: the |
126 |
|
|
callback is simply the last argument passed. If there are "optional" |
127 |
|
|
arguments before the callback they can be left out. The callback itself |
128 |
|
|
can be left out or specified as "undef", in which case the function will |
129 |
|
|
be executed synchronously. |
130 |
|
|
|
131 |
|
|
For example, "db_env_txn_checkpoint" usually is called with all integer |
132 |
|
|
arguments zero. These can be left out, so all of these specify a call to |
133 |
|
|
"DB_ENV->txn_checkpoint", to be executed asynchronously with a callback |
134 |
|
|
to be called: |
135 |
|
|
|
136 |
|
|
db_env_txn_checkpoint $db_env, 0, 0, 0, sub { }; |
137 |
|
|
db_env_txn_checkpoint $db_env, 0, 0, sub { }; |
138 |
|
|
db_env_txn_checkpoint $db_env, sub { }; |
139 |
|
|
|
140 |
|
|
While these all specify a call to "DB_ENV->txn_checkpoint" to be |
141 |
|
|
executed synchronously: |
142 |
|
|
|
143 |
|
|
db_env_txn_checkpoint $db_env, 0, 0, 0, undef; |
144 |
|
|
db_env_txn_checkpoint $db_env, 0, 0, 0; |
145 |
|
|
db_env_txn_checkpoint $db_env, 0; |
146 |
|
|
|
147 |
root |
1.3 |
BDB functions |
148 |
|
|
Functions in the BDB namespace, exported by default: |
149 |
|
|
|
150 |
|
|
$env = db_env_create (U32 env_flags = 0) |
151 |
|
|
flags: RPCCLIENT |
152 |
|
|
|
153 |
root |
1.10 |
db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) |
154 |
root |
1.3 |
open_flags: INIT_CDB INIT_LOCK INIT_LOG INIT_MPOOL INIT_REP INIT_TXN RECOVER RECOVER_FATAL USE_ENVIRON USE_ENVIRON_ROOT CREATE LOCKDOWN PRIVATE REGISTER SYSTEM_MEM |
155 |
|
|
db_env_close (DB_ENV *env, U32 flags = 0, SV *callback = &PL_sv_undef) |
156 |
|
|
db_env_txn_checkpoint (DB_ENV *env, U32 kbyte = 0, U32 min = 0, U32 flags = 0, SV *callback = &PL_sv_undef) |
157 |
|
|
flags: FORCE |
158 |
|
|
db_env_lock_detect (DB_ENV *env, U32 flags = 0, U32 atype = DB_LOCK_DEFAULT, SV *dummy = 0, SV *callback = &PL_sv_undef) |
159 |
|
|
atype: LOCK_DEFAULT LOCK_EXPIRE LOCK_MAXLOCKS LOCK_MAXWRITE LOCK_MINLOCKS LOCK_MINWRITE LOCK_OLDEST LOCK_RANDOM LOCK_YOUNGEST |
160 |
|
|
db_env_memp_sync (DB_ENV *env, SV *dummy = 0, SV *callback = &PL_sv_undef) |
161 |
|
|
db_env_memp_trickle (DB_ENV *env, int percent, SV *dummy = 0, SV *callback = &PL_sv_undef) |
162 |
root |
1.10 |
db_env_dbremove (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, U32 flags = 0, SV *callback = &PL_sv_undef) |
163 |
|
|
db_env_dbrename (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, bdb_filename newname, U32 flags = 0, SV *callback = &PL_sv_undef) |
164 |
root |
1.3 |
|
165 |
|
|
$db = db_create (DB_ENV *env = 0, U32 flags = 0) |
166 |
|
|
flags: XA_CREATE |
167 |
|
|
|
168 |
root |
1.10 |
db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) |
169 |
root |
1.3 |
flags: AUTO_COMMIT CREATE EXCL MULTIVERSION NOMMAP RDONLY READ_UNCOMMITTED THREAD TRUNCATE |
170 |
|
|
db_close (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
171 |
|
|
flags: DB_NOSYNC |
172 |
root |
1.10 |
db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef) |
173 |
root |
1.3 |
db_compact (DB *db, DB_TXN_ornull *txn = 0, SV *start = 0, SV *stop = 0, SV *unused1 = 0, U32 flags = DB_FREE_SPACE, SV *unused2 = 0, SV *callback = &PL_sv_undef) |
174 |
|
|
flags: FREELIST_ONLY FREE_SPACE |
175 |
|
|
db_sync (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
176 |
|
|
db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = &PL_sv_undef) |
177 |
|
|
db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
178 |
|
|
flags: APPEND NODUPDATA NOOVERWRITE |
179 |
root |
1.11 |
db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0) (v4.6) |
180 |
root |
1.3 |
db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
181 |
|
|
flags: CONSUME CONSUME_WAIT GET_BOTH SET_RECNO MULTIPLE READ_COMMITTED READ_UNCOMMITTED RMW |
182 |
|
|
db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
183 |
|
|
flags: CONSUME CONSUME_WAIT GET_BOTH SET_RECNO MULTIPLE READ_COMMITTED READ_UNCOMMITTED RMW |
184 |
|
|
db_del (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = &PL_sv_undef) |
185 |
|
|
db_txn_commit (DB_TXN *txn, U32 flags = 0, SV *callback = &PL_sv_undef) |
186 |
|
|
flags: TXN_NOSYNC TXN_SYNC |
187 |
|
|
db_txn_abort (DB_TXN *txn, SV *callback = &PL_sv_undef) |
188 |
|
|
|
189 |
|
|
db_c_close (DBC *dbc, SV *callback = &PL_sv_undef) |
190 |
|
|
db_c_count (DBC *dbc, SV *count, U32 flags = 0, SV *callback = &PL_sv_undef) |
191 |
|
|
db_c_put (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
192 |
|
|
flags: AFTER BEFORE CURRENT KEYFIRST KEYLAST NODUPDATA |
193 |
|
|
db_c_get (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
194 |
|
|
flags: CURRENT FIRST GET_BOTH GET_BOTH_RANGE GET_RECNO JOIN_ITEM LAST NEXT NEXT_DUP NEXT_NODUP PREV PREV_DUP PREV_NODUP SET SET_RANGE SET_RECNO READ_UNCOMMITTED MULTIPLE MULTIPLE_KEY RMW |
195 |
|
|
db_c_pget (DBC *dbc, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
196 |
|
|
db_c_del (DBC *dbc, U32 flags = 0, SV *callback = &PL_sv_undef) |
197 |
|
|
|
198 |
|
|
db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = &PL_sv_undef) |
199 |
|
|
flags: CREATE EXCL |
200 |
|
|
db_sequence_close (DB_SEQUENCE *seq, U32 flags = 0, SV *callback = &PL_sv_undef) |
201 |
|
|
db_sequence_get (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, int delta, SV *seq_value, U32 flags = DB_TXN_NOSYNC, SV *callback = &PL_sv_undef) |
202 |
|
|
flags: TXN_NOSYNC |
203 |
|
|
db_sequence_remove (DB_SEQUENCE *seq, DB_TXN_ornull *txnid = 0, U32 flags = 0, SV *callback = &PL_sv_undef) |
204 |
|
|
flags: TXN_NOSYNC |
205 |
|
|
|
206 |
|
|
db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = &PL_sv_undef) |
207 |
|
|
This is not actually a Berkeley DB function but a BDB module extension. |
208 |
|
|
The background for this exytension is: It is very annoying to have to |
209 |
|
|
check every single BDB function for error returns and provide a codepath |
210 |
|
|
out of your transaction. While the BDB module still makes this possible, |
211 |
|
|
it contains the following extensions: |
212 |
|
|
|
213 |
|
|
When a transaction-protected function returns any operating system error |
214 |
|
|
(errno > 0), BDB will set the "TXN_DEADLOCK" flag on the transaction. |
215 |
|
|
This flag is also set by Berkeley DB functions themselves when an |
216 |
|
|
operation fails with LOCK_DEADLOCK, and it causes all further operations |
217 |
|
|
on that transaction (including "db_txn_commit") to fail. |
218 |
|
|
|
219 |
|
|
The "db_txn_finish" request will look at this flag, and, if it is set, |
220 |
|
|
will automatically call "db_txn_abort" (setting errno to "LOCK_DEADLOCK" |
221 |
|
|
if it isn't set to something else yet). If it isn't set, it will call |
222 |
|
|
"db_txn_commit" and return the error normally. |
223 |
|
|
|
224 |
|
|
How to use this? Easy: just write your transaction normally: |
225 |
|
|
|
226 |
|
|
my $txn = $db_env->txn_begin; |
227 |
|
|
db_get $db, $txn, "key", my $data; |
228 |
|
|
db_put $db, $txn, "key", $data + 1 unless $! == BDB::NOTFOUND; |
229 |
|
|
db_txn_finish $txn; |
230 |
|
|
die "transaction failed" if $!; |
231 |
|
|
|
232 |
|
|
That is, handle only the expected errors. If something unexpected |
233 |
|
|
happens (EIO, LOCK_NOTGRANTED or a deadlock in either db_get or db_put), |
234 |
|
|
then the remaining requests (db_put in this case) will simply be skipped |
235 |
|
|
(they will fail with LOCK_DEADLOCK) and the transaction will be aborted. |
236 |
|
|
|
237 |
|
|
You can use the "$txn->failed" method to check wether a transaction has |
238 |
|
|
failed in this way and abort further processing (excluding |
239 |
|
|
"db_txn_finish"). |
240 |
|
|
|
241 |
|
|
DB_ENV/database environment methods |
242 |
|
|
Methods available on DB_ENV/$env handles: |
243 |
|
|
|
244 |
|
|
DESTROY (DB_ENV_ornull *env) |
245 |
|
|
CODE: |
246 |
|
|
if (env) |
247 |
|
|
env->close (env, 0); |
248 |
|
|
|
249 |
|
|
$int = $env->set_data_dir (const char *dir) |
250 |
|
|
$int = $env->set_tmp_dir (const char *dir) |
251 |
|
|
$int = $env->set_lg_dir (const char *dir) |
252 |
|
|
$int = $env->set_shm_key (long shm_key) |
253 |
|
|
$int = $env->set_cachesize (U32 gbytes, U32 bytes, int ncache = 0) |
254 |
root |
1.10 |
$int = $env->set_flags (U32 flags, int onoff = 1) |
255 |
root |
1.11 |
$int = $env->log_set_config (U32 flags, int onoff = 1) (v4.7) |
256 |
|
|
$int = $env->set_intermediate_dir_mode (const char *modestring) (v4.7) |
257 |
root |
1.3 |
$env->set_errfile (FILE *errfile = 0) |
258 |
|
|
$env->set_msgfile (FILE *msgfile = 0) |
259 |
|
|
$int = $env->set_verbose (U32 which, int onoff = 1) |
260 |
|
|
$int = $env->set_encrypt (const char *password, U32 flags = 0) |
261 |
|
|
$int = $env->set_timeout (NV timeout_seconds, U32 flags = SET_TXN_TIMEOUT) |
262 |
|
|
$int = $env->set_mp_max_openfd (int maxopenfd); |
263 |
|
|
$int = $env->set_mp_max_write (int maxwrite, int maxwrite_sleep); |
264 |
|
|
$int = $env->set_mp_mmapsize (int mmapsize_mb) |
265 |
|
|
$int = $env->set_lk_detect (U32 detect = DB_LOCK_DEFAULT) |
266 |
|
|
$int = $env->set_lk_max_lockers (U32 max) |
267 |
|
|
$int = $env->set_lk_max_locks (U32 max) |
268 |
|
|
$int = $env->set_lk_max_objects (U32 max) |
269 |
|
|
$int = $env->set_lg_bsize (U32 max) |
270 |
|
|
$int = $env->set_lg_max (U32 max) |
271 |
root |
1.4 |
$int = $env->mutex_set_increment (U32 increment) |
272 |
|
|
$int = $env->mutex_set_tas_spins (U32 tas_spins) |
273 |
|
|
$int = $env->mutex_set_max (U32 max) |
274 |
|
|
$int = $env->mutex_set_align (U32 align) |
275 |
root |
1.3 |
|
276 |
|
|
$txn = $env->txn_begin (DB_TXN_ornull *parent = 0, U32 flags = 0) |
277 |
|
|
flags: READ_COMMITTED READ_UNCOMMITTED TXN_NOSYNC TXN_NOWAIT TXN_SNAPSHOT TXN_SYNC TXN_WAIT TXN_WRITE_NOSYNC |
278 |
root |
1.11 |
$txn = $env->cdsgroup_begin; (v4.5) |
279 |
root |
1.3 |
|
280 |
|
|
Example: |
281 |
|
|
use AnyEvent; |
282 |
|
|
use BDB; |
283 |
|
|
|
284 |
|
|
our $FH; open $FH, "<&=" . BDB::poll_fileno; |
285 |
|
|
our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&BDB::poll_cb); |
286 |
|
|
|
287 |
|
|
BDB::min_parallel 8; |
288 |
|
|
|
289 |
|
|
my $env = db_env_create; |
290 |
|
|
|
291 |
|
|
mkdir "bdtest", 0700; |
292 |
|
|
db_env_open |
293 |
|
|
$env, |
294 |
|
|
"bdtest", |
295 |
|
|
BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN | BDB::RECOVER | BDB::USE_ENVIRON | BDB::CREATE, |
296 |
|
|
0600; |
297 |
|
|
|
298 |
|
|
$env->set_flags (BDB::AUTO_COMMIT | BDB::TXN_NOSYNC, 1); |
299 |
|
|
|
300 |
|
|
DB/database methods |
301 |
|
|
Methods available on DB/$db handles: |
302 |
|
|
|
303 |
|
|
DESTROY (DB_ornull *db) |
304 |
|
|
CODE: |
305 |
|
|
if (db) |
306 |
|
|
{ |
307 |
|
|
SV *env = (SV *)db->app_private; |
308 |
|
|
db->close (db, 0); |
309 |
|
|
SvREFCNT_dec (env); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
$int = $db->set_cachesize (U32 gbytes, U32 bytes, int ncache = 0) |
313 |
|
|
$int = $db->set_flags (U32 flags) |
314 |
|
|
flags: CHKSUM ENCRYPT TXN_NOT_DURABLE |
315 |
|
|
Btree: DUP DUPSORT RECNUM REVSPLITOFF |
316 |
|
|
Hash: DUP DUPSORT |
317 |
|
|
Queue: INORDER |
318 |
|
|
Recno: RENUMBER SNAPSHOT |
319 |
|
|
|
320 |
|
|
$int = $db->set_encrypt (const char *password, U32 flags) |
321 |
|
|
$int = $db->set_lorder (int lorder) |
322 |
|
|
$int = $db->set_bt_minkey (U32 minkey) |
323 |
|
|
$int = $db->set_re_delim (int delim) |
324 |
|
|
$int = $db->set_re_pad (int re_pad) |
325 |
|
|
$int = $db->set_re_source (char *source) |
326 |
|
|
$int = $db->set_re_len (U32 re_len) |
327 |
|
|
$int = $db->set_h_ffactor (U32 h_ffactor) |
328 |
|
|
$int = $db->set_h_nelem (U32 h_nelem) |
329 |
|
|
$int = $db->set_q_extentsize (U32 extentsize) |
330 |
|
|
|
331 |
|
|
$dbc = $db->cursor (DB_TXN_ornull *txn = 0, U32 flags = 0) |
332 |
|
|
flags: READ_COMMITTED READ_UNCOMMITTED WRITECURSOR TXN_SNAPSHOT |
333 |
|
|
$seq = $db->sequence (U32 flags = 0) |
334 |
|
|
|
335 |
|
|
Example: |
336 |
|
|
my $db = db_create $env; |
337 |
|
|
db_open $db, undef, "table", undef, BDB::BTREE, BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0600; |
338 |
|
|
|
339 |
|
|
for (1..1000) { |
340 |
|
|
db_put $db, undef, "key $_", "data $_"; |
341 |
|
|
|
342 |
|
|
db_key_range $db, undef, "key $_", my $keyrange; |
343 |
|
|
my ($lt, $eq, $gt) = @$keyrange; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
db_del $db, undef, "key $_" for 1..1000; |
347 |
|
|
|
348 |
|
|
db_sync $db; |
349 |
|
|
|
350 |
|
|
DB_TXN/transaction methods |
351 |
|
|
Methods available on DB_TXN/$txn handles: |
352 |
|
|
|
353 |
|
|
DESTROY (DB_TXN_ornull *txn) |
354 |
|
|
CODE: |
355 |
|
|
if (txn) |
356 |
|
|
txn->abort (txn); |
357 |
|
|
|
358 |
|
|
$int = $txn->set_timeout (NV timeout_seconds, U32 flags = SET_TXN_TIMEOUT) |
359 |
|
|
flags: SET_LOCK_TIMEOUT SET_TXN_TIMEOUT |
360 |
|
|
|
361 |
|
|
$bool = $txn->failed |
362 |
|
|
# see db_txn_finish documentation, above |
363 |
|
|
|
364 |
|
|
DBC/cursor methods |
365 |
|
|
Methods available on DBC/$dbc handles: |
366 |
|
|
|
367 |
|
|
DESTROY (DBC_ornull *dbc) |
368 |
|
|
CODE: |
369 |
|
|
if (dbc) |
370 |
|
|
dbc->c_close (dbc); |
371 |
|
|
|
372 |
root |
1.11 |
$int = $cursor->set_priority ($priority = PRIORITY_*) (v4.6) |
373 |
root |
1.6 |
|
374 |
root |
1.3 |
Example: |
375 |
|
|
my $c = $db->cursor; |
376 |
|
|
|
377 |
|
|
for (;;) { |
378 |
|
|
db_c_get $c, my $key, my $data, BDB::NEXT; |
379 |
|
|
warn "<$!,$key,$data>"; |
380 |
|
|
last if $!; |
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
db_c_close $c; |
384 |
|
|
|
385 |
|
|
DB_SEQUENCE/sequence methods |
386 |
|
|
Methods available on DB_SEQUENCE/$seq handles: |
387 |
|
|
|
388 |
|
|
DESTROY (DB_SEQUENCE_ornull *seq) |
389 |
|
|
CODE: |
390 |
|
|
if (seq) |
391 |
|
|
seq->close (seq, 0); |
392 |
|
|
|
393 |
|
|
$int = $seq->initial_value (db_seq_t value) |
394 |
|
|
$int = $seq->set_cachesize (U32 size) |
395 |
|
|
$int = $seq->set_flags (U32 flags) |
396 |
|
|
flags: SEQ_DEC SEQ_INC SEQ_WRAP |
397 |
|
|
$int = $seq->set_range (db_seq_t min, db_seq_t max) |
398 |
|
|
|
399 |
|
|
Example: |
400 |
|
|
my $seq = $db->sequence; |
401 |
root |
1.7 |
|
402 |
|
|
db_sequence_open $seq, undef, "seq", BDB::CREATE; |
403 |
root |
1.3 |
db_sequence_get $seq, undef, 1, my $value; |
404 |
|
|
|
405 |
|
|
SUPPORT FUNCTIONS |
406 |
|
|
EVENT PROCESSING AND EVENT LOOP INTEGRATION |
407 |
root |
1.5 |
$msg = BDB::strerror [$errno] |
408 |
|
|
Returns the string corresponding to the given errno value. If no |
409 |
|
|
argument is given, use $!. |
410 |
|
|
|
411 |
root |
1.7 |
Note that the BDB module also patches the $! variable directly, so |
412 |
|
|
you should be able to get a bdb error string by simply stringifying |
413 |
|
|
$!. |
414 |
|
|
|
415 |
root |
1.3 |
$fileno = BDB::poll_fileno |
416 |
|
|
Return the *request result pipe file descriptor*. This filehandle |
417 |
|
|
must be polled for reading by some mechanism outside this module |
418 |
|
|
(e.g. Event or select, see below or the SYNOPSIS). If the pipe |
419 |
|
|
becomes readable you have to call "poll_cb" to check the results. |
420 |
|
|
|
421 |
|
|
See "poll_cb" for an example. |
422 |
|
|
|
423 |
|
|
BDB::poll_cb |
424 |
|
|
Process some outstanding events on the result pipe. You have to call |
425 |
|
|
this regularly. Returns the number of events processed. Returns |
426 |
|
|
immediately when no events are outstanding. The amount of events |
427 |
|
|
processed depends on the settings of "BDB::max_poll_req" and |
428 |
|
|
"BDB::max_poll_time". |
429 |
|
|
|
430 |
|
|
If not all requests were processed for whatever reason, the |
431 |
|
|
filehandle will still be ready when "poll_cb" returns. |
432 |
|
|
|
433 |
|
|
Example: Install an Event watcher that automatically calls |
434 |
|
|
BDB::poll_cb with high priority: |
435 |
|
|
|
436 |
|
|
Event->io (fd => BDB::poll_fileno, |
437 |
|
|
poll => 'r', async => 1, |
438 |
|
|
cb => \&BDB::poll_cb); |
439 |
|
|
|
440 |
|
|
BDB::max_poll_reqs $nreqs |
441 |
|
|
BDB::max_poll_time $seconds |
442 |
|
|
These set the maximum number of requests (default 0, meaning |
443 |
|
|
infinity) that are being processed by "BDB::poll_cb" in one call, |
444 |
|
|
respectively the maximum amount of time (default 0, meaning |
445 |
|
|
infinity) spent in "BDB::poll_cb" to process requests (more |
446 |
|
|
correctly the mininum amount of time "poll_cb" is allowed to use). |
447 |
|
|
|
448 |
|
|
Setting "max_poll_time" to a non-zero value creates an overhead of |
449 |
|
|
one syscall per request processed, which is not normally a problem |
450 |
|
|
unless your callbacks are really really fast or your OS is really |
451 |
|
|
really slow (I am not mentioning Solaris here). Using |
452 |
|
|
"max_poll_reqs" incurs no overhead. |
453 |
|
|
|
454 |
|
|
Setting these is useful if you want to ensure some level of |
455 |
|
|
interactiveness when perl is not fast enough to process all requests |
456 |
|
|
in time. |
457 |
|
|
|
458 |
|
|
For interactive programs, values such as 0.01 to 0.1 should be fine. |
459 |
|
|
|
460 |
root |
1.4 |
Example: Install an EV watcher that automatically calls BDB::poll_cb |
461 |
|
|
with low priority, to ensure that other parts of the program get the |
462 |
|
|
CPU sometimes even under high load. |
463 |
root |
1.3 |
|
464 |
|
|
# try not to spend much more than 0.1s in poll_cb |
465 |
|
|
BDB::max_poll_time 0.1; |
466 |
|
|
|
467 |
root |
1.4 |
my $bdb_poll = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb); |
468 |
root |
1.3 |
|
469 |
|
|
BDB::poll_wait |
470 |
|
|
If there are any outstanding requests and none of them in the result |
471 |
|
|
phase, wait till the result filehandle becomes ready for reading |
472 |
|
|
(simply does a "select" on the filehandle. This is useful if you |
473 |
|
|
want to synchronously wait for some requests to finish). |
474 |
|
|
|
475 |
|
|
See "nreqs" for an example. |
476 |
|
|
|
477 |
|
|
BDB::poll |
478 |
|
|
Waits until some requests have been handled. |
479 |
|
|
|
480 |
|
|
Returns the number of requests processed, but is otherwise strictly |
481 |
|
|
equivalent to: |
482 |
|
|
|
483 |
|
|
BDB::poll_wait, BDB::poll_cb |
484 |
|
|
|
485 |
|
|
BDB::flush |
486 |
root |
1.4 |
Wait till all outstanding BDB requests have been handled. |
487 |
root |
1.3 |
|
488 |
|
|
Strictly equivalent to: |
489 |
|
|
|
490 |
|
|
BDB::poll_wait, BDB::poll_cb |
491 |
|
|
while BDB::nreqs; |
492 |
|
|
|
493 |
root |
1.10 |
VERSION CHECKING |
494 |
|
|
BerkeleyDB comes in various versions, many of them have minor |
495 |
|
|
incompatibilities. This means that traditional "at least version x.x" |
496 |
|
|
checks are often not sufficient. |
497 |
|
|
|
498 |
root |
1.12 |
Example: set the log_autoremove option in a way compatible with <v4.7 |
499 |
root |
1.10 |
and v4.7. Note the use of & on the constants to avoid triggering a |
500 |
|
|
compiletime bug when the symbol isn't available. |
501 |
|
|
|
502 |
|
|
$DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
503 |
|
|
$DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
504 |
|
|
|
505 |
|
|
BDB::VERSION |
506 |
|
|
The "BDB::VERSION" function, when called without arguments, returns |
507 |
|
|
the Berkeley DB version as a v-string (usually with 3 components). |
508 |
|
|
You should use "lt" and "ge" operators exclusively to make |
509 |
|
|
comparisons. |
510 |
|
|
|
511 |
|
|
Example: check for at least version 4.7. |
512 |
|
|
|
513 |
|
|
BDB::VERSION ge v4.7 or die; |
514 |
|
|
|
515 |
|
|
BDB::VERSION min-version |
516 |
|
|
Returns true if the BDB version is at least the given version |
517 |
|
|
(specified as a v-string), false otherwise. |
518 |
|
|
|
519 |
|
|
Example: check for at least version 4.5. |
520 |
|
|
|
521 |
|
|
BDB::VERSION v4.7 or die; |
522 |
|
|
|
523 |
|
|
BDB::VERSION min-version, max-version |
524 |
|
|
Returns true of the BDB version is at least version "min-version" |
525 |
|
|
(specify "undef" or "v0" for any minimum version) and less then |
526 |
|
|
"max-version". |
527 |
|
|
|
528 |
|
|
Example: check wether version is strictly less then v4.7. |
529 |
|
|
|
530 |
|
|
BDB::VERSION v0, v4.7 |
531 |
|
|
or die "version 4.7 is not yet supported"; |
532 |
|
|
|
533 |
root |
1.3 |
CONTROLLING THE NUMBER OF THREADS |
534 |
|
|
BDB::min_parallel $nthreads |
535 |
root |
1.4 |
Set the minimum number of BDB threads to $nthreads. The current |
536 |
root |
1.3 |
default is 8, which means eight asynchronous operations can execute |
537 |
|
|
concurrently at any one time (the number of outstanding requests, |
538 |
|
|
however, is unlimited). |
539 |
|
|
|
540 |
root |
1.4 |
BDB starts threads only on demand, when an BDB request is queued and |
541 |
root |
1.3 |
no free thread exists. Please note that queueing up a hundred |
542 |
|
|
requests can create demand for a hundred threads, even if it turns |
543 |
|
|
out that everything is in the cache and could have been processed |
544 |
|
|
faster by a single thread. |
545 |
|
|
|
546 |
|
|
It is recommended to keep the number of threads relatively low, as |
547 |
|
|
some Linux kernel versions will scale negatively with the number of |
548 |
|
|
threads (higher parallelity => MUCH higher latency). With current |
549 |
|
|
Linux 2.6 versions, 4-32 threads should be fine. |
550 |
|
|
|
551 |
|
|
Under most circumstances you don't need to call this function, as |
552 |
|
|
the module selects a default that is suitable for low to moderate |
553 |
|
|
load. |
554 |
|
|
|
555 |
|
|
BDB::max_parallel $nthreads |
556 |
root |
1.4 |
Sets the maximum number of BDB threads to $nthreads. If more than |
557 |
root |
1.3 |
the specified number of threads are currently running, this function |
558 |
|
|
kills them. This function blocks until the limit is reached. |
559 |
|
|
|
560 |
|
|
While $nthreads are zero, aio requests get queued but not executed |
561 |
|
|
until the number of threads has been increased again. |
562 |
|
|
|
563 |
|
|
This module automatically runs "max_parallel 0" at program end, to |
564 |
|
|
ensure that all threads are killed and that there are no outstanding |
565 |
|
|
requests. |
566 |
|
|
|
567 |
|
|
Under normal circumstances you don't need to call this function. |
568 |
|
|
|
569 |
|
|
BDB::max_idle $nthreads |
570 |
|
|
Limit the number of threads (default: 4) that are allowed to idle |
571 |
|
|
(i.e., threads that did not get a request to process within 10 |
572 |
|
|
seconds). That means if a thread becomes idle while $nthreads other |
573 |
|
|
threads are also idle, it will free its resources and exit. |
574 |
|
|
|
575 |
|
|
This is useful when you allow a large number of threads (e.g. 100 or |
576 |
|
|
1000) to allow for extremely high load situations, but want to free |
577 |
|
|
resources under normal circumstances (1000 threads can easily |
578 |
|
|
consume 30MB of RAM). |
579 |
|
|
|
580 |
|
|
The default is probably ok in most situations, especially if thread |
581 |
|
|
creation is fast. If thread creation is very slow on your system you |
582 |
|
|
might want to use larger values. |
583 |
|
|
|
584 |
|
|
$oldmaxreqs = BDB::max_outstanding $maxreqs |
585 |
|
|
This is a very bad function to use in interactive programs because |
586 |
|
|
it blocks, and a bad way to reduce concurrency because it is |
587 |
|
|
inexact: Better use an "aio_group" together with a feed callback. |
588 |
|
|
|
589 |
|
|
Sets the maximum number of outstanding requests to $nreqs. If you to |
590 |
|
|
queue up more than this number of requests, the next call to the |
591 |
|
|
"poll_cb" (and "poll_some" and other functions calling "poll_cb") |
592 |
|
|
function will block until the limit is no longer exceeded. |
593 |
|
|
|
594 |
|
|
The default value is very large, so there is no practical limit on |
595 |
|
|
the number of outstanding requests. |
596 |
|
|
|
597 |
|
|
You can still queue as many requests as you want. Therefore, |
598 |
|
|
"max_oustsanding" is mainly useful in simple scripts (with low |
599 |
|
|
values) or as a stop gap to shield against fatal memory overflow |
600 |
|
|
(with large values). |
601 |
|
|
|
602 |
|
|
BDB::set_sync_prepare $cb |
603 |
|
|
Sets a callback that is called whenever a request is created without |
604 |
|
|
an explicit callback. It has to return two code references. The |
605 |
root |
1.10 |
first is used as the request callback (it should save the return |
606 |
|
|
status), and the second is called to wait until the first callback |
607 |
|
|
has been called (it must set $! to the return status). |
608 |
|
|
|
609 |
|
|
This mechanism can be used to include BDB into other event |
610 |
|
|
mechanisms, such as AnyEvent::BDB or Coro::BDB. |
611 |
|
|
|
612 |
|
|
The default implementation works like this: |
613 |
root |
1.3 |
|
614 |
|
|
sub { |
615 |
|
|
my $status; |
616 |
|
|
( |
617 |
|
|
sub { $status = $! }, |
618 |
|
|
sub { BDB::poll while !defined $status; $! = $status }, |
619 |
|
|
) |
620 |
|
|
} |
621 |
|
|
|
622 |
root |
1.10 |
It simply blocks the process till the request has finished and then |
623 |
|
|
sets $! to the return value. This means that if you don't use a |
624 |
|
|
callback, BDB will simply fall back to synchronous operations. |
625 |
|
|
|
626 |
root |
1.3 |
STATISTICAL INFORMATION |
627 |
|
|
BDB::nreqs |
628 |
|
|
Returns the number of requests currently in the ready, execute or |
629 |
|
|
pending states (i.e. for which their callback has not been invoked |
630 |
|
|
yet). |
631 |
|
|
|
632 |
|
|
Example: wait till there are no outstanding requests anymore: |
633 |
|
|
|
634 |
|
|
BDB::poll_wait, BDB::poll_cb |
635 |
|
|
while BDB::nreqs; |
636 |
|
|
|
637 |
|
|
BDB::nready |
638 |
|
|
Returns the number of requests currently in the ready state (not yet |
639 |
|
|
executed). |
640 |
|
|
|
641 |
|
|
BDB::npending |
642 |
|
|
Returns the number of requests currently in the pending state |
643 |
|
|
(executed, but not yet processed by poll_cb). |
644 |
|
|
|
645 |
|
|
FORK BEHAVIOUR |
646 |
|
|
This module should do "the right thing" when the process using it forks: |
647 |
|
|
|
648 |
root |
1.4 |
Before the fork, BDB enters a quiescent state where no requests can be |
649 |
|
|
added in other threads and no results will be processed. After the fork |
650 |
|
|
the parent simply leaves the quiescent state and continues |
651 |
root |
1.3 |
request/result processing, while the child frees the request/result |
652 |
|
|
queue (so that the requests started before the fork will only be handled |
653 |
|
|
in the parent). Threads will be started on demand until the limit set in |
654 |
|
|
the parent process has been reached again. |
655 |
|
|
|
656 |
|
|
In short: the parent will, after a short pause, continue as if fork had |
657 |
root |
1.4 |
not been called, while the child will act as if BDB has not been used |
658 |
|
|
yet. |
659 |
root |
1.3 |
|
660 |
root |
1.5 |
Win32 note: there is no fork on win32, and perls emulation of it is too |
661 |
|
|
broken to be supported, so do not use BDB in a windows pseudo-fork, |
662 |
|
|
better yet, switch to a more capable platform. |
663 |
|
|
|
664 |
root |
1.3 |
MEMORY USAGE |
665 |
|
|
Per-request usage: |
666 |
|
|
|
667 |
|
|
Each aio request uses - depending on your architecture - around 100-200 |
668 |
|
|
bytes of memory. In addition, stat requests need a stat buffer (possibly |
669 |
|
|
a few hundred bytes), readdir requires a result buffer and so on. Perl |
670 |
|
|
scalars and other data passed into aio requests will also be locked and |
671 |
|
|
will consume memory till the request has entered the done state. |
672 |
|
|
|
673 |
|
|
This is not awfully much, so queuing lots of requests is not usually a |
674 |
|
|
problem. |
675 |
|
|
|
676 |
|
|
Per-thread usage: |
677 |
|
|
|
678 |
|
|
In the execution phase, some aio requests require more memory for |
679 |
|
|
temporary buffers, and each thread requires a stack and other data |
680 |
|
|
structures (usually around 16k-128k, depending on the OS). |
681 |
|
|
|
682 |
|
|
KNOWN BUGS |
683 |
|
|
Known bugs will be fixed in the next release, except: |
684 |
|
|
|
685 |
|
|
If you use a transaction in any request, and the request returns |
686 |
|
|
with an operating system error or DB_LOCK_NOTGRANTED, the internal |
687 |
|
|
TXN_DEADLOCK flag will be set on the transaction. See C<db_txn_finish>, |
688 |
|
|
above. |
689 |
|
|
|
690 |
|
|
SEE ALSO |
691 |
root |
1.9 |
AnyEvent::BDB (event loop integration), Coro::BDB (more natural syntax), |
692 |
|
|
IO::AIO (nice to have). |
693 |
root |
1.3 |
|
694 |
|
|
AUTHOR |
695 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
696 |
|
|
http://home.schmorp.de/ |
697 |
|
|
|