sbdb/README

57 lines
1.1 KiB
Plaintext
Raw Normal View History

2010-01-30 15:29:46 +01:00
= Dependencies
== Bdb
2010-01-30 00:12:43 +01:00
Your need first this library:
http://github.com/DenisKnauf/bdb
2010-01-30 15:29:46 +01:00
= Download
2010-01-30 00:12:43 +01:00
via git:
git clone git://github.com/DenisKnauf/sbdb.git
2010-01-30 15:29:46 +01:00
= Installation
2010-01-30 00:12:43 +01:00
2010-01-30 15:29:46 +01:00
gem build sbdb.gemspec
2010-01-30 00:53:17 +01:00
gem install sbdb-*.gem
2010-01-30 15:29:46 +01:00
= Usage
2010-01-30 00:12:43 +01:00
First, open environment and database
2010-01-30 00:44:27 +01:00
require 'sbdb'
2010-01-30 00:12:43 +01:00
Dir.mkdir 'newenv' rescue Errno::EEXIST
env = SBDB::Env.new 'newenv', SBDB::CREATE
db = env.open SBDB::Btree, 'newdb.db', 'mynewdb', 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
2010-01-30 15:29:46 +01:00
SBDB::DB#each uses a SBDB::Cursor:
2010-01-30 00:12:43 +01:00
cursor = db.cursor
cursor.each {|k,v| puts "#{k}: ${v}" }
2010-01-30 15:29:46 +01:00
<strong>Don't forget to close everything, you've opened!</strong>
2010-01-30 00:12:43 +01:00
cursor.close
db.close
env.close
2010-01-30 15:29:46 +01:00
But you can use a <em>lambda</em> to ensure to close everything:
2010-01-30 00:12:43 +01:00
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
env.open SBDB::Btree, 'newdb.db', 'mynewdb', SBDB::CREATE do |db|
db.to_hash
end
end
2010-01-30 15:29:46 +01:00
SBDB::DB#to_hash creates a cursor and close it later.