Class: ActiveRecord::Turntable::ShardRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/turntable/shard_registry.rb

Defined Under Namespace

Classes: ShardMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster) ⇒ ShardRegistry

Returns a new instance of ShardRegistry.



9
10
11
12
13
# File 'lib/active_record/turntable/shard_registry.rb', line 9

def initialize(cluster)
  @cluster = cluster
  @shards_names_hash = {}.with_indifferent_access
  @shard_maps = []
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



7
8
9
# File 'lib/active_record/turntable/shard_registry.rb', line 7

def cluster
  @cluster
end

#shard_mapsObject (readonly)

Returns the value of attribute shard_maps.



7
8
9
# File 'lib/active_record/turntable/shard_registry.rb', line 7

def shard_maps
  @shard_maps
end

Instance Method Details

#[](name) ⇒ Object



32
33
34
# File 'lib/active_record/turntable/shard_registry.rb', line 32

def [](name)
  @shards_names_hash[name]
end

#add(setting) ⇒ Object



15
16
17
18
19
# File 'lib/active_record/turntable/shard_registry.rb', line 15

def add(setting)
  shard = (@shards_names_hash[setting.name] ||= Shard.new(cluster, setting.name, setting.slaves))
  @shard_maps << ShardMap.new(setting.range, shard)
  @shard_maps.sort_by! { |m| m.range.min }
end

#release!Object



26
27
28
29
30
# File 'lib/active_record/turntable/shard_registry.rb', line 26

def release!
  shards.each do |shard|
    shard.connection_pool.clear_all_connections!
  end
end

#shardsObject Also known as: all



21
22
23
# File 'lib/active_record/turntable/shard_registry.rb', line 21

def shards
  @shards_names_hash.values
end