transaction support added

This commit is contained in:
Denis Knauf 2010-03-09 22:55:07 +01:00
parent 586f239f00
commit febd0ea1bb
2 changed files with 44 additions and 3 deletions

View file

@ -47,7 +47,7 @@ module SBDB
end
def transaction flags = nil, &e
Transaction.new flags, &e
Transaction.new self, flags, &e
end
alias txn transaction
@ -62,8 +62,7 @@ module SBDB
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
@env.log_config opts[:log_config], 1 if opts[:log_config]
p lg_bsize: @env.lg_bsize
@env.lg_bsize = 5*2**20 if opts[:lg_bsize]
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
rescue Object
close

42
lib/sbdb/transaction.rb Normal file
View file

@ -0,0 +1,42 @@
require 'sbdb'
require 'bdb'
module SBDB
class Transaction
NOSYNC = Bdb::DB_TXN_NOSYNC
SYNC = Bdb::DB_TXN_SYNC
NOWAIT = Bdb::DB_TXN_NOWAIT
WRITE_NOSYNC = Bdb::DB_TXN_WRITE_NOSYNC
SNAPSHOT = Bdb::DB_TXN_SNAPSHOT
READ_COMMITED = Bdb::DB_READ_COMMITTED
READ_UNCOMMITED = Bdb::DB_READ_UNCOMMITTED
def bdb_object() @txn end
def self.new *p, &e
r = obj = super( *p)
begin
r = e.call obj
rescue Object
obj.abort
raise $!
ensure
obj.commit
end if e
r
end
def initialize env, flags = nil, parent = nil
@txn = env.bdb_object.txn_begin parent, flags || 0
end
def commit flags = nil
@txn.commit flags || 0
end
def abort
@txn.abort
end
end
TXN = Transaction
end