2 import sys, math, struct, glob, resource
3 from bup import options, git
4 from bup.helpers import *
7 SHA_PER_PAGE=PAGE_SIZE/20.
10 bup midx [options...] <idxnames...>
12 o,output= output midx filename (default: auto-generated)
13 a,auto automatically create .midx from any unindexed .idx files
14 f,force automatically create .midx from *all* .idx files
15 p,print print names of generated midx files
16 max-files= maximum number of idx files to open at once [-1]
17 dir= directory containing idx/midx files
21 for i in xrange(0, len(l), count):
26 mf = min(resource.getrlimit(resource.RLIMIT_NOFILE))
28 mf -= 20 # just a safety margin
30 mf -= 6 # minimum safety margin
34 def merge(idxlist, bits, table):
36 for e in git.idxmerge(idxlist, final_progress=False):
38 prefix = git.extract_bits(e, bits)
43 def _do_midx(outdir, outfilename, infilenames, prefixstr):
46 sum = Sha1('\0'.join(infilenames)).hexdigest()
47 outfilename = '%s/midx-%s.midx' % (outdir, sum)
52 for name in infilenames:
53 ix = git.open_idx(name)
59 log('midx: %screating from %d files (%d objects).\n'
60 % (prefixstr, len(infilenames), total))
61 if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
62 or len(infilenames) < 2 \
63 or (opt.force and not total):
64 debug1('midx: nothing to do.\n')
67 pages = int(total/SHA_PER_PAGE) or 1
68 bits = int(math.ceil(math.log(pages, 2)))
70 debug1('midx: table size: %d (%d bits)\n' % (entries*4, bits))
75 os.unlink(outfilename)
78 f = open(outfilename + '.tmp', 'w+')
79 f.write('MIDX\0\0\0\2')
80 f.write(struct.pack('!I', bits))
81 assert(f.tell() == 12)
82 f.write('\0'*4*entries)
84 for e in merge(inp, bits, table):
87 f.write('\0'.join(os.path.basename(p) for p in allfilenames.keys()))
90 f.write(struct.pack('!%dI' % entries, *table))
92 os.rename(outfilename + '.tmp', outfilename)
94 # this is just for testing
96 p = git.PackMidx(outfilename)
97 assert(len(p.idxnames) == len(infilenames))
99 assert(len(p) == total)
101 for i in merge(inp, total, bits, table):
102 assert(i == pi.next())
105 return total,outfilename
108 def do_midx(outdir, outfilename, infilenames, prefixstr):
109 rv = _do_midx(outdir, outfilename, infilenames, prefixstr)
110 if rv and opt['print']:
114 def do_midx_dir(path):
117 if opt.force and not opt.auto:
118 midxs = [] # don't use existing midx files
120 midxs = glob.glob('%s/*.midx' % path)
123 m = git.open_idx(mname)
124 contents[mname] = [('%s/%s' % (path,i)) for i in m.idxnames]
125 sizes[mname] = len(m)
127 # sort the biggest midxes first, so that we can eliminate smaller
128 # redundant ones that come later in the list
129 midxs.sort(lambda x,y: -cmp(sizes[x], sizes[y]))
133 for iname in contents[mname]:
134 if not already.get(iname):
138 debug1('%r is redundant\n' % mname)
142 midxs = [k for k in midxs if not already.get(k)]
143 idxs = [k for k in glob.glob('%s/*.idx' % path) if not already.get(k)]
146 i = git.open_idx(iname)
147 sizes[iname] = len(i)
149 all = [(sizes[n],n) for n in (midxs + idxs)]
151 # FIXME: what are the optimal values? Does this make sense?
152 DESIRED_HWM = opt.force and 1 or 5
153 DESIRED_LWM = opt.force and 1 or 2
154 existed = dict((name,1) for sz,name in all)
155 debug1('midx: %d indexes; want no more than %d.\n'
156 % (len(all), DESIRED_HWM))
157 if len(all) <= DESIRED_HWM:
158 debug1('midx: nothing to do.\n')
159 while len(all) > DESIRED_HWM:
161 part1 = [name for sz,name in all[:len(all)-DESIRED_LWM+1]]
162 part2 = all[len(all)-DESIRED_LWM+1:]
163 all = list(do_midx_group(path, part1)) + part2
164 if len(all) > DESIRED_HWM:
165 debug1('\nStill too many indexes (%d > %d). Merging again.\n'
166 % (len(all), DESIRED_HWM))
170 if not existed.get(name):
174 def do_midx_group(outdir, infiles):
175 groups = list(_group(infiles, opt.max_files))
177 for n,sublist in enumerate(groups):
179 gprefix = 'Group %d: ' % (n+1)
180 rv = _do_midx(path, None, sublist, gprefix)
187 o = options.Options(optspec)
188 (opt, flags, extra) = o.parse(sys.argv[1:])
190 if extra and (opt.auto or opt.force):
191 o.fatal("you can't use -f/-a and also provide filenames")
193 git.check_repo_or_die()
195 if opt.max_files < 0:
196 opt.max_files = max_files()
197 assert(opt.max_files >= 5)
200 do_midx(git.repo('objects/pack'), opt.output, extra, '')
201 elif opt.auto or opt.force:
205 paths = [git.repo('objects/pack')]
206 paths += glob.glob(git.repo('index-cache/*/.'))
208 debug1('midx: scanning %s\n' % path)
212 o.fatal("you must use -f or -a or provide input filenames")