Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/redis/crc.py: 38%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from binascii import crc_hqx
3from redis.typing import EncodedT
5# Redis Cluster's key space is divided into 16384 slots.
6# For more information see: https://github.com/redis/redis/issues/2576
7REDIS_CLUSTER_HASH_SLOTS = 16384
9__all__ = ["key_slot", "REDIS_CLUSTER_HASH_SLOTS"]
12def key_slot(key: EncodedT, bucket: int = REDIS_CLUSTER_HASH_SLOTS) -> int:
13 """Calculate key slot for a given key.
14 See Keys distribution model in https://redis.io/topics/cluster-spec
15 :param key - bytes
16 :param bucket - int
17 """
18 if isinstance(key, memoryview):
19 # The encoder passes memoryviews through, and they have no find().
20 key = key.tobytes()
21 start = key.find(b"{")
22 if start > -1:
23 end = key.find(b"}", start + 1)
24 if end > -1 and end != start + 1:
25 key = key[start + 1 : end]
26 return crc_hqx(key, 0) % bucket