python基于redis的布隆过滤器示例
"""
布隆过滤器基础类
"""
import mmh3
from redis import Redis
import math
from typing import List, Union, Tuple
from loguru import logger
import hashlib
class BaseBloom:
# 内置种子,用于对值做哈希计算
SEEDS = [
543, 460, 171, 876, 796, 607, 650, 81, 837, 545, 591, 946, 846, 521, 913, 636, 878, 735, 414, 372,
344, 324, 223, 180, 327, 891, 798, 933, 493, 293, 836, 10, 6, 544, 924, 849, 438, 41, 862, 648, 338,
465, 562, 693, 979, 52, 763, 103, 387, 374, 349, 94, 384, 680, 574, 480, 307, 580, 71, 535, 300, 53,
481, 519, 644, 219, 686, 236, 424, 326, 244, 212, 909, 202, 951, 56, 812, 901, 926, 250, 507, 739, 371,
63, 584, 154, 7, 284, 617, 332, 472, 140, 605, 262, 355, 526, 647, 923, 199, 518
]
def __init__(
self,
capacity: int,
error_rate: float,
redis_conn: Redis,
piece_size_byte=1024 * 2, # 分片bitmap的大小
name: str = "bloom",
tag: str = "default"
):
self.capacity = capacity
self.error_rate = error_rate
self.redis_conn = redis_conn
self.piece_size_byte = piece_size_byte # bit map片的大小
self.piece_size_bit = piece_size_byte * 8
self.name = name
self.tag = tag
self._m = math.ceil(capacity * math.log2(math.e) * math.log2(1 / error_rate)) # 至少需要位数
self._k = math.ceil(math.log1p(2)