]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
ed148fce97f7bd8161dd544936129dfd5c407d3d
[bup.git] / cmd / bloom-cmd.py
1 #!/usr/bin/env python
2 import sys, glob, tempfile
3 from bup import options, git
4 from bup.helpers import *
5
6 optspec = """
7 bup bloom [options...]
8 --
9 o,output=  output bloom filename (default: auto)
10 d,dir=     input directory to look for idx files (default: auto)
11 k,hashes=  number of hash functions to use (4 or 5) (default: auto)
12 """
13
14 _first = None
15 def do_bloom(path, outfilename):
16     global _first
17     if not outfilename:
18         assert(path)
19         outfilename = os.path.join(path, 'bup.bloom')
20
21     b = None
22     if os.path.exists(outfilename):
23         b = git.ShaBloom(outfilename)
24         if not b.valid():
25             debug1("bloom: Existing invalid bloom found, regenerating.\n")
26             b = None
27
28     add = []
29     rest = []
30     add_count = 0
31     rest_count = 0
32     for i,name in enumerate(glob.glob('%s/*.idx' % path)):
33         progress('bloom: counting: %d\r' % i)
34         ix = git.open_idx(name)
35         ixbase = os.path.basename(name)
36         if b and (ixbase in b.idxnames):
37             rest.append(name)
38             rest_count += len(ix)
39         else:
40             add.append(name)
41             add_count += len(ix)
42     total = add_count + rest_count
43
44     if not add:
45         debug1("bloom: nothing to do.\n")
46         return
47
48     if b:
49         if len(b) != rest_count:
50             log("bloom: size %d != idx total %d, regenerating\n"
51                     % (len(b), rest_count))
52             b = None
53         elif (b.bits < git.MAX_BLOOM_BITS and
54               b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE):
55             log("bloom: regenerating: adding %d entries gives "
56                 "%.2f%% false positives.\n"
57                     % (add_count, b.pfalse_positive(add_count)))
58             b = None
59         else:
60             b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
61     if not b: # Need all idxs to build from scratch
62         add += rest
63         add_count += rest_count
64     del rest
65     del rest_count
66
67     msg = b is None and 'creating from' or 'adding'
68     if not _first: _first = path
69     dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
70     log('bloom: %s%s %d file%s (%d object%s).\n'
71         % (dirprefix, msg,
72            len(add), len(add)!=1 and 's' or '',
73            add_count, add_count!=1 and 's' or ''))
74
75     tfname = None
76     if b is None:
77         tfname = os.path.join(path, 'bup.tmp.bloom')
78         tf = open(tfname, 'w+')
79         b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
80     count = 0
81     icount = 0
82     for name in add:
83         ix = git.open_idx(name)
84         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
85                   % (icount*100.0/add_count, icount, add_count))
86         b.add_idx(ix)
87         count += 1
88         icount += len(ix)
89
90     if tfname:
91         os.rename(tfname, outfilename)
92
93
94 handle_ctrl_c()
95
96 o = options.Options(optspec)
97 (opt, flags, extra) = o.parse(sys.argv[1:])
98
99 if extra:
100     o.fatal('no positional parameters expected')
101
102 if opt.k and opt.k not in (4,5):
103     o.fatal('only k values of 4 and 5 are supported')
104
105 git.check_repo_or_die()
106
107 paths = opt.dir and [opt.dir] or git.all_packdirs()
108 for path in paths:
109     debug1('bloom: scanning %s\n' % path)
110     do_bloom(path, opt.output)