module Sequel::SchemaSharding::ShardedModel::ClassMethods

Public Instance Methods

create(values = {}, &block) click to toggle source
Calls superclass method
# File lib/sequel/schema-sharding/model.rb, line 79
def create(values = {}, &block)
  sharded_column_value = values[sharded_column]
  shard_number = result_for(sharded_column_value).shard_number
  super.tap do |m|
    m.values[:shard_number] = shard_number
  end
end
read_only_shard_for(id) click to toggle source
# File lib/sequel/schema-sharding/model.rb, line 63
def read_only_shard_for(id)
  shard_for(id).server(:read_only).tap do |d|
    Sequel::SchemaSharding::DTraceProvider.provider.read_only_shard_for.fire(id.to_s, d.shard_number, self.table_name_s) if Sequel::SchemaSharding::DTraceProvider.provider.read_only_shard_for.enabled?
  end
end
result_for(id) click to toggle source

The result of a lookup for the given id. See Sequel::SchemaSharding::Finder::Result

# File lib/sequel/schema-sharding/model.rb, line 70
def result_for(id)
  Sequel::SchemaSharding::Finder.instance.lookup(self.implicit_table_name, id)
end
schema_and_table(result) click to toggle source

Construct the schema and table for use in a dataset.

# File lib/sequel/schema-sharding/model.rb, line 75
def schema_and_table(result)
  :"#{result.schema}__#{self.implicit_table_name}"
end
set_sharded_column(column) click to toggle source

Set the column on which the current model is sharded. This is used when saving, inserting and finding to decide which connection to use.

# File lib/sequel/schema-sharding/model.rb, line 40
def set_sharded_column(column)
  @sharded_column = column
end
shard_for(id) click to toggle source

Return a valid Sequel::Dataset that is tied to the shard table and connection for the id and will load values run by the query into the model.

# File lib/sequel/schema-sharding/model.rb, line 51
def shard_for(id)
  result = self.result_for(id)
  ds = result.connection[schema_and_table(result)]
  ds.row_proc = self
  dataset_method_modules.each { |m| ds.instance_eval { extend(m) } }
  ds.shard_number = result.shard_number
  ds.model = self
  ds.tap do |d|
    Sequel::SchemaSharding::DTraceProvider.provider.shard_for.fire(id.to_s, d.shard_number, self.table_name_s) if Sequel::SchemaSharding::DTraceProvider.provider.shard_for.enabled?
  end
end
sharded_column() click to toggle source

Accessor for the sharded_columns

# File lib/sequel/schema-sharding/model.rb, line 45
def sharded_column
  @sharded_column
end
table_name_s() click to toggle source
# File lib/sequel/schema-sharding/model.rb, line 87
def table_name_s
  @table_name_as_string ||= self.implicit_table_name.to_s
end