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.



396
397
398
399
400
401
# File 'lib/archipelago/hashish.rb', line 396

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

Instance Method Details

#close!Object

Close all our databases.



441
442
443
444
445
446
447
448
# File 'lib/archipelago/hashish.rb', line 441

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

#get_cached_hashish(options) ⇒ Object

Get a CachedHashish either backed by a Berkeley Database (if :name is given) or by an Archipelago::Sanitation network (if :officer is given).



406
407
408
409
410
411
412
413
414
415
416
# File 'lib/archipelago/hashish.rb', line 406

def get_cached_hashish(options)
  if options[:officer]
    hashish = DumpHashish.new(options[:officer])
    @berkeley_hashishes << hashish
    return hashish
  elsif options[:name]
    hashish = BerkeleyHashish.new(options[:name], @env)
    @berkeley_hashishes << hashish
    return hashish
  end
end

#get_dup_tree(name, flags = BDB::CREATE | BDB::NOMMAP) ⇒ Object

Get a BDB::Btree allowing duplicates with name and flags.



420
421
422
423
424
425
426
427
428
429
# File 'lib/archipelago/hashish.rb', line 420

def get_dup_tree(name, flags = BDB::CREATE | BDB::NOMMAP)
  db = BDB::Btree.open(Pathname.new(File.join(@env.home, name)).expand_path, 
                      nil, 
                      flags,
                      0,
                      "env" => @env,
                      "set_flags" => BDB::DUP)
  @bdb_dbs << db
  return db
end

#get_hashish(name, flags = BDB::CREATE | BDB::NOMMAP) ⇒ Object

Get a BDB::Hash with name and flags.



433
434
435
436
437
# File 'lib/archipelago/hashish.rb', line 433

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

#unlink!Object

Close and remove all our databases.



452
453
454
455
456
457
# File 'lib/archipelago/hashish.rb', line 452

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