]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/hash_ring.rb
3 attr_reader
:ring, :sorted_keys, :replicas, :nodes
4 # nodes is a list of objects that have a proper to_s representation.
5 # replicas indicates how many virtual points should be used pr. node,
6 # replicas are required to improve the distribution.
7 def initialize(nodes
=[], replicas
=3)
17 # Adds a `node` to the hash ring (including a number of replicas).
20 @replicas.times
do |i
|
21 key
= gen_key("#{node}:#{i}")
29 @replicas.times
do |i
|
30 key
= gen_key("#{node}:#{count}")
32 @sorted_keys.reject!
{|k
| k
== key
}
36 # get the node in the hash ring for this key
42 return [nil,nil] if @ring.size
== 0
45 nodes
.size
.times
do |i
|
48 return [@ring[node
], i
]
55 return [nil,nil] if @ring.size
== 0
56 node
, pos
= get_node_pos(key
)
57 @sorted_keys[pos
..-1].each
do |k
|
63 key
= Digest
::MD5.hexdigest(key
)
64 ((key
[3] << 24) | (key
[2] << 16) | (key
[1] << 8) | key
[0])
69 # ring = HashRing.new ['server1', 'server2', 'server3']
72 # p ring.get_node "kjhjkjlkjlkkh"