18 lines
487 B
Python
18 lines
487 B
Python
import binascii
|
|
|
|
|
|
def _consistent_hash(value, ring_size):
|
|
"""
|
|
Maps the value to a node value between 0 and 4095
|
|
using CRC, then down to one of the ring nodes.
|
|
"""
|
|
if ring_size == 1:
|
|
# Avoid the overhead of hashing and modulo when it is unnecessary.
|
|
return 0
|
|
|
|
if isinstance(value, str):
|
|
value = value.encode("utf8")
|
|
bigval = binascii.crc32(value) & 0xFFF
|
|
ring_divisor = 4096 / float(ring_size)
|
|
return int(bigval / ring_divisor)
|