]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
README: add 0.30.x to CI status table
[bup.git] / cmd / bloom-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 from __future__ import absolute_import
9 import glob, os, sys, tempfile
10
11 from bup import options, git, bloom
12 from bup.compat import argv_bytes, hexstr
13 from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
14                          saved_errors)
15 from bup.io import path_msg
16
17
18 optspec = """
19 bup bloom [options...]
20 --
21 ruin       ruin the specified bloom file (clearing the bitfield)
22 f,force    ignore existing bloom file and regenerate it from scratch
23 o,output=  output bloom filename (default: auto)
24 d,dir=     input directory to look for idx files (default: auto)
25 k,hashes=  number of hash functions to use (4 or 5) (default: auto)
26 c,check=   check the given .idx file against the bloom filter
27 """
28
29
30 def ruin_bloom(bloomfilename):
31     rbloomfilename = git.repo_rel(bloomfilename)
32     if not os.path.exists(bloomfilename):
33         log(path_msg(bloomfilename) + '\n')
34         add_error('bloom: %s not found to ruin\n' % path_msg(rbloomfilename))
35         return
36     b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
37     b.map[16 : 16 + 2**b.bits] = b'\0' * 2**b.bits
38
39
40 def check_bloom(path, bloomfilename, idx):
41     rbloomfilename = git.repo_rel(bloomfilename)
42     ridx = git.repo_rel(idx)
43     if not os.path.exists(bloomfilename):
44         log('bloom: %s: does not exist.\n' % path_msg(rbloomfilename))
45         return
46     b = bloom.ShaBloom(bloomfilename)
47     if not b.valid():
48         add_error('bloom: %r is invalid.\n' % path_msg(rbloomfilename))
49         return
50     base = os.path.basename(idx)
51     if base not in b.idxnames:
52         log('bloom: %s does not contain the idx.\n' % path_msg(rbloomfilename))
53         return
54     if base == idx:
55         idx = os.path.join(path, idx)
56     log('bloom: bloom file: %s\n' % path_msg(rbloomfilename))
57     log('bloom:   checking %s\n' % path_msg(ridx))
58     for objsha in git.open_idx(idx):
59         if not b.exists(objsha):
60             add_error('bloom: ERROR: object %s missing' % hexstr(objsha))
61
62
63 _first = None
64 def do_bloom(path, outfilename, k):
65     global _first
66     assert k in (None, 4, 5)
67     b = None
68     if os.path.exists(outfilename) and not opt.force:
69         b = bloom.ShaBloom(outfilename)
70         if not b.valid():
71             debug1("bloom: Existing invalid bloom found, regenerating.\n")
72             b = None
73
74     add = []
75     rest = []
76     add_count = 0
77     rest_count = 0
78     for i, name in enumerate(glob.glob(b'%s/*.idx' % path)):
79         progress('bloom: counting: %d\r' % i)
80         ix = git.open_idx(name)
81         ixbase = os.path.basename(name)
82         if b and (ixbase in b.idxnames):
83             rest.append(name)
84             rest_count += len(ix)
85         else:
86             add.append(name)
87             add_count += len(ix)
88
89     if not add:
90         debug1("bloom: nothing to do.\n")
91         return
92
93     if b:
94         if len(b) != rest_count:
95             debug1("bloom: size %d != idx total %d, regenerating\n"
96                    % (len(b), rest_count))
97             b = None
98         elif k is not None and k != b.k:
99             debug1("bloom: new k %d != existing k %d, regenerating\n"
100                    % (k, b.k))
101             b = None
102         elif (b.bits < bloom.MAX_BLOOM_BITS[b.k] and
103               b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
104             debug1("bloom: regenerating: adding %d entries gives "
105                    "%.2f%% false positives.\n"
106                    % (add_count, b.pfalse_positive(add_count)))
107             b = None
108         else:
109             b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
110     if not b: # Need all idxs to build from scratch
111         add += rest
112         add_count += rest_count
113     del rest
114     del rest_count
115
116     msg = b is None and 'creating from' or 'adding'
117     if not _first: _first = path
118     dirprefix = (_first != path) and git.repo_rel(path) + b': ' or b''
119     progress('bloom: %s%s %d file%s (%d object%s).\r'
120         % (path_msg(dirprefix), msg,
121            len(add), len(add)!=1 and 's' or '',
122            add_count, add_count!=1 and 's' or ''))
123
124     tfname = None
125     if b is None:
126         tfname = os.path.join(path, b'bup.tmp.bloom')
127         b = bloom.create(tfname, expected=add_count, k=k)
128     count = 0
129     icount = 0
130     for name in add:
131         ix = git.open_idx(name)
132         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
133                   % (icount*100.0/add_count, icount, add_count))
134         b.add_idx(ix)
135         count += 1
136         icount += len(ix)
137
138     # Currently, there's an open file object for tfname inside b.
139     # Make sure it's closed before rename.
140     b.close()
141
142     if tfname:
143         os.rename(tfname, outfilename)
144
145
146 handle_ctrl_c()
147
148 o = options.Options(optspec)
149 (opt, flags, extra) = o.parse(sys.argv[1:])
150
151 if extra:
152     o.fatal('no positional parameters expected')
153
154 if not opt.check and opt.k and opt.k not in (4,5):
155     o.fatal('only k values of 4 and 5 are supported')
156
157 if opt.check:
158     opt.check = argv_bytes(opt.check)
159
160 git.check_repo_or_die()
161
162 output = argv_bytes(opt.output) if opt.output else None
163 paths = opt.dir and [argv_bytes(opt.dir)] or git.all_packdirs()
164 for path in paths:
165     debug1('bloom: scanning %s\n' % path_msg(path))
166     outfilename = output or os.path.join(path, b'bup.bloom')
167     if opt.check:
168         check_bloom(path, outfilename, opt.check)
169     elif opt.ruin:
170         ruin_bloom(outfilename)
171     else:
172         do_bloom(path, outfilename, opt.k)
173
174 if saved_errors:
175     log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
176     sys.exit(1)
177 elif opt.check:
178     log('All tests passed.\n')