• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

drecom / activerecord-turntable / #1259

31 Aug 2023 03:28AM UTC coverage: 88.603% (-0.03%) from 88.629%
#1259

push

web-flow
Merge pull request #20 from mixigroup/fix-ci-matrix

Change CI matrix, Not support Ruby 2.4 in Rails6.

2651 of 2992 relevant lines covered (88.6%)

389.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/lib/active_record/turntable/algorithm/hash_slot_algorithm.rb
1
require "zlib"
1✔
2

3
module ActiveRecord::Turntable::Algorithm
1✔
4
  class HashSlotAlgorithm < Base
1✔
5
    DEFAULT_HASH_FUNC = ->(key) { Zlib.crc32(key.to_s) }
4✔
6

7
    attr_reader :hash_func
1✔
8

9
    def initialize(config = {})
1✔
10
      super
20✔
11
      @hash_func = @config[:hash_func] || DEFAULT_HASH_FUNC
20✔
12
    end
13

14
    def choose(shard_maps, key)
1✔
15
      slot = slot_for_key(key, shard_maps.last.range.max)
4✔
16
      shard_map = shard_maps.bsearch { |shard| slot <= shard.range.max }
15✔
17
      raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key}" unless shard_map
4✔
18
      shard_map.shard
3✔
19
    end
20

21
    def choose_index(shard_maps, key)
1✔
22
      slot = slot_for_key(key, shard_maps.last.range.max)
×
23
      (0...shard_maps.size).bsearch { |idx| slot <= shard_maps[idx].range.max } or
×
24
        raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key}"
×
25
    end
26

27
    def slot_for_key(key, max)
1✔
28
      hash_func.call(key) % (max + 1)
6✔
29
    end
30

31
    def shard_weights(shard_maps, current_sequence_value)
1✔
32
      shard_maps.map { |shard_map| [shard_map.shard, shard_map.range.size] }.to_h
10✔
33
    end
34
  end
35
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc