Module: ActiveRecord::Turntable::Base::ClassMethods

Defined in:
lib/active_record/turntable/base.rb

Instance Method Summary collapse

Instance Method Details

#current_last_shardObject



93
94
95
# File 'lib/active_record/turntable/base.rb', line 93

def current_last_shard
  turntable_cluster.select_shard(current_sequence_value) if sequencer_enabled?
end

#sequencer(sequencer_name, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/active_record/turntable/base.rb', line 67

def sequencer(sequencer_name, *args)
  class_attribute :turntable_sequencer_name
  class << self
    prepend ActiveRecordExt::Sequencer
  end

  self.turntable_sequencer_enabled = true
  self.turntable_sequencer_name = sequencer_name
end

#sequencer_enabled?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/active_record/turntable/base.rb', line 89

def sequencer_enabled?
  turntable_sequencer_enabled
end

#sharding_condition_needed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/active_record/turntable/base.rb', line 85

def sharding_condition_needed?
  turntable_enabled? && primary_key != turntable_shard_key.to_s
end

#turntable(cluster_name, shard_key_name, options = {}) ⇒ Object

Parameters:

  • cluster_name (Symbol)

    cluster name for this class

  • shard_key_name (Symbol)

    shard key attribute name

  • options (Hash) (defaults to: {})


41
42
43
44
45
46
47
48
# File 'lib/active_record/turntable/base.rb', line 41

def turntable(cluster_name, shard_key_name, options = {})
  class_attribute :turntable_shard_key, :turntable_cluster_name

  self.turntable_enabled = true
  self.turntable_cluster_name = cluster_name
  self.turntable_shard_key = shard_key_name
  turntable_replace_connection_pool
end

#turntable_clusterObject



50
51
52
# File 'lib/active_record/turntable/base.rb', line 50

def turntable_cluster
  turntable_clusters[turntable_cluster_name]
end

#turntable_enabled?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/active_record/turntable/base.rb', line 81

def turntable_enabled?
  turntable_enabled
end

#turntable_pool_listObject



54
55
56
# File 'lib/active_record/turntable/base.rb', line 54

def turntable_pool_list
  turntable_clusters.values.map { |cluster| cluster.shards.map(&:connection_pool) }.flatten.uniq
end

#turntable_replace_connection_poolObject



58
59
60
61
62
63
64
65
# File 'lib/active_record/turntable/base.rb', line 58

def turntable_replace_connection_pool
  ch = connection_handler
  cp = ConnectionProxy.new(self, turntable_cluster)
  pp = PoolProxy.new(cp)

  self.connection_specification_name = "turntable_pool_proxy::#{name}"
  ch.owner_to_turntable_pool[connection_specification_name] = pp
end

#turntable_sequencerObject



77
78
79
# File 'lib/active_record/turntable/base.rb', line 77

def turntable_sequencer
  turntable_sequencers[turntable_sequencer_name]
end

#with_masterObject



113
114
115
# File 'lib/active_record/turntable/base.rb', line 113

def with_master
  connection.with_master { yield }
end

#with_shard(any_shard) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_record/turntable/base.rb', line 97

def with_shard(any_shard)
  shard = case any_shard
          when Numeric
            turntable_cluster.shard_for(any_shard)
          when ActiveRecord::Base
            turntable_cluster.shard_for(any_shard.send(any_shard.turntable_shard_key))
          else
            shard_or_key
          end
  connection.with_shard(shard) { yield }
end

#with_slaveObject



109
110
111
# File 'lib/active_record/turntable/base.rb', line 109

def with_slave
  connection.with_slave { yield }
end