BerkeleyDB::Manager - General purpose BerkeleyDB wrapper


        use BerkeleyDB::Manager;

        my $m = BerkeleyDB::Manager->new(
                home => Path::Class::Dir->new( ... ), # if you want to use rel paths
                db_class => "BerkeleyDB::Hash", # the default class for new DBs

        my $db = $m->open_db( file => "foo" ); # defaults

        $m->txn_do(sub {
                $db->db_put("foo", "bar");
                die "error!"; # rolls back

        # fetch all key/value pairs as a Data::Stream::Bulk
        my $pairs = $m->cursor_stream( db => $db );


This object provides a convenience wrapper for BerkeleyDB



The path to pass as -Home to BerkeleyDB::Env->new.

If provided the file arguments to open_db should be relative paths.

If not provided, BerkeleyDB will use the current working directory for transaction journals, etc.


Whether DB_CREATE is passed to Env or instantiate_db by default. Defaults to true.


Whether or not to enable transactions.

Defaults to true.


Whether or not a top level transaction is automatically created by BerkeleyDB. Defaults to true.

If you turn this off note that all database handles must be opened inside a transaction, unless transactions are disabled.


If true (the default) DB_REGISTER and DB_RECOVER are enabled in the flags to the env.

This will enable automatic recovery in case of a crash.


Enables DB_DUP in -Properties by default, allowing duplicate keys in the db.


Enables DB_DUPSORT in -Properties by default.


The default class to use when instantiating new DB objects. Defaults to BerkeleyDB::Btree.


Flags to pass to the env. Overrides transactions, create and recover.


Flags to pass to instantiate_db. Overrides create and autocommit.


Properties to pass to instantiate_db. Overrides dup and dupsort.


The hash of currently open dbs.


See cursor_stream.

Defaults to 500.


open_db %args

Fetch a database handle, opening it as necessary.

If name is provided, it is used as the key in open_dbs. Otherwise file is taken from %args.

Calls instantiate_db

close_db $name

Close the DB with the key $name

get_db $name

Fetch the db specified by $name if it is already open.

register_db $name, $handle

Registers the DB as open.

instantiate_db %args

Instantiates a new database handle.

file is a required argument.

If class is not provided, the "db_class" will be used in place.

If txn is not provided and the env has transactions enabled, the current transaction if any is used. See txn_do

flags and properties can be overridden manually. If they are not provided build_db_flags and build_db_properties will be used.


Convenience wrappers for instantiate_db that set class.

build_db_properties %args

Merges argument options into a flag integer.

Default arguments are taken from the dup and dupsort attrs.

build_db_flags %args

Merges argument options into a flag integer.

Default arguments are taken from the autocommit and create attrs.

txn_do sub { }

Executes the subroutine in an eval block. Calls txn_commit if the transaction was successful, or txn_rollback if it wasn't.

Transactions are kept on a stack internally.

txn_begin $parent_txn

Begin a new transaction.

If $parent_txn is provided the new transaction will be a child transaction.

The new transaction is set as the active transaction for all registered database handles.

txn_commit $txn

Commit a transaction.

Will die on error.

txn_rollback $txn

Rollback a transaction.

associate %args

Associate secondary with primary, using callback to extract keys.

callback is invoked with the primary DB key and the value on every update to primary, and is expected to return a key (or with recent BerkeleyDB also an array reference of keys) with which to create indexed entries.

Fetching on secondary with a secondary key returns the value from primary.

Fetching with pb_get will also return the primary key.

See the BDB documentation for more details.


Returns a list of all the registered databases.

cursor_stream %args

Fetches data from a cursor, returning a Data::Stream::Bulk.

If cursor is not provided but db is, a new cursor will be created.

If callback is provided it will be invoked on the cursor with an accumilator array repeatedly until it returns a false value. For example, to extract triplets from a secondary index, you can use this callback:

        my ( $sk, $pk, $v ) = ( '', '', '' ); # to avoid uninitialized warnings from BDB

                db => $db,
                callback => {
                        my ( $cursor, $accumilator ) = @_;

                        if ( $cursor->c_pget( $sk, $pk, $v ) == 0 ) {
                                push @$accumilator, [ $sk, $pk, $v ];
                                return 1;

                        return; # nothing left

If it is not provided, c_get will be used, returning [ $key, $value ] for each cursor position. flag can be passed, and defaults to DB_NEXT.

chunk_size controls the number of pairs returned in each chunk. If it isn't provided the attribute chunk_size is used instead.

If values or keys is set to a true value then only values or keys will be returned. These two arguments are mutually exclusive.

Lastly, init is an optional callback that is invoked once before each chunk, that can be used to set up the database. The return value is retained until the chunk is finished, so this callback can return a Scope::Guard to perform cleanup.

dup_cursor_stream %args

A specialization of cursor_stream for fetching duplicate key entries.

Takes the same arguments as cursor_stream, but adds a few more.

key can be passed in to initialize the cursor with DB_SET.

To do manual initialization callback_first can be provided instead.

callback is generated to use DB_NEXT_DUP instead of DB_NEXT, and flag is ignored.


This module is maintained using Darcs. You can get the latest version from, and use darcs send to commit changes.


Yuval Kogman <>


        Copyright (c) 2008 Yuval Kogman. All rights reserved
        This program is free software; you can redistribute
        it and/or modify it under the same terms as Perl itself.