Skip to content

Instantly share code, notes, and snippets.

@alexmoore
Last active December 16, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmoore/5504173 to your computer and use it in GitHub Desktop.
Save alexmoore/5504173 to your computer and use it in GitHub Desktop.
Little helper class to figure out on which vnodes a particular hash resides.
class VNodeHelper
attr_reader :ring_size, :n_val
def initialize(ring_size, n_val)
@ring_size = ring_size
@n_val = n_val
@vnode_size = (2 ** 160 / @ring_size)
end
def get_vnode_addresses(hash)
home_vnode = get_home_vnode_index(hash)
(0...@n_val).map { |i| ((home_vnode + i) * @vnode_size) % 2 ** 160 }
end
def get_home_vnode_index(hash)
(hash / @vnode_size) + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment