]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
Move bloom-related stuff from git.py to a new bloom.py.
[bup.git] / cmd / bloom-cmd.py
1 #!/usr/bin/env python
2 import sys, glob, tempfile
3 from bup import options, git, bloom
4 from bup.helpers import *
5
6 optspec = """
7 bup bloom [options...]
8 --
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 """
14
15 _first = None
16 def do_bloom(path, outfilename):
17     global _first
18     if not outfilename:
19         assert(path)
20         outfilename = os.path.join(path, 'bup.bloom')
21
22     b = None
23     if os.path.exists(outfilename) and not opt.force:
24         b = bloom.ShaBloom(outfilename)
25         if not b.valid():
26             debug1("bloom: Existing invalid bloom found, regenerating.\n")
27             b = None
28
29     add = []
30     rest = []
31     add_count = 0
32     rest_count = 0
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):
38             rest.append(name)
39             rest_count += len(ix)
40         else:
41             add.append(name)
42             add_count += len(ix)
43     total = add_count + rest_count
44
45     if not add:
46         debug1("bloom: nothing to do.\n")
47         return
48
49     if b:
50         if len(b) != rest_count:
51             log("bloom: size %d != idx total %d, regenerating\n"
52                     % (len(b), rest_count))
53             b = None
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)))
59             b = None
60         else:
61             b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
62     if not b: # Need all idxs to build from scratch
63         add += rest
64         add_count += rest_count
65     del rest
66     del rest_count
67
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'
72         % (dirprefix, msg,
73            len(add), len(add)!=1 and 's' or '',
74            add_count, add_count!=1 and 's' or ''))
75
76     tfname = None
77     if b is None:
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)
81     count = 0
82     icount = 0
83     for name in add:
84         ix = git.open_idx(name)
85         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
86                   % (icount*100.0/add_count, icount, add_count))
87         b.add_idx(ix)
88         count += 1
89         icount += len(ix)
90
91     if tfname:
92         os.rename(tfname, outfilename)
93
94
95 handle_ctrl_c()
96
97 o = options.Options(optspec)
98 (opt, flags, extra) = o.parse(sys.argv[1:])
99
100 if extra:
101     o.fatal('no positional parameters expected')
102
103 if opt.k and opt.k not in (4,5):
104     o.fatal('only k values of 4 and 5 are supported')
105
106 git.check_repo_or_die()
107
108 paths = opt.dir and [opt.dir] or git.all_packdirs()
109 for path in paths:
110     debug1('bloom: scanning %s\n' % path)
111     do_bloom(path, opt.output)