Class: Shameless::Store
- Inherits:
-
Object
- Object
- Shameless::Store
- Defined in:
- lib/shameless/store.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #attach(model_class, name = nil) ⇒ Object
- #create_table!(table_name, &block) ⇒ Object
- #create_tables! ⇒ Object
- #disconnect ⇒ Object
- #each_partition(&block) ⇒ Object
- #each_shard(&block) ⇒ Object
- #find_shard(shardable_value) ⇒ Object
- #find_table(table_name, shardable_value) ⇒ Object
-
#initialize(name, &block) ⇒ Store
constructor
A new instance of Store.
- #padded_shard(shardable_value) ⇒ Object
- #put(table_name, shardable_value, values) ⇒ Object
- #where(table_name, shardable_value, query, &block) ⇒ Object
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
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/shameless/store.rb', line 7 def configuration @configuration end |
#name ⇒ Object (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) = @configuration. || {} partition.create_table(sharded_table_name, ) { 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 |
#disconnect ⇒ Object
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 |