Class: ActiveDocument::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Environment

Returns a new instance of Environment.



2
3
4
# File 'lib/active_document/environment.rb', line 2

def initialize(path)
  @path = path
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/active_document/environment.rb', line 6

def env
  @env
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/active_document/environment.rb', line 6

def path
  @path
end

Instance Method Details

#closeObject



26
27
28
29
30
31
# File 'lib/active_document/environment.rb', line 26

def close
  if @env
    @env.close
    @env = nil
  end
end

#dbObject



8
9
10
# File 'lib/active_document/environment.rb', line 8

def db
  env.db
end

#openObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_document/environment.rb', line 12

def open
  if @env.nil?
    @env = Bdb::Env.new(0)
    env_flags =  Bdb::DB_CREATE     | # Create the environment if it does not already exist.
                 Bdb::DB_INIT_TXN   | # Initialize transactions        
                 Bdb::DB_INIT_LOCK  | # Initialize locking.
                 Bdb::DB_INIT_LOG   | # Initialize logging
                 Bdb::DB_INIT_MPOOL   # Initialize the in-memory cache.
    @env.cachesize = 10 * 1024 * 1024
    @env.flags_on = Bdb::DB_TXN_WRITE_NOSYNC
    @env.open(path, env_flags, 0)
  end
end

#transactionObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_document/environment.rb', line 33

def transaction
  if block_given?
    parent = @transaction
    @transaction = env.txn_begin(nil, 0)
    begin
      value = yield
      @transaction.commit(0)
    rescue Exception => e
      @transaction.abort
      raise e
    ensure
      @transaction = parent
    end
    value
  else
    @transaction
  end
end