Class: Archipelago::Hashish::BerkeleyHashishProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/archipelago/hashish.rb

Overview

A simple persistence provider backed by Berkeley db.

Instance Method Summary collapse

Constructor Details

#initialize(env_path) ⇒ BerkeleyHashishProvider

Initialize an instance with the given env_path to its database dir.



244
245
246
247
248
249
# File 'lib/archipelago/hashish.rb', line 244

def initialize(env_path)
  env_path.mkpath
  @env = BDB::Env.open(env_path, BDB::CREATE | BDB::INIT_MPOOL)
  @berkeley_hashishes = []
  @bdb_dbs = []
end

Instance Method Details

#close!Object

Closes databases opened by this instance.



271
272
273
274
275
276
277
278
# File 'lib/archipelago/hashish.rb', line 271

def close!
  @berkeley_hashishes.each do |h|
    h.close!
  end
  @bdb_dbs.each do |d|
    d.close
  end
end

#get_cached_hashish(name) ⇒ Object

Returns a cleverly cached (but slightly inefficient) hash-like instance (see Archipelago::Hashish::BerkeleyHashish) using name.



255
256
257
258
259
# File 'lib/archipelago/hashish.rb', line 255

def get_cached_hashish(name)
  hashish = BerkeleyHashish.new(name, @env)
  @berkeley_hashishes << hashish
  return hashish
end

#get_hashish(name) ⇒ Object

Returns a normal hash-like instance using name.



263
264
265
266
267
# File 'lib/archipelago/hashish.rb', line 263

def get_hashish(name)
  db = @env.open_db(BDB::HASH, name, nil, BDB::CREATE | BDB::NOMMAP)
  @bdb_dbs << db
  return db
end

#unlink!Object

Closes databases opened by this instance and removes the persistent files.



282
283
284
285
286
287
# File 'lib/archipelago/hashish.rb', line 282

def unlink!
  close!
  home = Pathname.new(@env.home)
  @env.close
  home.rmtree if home.exist?
end