布隆过滤器

三世轮回 提交于 2019-12-09 18:35:03
from .defaults import BLOOMFILTER_BIT, BLOOMFILTER_HASH_NUMBERclass HashMap(object):    def __init__(self, m, seed):        self.m = m        self.seed = seed        def hash(self, value):        """        Hash Algorithm        :param value: Value        :return: Hash Value        """        ret = 0        for i in range(len(value)):            ret += self.seed * ret + ord(value[i])        return (self.m - 1) & retclass BloomFilter(object):    def __init__(self, server, key, bit=BLOOMFILTER_BIT, hash_number=BLOOMFILTER_HASH_NUMBER):        """        Initialize BloomFilter        :param server: Redis Server        :param key: BloomFilter Key        :param bit: m = 2 ^ bit        :param hash_number: the number of hash function        """        # default to 1 << 30 = 10,7374,1824 = 2^30 = 128MB, max filter 2^30/hash_number = 1,7895,6970 fingerprints        self.m = 1 << bit        self.seeds = range(hash_number)        self.server = server        self.key = key        self.maps = [HashMap(self.m, seed) for seed in self.seeds]        def exists(self, value):        """        if value exists        :param value:        :return:        """        if not value:            return False        exist = True        for map in self.maps:            offset = map.hash(value)            exist = exist & self.server.getbit(self.key, offset)        return exist        def insert(self, value):        """        add value to bloom        :param value:        :return:        """        for f in self.maps:            offset = f.hash(value)            self.server.setbit(self.key, offset, 1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!