yuemori/activerecord-shard_for

View on GitHub
lib/activerecord/shard_for/hash_modulo_router.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
require 'zlib'

module ActiveRecord
  module ShardFor
    class HashModuloRouter < ConnectionRouter
      # @param [String] key sharding key
      def route(key)
        hash(key) % connection_count
      end

      private

      def hash(v)
        Zlib.crc32(v.to_s)
      end
    end
  end
end