1 | NAME |
1 | NAME |
2 | BDB - Asynchronous Berkeley DB access |
2 | BDB - Asynchronous Berkeley DB access |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use BDB; |
5 | use BDB; |
|
|
6 | |
|
|
7 | 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 | # when you also use Coro, management is easy: |
|
|
28 | use Coro::BDB; |
|
|
29 | |
|
|
30 | # automatic result processing with AnyEvent: |
|
|
31 | our $FH; open $FH, "<&=" . BDB::poll_fileno; |
|
|
32 | our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&BDB::poll_cb); |
|
|
33 | |
|
|
34 | # automatic result processing with EV: |
|
|
35 | my $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
36 | |
|
|
37 | # with Glib: |
|
|
38 | add_watch Glib::IO BDB::poll_fileno, |
|
|
39 | in => sub { BDB::poll_cb; 1 }; |
|
|
40 | |
|
|
41 | # or simply flush manually |
|
|
42 | BDB::flush; |
6 | |
43 | |
7 | DESCRIPTION |
44 | DESCRIPTION |
8 | See the BerkeleyDB documentation |
45 | See the BerkeleyDB documentation |
9 | (<http://www.oracle.com/technology/documentation/berkeley-db/db/index.ht |
46 | (<http://www.oracle.com/technology/documentation/berkeley-db/db/index.ht |
10 | ml>). The BDB API is very similar to the C API (the translation has been |
47 | ml>). The BDB API is very similar to the C API (the translation has been |
… | |
… | |
53 | either do nothing or result in a runtime error). |
90 | either do nothing or result in a runtime error). |
54 | |
91 | |
55 | BERKELEYDB FUNCTIONS |
92 | BERKELEYDB FUNCTIONS |
56 | All of these are functions. The create functions simply return a new |
93 | All of these are functions. The create functions simply return a new |
57 | object and never block. All the remaining functions all take an optional |
94 | object and never block. All the remaining functions all take an optional |
58 | callback as last argument. If it is missing, then the fucntion will be |
95 | callback as last argument. If it is missing, then the function will be |
59 | executed synchronously. |
96 | executed synchronously. In both cases, $! will reflect the return value |
|
|
97 | of the function. |
60 | |
98 | |
61 | BDB functions that cannot block (mostly functions that manipulate |
99 | BDB functions that cannot block (mostly functions that manipulate |
62 | settings) are method calls on the relevant objects, so the rule of thumb |
100 | settings) are method calls on the relevant objects, so the rule of thumb |
63 | is: if its a method, its not blocking, if its a function, it takes a |
101 | is: if its a method, its not blocking, if its a function, it takes a |
64 | callback as last argument. |
102 | callback as last argument. |
… | |
… | |
98 | |
136 | |
99 | db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) |
137 | db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) |
100 | flags: AUTO_COMMIT CREATE EXCL MULTIVERSION NOMMAP RDONLY READ_UNCOMMITTED THREAD TRUNCATE |
138 | flags: AUTO_COMMIT CREATE EXCL MULTIVERSION NOMMAP RDONLY READ_UNCOMMITTED THREAD TRUNCATE |
101 | db_close (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
139 | db_close (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
102 | flags: DB_NOSYNC |
140 | flags: DB_NOSYNC |
|
|
141 | db_upgrade (DB *db, octetstring file, U32 flags = 0, SV *callback = &PL_sv_undef) |
103 | 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) |
142 | 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) |
104 | flags: FREELIST_ONLY FREE_SPACE |
143 | flags: FREELIST_ONLY FREE_SPACE |
105 | db_sync (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
144 | db_sync (DB *db, U32 flags = 0, SV *callback = &PL_sv_undef) |
106 | db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = &PL_sv_undef) |
145 | db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = &PL_sv_undef) |
107 | db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
146 | db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = &PL_sv_undef) |
… | |
… | |
193 | $int = $env->set_lk_max_lockers (U32 max) |
232 | $int = $env->set_lk_max_lockers (U32 max) |
194 | $int = $env->set_lk_max_locks (U32 max) |
233 | $int = $env->set_lk_max_locks (U32 max) |
195 | $int = $env->set_lk_max_objects (U32 max) |
234 | $int = $env->set_lk_max_objects (U32 max) |
196 | $int = $env->set_lg_bsize (U32 max) |
235 | $int = $env->set_lg_bsize (U32 max) |
197 | $int = $env->set_lg_max (U32 max) |
236 | $int = $env->set_lg_max (U32 max) |
|
|
237 | $int = $env->mutex_set_increment (U32 increment) |
|
|
238 | $int = $env->mutex_set_tas_spins (U32 tas_spins) |
|
|
239 | $int = $env->mutex_set_max (U32 max) |
|
|
240 | $int = $env->mutex_set_align (U32 align) |
198 | |
241 | |
199 | $txn = $env->txn_begin (DB_TXN_ornull *parent = 0, U32 flags = 0) |
242 | $txn = $env->txn_begin (DB_TXN_ornull *parent = 0, U32 flags = 0) |
200 | flags: READ_COMMITTED READ_UNCOMMITTED TXN_NOSYNC TXN_NOWAIT TXN_SNAPSHOT TXN_SYNC TXN_WAIT TXN_WRITE_NOSYNC |
243 | flags: READ_COMMITTED READ_UNCOMMITTED TXN_NOSYNC TXN_NOWAIT TXN_SNAPSHOT TXN_SYNC TXN_WAIT TXN_WRITE_NOSYNC |
201 | |
244 | |
202 | Example: |
245 | Example: |
… | |
… | |
289 | DESTROY (DBC_ornull *dbc) |
332 | DESTROY (DBC_ornull *dbc) |
290 | CODE: |
333 | CODE: |
291 | if (dbc) |
334 | if (dbc) |
292 | dbc->c_close (dbc); |
335 | dbc->c_close (dbc); |
293 | |
336 | |
|
|
337 | $int = $cursor->set_priority ($priority = PRIORITY_*) |
|
|
338 | |
294 | Example: |
339 | Example: |
295 | my $c = $db->cursor; |
340 | my $c = $db->cursor; |
296 | |
341 | |
297 | for (;;) { |
342 | for (;;) { |
298 | db_c_get $c, my $key, my $data, BDB::NEXT; |
343 | db_c_get $c, my $key, my $data, BDB::NEXT; |
… | |
… | |
322 | db_sequence_open $seq, undef, "seq", BDB::CREATE; |
367 | db_sequence_open $seq, undef, "seq", BDB::CREATE; |
323 | db_sequence_get $seq, undef, 1, my $value; |
368 | db_sequence_get $seq, undef, 1, my $value; |
324 | |
369 | |
325 | SUPPORT FUNCTIONS |
370 | SUPPORT FUNCTIONS |
326 | EVENT PROCESSING AND EVENT LOOP INTEGRATION |
371 | EVENT PROCESSING AND EVENT LOOP INTEGRATION |
|
|
372 | $msg = BDB::strerror [$errno] |
|
|
373 | Returns the string corresponding to the given errno value. If no |
|
|
374 | argument is given, use $!. |
|
|
375 | |
327 | $fileno = BDB::poll_fileno |
376 | $fileno = BDB::poll_fileno |
328 | Return the *request result pipe file descriptor*. This filehandle |
377 | Return the *request result pipe file descriptor*. This filehandle |
329 | must be polled for reading by some mechanism outside this module |
378 | must be polled for reading by some mechanism outside this module |
330 | (e.g. Event or select, see below or the SYNOPSIS). If the pipe |
379 | (e.g. Event or select, see below or the SYNOPSIS). If the pipe |
331 | becomes readable you have to call "poll_cb" to check the results. |
380 | becomes readable you have to call "poll_cb" to check the results. |
… | |
… | |
367 | interactiveness when perl is not fast enough to process all requests |
416 | interactiveness when perl is not fast enough to process all requests |
368 | in time. |
417 | in time. |
369 | |
418 | |
370 | For interactive programs, values such as 0.01 to 0.1 should be fine. |
419 | For interactive programs, values such as 0.01 to 0.1 should be fine. |
371 | |
420 | |
372 | Example: Install an Event watcher that automatically calls |
421 | Example: Install an EV watcher that automatically calls BDB::poll_cb |
373 | BDB::poll_cb with low priority, to ensure that other parts of the |
422 | with low priority, to ensure that other parts of the program get the |
374 | program get the CPU sometimes even under high AIO load. |
423 | CPU sometimes even under high load. |
375 | |
424 | |
376 | # try not to spend much more than 0.1s in poll_cb |
425 | # try not to spend much more than 0.1s in poll_cb |
377 | BDB::max_poll_time 0.1; |
426 | BDB::max_poll_time 0.1; |
378 | |
427 | |
379 | # use a low priority so other tasks have priority |
428 | my $bdb_poll = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb); |
380 | Event->io (fd => BDB::poll_fileno, |
|
|
381 | poll => 'r', nice => 1, |
|
|
382 | cb => &BDB::poll_cb); |
|
|
383 | |
429 | |
384 | BDB::poll_wait |
430 | BDB::poll_wait |
385 | If there are any outstanding requests and none of them in the result |
431 | If there are any outstanding requests and none of them in the result |
386 | phase, wait till the result filehandle becomes ready for reading |
432 | phase, wait till the result filehandle becomes ready for reading |
387 | (simply does a "select" on the filehandle. This is useful if you |
433 | (simply does a "select" on the filehandle. This is useful if you |
… | |
… | |
396 | equivalent to: |
442 | equivalent to: |
397 | |
443 | |
398 | BDB::poll_wait, BDB::poll_cb |
444 | BDB::poll_wait, BDB::poll_cb |
399 | |
445 | |
400 | BDB::flush |
446 | BDB::flush |
401 | Wait till all outstanding AIO requests have been handled. |
447 | Wait till all outstanding BDB requests have been handled. |
402 | |
448 | |
403 | Strictly equivalent to: |
449 | Strictly equivalent to: |
404 | |
450 | |
405 | BDB::poll_wait, BDB::poll_cb |
451 | BDB::poll_wait, BDB::poll_cb |
406 | while BDB::nreqs; |
452 | while BDB::nreqs; |
407 | |
453 | |
408 | CONTROLLING THE NUMBER OF THREADS |
454 | CONTROLLING THE NUMBER OF THREADS |
409 | BDB::min_parallel $nthreads |
455 | BDB::min_parallel $nthreads |
410 | Set the minimum number of AIO threads to $nthreads. The current |
456 | Set the minimum number of BDB threads to $nthreads. The current |
411 | default is 8, which means eight asynchronous operations can execute |
457 | default is 8, which means eight asynchronous operations can execute |
412 | concurrently at any one time (the number of outstanding requests, |
458 | concurrently at any one time (the number of outstanding requests, |
413 | however, is unlimited). |
459 | however, is unlimited). |
414 | |
460 | |
415 | BDB starts threads only on demand, when an AIO request is queued and |
461 | BDB starts threads only on demand, when an BDB request is queued and |
416 | no free thread exists. Please note that queueing up a hundred |
462 | no free thread exists. Please note that queueing up a hundred |
417 | requests can create demand for a hundred threads, even if it turns |
463 | requests can create demand for a hundred threads, even if it turns |
418 | out that everything is in the cache and could have been processed |
464 | out that everything is in the cache and could have been processed |
419 | faster by a single thread. |
465 | faster by a single thread. |
420 | |
466 | |
… | |
… | |
426 | Under most circumstances you don't need to call this function, as |
472 | Under most circumstances you don't need to call this function, as |
427 | the module selects a default that is suitable for low to moderate |
473 | the module selects a default that is suitable for low to moderate |
428 | load. |
474 | load. |
429 | |
475 | |
430 | BDB::max_parallel $nthreads |
476 | BDB::max_parallel $nthreads |
431 | Sets the maximum number of AIO threads to $nthreads. If more than |
477 | Sets the maximum number of BDB threads to $nthreads. If more than |
432 | the specified number of threads are currently running, this function |
478 | the specified number of threads are currently running, this function |
433 | kills them. This function blocks until the limit is reached. |
479 | kills them. This function blocks until the limit is reached. |
434 | |
480 | |
435 | While $nthreads are zero, aio requests get queued but not executed |
481 | While $nthreads are zero, aio requests get queued but not executed |
436 | until the number of threads has been increased again. |
482 | until the number of threads has been increased again. |
… | |
… | |
509 | (executed, but not yet processed by poll_cb). |
555 | (executed, but not yet processed by poll_cb). |
510 | |
556 | |
511 | FORK BEHAVIOUR |
557 | FORK BEHAVIOUR |
512 | This module should do "the right thing" when the process using it forks: |
558 | This module should do "the right thing" when the process using it forks: |
513 | |
559 | |
514 | Before the fork, IO::AIO enters a quiescent state where no requests can |
560 | Before the fork, BDB enters a quiescent state where no requests can be |
515 | be added in other threads and no results will be processed. After the |
561 | added in other threads and no results will be processed. After the fork |
516 | fork the parent simply leaves the quiescent state and continues |
562 | the parent simply leaves the quiescent state and continues |
517 | request/result processing, while the child frees the request/result |
563 | request/result processing, while the child frees the request/result |
518 | queue (so that the requests started before the fork will only be handled |
564 | queue (so that the requests started before the fork will only be handled |
519 | in the parent). Threads will be started on demand until the limit set in |
565 | in the parent). Threads will be started on demand until the limit set in |
520 | the parent process has been reached again. |
566 | the parent process has been reached again. |
521 | |
567 | |
522 | In short: the parent will, after a short pause, continue as if fork had |
568 | In short: the parent will, after a short pause, continue as if fork had |
523 | not been called, while the child will act as if IO::AIO has not been |
569 | not been called, while the child will act as if BDB has not been used |
524 | used yet. |
570 | yet. |
|
|
571 | |
|
|
572 | Win32 note: there is no fork on win32, and perls emulation of it is too |
|
|
573 | broken to be supported, so do not use BDB in a windows pseudo-fork, |
|
|
574 | better yet, switch to a more capable platform. |
525 | |
575 | |
526 | MEMORY USAGE |
576 | MEMORY USAGE |
527 | Per-request usage: |
577 | Per-request usage: |
528 | |
578 | |
529 | Each aio request uses - depending on your architecture - around 100-200 |
579 | Each aio request uses - depending on your architecture - around 100-200 |
… | |
… | |
548 | with an operating system error or DB_LOCK_NOTGRANTED, the internal |
598 | with an operating system error or DB_LOCK_NOTGRANTED, the internal |
549 | TXN_DEADLOCK flag will be set on the transaction. See C<db_txn_finish>, |
599 | TXN_DEADLOCK flag will be set on the transaction. See C<db_txn_finish>, |
550 | above. |
600 | above. |
551 | |
601 | |
552 | SEE ALSO |
602 | SEE ALSO |
553 | Coro::AIO. |
603 | Coro::BDB, IO::AIO. |
554 | |
604 | |
555 | AUTHOR |
605 | AUTHOR |
556 | Marc Lehmann <schmorp@schmorp.de> |
606 | Marc Lehmann <schmorp@schmorp.de> |
557 | http://home.schmorp.de/ |
607 | http://home.schmorp.de/ |
558 | |
608 | |