Class: SBDB::DB

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sbdb/db.rb

Direct Known Subclasses

Btree, Hash, Queue, Recno, Unknown

Constant Summary collapse

UNKNOWN =
Bdb::Db::UNKNOWN
BTREE =
Bdb::Db::BTREE
HASH =
Bdb::Db::HASH
QUEUE =
Bdb::Db::QUEUE
ARRAY =
RECNO = Bdb::Db::RECNO
RDONLY =
READLONY = Bdb::DB_RDONLY
CONSUME =
Bdb::DB_CONSUME
CONSUME_WAIT =
Bdb::DB_CONSUME_WAIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, *args) ⇒ DB

Arguments:

  • file

  • name

  • type

  • flags

  • mode

  • env

or: *file*, *opts*. opts must be a ::Hash with keys like above, excluded file.


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sbdb/db.rb', line 78

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
	#type = BTREE  if type == UNKNOWN and (flags & CREATE) == CREATE
	@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 = nil
	txn = opts[:txn]  # First is the global txn, second only for open.
	begin
		@db.open txn && txn.bdb_object, file, opts[:name], opts[:type], opts[:flags] || 0, opts[:mode] || 0
	rescue Object => exc
		close
		raise exc
	end
end

Instance Attribute Details

#homeObject (readonly)

Returns the value of attribute home


16
17
18
# File 'lib/sbdb/db.rb', line 16

def home
  @home
end

#txnObject

Returns the value of attribute txn


17
18
19
# File 'lib/sbdb/db.rb', line 17

def txn
  @txn
end

Class Method Details

.new(*ps, &exe) ⇒ Object Also known as: open


49
50
51
52
53
54
55
56
57
# File 'lib/sbdb/db.rb', line 49

def new *ps, &exe
	ret = obj = super( *ps)
	begin ret = exe.call obj
	ensure
		SBDB::raise_barrier &obj.method(:sync)
		SBDB::raise_barrier &obj.method(:close)
	end  if exe
	ret
end

Instance Method Details

#[]=(key, val) ⇒ Object


39
40
41
# File 'lib/sbdb/db.rb', line 39

def []= key, val
	put key, val
end

#_txn(txn) ⇒ Object


61
62
63
64
# File 'lib/sbdb/db.rb', line 61

def _txn txn
	txn ||= @txn
	txn && txn.bdb_object
end

#at(key, txn = nil) ⇒ Object Also known as: []


24
25
26
27
28
# File 'lib/sbdb/db.rb', line 24

def at key, txn = nil
	@db.get _txn(txn), key.nil? ? nil : key.to_s, nil, 0
rescue Bdb::KeyEmpty
	return nil
end

#bdb_objectObject


19
# File 'lib/sbdb/db.rb', line 19

def bdb_object()  @db  end

#close(flg = nil) ⇒ Object


21
# File 'lib/sbdb/db.rb', line 21

def close( flg = nil)  @db.close flg || 0  end

#cursor(&exe) ⇒ Object


22
# File 'lib/sbdb/db.rb', line 22

def cursor( &exe)  Cursor.new self, &exe  end

#delete(key, txn = nil) ⇒ Object Also known as: del


43
44
45
# File 'lib/sbdb/db.rb', line 43

def delete key, txn = nil
	@db.del _txn(txn), key.to_s
end

#each(key = nil, val = nil, &exe) ⇒ Object


95
96
97
# File 'lib/sbdb/db.rb', line 95

def each key = nil, val = nil, &exe
	cursor {|c| c.each key, val, &exe }
end

#put(key, val, txn = nil) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/sbdb/db.rb', line 31

def put key, val, txn = nil
	if val.nil?
		@db.del _txn(txn), key.to_s, 0
	else
		@db.put _txn(txn), key.nil? ? nil : key.to_s, val.to_s, 0
	end
end

#reverse(key = nil, val = nil, &exe) ⇒ Object


99
100
101
# File 'lib/sbdb/db.rb', line 99

def reverse key = nil, val = nil, &exe
	cursor {|c| c.reverse key, val, &exe }
end

#syncObject


20
# File 'lib/sbdb/db.rb', line 20

def sync()  @db.sync  end

#to_hash(key = nil, val = nil) ⇒ Object


103
104
105
106
107
# File 'lib/sbdb/db.rb', line 103

def to_hash key = nil, val = nil
	ht = {}
	each key, val, &ht.method( :[]=)
	ht
end

#transaction(flg = nil, &exe) ⇒ Object


66
67
68
# File 'lib/sbdb/db.rb', line 66

def transaction flg = nil, &exe
	block_given? ? home.transaction( flg, &exe) : home.transaction( flg)
end

#truncate(txn = nil) ⇒ Object


109
110
111
# File 'lib/sbdb/db.rb', line 109

def truncate txn = nil
	@db.truncate _txn(txn)
end