Simple BDB wrapper library for bdb
Find a file
2010-02-23 20:43:05 +01:00
lib re_len-support (never try to get re_len if it is not a Queue) 2010-02-23 20:28:38 +01:00
AUTHOR license added 2010-01-30 15:07:09 +01:00
LICENSE license added 2010-01-30 15:07:09 +01:00
Rakefile Dependency added 2010-01-30 14:59:26 +01:00
README.md README uptodate 2010-02-23 20:43:05 +01:00
sbdb.gemspec 0.0.7 2010-02-23 20:33:50 +01:00
VERSION 0.0.7 2010-02-23 20:33:50 +01:00

Dependencies

Bdb

Your need first this library:

http://github.com/DenisKnauf/bdb

Download

via git:

git clone git://github.com/DenisKnauf/sbdb.git

Install

gem build sbdb.gemspec
gem install sbdb-*.gem

Usage

First, open environment and database

require 'sbdb'
Dir.mkdir 'newenv'  rescue Errno::EEXIST
env = SBDB::Env.new 'newenv', SBDB::CREATE
db = env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE

It works nearly like a Ruby-Hash:

db['key'] = 'value'
db['key']		# => 'value'
db.to_hash		# => {'key'=>'value'}
db.map {|k, v| [k, v].join ' => '}		# => ["key => value"]
db.count		# => 1

SBDB::DB#each uses a SBDB::Cursor:

cursor = db.cursor
cursor.each {|k,v| puts "#{k}: ${v}" }

Don't forget to close everything, you've opened!

cursor.close
db.close
env.close

But you can use a lambda to ensure to close everything:

SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
	env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
		db.to_hash
	end
end

SBDB::DB#to_hash creates a cursor and close it later.