Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

bdb: Improved Ruby Bindings for Berkeley DB

By Peter Cooper / January 9, 2009

berkdb.png Berkeley DB is a high performance database system initially developed in the early 1990s. It's not an SQL driven database engine - it just stores data in key/value pairs - but BDB is very fast, available to use on most operating systems, and is dual licensed for open source and commercial use. It has several benefits to just using a flat file or a PStore: transactions, fine-grained locking, replication, and hot backups, for starters.

While Ruby bindings already exist for BDB, Matt Bauer has just released some all new shiny ones that are fast and easy to use. You'll need to have Berkeley DB installed as a library on your system before you get started, of course. On OS X, the MacPort for Berkeley DB 4.6 is called db46. On Ubuntu try libdb-dev. Alternatively, go to the official Berkeley DB site and download the source. Follow Matt's README for instructions on installing the Ruby bindings.

Sample usage:

env = Bdb::Env.new(0)
env_flags =  Bdb::DB_CREATE |    # Create the environment if it does not already exist.
             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.
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)    

txn = env.txn_begin(nil, 0)
db.put(txn, 'key', 'value', 0)
txn.commit(0)

p db.get(nil, 'key', nil, 0)

db.close(0)
env.close

Okay, it's not the easiest to read code in the world, but Berkeley DB's power outweighs the modest complexity of its API. If Berkeley DB and its ways are still fresh to you, check out this guide (slightly but not significantly out of date) to get up to speed on the main concepts.

Comments

  1. Matt Bauer says:

    Thanks Peter for the write up. I would like to make this more Ruby like as it follows the C API very closely right now. I have some ideas on paper, just need a weekend to work on it. I also want to mention that Dan Janowski deserves a lot of credit here too. He did the original work on the binding. Also worth noting is Guy Decoux who did the very first Ruby binding which inspired Dan to do his.

  2. Zhao Difei says:

    Did you guys noticed tokyocabinet?

  3. Peter Cooper says:

    Yeah, I'm looking into it right now :) As well as MemcacheDB.

  4. Mike says:

    Hey this looks nice, I'll start my experiments with it right now (we are building a 500mio records a year system for a bank).

    I assume every Ruby aficionado here will agree that the transactions should be using blocks.

  5. Mike says:

    Can somebody please explain how to add records to a QUEUE or RECNO database? I keep getting 'illegal record number size' errors.

Other Posts to Enjoy

Twitter Mentions