【问题标题】:BloomFilter PythonBloomFilter Python
【发布时间】:2012-02-20 01:00:55
【问题描述】:

我是 python 新手,正在尝试基于 Bit torrent BEP 33 创建一个bloomFilter。 我已经创建了 Bloom Filter,但它并不是我想要的。这就是我需要的,我还没有完全理解这种情况。如果这里有人可以解释...

//fixed parameters

k = 2

m = 256*8

//the filter
byte[m/8] bloom   ## What is this part?

function insertIP(byte[] ip) {

    byte[20] hash = sha1(ip)

    int index1 = hash[0] | hash[1] << 8
    int index2 = hash[2] | hash[3] << 8

    // truncate index to m (11 bits required)
    index1 %= m  ## ?
    index2 %= m  ## ?

    // set bits at index1 and index2
    bloom[index1 / 8] |= 0x01 << index1 % 8   ## ??
    bloom[index2 / 8] |= 0x01 << index2 % 8   ## ??
 }

 // insert IP 192.168.1.1 into the filter:
 insertIP(byte[4] {192,168,1,1})

这就是我创建的

import hashlib
m = 2048
def hashes(s):
    index = [0, 0]
    #for c in s:
        #o = ord(c)
    index[0] = hashlib.sha224(index[0]).hexdigest ## This needs integer hash
    index[1] = hashlib.sha224(index[1]).hexdigest ## same as above 

    return [x % m for x in index]

class BloomFilter(object):
    def __init__(self):
        self.bitarray = [0] * m

    def add(self, s):
        for x in hashes(s):
            self.bitarray[x] = 1
        #print self.bitarray
    def query(self, s):
        return all(self.bitarray[x] == 1 for x in hashes(s))

shazib=BloomFilter()
shazib.add('192.168.0.1')
print shazib.query('192.168.0.1')

【问题讨论】:

  • 我已经在上面的代码里面写了。如何在我的代码中添加此过滤器字节 [m/8] 布隆?请参阅上面代码中的 cmets 我创建的较低代码。

标签: python bloom-filter


【解决方案1】:

首先,对代码的解释……

//fixed parameters

k = 2

这对我来说是最莫名其妙的一句话; k 根本没有使用...

m = 256*8

这是 256 字节中的位数。

//the filter
byte[m/8] bloom   ## What is this part?

bloom 是一个 256 字节的数组,即 256 * 8 位,即m 位。 bloom 中的每个位都将包含有关过滤器中的值的信息。

function insertIP(byte[] ip) {

    byte[20] hash = sha1(ip)

这会创建一个 ip 的 20 字节哈希。

    int index1 = hash[0] | hash[1] << 8
    int index2 = hash[2] | hash[3] << 8

这两行根据哈希计算两个索引到bloom。基本上,index1hash 的前两个字节的串联,index2hash 的后两个字节的串联。

    // truncate index to m (11 bits required)
    index1 %= m  ## ?
    index2 %= m  ## ?

这两行截断了这些值,这样它们就不会超出bloom 的可能索引范围。 % 是 mod 运算符;它返回除法后的余数。 (17 % 4 = 1、22 % 5 = 2 等等。)还记得布隆是 256 * 8 位长吗? 11 位允许我们编码 2 ** 11 个可能的索引,即 2048 个值,即 256 * 8 个值。

    // set bits at index1 and index2
    bloom[index1 / 8] |= 0x01 << index1 % 8   ## ??
    bloom[index2 / 8] |= 0x01 << index2 % 8   ## ??

我们将bloom 视为一个位数组,因此我们必须进行一些位旋转才能访问正确的位。首先,将indexA 除以 8 以获得正确的字节,然后使用 % 运算符截断 indexA 以获得该字节中的正确位。

}

// insert IP 192.168.1.1 into the filter:
insertIP(byte[4] {192,168,1,1})

瞧,我们有一个布隆过滤器。如果你按位打印出来,它会是这样的:

data->    001011000101110011000001001000100...

indices-> 000000000011111111112222222222333...
          012345678901234567890123456789012...

如果一个特定的 ip 在散列时生成5index19index2,那么它将被视为“在”过滤器中,因为索引5 处的位和9 设置为1。当然,可能存在误报,因为多个不同的值可能导致相同的索引;但不能有假阴性。

import hashlib
m = 2048
def hashes(s):
    index = [0, 0]
    #for c in s:
        #o = ord(c)
    index[0] = hashlib.sha224(index[0]).hexdigest ## This needs integer hash
    index[1] = hashlib.sha224(index[1]).hexdigest ## same as above 

这是您的第一个问题。 index[0]index[1] 需要是整数。此外,hashlib.sha224(index[0]).hexdigest 返回一个方法。您必须调用该方法才能从中获取任何信息,例如:hashlib.sha224(index[0]).hexdigest()。此外,如果您希望它以与上述代码相同的方式工作,您可以将哈希转换为 int(您可以使用 int(x, 16) 将十六进制字符串转换为整数),然后使用提取前两个字节&amp; 65535,然后使用&gt;&gt; 16 将其移动两个字节,然后再次使用&amp; 65535 提取这两个字节。一旦你得到了正确的,其余的工作。

    return [x % m for x in index]

class BloomFilter(object):
    def __init__(self):
        self.bitarray = [0] * m

    def add(self, s):
        for x in hashes(s):
            self.bitarray[x] = 1
        #print self.bitarray
    def query(self, s):
        return all(self.bitarray[x] == 1 for x in hashes(s))

shazib=BloomFilter()
shazib.add('192.168.0.1')
print shazib.query('192.168.0.1')

【讨论】:

  • 想多了,下面更接近原来提取前四个字节的方法。我不确定哪个更快。 hbyte = hashlib.sha224('fdsa').digest() 然后是 index[0] = ord(hbyte[-1]) | ord(hbyte[-2]) &lt;&lt; 8index[1] = ord(hbyte[-3]) | ord(hbyte[-4]) &lt;&lt; 8
  • 另外,仅供参考,hashlib 模块也有一个 sha1 函数,如果你想将一个 ip 转换为一个整数序列,你可以这样做:[int(x) for x in '192.168.1.1'.split('.')]
猜你喜欢
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多