Python 布隆过滤器实现

# -*- coding:utf-8 -*-
# @Author: YOYO
# @Time: 2018/9/11 21:41
# @说明:


import mmh3
import redis


BIT_SIZE = 5000000
SEEDS = [50, 51, 52, 53, 54, 55, 56]


def get_redis(host='localhost', port=6379, db=0):
    return redis.Redis(host=host, port=port, db=db)


class BloomFilter(object):

    def __init__(self, key='bloomfilter'):
        self.db = get_redis()
        self.key = key

    def cal_offsets(self, content):
        return [mmh3.hash(content, seed) % BIT_SIZE for seed in SEEDS]

    def is_contains(self, content):
        if not content:
            return False
        locs = self.cal_offsets(content)

        return all(True if self.db.getbit(self.key, loc) else False for loc in locs)

    def insert(self, content):
        locs = self.cal_offsets(content)
        for loc in locs:
            self.db.setbit(self.key, loc, 1)


if __name__ == '__main__':
    bloom_filter = BloomFilter()

    test_url = 'https://douban.com'

    print 'before'
    if bloom_filter.is_contains(test_url):
        print test_url + ' is existed'
    else:
        print test_url + ' is not existed'

    bloom_filter.insert(test_url)

    print 'after'
    if bloom_filter.is_contains(test_url):
        print test_url + ' is existed'
    else:
        print test_url + ' is not existed'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值