]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
Add a --check behavior to verify bloom
[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-generated)
10 d,dir=     input directory to look for idx files (default: auto-generated)
11 k,hashes=  number of hash functions to use (4 or 5) (default: auto-generated)
12 c,check=   an idx file to check against an existing bloom filter
13 """
14
15 def check_bloom(path, bloomfilename, idx):
16     if not os.path.exists(bloomfilename):
17         log("bloom: %s not found to check\n" % bloomfilename)
18         return
19     b = git.ShaBloom(bloomfilename)
20     if not b.valid():
21         log("bloom: %s could not be opened to check\n" % bloomfilename)
22         return
23     base = os.path.basename(idx)
24     if base not in b.idxnames:
25         log("bloom: filter does not contain %s, nothing to check\n" % idx)
26         return
27     if base == idx:
28         idx = os.path.join(path, idx)
29     log("bloom: checking %s" % idx)
30     for objsha in git.open_idx(idx):
31         if not b.exists(objsha):
32             add_error("bloom: ERROR: %s missing from bloom" 
33                       % str(objsha).encode('hex'))
34
35
36 def do_bloom(path, outfilename):
37     b = None
38     if os.path.exists(outfilename):
39         b = git.ShaBloom(outfilename)
40         if not b.valid():
41             debug1("bloom: Existing invalid bloom found, regenerating.\n")
42             b = None
43
44     add = []
45     rest = []
46     add_count = 0
47     rest_count = 0
48     for name in glob.glob('%s/*.idx' % path):
49         ix = git.open_idx(name)
50         ixbase = os.path.basename(name)
51         if b and (ixbase in b.idxnames):
52             rest.append(name)
53             rest_count += len(ix)
54         else:
55             add.append(name)
56             add_count += len(ix)
57     total = add_count + rest_count
58
59     if not add:
60         log("bloom: Nothing to do\n")
61         return
62
63     if b:
64         if len(b) != rest_count:
65             log("bloom: size %d != idx total %d, regenerating\n"
66                     % (len(b), rest_count))
67             b = None
68         elif (b.bits < git.MAX_BLOOM_BITS and
69               b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE):
70             log("bloom: %d more entries => %.2f false positive, regenerating\n"
71                     % (add_count, b.pfalse_positive(add_count)))
72             b = None
73         else:
74             b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
75     if not b: # Need all idxs to build from scratch
76         add += rest
77         add_count += rest_count
78     del rest
79     del rest_count
80
81     msg = b is None and 'creating from' or 'adding'
82     log('bloom: %s %d file%s (%d object%s).\n' % (msg, len(add),
83                                                   len(add)!=1 and 's' or '',
84                                                   add_count,
85                                                   add_count!=1 and 's' or ''))
86
87     tfname = None
88     if b is None:
89         tfname = os.path.join(path, 'bup.tmp.bloom')
90         tf = open(tfname, 'w+')
91         b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
92     count = 0
93     for name in add:
94         ix = git.open_idx(name)
95         progress('Writing bloom: %d/%d\r' % (count, len(add)))
96         b.add_idx(ix)
97         count += 1
98     log('Writing bloom: %d/%d, done.\n' % (count, len(add)))
99
100     if tfname:
101         os.rename(tfname, outfilename)
102
103
104 handle_ctrl_c()
105
106 o = options.Options(optspec)
107 (opt, flags, extra) = o.parse(sys.argv[1:])
108
109 if extra:
110     o.fatal('no positional parameters expected')
111
112 git.check_repo_or_die()
113 bloompath = opt.dir or git.repo('objects/pack')
114
115 if not opt.output:
116     assert(bloompath)
117 outfilename = opt.output or os.path.join(bloompath, 'bup.bloom')
118
119 if opt.check:
120     check_bloom(bloompath, outfilename, opt.check)
121 else:
122     if opt.k and opt.k not in (4,5):
123         o.fatal('only k values of 4 and 5 are supported')
124
125     do_bloom(bloompath, outfilename)
126
127 if saved_errors:
128     log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
129     sys.exit(1)
130