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

View file

@ -81,7 +81,7 @@ module SBDB
# 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.
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
Env = Environment