class Clandestined::RendezvousHash

Attributes

hash_function[R]
nodes[R]
seed[R]

Public Class Methods

new(nodes=nil, seed=0) click to toggle source
# File lib/clandestined/rendezvous_hash.rb, line 12
def initialize(nodes=nil, seed=0)
  @nodes = nodes || []
  @seed = seed

  @hash_function = lambda { |key| murmur3_32(key, seed) }
end

Public Instance Methods

add_node(node) click to toggle source
# File lib/clandestined/rendezvous_hash.rb, line 19
def add_node(node)
  @nodes.push(node) unless @nodes.include?(node)
end
find_node(key) click to toggle source
# File lib/clandestined/rendezvous_hash.rb, line 31
def find_node(key)
  high_score = -1
  winner = nil
  nodes.each do |node|
    score = hash_function.call("#{node}-#{key}")
    if score > high_score
      high_score, winner = score, node
    elsif score == high_score
      high_score, winner = score, [node.to_s, winner.to_s].max
    end
  end
  winner
end
remove_node(node) click to toggle source
# File lib/clandestined/rendezvous_hash.rb, line 23
def remove_node(node)
  if @nodes.include?(node)
    @nodes.delete(node)
  else
    raise ArgumentError, "No such node #{node} to remove"
  end
end