lib/consistent_hashr.rb in consistent_hashr-1.0.0 vs lib/consistent_hashr.rb in consistent_hashr-1.1.0
- old
+ new
@@ -1,35 +1,27 @@
require 'zlib'
+module ConsistentHashr
-class ConsistentHashr
+ @circle = {}
+ @number_of_replicas = 20
##
- # Creates a new Hasher. Servers should be a hash.
- def initialize(_servers = {}, _number_of_replicas = 50)
- @circle = {}
- @number_of_replicas = _number_of_replicas
- _servers.each do |name, server|
- add_server(name, server)
- end
- end
-
- ##
# Computes a key
- def hash_key(key)
+ def self.hash_key(key)
Zlib.crc32("#{key}")
end
##
# Adds a server to the circle
- def add_server(_name, _server)
+ def self.add_server(_name, _server)
@number_of_replicas.times do |t|
@circle[hash_key("#{_name}+#{t}")] = _server
end
end
##
# Returns the server for the provided key
- def get(key)
+ def self.get(key)
return nil if @circle.empty?
return @circle.first.last if @circle.size == 1
hash = hash_key(key)
\ No newline at end of file