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)
16 def do_bloom(path, outfilename):
20 outfilename = os.path.join(path, 'bup.bloom')
23 if os.path.exists(outfilename) and not opt.force:
24 b = bloom.ShaBloom(outfilename)
26 debug1("bloom: Existing invalid bloom found, regenerating.\n")
33 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
34 progress('bloom: counting: %d\r' % i)
35 ix = git.open_idx(name)
36 ixbase = os.path.basename(name)
37 if b and (ixbase in b.idxnames):
43 total = add_count + rest_count
46 debug1("bloom: nothing to do.\n")
50 if len(b) != rest_count:
51 log("bloom: size %d != idx total %d, regenerating\n"
52 % (len(b), rest_count))
54 elif (b.bits < bloom.MAX_BLOOM_BITS and
55 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
56 log("bloom: regenerating: adding %d entries gives "
57 "%.2f%% false positives.\n"
58 % (add_count, b.pfalse_positive(add_count)))
61 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
62 if not b: # Need all idxs to build from scratch
64 add_count += rest_count
68 msg = b is None and 'creating from' or 'adding'
69 if not _first: _first = path
70 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
71 log('bloom: %s%s %d file%s (%d object%s).\n'
73 len(add), len(add)!=1 and 's' or '',
74 add_count, add_count!=1 and 's' or ''))
78 tfname = os.path.join(path, 'bup.tmp.bloom')
79 tf = open(tfname, 'w+')
80 b = bloom.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
84 ix = git.open_idx(name)
85 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
86 % (icount*100.0/add_count, icount, add_count))
92 os.rename(tfname, outfilename)
97 o = options.Options(optspec)
98 (opt, flags, extra) = o.parse(sys.argv[1:])
101 o.fatal('no positional parameters expected')
103 if opt.k and opt.k not in (4,5):
104 o.fatal('only k values of 4 and 5 are supported')
106 git.check_repo_or_die()
108 paths = opt.dir and [opt.dir] or git.all_packdirs()
110 debug1('bloom: scanning %s\n' % path)
111 do_bloom(path, opt.output)