+def create(name, expected, delaywrite=None, f=None, k=None):
+ """Create and return a bloom filter for `expected` entries."""
+ bits = int(math.floor(math.log(expected*MAX_BITS_EACH/8,2)))
+ k = k or ((bits <= MAX_BLOOM_BITS[5]) and 5 or 4)
+ if bits > MAX_BLOOM_BITS[k]:
+ log('bloom: warning, max bits exceeded, non-optimal\n')
+ bits = MAX_BLOOM_BITS[k]
+ debug1('bloom: using 2^%d bytes and %d hash functions\n' % (bits, k))
+ f = f or open(name, 'w+b')
+ f.write('BLOM')
+ f.write(struct.pack('!IHHI', BLOOM_VERSION, bits, k, 0))
+ assert(f.tell() == 16)
+ # NOTE: On some systems this will not extend+zerofill, but it does on
+ # darwin, linux, bsd and solaris.
+ f.truncate(16+2**bits)
+ f.seek(0)
+ if delaywrite != None and not delaywrite:
+ # tell it to expect very few objects, forcing a direct mmap
+ expected = 1
+ return ShaBloom(name, f=f, readwrite=True, expected=expected)
+
+
+def clear_bloom(dir):
+ unlink(os.path.join(dir, 'bup.bloom'))