Class: Bdb::PartitionedDatabase

Inherits:
Base show all
Defined in:
lib/bdb/partitioned_database.rb

Constant Summary collapse

SEPARATOR =
'__'
PARTITION_PATTERN =
/^[-\w]*$/

Instance Attribute Summary collapse

Attributes inherited from Base

#indexes

Instance Method Summary collapse

Methods inherited from Base

#checkpoint, #config, #environment, #index_by, #master?, #path, #synchronize, #transaction

Constructor Details

#initialize(base_name, opts = {}) ⇒ PartitionedDatabase

Returns a new instance of PartitionedDatabase


7
8
9
10
11
# File 'lib/bdb/partitioned_database.rb', line 7

def initialize(base_name, opts = {})
  @base_name    = base_name
  @partition_by = opts.delete(:partition_by)
  super(opts)
end

Instance Attribute Details

#base_nameObject (readonly)

Returns the value of attribute base_name


12
13
14
# File 'lib/bdb/partitioned_database.rb', line 12

def base_name
  @base_name
end

#partitionObject (readonly)

Returns the value of attribute partition


12
13
14
# File 'lib/bdb/partitioned_database.rb', line 12

def partition
  @partition
end

#partition_byObject (readonly)

Returns the value of attribute partition_by


12
13
14
# File 'lib/bdb/partitioned_database.rb', line 12

def partition_by
  @partition_by
end

Instance Method Details

#closeObject


47
48
49
50
51
52
# File 'lib/bdb/partitioned_database.rb', line 47

def close
  databases.each do |partition, database|
    database.close
  end
  @databases.clear
end

#database(partition = nil) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bdb/partitioned_database.rb', line 18

def database(partition = nil)
  partition ||= self.partition
  raise 'partition value required' if partition.nil?
  partition = partition.to_s
  raise "invalid partition value: #{partition}" unless partition =~ PARTITION_PATTERN

  databases[partition] ||= begin
    name = [partition, base_name].join(SEPARATOR)
    database = Bdb::Database.new(name, config)
    indexes.each do |field, opts|
      database.index_by(field, opts)
    end
    database
  end
end

#databasesObject


14
15
16
# File 'lib/bdb/partitioned_database.rb', line 14

def databases
  @databases ||= {}
end

#delete(key, opts = {}) ⇒ Object


64
65
66
# File 'lib/bdb/partitioned_database.rb', line 64

def delete(key, opts = {})
  database(opts[partition_by]).delete(key)
end

#get(*keys, &block) ⇒ Object


54
55
56
57
# File 'lib/bdb/partitioned_database.rb', line 54

def get(*keys, &block)
  opts = keys.last.kind_of?(Hash) ? keys.last : {}
  database(opts[partition_by]).get(*keys, &block)
end

#partitionsObject


34
35
36
37
38
# File 'lib/bdb/partitioned_database.rb', line 34

def partitions
  Dir[environment.path + "/*#{SEPARATOR}#{base_name}"].collect do |file|
    File.basename(file).split(SEPARATOR).first
  end
end

#set(key, value, opts = {}) ⇒ Object


59
60
61
62
# File 'lib/bdb/partitioned_database.rb', line 59

def set(key, value, opts = {})
  partition = get_field(partition_by, value)
  database(partition).set(key, value, opts)
end

#truncate!Object

Deletes all records in the database. Beware!


69
70
71
72
73
# File 'lib/bdb/partitioned_database.rb', line 69

def truncate!
  partitions.each do |partition|
    database(partition).truncate!
  end
end

#with_partition(partition) ⇒ Object


40
41
42
43
44
45
# File 'lib/bdb/partitioned_database.rb', line 40

def with_partition(partition)
  @partition, old_partition = partition, @partition
  yield
ensure
  @partition = old_partition
end