transaction support added
This commit is contained in:
parent
586f239f00
commit
febd0ea1bb
|
@ -47,7 +47,7 @@ module SBDB
|
||||||
end
|
end
|
||||||
|
|
||||||
def transaction flags = nil, &e
|
def transaction flags = nil, &e
|
||||||
Transaction.new flags, &e
|
Transaction.new self, flags, &e
|
||||||
end
|
end
|
||||||
alias txn transaction
|
alias txn transaction
|
||||||
|
|
||||||
|
@ -62,8 +62,7 @@ module SBDB
|
||||||
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
||||||
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
|
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
|
||||||
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
||||||
p lg_bsize: @env.lg_bsize
|
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
|
||||||
@env.lg_bsize = 5*2**20 if opts[:lg_bsize]
|
|
||||||
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
|
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
|
||||||
rescue Object
|
rescue Object
|
||||||
close
|
close
|
||||||
|
|
42
lib/sbdb/transaction.rb
Normal file
42
lib/sbdb/transaction.rb
Normal 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
|
Loading…
Reference in a new issue