2 import sys, glob, tempfile
3 from bup import options, git, bloom
4 from bup.helpers import *
9 f,force ignore existing bloom file and regenerate it from scratch
10 o,output= output bloom filename (default: auto)
11 d,dir= input directory to look for idx files (default: auto)
12 k,hashes= number of hash functions to use (4 or 5) (default: auto)
13 c,check= check the given .idx file against the bloom filter
16 def check_bloom(path, bloomfilename, idx):
17 rbloomfilename = git.repo_rel(bloomfilename)
18 ridx = git.repo_rel(idx)
19 if not os.path.exists(bloomfilename):
20 log("bloom: %s: does not exist.\n" % rbloomfilename)
22 b = bloom.ShaBloom(bloomfilename)
24 add_error("bloom: %r is invalid.\n" % rbloomfilename)
26 base = os.path.basename(idx)
27 if base not in b.idxnames:
28 log("bloom: %s does not contain the idx.\n" % rbloomfilename)
31 idx = os.path.join(path, idx)
32 log("bloom: bloom file: %s\n" % rbloomfilename)
33 log("bloom: checking %s\n" % ridx)
34 for objsha in git.open_idx(idx):
35 if not b.exists(objsha):
36 add_error("bloom: ERROR: object %s missing"
37 % str(objsha).encode('hex'))
41 def do_bloom(path, outfilename):
44 if os.path.exists(outfilename) and not opt.force:
45 b = bloom.ShaBloom(outfilename)
47 debug1("bloom: Existing invalid bloom found, regenerating.\n")
54 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
55 progress('bloom: counting: %d\r' % i)
56 ix = git.open_idx(name)
57 ixbase = os.path.basename(name)
58 if b and (ixbase in b.idxnames):
64 total = add_count + rest_count
67 debug1("bloom: nothing to do.\n")
71 if len(b) != rest_count:
72 log("bloom: size %d != idx total %d, regenerating\n"
73 % (len(b), rest_count))
75 elif (b.bits < bloom.MAX_BLOOM_BITS and
76 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
77 log("bloom: regenerating: adding %d entries gives "
78 "%.2f%% false positives.\n"
79 % (add_count, b.pfalse_positive(add_count)))
82 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
83 if not b: # Need all idxs to build from scratch
85 add_count += rest_count
89 msg = b is None and 'creating from' or 'adding'
90 if not _first: _first = path
91 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
92 log('bloom: %s%s %d file%s (%d object%s).\n'
94 len(add), len(add)!=1 and 's' or '',
95 add_count, add_count!=1 and 's' or ''))
99 tfname = os.path.join(path, 'bup.tmp.bloom')
100 tf = open(tfname, 'w+')
101 b = bloom.create(tfname, f=tf, expected=add_count, k=opt.k)
105 ix = git.open_idx(name)
106 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
107 % (icount*100.0/add_count, icount, add_count))
113 os.rename(tfname, outfilename)
118 o = options.Options(optspec)
119 (opt, flags, extra) = o.parse(sys.argv[1:])
122 o.fatal('no positional parameters expected')
124 git.check_repo_or_die()
126 if not opt.check and opt.k and opt.k not in (4,5):
127 o.fatal('only k values of 4 and 5 are supported')
129 paths = opt.dir and [opt.dir] or git.all_packdirs()
131 debug1('bloom: scanning %s\n' % path)
132 outfilename = opt.output or os.path.join(path, 'bup.bloom')
134 check_bloom(path, outfilename, opt.check)
136 do_bloom(path, outfilename)
139 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
142 log('all tests passed.\n')