2 import sys, glob, tempfile
3 from bup import options, git, bloom
4 from bup.helpers import *
9 ruin ruin the specified bloom file (clearing the bitfield)
10 f,force ignore existing bloom file and regenerate it from scratch
11 o,output= output bloom filename (default: auto)
12 d,dir= input directory to look for idx files (default: auto)
13 k,hashes= number of hash functions to use (4 or 5) (default: auto)
14 c,check= check the given .idx file against the bloom filter
18 def ruin_bloom(bloomfilename):
19 rbloomfilename = git.repo_rel(bloomfilename)
20 if not os.path.exists(bloomfilename):
21 log("%s\n" % bloomfilename)
22 add_error("bloom: %s not found to ruin\n" % rbloomfilename)
24 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
25 b.map[16:16+2**b.bits] = '\0' * 2**b.bits
28 def check_bloom(path, bloomfilename, idx):
29 rbloomfilename = git.repo_rel(bloomfilename)
30 ridx = git.repo_rel(idx)
31 if not os.path.exists(bloomfilename):
32 log("bloom: %s: does not exist.\n" % rbloomfilename)
34 b = bloom.ShaBloom(bloomfilename)
36 add_error("bloom: %r is invalid.\n" % rbloomfilename)
38 base = os.path.basename(idx)
39 if base not in b.idxnames:
40 log("bloom: %s does not contain the idx.\n" % rbloomfilename)
43 idx = os.path.join(path, idx)
44 log("bloom: bloom file: %s\n" % rbloomfilename)
45 log("bloom: checking %s\n" % ridx)
46 for objsha in git.open_idx(idx):
47 if not b.exists(objsha):
48 add_error("bloom: ERROR: object %s missing"
49 % str(objsha).encode('hex'))
53 def do_bloom(path, outfilename):
56 if os.path.exists(outfilename) and not opt.force:
57 b = bloom.ShaBloom(outfilename)
59 debug1("bloom: Existing invalid bloom found, regenerating.\n")
66 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
67 progress('bloom: counting: %d\r' % i)
68 ix = git.open_idx(name)
69 ixbase = os.path.basename(name)
70 if b and (ixbase in b.idxnames):
76 total = add_count + rest_count
79 debug1("bloom: nothing to do.\n")
83 if len(b) != rest_count:
84 debug1("bloom: size %d != idx total %d, regenerating\n"
85 % (len(b), rest_count))
87 elif (b.bits < bloom.MAX_BLOOM_BITS and
88 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
89 debug1("bloom: regenerating: adding %d entries gives "
90 "%.2f%% false positives.\n"
91 % (add_count, b.pfalse_positive(add_count)))
94 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
95 if not b: # Need all idxs to build from scratch
97 add_count += rest_count
101 msg = b is None and 'creating from' or 'adding'
102 if not _first: _first = path
103 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
104 progress('bloom: %s%s %d file%s (%d object%s).\n'
106 len(add), len(add)!=1 and 's' or '',
107 add_count, add_count!=1 and 's' or ''))
111 tfname = os.path.join(path, 'bup.tmp.bloom')
112 b = bloom.create(tfname, expected=add_count, k=opt.k)
116 ix = git.open_idx(name)
117 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
118 % (icount*100.0/add_count, icount, add_count))
123 # Currently, there's an open file object for tfname inside b.
124 # Make sure it's closed before rename.
128 os.rename(tfname, outfilename)
133 o = options.Options(optspec)
134 (opt, flags, extra) = o.parse(sys.argv[1:])
137 o.fatal('no positional parameters expected')
139 git.check_repo_or_die()
141 if not opt.check and opt.k and opt.k not in (4,5):
142 o.fatal('only k values of 4 and 5 are supported')
144 paths = opt.dir and [opt.dir] or git.all_packdirs()
146 debug1('bloom: scanning %s\n' % path)
147 outfilename = opt.output or os.path.join(path, 'bup.bloom')
149 check_bloom(path, outfilename, opt.check)
151 ruin_bloom(outfilename)
153 do_bloom(path, outfilename)
156 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
159 log('All tests passed.\n')