Class: Shameless::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/shameless/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Store

Returns a new instance of Store.



9
10
11
12
13
# File 'lib/shameless/store.rb', line 9

def initialize(name, &block)
  @name = name
  @configuration = Configuration.new
  block.call(@configuration)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/shameless/store.rb', line 7

def configuration
  @configuration
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/shameless/store.rb', line 7

def name
  @name
end

Instance Method Details

#attach(model_class, name = nil) ⇒ Object



15
16
17
18
19
# File 'lib/shameless/store.rb', line 15

def attach(model_class, name = nil)
  model_class.extend(Model)
  model_class.attach_to(self, name)
  models_hash[name] = model_class
end

#create_table!(table_name, &block) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/shameless/store.rb', line 45

def create_table!(table_name, &block)
  each_shard do |shard|
    partition = find_partition_for_shard(shard)
    sharded_table_name = table_name_with_shard(table_name, shard)
    options = @configuration.create_table_options || {}
    partition.create_table(sharded_table_name, options) { block.call(self, sharded_table_name) }
  end
end

#create_tables!Object



41
42
43
# File 'lib/shameless/store.rb', line 41

def create_tables!
  models.each(&:create_tables!)
end

#disconnectObject



29
30
31
32
33
# File 'lib/shameless/store.rb', line 29

def disconnect
  if instance_variable_defined?(:@partitions)
    partitions.each(&:disconnect)
  end
end

#each_partition(&block) ⇒ Object



35
36
37
38
39
# File 'lib/shameless/store.rb', line 35

def each_partition(&block)
  partitions.each do |partition|
    block.call(partition, table_names_on_partition(partition))
  end
end

#each_shard(&block) ⇒ Object



59
60
61
# File 'lib/shameless/store.rb', line 59

def each_shard(&block)
  0.upto(@configuration.shards_count - 1, &block)
end

#find_shard(shardable_value) ⇒ Object



63
64
65
# File 'lib/shameless/store.rb', line 63

def find_shard(shardable_value)
  shardable_value % @configuration.shards_count
end

#find_table(table_name, shardable_value) ⇒ Object



67
68
69
70
71
72
# File 'lib/shameless/store.rb', line 67

def find_table(table_name, shardable_value)
  shard = find_shard(shardable_value)
  partition = find_partition_for_shard(shard)
  table_name = table_name_with_shard(table_name, shard)
  partition.from(table_name)
end

#padded_shard(shardable_value) ⇒ Object



54
55
56
57
# File 'lib/shameless/store.rb', line 54

def padded_shard(shardable_value)
  shard = find_shard(shardable_value)
  format_shard(shard)
end

#put(table_name, shardable_value, values) ⇒ Object



21
22
23
# File 'lib/shameless/store.rb', line 21

def put(table_name, shardable_value, values)
  find_table(table_name, shardable_value).insert(values)
end

#where(table_name, shardable_value, query, &block) ⇒ Object



25
26
27
# File 'lib/shameless/store.rb', line 25

def where(table_name, shardable_value, query, &block)
  find_table(table_name, shardable_value).where(query, &block)
end