class Redis::Cluster::Slot

Keep slot and node key map for Redis Cluster Client

Constants

ROLE_SLAVE

Public Class Methods

new(available_slots, node_flags = {}, with_replica = false) click to toggle source
# File lib/redis/cluster/slot.rb, line 10
def initialize(available_slots, node_flags = {}, with_replica = false)
  @with_replica = with_replica
  @node_flags = node_flags
  @map = build_slot_node_key_map(available_slots)
end

Public Instance Methods

exists?(slot) click to toggle source
# File lib/redis/cluster/slot.rb, line 16
def exists?(slot)
  @map.key?(slot)
end
find_node_key_of_master(slot) click to toggle source
# File lib/redis/cluster/slot.rb, line 20
def find_node_key_of_master(slot)
  return nil unless exists?(slot)

  @map[slot][:master]
end
find_node_key_of_slave(slot) click to toggle source
# File lib/redis/cluster/slot.rb, line 26
def find_node_key_of_slave(slot)
  return nil unless exists?(slot)
  return find_node_key_of_master(slot) if replica_disabled?

  @map[slot][:slaves].to_a.sample
end
put(slot, node_key) click to toggle source
# File lib/redis/cluster/slot.rb, line 33
def put(slot, node_key)
  assign_node_key(@map, slot, node_key)
  nil
end

Private Instance Methods

assign_node_key(mappings, slot, node_key) click to toggle source
# File lib/redis/cluster/slot.rb, line 58
def assign_node_key(mappings, slot, node_key)
  mappings[slot] ||= { master: nil, slaves: ::Set.new }
  if master?(node_key)
    mappings[slot][:master] = node_key
  else
    mappings[slot][:slaves].add(node_key)
  end
end
build_slot_node_key_map(available_slots) click to toggle source
# File lib/redis/cluster/slot.rb, line 52
def build_slot_node_key_map(available_slots)
  available_slots.each_with_object({}) do |(node_key, slots), acc|
    slots.each { |slot| assign_node_key(acc, slot, node_key) }
  end
end
master?(node_key) click to toggle source
# File lib/redis/cluster/slot.rb, line 44
def master?(node_key)
  !slave?(node_key)
end
replica_disabled?() click to toggle source
# File lib/redis/cluster/slot.rb, line 40
def replica_disabled?
  !@with_replica
end
slave?(node_key) click to toggle source
# File lib/redis/cluster/slot.rb, line 48
def slave?(node_key)
  @node_flags[node_key] == ROLE_SLAVE
end