]> arthur.barton.de Git - bup.git/blob - cmd/midx-cmd.py
e52920717e6b693049ec1eec626a9fe4de558d54
[bup.git] / cmd / midx-cmd.py
1 #!/usr/bin/env python
2 import sys, math, struct, glob, resource
3 from bup import options, git
4 from bup.helpers import *
5
6 PAGE_SIZE=4096
7 SHA_PER_PAGE=PAGE_SIZE/20.
8
9
10 def _group(l, count):
11     for i in xrange(0, len(l), count):
12         yield l[i:i+count]
13         
14         
15 def max_files():
16     mf = min(resource.getrlimit(resource.RLIMIT_NOFILE))
17     if mf > 32:
18         mf -= 20  # just a safety margin
19     else:
20         mf -= 6   # minimum safety margin
21     return mf
22
23
24 def merge(idxlist, bits, table):
25     count = 0
26     for e in git.idxmerge(idxlist):
27         count += 1
28         prefix = git.extract_bits(e, bits)
29         table[prefix] = count
30         yield e
31
32
33 def do_midx(outdir, outfilename, infilenames):
34     if not outfilename:
35         assert(outdir)
36         sum = Sha1('\0'.join(infilenames)).hexdigest()
37         outfilename = '%s/midx-%s.midx' % (outdir, sum)
38     
39     inp = []
40     total = 0
41     for name in infilenames:
42         ix = git.PackIdx(name)
43         inp.append(ix)
44         total += len(ix)
45
46     log('Merging %d indexes (%d objects).\n' % (len(infilenames), total))
47     if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
48        or (opt.force and not total):
49         log('midx: nothing to do.\n')
50         return
51
52     pages = int(total/SHA_PER_PAGE) or 1
53     bits = int(math.ceil(math.log(pages, 2)))
54     entries = 2**bits
55     log('Table size: %d (%d bits)\n' % (entries*4, bits))
56     
57     table = [0]*entries
58
59     try:
60         os.unlink(outfilename)
61     except OSError:
62         pass
63     f = open(outfilename + '.tmp', 'w+')
64     f.write('MIDX\0\0\0\2')
65     f.write(struct.pack('!I', bits))
66     assert(f.tell() == 12)
67     f.write('\0'*4*entries)
68     
69     for e in merge(inp, bits, table):
70         f.write(e)
71         
72     f.write('\0'.join(os.path.basename(p) for p in infilenames))
73
74     f.seek(12)
75     f.write(struct.pack('!%dI' % entries, *table))
76     f.close()
77     os.rename(outfilename + '.tmp', outfilename)
78
79     # this is just for testing
80     if 0:
81         p = git.PackMidx(outfilename)
82         assert(len(p.idxnames) == len(infilenames))
83         print p.idxnames
84         assert(len(p) == total)
85         pi = iter(p)
86         for i in merge(inp, total, bits, table):
87             assert(i == pi.next())
88             assert(p.exists(i))
89
90     print outfilename
91
92 optspec = """
93 bup midx [options...] <idxnames...>
94 --
95 o,output=  output midx filename (default: auto-generated)
96 a,auto     automatically create .midx from any unindexed .idx files
97 f,force    automatically create .midx from *all* .idx files
98 max-files= maximum number of idx files to open at once [-1]
99 """
100 o = options.Options('bup midx', optspec)
101 (opt, flags, extra) = o.parse(sys.argv[1:])
102
103 if extra and (opt.auto or opt.force):
104     o.fatal("you can't use -f/-a and also provide filenames")
105
106 git.check_repo_or_die()
107
108 if opt.max_files < 0:
109     opt.max_files = max_files()
110 assert(opt.max_files >= 5)
111
112 if extra:
113     do_midx(git.repo('objects/pack'), opt.output, extra)
114 elif opt.auto or opt.force:
115     paths = [git.repo('objects/pack')]
116     paths += glob.glob(git.repo('index-cache/*/.'))
117     for path in paths:
118         log('midx: scanning %s\n' % path)
119         if opt.force:
120             idxs = glob.glob('%s/*.idx' % path)
121             for sublist in _group(idxs, opt.max_files):
122                 do_midx(path, opt.output, sublist)
123         elif opt.auto:
124             m = git.PackIdxList(path)
125             needed = {}
126             for pack in m.packs:  # only .idx files without a .midx are open
127                 if pack.name.endswith('.idx'):
128                     needed[pack.name] = 1
129             del m
130             do_midx(path, opt.output, needed.keys())
131         log('\n')
132 else:
133     o.fatal("you must use -f or -a or provide input filenames")