2010-02-23 21:36:46 +01:00
Description
===
2006-02-14 03:56:36 +01:00
2010-03-28 17:23:40 +02:00
Ruby bindings for Berkeley DB versions 4.2-4.8.
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
Installation
============
From Git
--------
2008-12-31 04:51:14 +01:00
You can check out the latest source from git:
2010-02-23 21:36:46 +01:00
git clone git://github.com/DenisKnauf/bdb.git
2008-12-31 04:51:14 +01:00
2010-02-23 21:36:46 +01:00
As a Gem
========
2006-02-14 03:56:36 +01:00
2008-12-31 04:51:14 +01:00
At the moment this library is not available on Rubyforge. To install it as a
2010-03-28 17:23:40 +02:00
gem, do the following:
2006-02-14 03:56:36 +01:00
2010-03-28 17:23:40 +02:00
sudo gem install dk-bdb
2008-12-31 04:56:49 +01:00
2009-11-20 22:45:39 +01:00
For Berkeley DB v4.7 installed from MacPorts do the following:
2009-01-10 21:29:26 +01:00
2010-03-28 17:23:40 +02:00
sudo env ARCHFLAGS="-arch i386" gem install dk-bdb
2009-01-10 21:29:26 +01:00
2008-12-31 04:51:14 +01:00
This assumes you're on OS X and BerkeleyDB wasn't compiled as a universal binary.
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
Sample Usage
============
env = Bdb::Env.new(0)
env_flags = Bdb::DB_CREATE | # Create the environment if it does not already exist.
2010-02-23 21:38:18 +01:00
Bdb::DB_INIT_TXN | # Initialize transactions
Bdb::DB_INIT_LOCK | # Initialize locking.
Bdb::DB_INIT_LOG | # Initialize logging
Bdb::DB_INIT_MPOOL # Initialize the in-memory cache.
2010-02-23 21:36:46 +01:00
env.open(File.join(File.dirname(__FILE__), 'tmp'), env_flags, 0);
db = env.db
db.open(nil, 'db1.db', nil, Bdb::Db::BTREE, Bdb::DB_CREATE | Bdb::DB_AUTO_COMMIT, 0)
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
txn = env.txn_begin(nil, 0)
db.put(txn, 'key', 'value', 0)
txn.commit(0)
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
value = db.get(nil, 'key', nil, 0)
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
db.close(0)
env.close
2006-02-14 03:56:36 +01:00
2010-02-23 21:36:46 +01:00
API
===
2006-02-14 03:56:36 +01:00
2008-12-31 04:51:14 +01:00
This interface is most closely based on the DB4 C api and tries to maintain close
2010-02-23 21:40:21 +01:00
interface proximity.
[That API is published by Oracle ](http://www.oracle.com/technology/documentation/berkeley-db/db/api_reference/C/frame_main.html ).
2006-02-14 03:56:36 +01:00
2008-12-31 04:51:14 +01:00
All function arguments systematically omit the leading DB handles and TXN handles.
2010-02-23 21:39:08 +01:00
A few calls omit the flags parameter when the documentation indicates that no
flag values are used - cursor.close is one.
2006-02-14 03:56:36 +01:00
2010-02-23 21:44:41 +01:00
Alternative API
---------------
You can use [SBDB ](http://github.com/DenisKnauf/sbdb ), too. It is easier to use, but base on this library.
2010-02-23 21:36:46 +01:00
Notes
=====
2006-02-14 03:56:36 +01:00
2008-12-31 04:51:14 +01:00
The defines generator is imperfect and includes some defines that are not
flags. While it could be improved, it is easier to delete the incorrect ones.
Thus, if you decide to rebuild the defines, you will need to edit the resulting
file. This may be necessary if using a different release of DB4 than the ones
the authors developed against. In nearly every case the defines generator works
flawlessly.
The authors have put all possible caution into ensuring that DB and Ruby cooperate.
The memory access was one aspect carefully considered. Since Ruby copies
when doing String#new, all key/data retrieval from DB is done with a 0 flag,
2010-02-23 21:42:23 +01:00
meaning that DB will be responsible. See [*this* news group posting ](http://groups.google.com/group/comp.databases.berkeley-db/browse_frm/thread/4f70a9999b64ce6a/c06b94692e3cbc41?tvc=1&q=dbt+malloc#c06b94692e3cbc41 )
about the effect of that.
2008-12-31 04:51:14 +01:00
The only other design consideration of consequence was associate. The prior
version used a Ruby thread local variable and kept track of the current
database in use. The authors decided to take a simpler approach since Ruby is green
threads. A global array stores the VALUE of the Proc for a given association
by the file descriptor number of the underlying database. This is looked
up when the first layer callback is made. It would have been better considered
if DB allowed the passing of a (void *) user data into the alloc that would
be supplied during callback. So far this design has not produced any problems.