BDB::DB supports transactions now. Version 0.0.8

This commit is contained in:
Denis Knauf 2010-03-11 00:18:38 +01:00
parent febd0ea1bb
commit f04fcac66d
7 changed files with 63 additions and 32 deletions

View file

@ -12,7 +12,7 @@ begin
gem.authors = ["Denis Knauf"]
gem.files = ["README.md", "VERSION", "lib/**/*.rb", "test/**/*.rb"]
gem.require_paths = ["lib"]
gem.add_dependency 'bdb'
gem.add_dependency 'dk-bdb'
end
Jeweler::GemcutterTasks.new
rescue LoadError

View file

@ -1 +1 @@
0.0.7
0.0.8

View file

@ -2,6 +2,7 @@ require 'bdb'
require 'sbdb/environment'
require 'sbdb/db'
require 'sbdb/cursor'
require 'sbdb/transaction'
module SBDB
CREATE = Bdb::DB_CREATE

View file

@ -21,19 +21,29 @@ module SBDB
def close( f = nil) @db.close f || 0 end
def cursor( &e) Cursor.new self, &e end
def [] k
@db.get nil, k.nil? ? nil : k.to_s, nil, 0
def at k, txn = nil
@db.get _txn(txn), k.nil? ? nil : k.to_s, nil, 0
rescue Bdb::KeyEmpty
return nil
end
alias [] at
def put k, v, txn = nil
if v.nil?
@db.del _txn(txn), k.to_s, 0
else
@db.put _txn(txn), k.nil? ? nil : k.to_s, v.to_s, 0
end
end
def []= k, v
if v.nil?
@db.del nil, k.to_s, 0
else
@db.put nil, k.nil? ? nil : k.to_s, v.to_s, 0
put k, v
end
def delete k, txn = nil
@db.del _txn(txn), k.to_s
end
alias del delete
class << self
def new *p, &e
@ -41,13 +51,24 @@ module SBDB
return x unless e
begin e.call x
ensure
x.sync
x.close
begin x.sync
rescue Object
$stderr.puts [$!.class,$!,$!.backtrace].inspect
end
begin x.close
rescue Object
$stderr.puts [$!.class,$!,$!.backtrace].inspect
end
end
end
alias open new
end
def _txn t
t ||= @txn
t && t.bdb_object
end
def initialize file, *args
opts = ::Hash === args.last ? args.pop : {}
opts = {:name => args[0], :type => args[1], :flags => args[2], :mode => args[3], :env => args[4]}.update opts
@ -55,7 +76,7 @@ module SBDB
@home, @db = opts[:env], opts[:env] ? opts[:env].bdb_object.db : Bdb::Db.new
opts[:type] = TYPES.index(self.class) || UNKNOWN
@db.re_len = opts[:re_len] if opts[:re_len]
txn = opts[:txn] || @txn
txn = opts[:txn]
begin
@db.open txn && txn.bdb_object, file, opts[:name], opts[:type], opts[:flags] || 0, opts[:mode] || 0
rescue Object
@ -77,6 +98,10 @@ module SBDB
each( k, v) {|k, v| h[ k] = v }
h
end
def truncate txn = nil
@db.truncate _txn(txn)
end
end
class Unknown < DB
@ -103,8 +128,8 @@ module SBDB
super [k].pack('I'), v
end
def push v
@db.put nil, "\0\0\0\0", v, Bdb::DB_APPEND
def push v, txn = nil
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
end
end
Array = Recno
@ -119,12 +144,12 @@ module SBDB
super [k].pack('I'), v
end
def unshift
@db.get nil, "\0\0\0\0", nil, Bdb::DB_CONSUME
def unshift txn = nil
@db.get _txn(txn), "\0\0\0\0", nil, Bdb::DB_CONSUME
end
def push v
@db.put nil, "\0\0\0\0", v, Bdb::DB_APPEND
def push v, txn = nil
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
end
end
TYPES[DB::QUEUE] = Queue

View file

@ -1,6 +1,7 @@
require 'bdb'
require 'sbdb/weakhash'
require 'sbdb/db'
require 'sbdb/transaction'
module SBDB
# Environments are for storing one or more databases and are important
@ -47,7 +48,7 @@ module SBDB
end
def transaction flags = nil, &e
Transaction.new self, flags, &e
SBDB::Transaction.new self, flags, &e
end
alias txn transaction
@ -63,18 +64,24 @@ module SBDB
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
@env.log_config opts[:log_config], 1 if opts[:log_config]
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
@env.lg_max = opts[:lg_max] if opts[:lg_max]
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
rescue Object
close
raise
end
return self unless block_given?
begin yield self
ensure close
end
nil
def self.new *args
obj = r = super( *args)
begin r = yield obj
ensure
begin obj.close
rescue Object
$stderr.puts [$!.class,$!,$!.backtrace].inspect
end
end if block_given?
r
end
# Close the Environment.

View file

@ -16,9 +16,7 @@ class WeakHash
end
end
@reclaim_key = lambda do |key_id|
if @key_map.has_key? key_id
@cache.delete @key_map.delete(key_id)
end
@cache.delete @key_map.delete(key_id) if @key_map.has_key? key_id
end
end

View file

@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{sbdb}
s.version = "0.0.7"
s.version = "0.0.8"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Denis Knauf"]
s.date = %q{2010-03-09}
s.date = %q{2010-03-11}
s.description = %q{Simple Ruby Berkeley DB wrapper library for bdb.}
s.email = %q{Denis.Knauf@gmail.com}
s.extra_rdoc_files = [
@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/DenisKnauf/bdb}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.rubygems_version = %q{1.3.6}
s.summary = %q{Simple Ruby Berkeley DB}
if s.respond_to? :specification_version then
@ -37,12 +37,12 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<bdb>, [">= 0"])
s.add_runtime_dependency(%q<dk-bdb>, [">= 0"])
else
s.add_dependency(%q<bdb>, [">= 0"])
s.add_dependency(%q<dk-bdb>, [">= 0"])
end
else
s.add_dependency(%q<bdb>, [">= 0"])
s.add_dependency(%q<dk-bdb>, [">= 0"])
end
end