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.



257
258
259
260
261
262
# File 'lib/archipelago/hashish.rb', line 257

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.



284
285
286
287
288
289
290
291
# File 'lib/archipelago/hashish.rb', line 284

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.



268
269
270
271
272
# File 'lib/archipelago/hashish.rb', line 268

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.



276
277
278
279
280
# File 'lib/archipelago/hashish.rb', line 276

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.



295
296
297
298
299
300
# File 'lib/archipelago/hashish.rb', line 295

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