SBDB::Unknown: Better support. Auto detection and type.

This commit is contained in:
Denis Knauf 2010-02-06 11:47:41 +01:00
parent 37a9161470
commit f3153fba9f
2 changed files with 12 additions and 10 deletions

View file

@ -11,6 +11,11 @@ module SBDB
RDONLY = READLONY = Bdb::DB_RDONLY RDONLY = READLONY = Bdb::DB_RDONLY
CONSUME = Bdb::DB_CONSUME CONSUME = Bdb::DB_CONSUME
CONSUME_WAIT = Bdb::DB_CONSUME_WAIT CONSUME_WAIT = Bdb::DB_CONSUME_WAIT
TYPES = []
TYPES[BTREE] = Btree
TYPES[HASH] = Hash
TYPES[RECNO] = Recno
TYPES[QUEUE] = Queue
attr_reader :home attr_reader :home
include Enumerable include Enumerable
@ -50,7 +55,7 @@ module SBDB
def initialize file, name = nil, type = nil, flags = nil, mode = nil, txn = nil, env = nil def initialize file, name = nil, type = nil, flags = nil, mode = nil, txn = nil, env = nil
flags ||= 0 flags ||= 0
type ||= UNKNOWN type ||= UNKNOWN
type = BTREE if type == UNKNOWN and (flags & CREATE) == CREATE #type = BTREE if type == UNKNOWN and (flags & CREATE) == CREATE
@home, @db = env, env ? env.bdb_object.db : Bdb::Db.new @home, @db = env, env ? env.bdb_object.db : Bdb::Db.new
begin @db.open txn, file, name, type, flags, mode || 0 begin @db.open txn, file, name, type, flags, mode || 0
rescue Object rescue Object
@ -77,14 +82,11 @@ module SBDB
class Unknown < DB class Unknown < DB
def self.new file, name, *p, &e def self.new file, name, *p, &e
db = super file, name, UNKNOWN, *p[2..-1] db = super file, name, UNKNOWN, *p[2..-1]
case db.bdb_object.get_type dbt = begin
when BTREE then Btree.new file, name, *p db.bdb_object.get_type
when HASH then Hash.new file, name *p ensure db.close
when RECNO then Recno.new file, name, *p end
when QUEUE then Queue.new file, name, *p TYPES[dbt] ? TYPES[dbt].new( file, name, *p, &e) : super( file, name, UNKNOWN, *p, &e)
else super file, name, UNKNOWN, *p, &e
end
ensure db.close
end end
end end

View file

@ -81,7 +81,7 @@ module SBDB
# If you use this, never use close. It's possible somebody else use it too. # If you use this, never use close. It's possible somebody else use it too.
# The Databases, which are opened, will close, if the Environment will close. # The Databases, which are opened, will close, if the Environment will close.
def [] file, name = nil, type = nil, flags = nil, mode = nil, &e def [] file, name = nil, type = nil, flags = nil, mode = nil, &e
@dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unkown).new file, name, flags, mode, nil, self, &e @dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unknown).new file, name, flags, mode, nil, self, &e
end end
end end
Env = Environment Env = Environment