Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/redis/crc.py: 45%

11 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:16 +0000

1from binascii import crc_hqx 

2 

3from redis.typing import EncodedT 

4 

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 

8 

9__all__ = ["key_slot", "REDIS_CLUSTER_HASH_SLOTS"] 

10 

11 

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 start = key.find(b"{") 

19 if start > -1: 

20 end = key.find(b"}", start + 1) 

21 if end > -1 and end != start + 1: 

22 key = key[start + 1 : end] 

23 return crc_hqx(key, 0) % bucket