File: rendezvous.py

package info (click to toggle)
python-pymemcache 4.0.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: python: 4,550; makefile: 209; sh: 11
file content (46 lines) | stat: -rw-r--r-- 1,273 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
from pymemcache.client.murmur3 import murmur3_32


class RendezvousHash:
    """
    Implements the Highest Random Weight (HRW) hashing algorithm most
    commonly referred to as rendezvous hashing.

    Originally developed as part of python-clandestined.

    Copyright (c) 2014 Ernest W. Durbin III
    """

    def __init__(self, nodes=None, seed=0, hash_function=murmur3_32):
        """
        Constructor.
        """
        self.nodes = []
        self.seed = seed
        if nodes is not None:
            self.nodes = nodes
        self.hash_function = lambda x: hash_function(x, seed)

    def add_node(self, node):
        if node not in self.nodes:
            self.nodes.append(node)

    def remove_node(self, node):
        if node in self.nodes:
            self.nodes.remove(node)
        else:
            raise ValueError("No such node %s to remove" % (node))

    def get_node(self, key):
        high_score = -1
        winner = None

        for node in self.nodes:
            score = self.hash_function(f"{node}-{key}")

            if score > high_score:
                (high_score, winner) = (score, node)
            elif score == high_score:
                (high_score, winner) = (score, max(str(node), str(winner)))

        return winner