Take me over?
NAME
BerkeleyDB::Manager - General purpose BerkeleyDB wrapper
SYNOPSIS
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 );
DESCRIPTION
This object provides a convenience wrapper for BerkeleyDB
ATTRIBUTES
- home
-
The path to pass as
-HometoBerkeleyDB::Env->new.If provided the
filearguments toopen_dbshould be relative paths.If not provided, BerkeleyDB will use the current working directory for transaction journals, etc.
- create
-
Whether
DB_CREATEis passed toEnvorinstantiate_dbby default. Defaults to false.If create and specified and an alternate log, data or tmp dir is set, a
DB_CONFIGconfiguration file with those parameters will be written allowing standard Berkeley DB tools to work with the environment home directory.An existing
DB_CONFIGfile will not be overwritten, nor will one be written in the current directory ifhomeis not specified. - lock
-
Whether
DB_INIT_LOCKis passed. Defaults to true.Can be set to false if ALL concurrent instances are readonly.
- deadlock_detection
-
Whether or not lock detection is set. The default is true.
- lk_detect
-
The type of lock detection to use if
deadlock_detectionis set. Defaults toDB_LOCK_DEFAULT. Additional possible values areDB_LOCK_MAXLOCKS,DB_LOCK_MINLOCKS,DB_LOCK_MINWRITE,DB_LOCK_OLDEST,DB_LOCK_RANDOM, andDB_LOCK_YOUNGEST. Seeset_lk_detectin the Berkeley DB reference guide. - readonly
-
Whether
DB_RDONLYis passed in the flags. Defaults to false. - transactions
-
Whether or not to enable transactions.
Defaults to true.
- autocommit
-
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.
- auto_checkpoint
-
When true
txn_checkpointwill be called withcheckpoint_kbyteandcheckpoint_minevery time a top level transaction is comitted.Defaults to true.
- checkpoint_kbyte
-
Passed to
txn_checkpoint.txn_checkpointwill write a checkpoint if that many kilobytes of data have been written since the last checkpoint.Defaults to 20 megabytes. If transactions are comitted quickly this value should avoid checkpoints being made too often.
- checkpoint_min
-
Passed to
txn_checkpoint.txn_checkpointwill write a checkpoint if the last checkpoint was more than this many minutes ago.Defaults to 1 minute. If transactions are not committed very often this parameter should balance the large-ish default value for
checkpoint_kbyte. - recover
-
If true
DB_REGISTERandDB_RECOVERare enabled in the flags to the env.This will enable automatic recovery in case of a crash.
See also the db_recover utility, and file:///usr/local/BerkeleyDB/docs/gsg_txn/C/architectrecovery.html#multiprocessrecovery
- multiversion
-
Enables multiversioning concurrency.
- snapshot
-
Whether or not
DB_TXN_SNAPSHOTshould be passed totxn_begin.If
multiversionis not true, this is a noop.Defaults to true.
Using
DB_TXN_SNAPSHOTmeans will cause copy on write multiversioning concurrency instead of locking concurrency.This can improve read responsiveness for applications with long running transactions, by allowing a page to be read even if it is being written to in another transaction since the writer is modifying its own copy of the page.
This is an alternative to enabling reading of uncomitted data, and provides the same read performance while maintaining snapshot isolation at the cost of more memory.
- read_uncomitted
-
Enables uncomitted reads.
This breaks the I in ACID, since transactions are no longer isolated.
A better approaach to increase read performance when there are long running writing transactions is to enable multiversioning.
- log_auto_remove
-
Enables automatic removal of logs.
Normally logs should be removed after being backed up, but if you are not interested in having full snapshot backups for catastrophic recovery scenarios, you can enable this.
See http://www.oracle.com/technology/documentation/berkeley-db/db/ref/transapp/logfile.html.
Defaults to false.
- sync
-
Enables syncing of BDB log writing.
Defaults to true.
If disabled, transaction writing will not be synced. This means that in the event of a crash some successfully comitted transactions might still be rolled back during recovery, but the database will still be in tact and atomicity is still guaranteed.
This is useful for bulk imports as it can significantly increase performance of smaller transactions.
- dup
-
Enables
DB_DUPin-Properties, allowing duplicate keys in the db.Defaults to false.
- dupsort
-
Enables
DB_DUPSORTin-Properties.Defaults to false.
- db_class
-
The default class to use when instantiating new DB objects. Defaults to BerkeleyDB::Btree.
- env_flags
-
Flags to pass to the env. Overrides
transactions,createandrecover. - db_flags
-
Flags to pass to
instantiate_db. Overridescreateandautocommit. - db_properties
-
Properties to pass to
instantiate_db. Overridesdupanddupsort. - open_dbs
-
The hash of currently open dbs.
- chunk_size
-
See
cursor_stream.Defaults to 500.
METHODS
- open_db %args
-
Fetch a database handle, opening it as necessary.
If
nameis provided, it is used as the key inopen_dbs. Otherwisefileis taken from%args.Calls
instantiate_db - close_db $name
-
Close the DB with the key
$name - get_db $name
-
Fetch the db specified by
$nameif it is already open. - register_db $name, $handle
-
Registers the DB as open.
- instantiate_db %args
-
Instantiates a new database handle.
fileis a required argument.If
classis not provided, the "db_class" will be used in place.If
txnis not provided and the env has transactions enabled, the current transaction if any is used. Seetxn_doflagsandpropertiescan be overridden manually. If they are not providedbuild_db_flagsandbuild_db_propertieswill be used. - instantiate_hash
- instantiate_btree
-
Convenience wrappers for
instantiate_dbthat setclass. - build_db_properties %args
-
Merges argument options into a flag integer.
Default arguments are taken from the
dupanddupsortattrs. - build_db_flags %args
-
Merges argument options into a flag integer.
Default arguments are taken from the
autocommitandcreateattrs. - txn_do sub { }
-
Executes the subroutine in an
evalblock. Callstxn_commitif the transaction was successful, ortxn_rollbackif it wasn't.Transactions are kept on a stack internally.
- txn_begin
-
Begin a new transaction.
The new transaction is set as the active transaction for all registered database handles.
If
multiversionis enabledDB_TXN_SNAPSHOTis passed in as well. - txn_commit
-
Commit the currnet transaction.
Will die on error.
- txn_rollback
-
Rollback the current transaction.
- txn_checkpoint
-
Calls
txn_checkpointonenvwithcheckpoint_kbyteandcheckpoint_min.This is called automatically by
txn_commitifauto_checkpointis set. - associate %args
-
Associate
secondarywithprimary, usingcallbackto extract keys.callbackis invoked with the primary DB key and the value on every update toprimary, 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
secondarywith a secondary key returns the value fromprimary.Fetching with
pb_getwill also return the primary key.See the BDB documentation for more details.
- all_open_dbs
-
Returns a list of all the registered databases.
- cursor_stream %args
-
Fetches data from a cursor, returning a Data::Stream::Bulk.
If
cursoris not provided butdbis, a new cursor will be created.If
callbackis 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 $m->cursor_stream( 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_getwill be used, returning[ $key, $value ]for each cursor position.flagcan be passed, and defaults toDB_NEXT.chunk_sizecontrols the number of pairs returned in each chunk. If it isn't provided the attributechunk_sizeis used instead.If
valuesorkeysis set to a true value then only values or keys will be returned. These two arguments are mutually exclusive.Lastly,
initis 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_streamfor fetching duplicate key entries.Takes the same arguments as
cursor_stream, but adds a few more.keycan be passed in to initialize the cursor withDB_SET.To do manual initialization
callback_firstcan be provided instead.callbackis generated to useDB_NEXT_DUPinstead ofDB_NEXT, andflagis ignored.
VERSION CONTROL
http://github.com/nothingmuch/berkeleydb-manager
AUTHOR
Yuval Kogman <nothingmuch@woobling.org>
COPYRIGHT
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.