Module: Sequel::SchemaSharding::ShardedModel::ClassMethods

Defined in:
lib/sequel/schema-sharding/model.rb

Instance Method Summary collapse

Instance Method Details

#create(values = {}, &block) ⇒ Object



79
80
81
82
83
84
85
# 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) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object

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



70
71
72
# 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) ⇒ Object

Construct the schema and table for use in a dataset.



75
76
77
# 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) ⇒ Object

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



40
41
42
# File 'lib/sequel/schema-sharding/model.rb', line 40

def set_sharded_column(column)
  @sharded_column = column
end

#shard_for(id) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
# 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_columnObject

Accessor for the sharded_columns



45
46
47
# File 'lib/sequel/schema-sharding/model.rb', line 45

def sharded_column
  @sharded_column
end

#table_name_sObject



87
88
89
# File 'lib/sequel/schema-sharding/model.rb', line 87

def table_name_s
  @table_name_as_string ||= self.implicit_table_name.to_s
end