Class: ConsistentHashing::Continuum

Inherits:
Object
  • Object
show all
Defined in:
lib/consistent_hashing.rb

Constant Summary collapse

DEFAULT_ANCHORS_PER_SERVER =
40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers, anchors_per_server = DEFAULT_ANCHORS_PER_SERVER) ⇒ Continuum

Returns a new instance of Continuum.



35
36
37
38
39
40
# File 'lib/consistent_hashing.rb', line 35

def initialize(servers, anchors_per_server=DEFAULT_ANCHORS_PER_SERVER)
  @servers = servers
  @anchors_per_server = anchors_per_server
  @hasher = JenkinsOneAtATime
  create_all_anchors!
end

Instance Attribute Details

#anchorsObject

Returns the value of attribute anchors.



28
29
30
# File 'lib/consistent_hashing.rb', line 28

def anchors
  @anchors
end

#anchors_per_serverObject

Returns the value of attribute anchors_per_server.



28
29
30
# File 'lib/consistent_hashing.rb', line 28

def anchors_per_server
  @anchors_per_server
end

#hasherObject

Returns the value of attribute hasher.



28
29
30
# File 'lib/consistent_hashing.rb', line 28

def hasher
  @hasher
end

#serversObject

Returns the value of attribute servers.



28
29
30
# File 'lib/consistent_hashing.rb', line 28

def servers
  @servers
end

Instance Method Details

#expected_loadObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/consistent_hashing.rb', line 50

def expected_load
  point = 0
  address_to_load = Hash.new(0)
  
  anchors.each do |anchor|
    address_to_load[anchor.server.address] += (anchor.value - point)
    point = anchor.value
  end
  
  address_to_load[anchors.first.server.address] += (1.0 - point)
  address_to_load
end

#find_server(search_value) ⇒ Object



42
43
44
# File 'lib/consistent_hashing.rb', line 42

def find_server(search_value)
  closest_anchor(search_value).server
end

#hash_to_unit_circle(key) ⇒ Object



46
47
48
# File 'lib/consistent_hashing.rb', line 46

def hash_to_unit_circle(key)
  hasher.hash_to_unit_circle(key)
end

#to_sObject



63
64
65
# File 'lib/consistent_hashing.rb', line 63

def to_s
  anchors.map{|p| p.to_s}.join("\n")
end