Class: SBDB::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/sbdb/environment.rb

Overview

Environments are for storing one or more databases and are important if you want to work with more than one process on one database. You needn't use Environment, but it's usefull.

Constant Summary collapse

INIT_TXN =
Bdb::DB_INIT_TXN
INIT_LOCK =
Bdb::DB_INIT_LOCK
INIT_LOG =
Bdb::DB_INIT_LOG
INIT_MPOOL =
Bdb::DB_INIT_MPOOL
INIT_TRANSACTION =
INIT_TXN | INIT_LOCK | INIT_LOG | INIT_MPOOL
LOCKDOWN =
Bdb::DB_LOCKDOWN
NOMMAP =
Bdb::DB_NOMMAP
PRIVATE =
Bdb::DB_PRIVATE
SYSTEM_MEM =
Bdb::DB_SYSTEM_MEM
TXN_NOSYNC =
Bdb::DB_TXN_NOSYNC
LOG_DIRECT =
Bdb::DB_LOG_DIRECT
LOG_DSYNC =
Bdb::DB_LOG_DSYNC
LOG_AUTO_REMOVE =
Bdb::DB_LOG_AUTO_REMOVE
LOG_IN_MEMORY =
Bdb::DB_LOG_IN_MEMORY
LOG_ZERO =
Bdb::DB_LOG_ZERO

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Environment

args:

args[0] => dir
args[1] => flags
args[3] => mode

possible options (via Hash):

:dir, :flags, :mode, :log_config


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sbdb/environment.rb', line 60

def initialize *args
	opts = ::Hash === args.last ? args.pop : {}
	opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
	@dbs, @env = Ref::WeakValueMap.new, Bdb::Env.new( 0)
	@env.log_config opts[:log_config], 1  if opts[:log_config]
	@env.lg_bsize = opts[:lg_bsize]  if opts[:lg_bsize]
	@env.lg_max = opts[:lg_max]  if opts[:lg_max]
	begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
	rescue Object
		close
		raise
	end
end

Class Method Details

.new(*args) ⇒ Object Also known as: open



74
75
76
77
78
79
80
# File 'lib/sbdb/environment.rb', line 74

def self.new *args
	obj = ret = super( *args)
	begin ret = yield obj
	ensure SBDB::raise_barrier &obj.method(:close)
	end  if block_given?
	ret
end

Instance Method Details

#[](file, *ps, &exe) ⇒ Object

Returns the DB like open, but if it's already opened, it returns the old instance. 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.



107
108
109
110
111
112
113
# File 'lib/sbdb/environment.rb', line 107

def [] file, *ps, &exe
	opts = ::Hash === ps.last ? ps.pop : {}
	opts[:env] = self
	name, type, flg = ps[0] || opts[:name], ps[1] || opts[:type], ps[2] || opts[:flags]
	ps.push opts
	@dbs[ [file, name, flg | CREATE]] ||= (type || SBDB::Unknown).new file, *ps, &exe
end

#bdb_objectObject

returns the Bdb-object.



27
# File 'lib/sbdb/environment.rb', line 27

def bdb_object()  @env  end

#btree(file, *ps, &exe) ⇒ Object

Opens a Btree in this Environment



29
30
31
# File 'lib/sbdb/environment.rb', line 29

def btree file, *ps, &exe
	open Btree, file, *ps, &exe
end

#closeObject

Close the Environment. First you should close all databases!



84
85
86
87
# File 'lib/sbdb/environment.rb', line 84

def close
	@dbs.each{|key, db|db.close}
	@env.close
end

#hash(file, *ps, &exe) ⇒ Object

Opens a Hash in this Environment



33
34
35
# File 'lib/sbdb/environment.rb', line 33

def hash file, *ps, &exe
	open Hash, file, *ps, &exe
end

#open(type, file, *ps, &exe) ⇒ Object Also known as: db, open_db

Opens a Database. see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue



95
96
97
98
99
# File 'lib/sbdb/environment.rb', line 95

def open type, file, *ps, &exe
	ps.push ::Hash.new  unless ::Hash === ps.last
	ps.last[:env] = self
	(type || SBDB::Unkown).new file, *ps, &exe
end

#queue(file, *ps, &exe) ⇒ Object

Opens a Queue in this Environment



41
42
43
# File 'lib/sbdb/environment.rb', line 41

def queue file, *ps, &exe
	open Queue, file, *ps, &exe
end

#recno(file, *ps, &exe) ⇒ Object

Opens a Recno in this Environment



37
38
39
# File 'lib/sbdb/environment.rb', line 37

def recno file, *ps, &exe
	open Recno, file, *ps, &exe
end

#transaction(flg = nil, &exe) ⇒ Object Also known as: txn



49
50
51
# File 'lib/sbdb/environment.rb', line 49

def transaction flg = nil, &exe
	SBDB::Transaction.new self, flg, &exe
end

#unknown(file, *ps, &exe) ⇒ Object

Opens a DB of unknown type in this Environment



45
46
47
# File 'lib/sbdb/environment.rb', line 45

def unknown file, *ps, &exe
	open Unknown, file, *ps, &exe
end