module Redis::Cluster::KeySlotConverter

Key to slot converter for Redis Cluster Client

We can test it by `CLUSTER KEYSLOT` command.

@see github.com/antirez/redis-rb-cluster

Reference implementation in Ruby

@see redis.io/topics/cluster-spec#appendix

Reference implementation in ANSI C

@see redis.io/commands/cluster-keyslot

CLUSTER KEYSLOT command reference

Copyright (C) 2013 Salvatore Sanfilippo <antirez@gmail.com>

Constants

HASH_SLOTS
XMODEM_CRC16_LOOKUP

Public Instance Methods

convert(key) click to toggle source

Convert key into slot.

@param key [String] the key of the redis command

@return [Integer] slot number

# File lib/redis/cluster/key_slot_converter.rb, line 62
def convert(key)
  crc = 0
  key.each_byte do |b|
    crc = ((crc << 8) & 0xffff) ^ XMODEM_CRC16_LOOKUP[((crc >> 8) ^ b) & 0xff]
  end

  crc % HASH_SLOTS
end