Class: ConsistentCluster::ConsistentHashing::Ring
- Inherits:
-
Object
- Object
- ConsistentCluster::ConsistentHashing::Ring
- Defined in:
- lib/consistent-cluster/consistent_hashing/ring.rb
Overview
Public: the hash ring containing all configured nodes
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Public: adds a new node into the hash ring like ‘add` but returns a reference to the ring to be used as a fluent interface.
-
#add(node) ⇒ Object
Public: adds a new node into the hash ring.
-
#delete(node) ⇒ Object
Public: removes a node from the hash ring.
-
#initialize(nodes = [], replicas = 3) ⇒ Ring
constructor
Public: returns a new ring object.
-
#length ⇒ Object
Public: returns the (virtual) points in the hash ring.
-
#node_for(key) ⇒ Object
Public: gets the node where to store the key.
-
#nodes ⇒ Object
Public: get all nodes in the ring.
-
#point_for(key) ⇒ Object
Public: gets the point for an arbitrary key.
-
#points ⇒ Object
Public: gets all points in the ring.
Constructor Details
#initialize(nodes = [], replicas = 3) ⇒ Ring
Public: returns a new ring object
14 15 16 17 18 19 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 14 def initialize(nodes = [], replicas = 3) @replicas = replicas @ring = RedBlackTree.new nodes.each { |node| add(node) } end |
Instance Method Details
#<<(node) ⇒ Object
Public: adds a new node into the hash ring like ‘add` but returns a reference to the ring to be used as a fluent interface
42 43 44 45 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 42 def <<(node) add(node) self end |
#add(node) ⇒ Object
Public: adds a new node into the hash ring
30 31 32 33 34 35 36 37 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 30 def add(node) @replicas.times do |i| # generate the key of this (virtual) point in the hash key = hash_key(node, i) @ring[key] = VirtualPoint.new(node, key) end end |
#delete(node) ⇒ Object
Public: removes a node from the hash ring
49 50 51 52 53 54 55 56 57 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 49 def delete(node) @replicas.times do |i| key = hash_key(node, i) @ring.delete key end self end |
#length ⇒ Object
Public: returns the (virtual) points in the hash ring
Returns: a Fixnum
24 25 26 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 24 def length @ring.length end |
#node_for(key) ⇒ Object
Public: gets the node where to store the key
Returns: the node Object
73 74 75 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 73 def node_for(key) point_for(key).node end |
#nodes ⇒ Object
Public: get all nodes in the ring
Returns: an Array of the nodes in the ring
80 81 82 83 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 80 def nodes nodes = points.map { |point| point.node } nodes.uniq end |
#point_for(key) ⇒ Object
Public: gets the point for an arbitrary key
62 63 64 65 66 67 68 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 62 def point_for(key) return nil if @ring.empty? key = hash_key(key) _, value = @ring.next_gte_pair(key) _, value = @ring.minimum_pair unless value value end |
#points ⇒ Object
Public: gets all points in the ring
Returns: an Array of the points in the ring
88 89 90 |
# File 'lib/consistent-cluster/consistent_hashing/ring.rb', line 88 def points @ring.map { |point| point[1] } end |