Last active
July 5, 2023 12:29
-
-
Save yunazuno/f98c0bb176c9bf4934511dac60d7ea0b to your computer and use it in GitHub Desktop.
Maglev Hashing with Python https://research.google.com/pubs/pub44824.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
class MaglevHash(object): | |
def __init__(self, m=65537): | |
self.m = m | |
self.permutation = {} | |
self.entry = [None] * self.m | |
def add(self, node): | |
if node in self.permutation.keys(): | |
raise KeyError("Node already exist") | |
permutation = self._generate_permutation(node) | |
self.permutation[node] = permutation | |
def remove(self, node): | |
if node not in self.permutation.keys(): | |
raise KeyError("Node not found") | |
del self.permutation[node] | |
def populate(self): | |
entry = self._populate_entry() | |
distance = [ | |
a == b for a, b in zip(self.entry, entry)].count(False) | |
self.entry = entry | |
return distance | |
def lookup(self, key): | |
index_hash = int(hashlib.sha256( | |
"{}:lookup".format(key).encode("ascii")).hexdigest(), 16) | |
index = index_hash % self.m | |
return self.entry[index] | |
def _generate_permutation(self, node): | |
offset_hash = int(hashlib.sha256( | |
"{}:offset".format(node).encode("ascii")).hexdigest(), 16) | |
offset = offset_hash % self.m | |
skip_hash = int(hashlib.sha256( | |
"{}:skip".format(node).encode("ascii")).hexdigest(), 16) | |
skip = (skip_hash % (self.m - 1)) + 1 | |
permutation = [None] * self.m | |
for j in range(self.m): | |
permutation[j] = (offset + j * skip) % self.m | |
return permutation | |
def _populate_entry(self): | |
if not self.permutation: | |
return [None] * self.m, self.m | |
next_tracker = dict.fromkeys(self.permutation.keys(), 0) | |
entry = [None] * self.m | |
n = 0 | |
while True: | |
for node in sorted(self.permutation.keys()): | |
c = self.permutation[node][next_tracker[node]] | |
while entry[c] is not None: | |
next_tracker[node] += 1 | |
c = self.permutation[node][next_tracker[node]] | |
entry[c] = node | |
next_tracker[node] += 1 | |
n += 1 | |
if n == self.m: | |
return entry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I came across this implementation of maglev hashing and want to use it for some private research. However, I would be able to use it only if it is licensed as open source (e.g Apache, MIT etc.). Would You be able mention the license somewhere in comments?