bdb: Improved Ruby Bindings for Berkeley DB
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.
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.