class Shameless::Store

Attributes

configuration[R]
name[R]

Public Class Methods

new(name, &block) click to toggle source
# File lib/shameless/store.rb, line 9
def initialize(name, &block)
  @name = name
  @configuration = Configuration.new
  block.call(@configuration)
end

Public Instance Methods

attach(model_class, name = nil) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# File lib/shameless/store.rb, line 41
def create_tables!
  models.each(&:create_tables!)
end
disconnect() click to toggle source
# File lib/shameless/store.rb, line 29
def disconnect
  if instance_variable_defined?(:@partitions)
    partitions.each(&:disconnect)
  end
end
each_partition(&block) click to toggle source
# 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) click to toggle source
# File lib/shameless/store.rb, line 59
def each_shard(&block)
  0.upto(@configuration.shards_count - 1, &block)
end
find_shard(shardable_value) click to toggle source
# File lib/shameless/store.rb, line 63
def find_shard(shardable_value)
  shardable_value % @configuration.shards_count
end
find_table(table_name, shardable_value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

connect(url) click to toggle source
# File lib/shameless/store.rb, line 88
def connect(url)
  Sequel.connect(url, @configuration.connection_options || Sequel::OPTS).tap do |db|
    db.extension(*@configuration.database_extensions)
  end
end
find_partition_for_shard(shard) click to toggle source
# File lib/shameless/store.rb, line 113
def find_partition_for_shard(shard)
  partition_index = shard / @configuration.shards_per_partition_count
  partitions[partition_index]
end
format_shard(shard) click to toggle source
# File lib/shameless/store.rb, line 109
def format_shard(shard)
  shard.to_s.rjust(6, '0')
end
models() click to toggle source
# File lib/shameless/store.rb, line 80
def models
  models_hash.values
end
models_hash() click to toggle source
# File lib/shameless/store.rb, line 76
def models_hash
  @models_hash ||= {}
end
partitions() click to toggle source
# File lib/shameless/store.rb, line 84
def partitions
  @partitions ||= @configuration.partition_urls.map {|url| connect(url) }
end
table_name_with_shard(table_name, shard) click to toggle source
# File lib/shameless/store.rb, line 104
def table_name_with_shard(table_name, shard)
  padded_shard = format_shard(shard)
  "#{table_name}_#{padded_shard}"
end
table_names_on_partition(partition) click to toggle source
# File lib/shameless/store.rb, line 94
def table_names_on_partition(partition)
  partition_index = partitions.index(partition)
  first_shard = partition_index * @configuration.shards_per_partition_count
  last_shard = first_shard + @configuration.shards_per_partition_count - 1
  shards = first_shard..last_shard
  table_names = models.flat_map(&:table_names)

  table_names.flat_map {|t| shards.map {|s| table_name_with_shard(t, s) } }
end