rubinius-compatible; ref.gem. README: INIT_TRANSACTION

master v0.0.11
Denis Knauf 2011-08-25 13:48:26 +02:00
parent bfe13fcce8
commit 6c686eb2d3
4 changed files with 18 additions and 11 deletions

View File

@ -23,21 +23,22 @@ First, open environment and database
require 'sbdb' require 'sbdb'
Dir.mkdir 'newenv' rescue Errno::EEXIST Dir.mkdir 'newenv' rescue Errno::EEXIST
env = SBDB::Env.new 'newenv', SBDB::CREATE env = SBDB::Env.new 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION
db = env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE db = env.btree 'newdb.db', :flags => SBDB::CREATE
It works nearly like a Ruby-Hash: It works nearly like a Ruby-Hash:
db['key'] = 'value' db['key'] = 'value'
db['key'] # => 'value' db['key'] # => 'value'
db.to_hash # => {'key'=>'value'} db.to_hash # => {'key'=>'value'}
db.map {|k, v| [k, v].join ' => '} # => ["key => value"] db.map {|k, v| "k => v" } # => ["key => value"]
db.count # => 1 db.count # => 1
db.each {|k,v| puts "#{k}: #{v}" }
SBDB::DB#each uses a SBDB::Cursor: `SBDB::DB#each` uses a `SBDB::Cursor`:
cursor = db.cursor cursor = db.cursor
cursor.each {|k,v| puts "#{k}: ${v}" } cursor.each {|k,v| puts "#{k}: #{v}" }
**Don't forget to close everything, you've opened!** **Don't forget to close everything, you've opened!**
@ -47,10 +48,14 @@ SBDB::DB#each uses a SBDB::Cursor:
But you can use a *lambda* to ensure to close everything: But you can use a *lambda* to ensure to close everything:
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env| SBDB::Env.new( 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION) do |env|
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db| env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
db.to_hash db.to_hash
end end
end end
SBDB::DB#to_hash creates a cursor and close it later. `SBDB::DB#to_hash` creates a cursor and close it later.
Tip:
Signal.trap 'EXIT', env.method( :close)

View File

@ -15,6 +15,7 @@ begin
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb] gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
gem.require_paths = %w[lib] gem.require_paths = %w[lib]
gem.add_dependency 'bdb' gem.add_dependency 'bdb'
gem.add_dependency 'ref'
end end
Jeweler::GemcutterTasks.new Jeweler::GemcutterTasks.new
rescue LoadError rescue LoadError

View File

@ -1 +1 @@
0.0.10.1 0.0.11

View File

@ -1,4 +1,5 @@
require 'bdb' require 'bdb'
require 'ref'
require 'sbdb/environment' require 'sbdb/environment'
require 'sbdb/db' require 'sbdb/db'
require 'sbdb/cursor' require 'sbdb/cursor'