-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashring.py
More file actions
39 lines (30 loc) · 1.26 KB
/
hashring.py
File metadata and controls
39 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from sortedcontainers import SortedDict
import mmh3
class HashRing():
def __init__(self):
self.cur_nodes = SortedDict()
self.hash = lambda x : mmh3.hash(str(x))
#self.hash = lambda x : xxhash.xxh32(str(x).encode()).intdigest()
def __getitem__(self, key):
hash_value = self.hash(key)
# retrieve position on ring corresponding to hash digest
# modulus to retrieve first node if insertion index was equal to the length of the list
next_node_index = self.cur_nodes.bisect_left(hash_value) % len(self.cur_nodes)
next_node_hash = self.cur_nodes.keys()[next_node_index]
return self.cur_nodes[next_node_hash]
def __setitem__(self, key, val):
raise Exception("Can't set item, __setitem__ not implemented")
def __contains__(self, node):
hash_value = self.hash(node)
return hash_value in self.cur_nodes
def __len__(self):
return len(self.cur_nodes)
def __str__(self):
return str(self.cur_nodes)
def add_node(self, nodeID):
# TODO: Handle Collisions
hash_value = self.hash(nodeID)
self.cur_nodes[hash_value] = nodeID
def remove_node(self, nodeID):
hash_value = self.hash(nodeID)
del self.cur_nodes[hash_value]