Class: ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm
- Inherits:
-
Base
- Object
- Base
- ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm
show all
- Defined in:
- lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Instance Method Details
#choose(shard_maps, key) ⇒ Object
3
4
5
6
7
|
# File 'lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb', line 3
def choose(shard_maps, key)
shard_map = shard_maps.bsearch { |shard| key <= shard.range.max }
raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key.inspect}" unless shard_map
shard_map.shard
end
|
#choose_index(shard_maps, key) ⇒ Object
9
10
11
12
|
# File 'lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb', line 9
def choose_index(shard_maps, key)
(0...shard_maps.size).bsearch { |idx| key <= shard_maps[idx].range.max } or
raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key.inspect}"
end
|
#shard_weights(shard_maps, current_sequence_value) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb', line 14
def shard_weights(shard_maps, current_sequence_value)
current_shard_index = choose_index(shard_maps, current_sequence_value)
shard_maps = shard_maps[0..current_shard_index]
weights_hash = Hash.new { |h, k| h[k] = 0 }
shard_maps.each_with_index do |shard_map, idx|
weights_hash[shard_map.shard] += if idx < current_shard_index
shard_map.range.size
else
current_sequence_value - shard_map.range.min + 1
end
end
weights_hash
end
|