2 import sys, math, struct, glob, resource
3 import tempfile, shutil
4 from bup import options, git
5 from bup.helpers import *
8 SHA_PER_PAGE=PAGE_SIZE/20.
11 bup midx [options...] <idxnames...>
13 o,output= output midx filename (default: auto-generated)
14 a,auto automatically create .midx from any unindexed .idx files
15 f,force automatically create .midx from *all* .idx files
16 p,print print names of generated midx files
17 max-files= maximum number of idx files to open at once [-1]
18 dir= directory containing idx/midx files
22 for i in xrange(0, len(l), count):
27 mf = min(resource.getrlimit(resource.RLIMIT_NOFILE))
29 mf -= 20 # just a safety margin
31 mf -= 6 # minimum safety margin
35 def merge_into(tf_sha, tf_nmap, idxlist, bits, entries, total):
37 it = git.idxmerge(idxlist, final_progress=False, total=total)
38 for i, (e, idx) in enumerate(it):
39 new_prefix = git.extract_bits(e, bits)
40 if new_prefix != prefix:
41 for p in xrange(prefix, new_prefix):
45 tf_nmap.write(struct.pack('!I', idx))
47 for p in xrange(prefix, entries):
51 def _do_midx(outdir, outfilename, infilenames, prefixstr):
54 sum = Sha1('\0'.join(infilenames)).hexdigest()
55 outfilename = '%s/midx-%s.midx' % (outdir, sum)
60 for name in infilenames:
61 ix = git.open_idx(name)
62 inp.append(ix.iter_with_idx_i(len(allfilenames)))
64 allfilenames.append(os.path.basename(n))
67 log('midx: %screating from %d files (%d objects).\n'
68 % (prefixstr, len(infilenames), total))
69 if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
70 or len(infilenames) < 2 \
71 or (opt.force and not total):
72 debug1('midx: nothing to do.\n')
75 pages = int(total/SHA_PER_PAGE) or 1
76 bits = int(math.ceil(math.log(pages, 2)))
78 debug1('midx: table size: %d (%d bits)\n' % (entries*4, bits))
81 os.unlink(outfilename)
84 f = open(outfilename + '.tmp', 'w+')
86 f.write(struct.pack('!II', git.MIDX_VERSION, bits))
87 assert(f.tell() == 12)
89 tf_sha = tempfile.TemporaryFile(dir=outdir)
90 tf_nmap = tempfile.TemporaryFile(dir=outdir)
91 for t in merge_into(tf_sha, tf_nmap, inp, bits, entries, total):
92 f.write(struct.pack('!I', t))
93 assert(f.tell() == 12 + 4*entries)
96 shutil.copyfileobj(tf_sha, f)
98 assert(f.tell() == 12 + 4*entries + 20*t) # t may be < total due to dupes
101 shutil.copyfileobj(tf_nmap, f)
103 assert(f.tell() == 12 + 4*entries + 24*t) # t may be < total due to dupes
105 f.write('\0'.join(allfilenames))
107 os.rename(outfilename + '.tmp', outfilename)
109 # this is just for testing
111 p = git.PackMidx(outfilename)
112 assert(len(p.idxnames) == len(infilenames))
114 assert(len(p) == total)
115 for pe, e in p, git.idxmerge(inp, final_progress=False):
116 assert(i == pi.next())
119 return total, outfilename
122 def do_midx(outdir, outfilename, infilenames, prefixstr):
123 rv = _do_midx(outdir, outfilename, infilenames, prefixstr)
124 if rv and opt['print']:
128 def do_midx_dir(path):
131 if opt.force and not opt.auto:
132 midxs = [] # don't use existing midx files
134 midxs = glob.glob('%s/*.midx' % path)
137 m = git.open_idx(mname)
138 contents[mname] = [('%s/%s' % (path,i)) for i in m.idxnames]
139 sizes[mname] = len(m)
141 # sort the biggest midxes first, so that we can eliminate smaller
142 # redundant ones that come later in the list
143 midxs.sort(lambda x,y: -cmp(sizes[x], sizes[y]))
147 for iname in contents[mname]:
148 if not already.get(iname):
152 debug1('%r is redundant\n' % mname)
156 midxs = [k for k in midxs if not already.get(k)]
157 idxs = [k for k in glob.glob('%s/*.idx' % path) if not already.get(k)]
160 i = git.open_idx(iname)
161 sizes[iname] = len(i)
163 all = [(sizes[n],n) for n in (midxs + idxs)]
165 # FIXME: what are the optimal values? Does this make sense?
166 DESIRED_HWM = opt.force and 1 or 5
167 DESIRED_LWM = opt.force and 1 or 2
168 existed = dict((name,1) for sz,name in all)
169 debug1('midx: %d indexes; want no more than %d.\n'
170 % (len(all), DESIRED_HWM))
171 if len(all) <= DESIRED_HWM:
172 debug1('midx: nothing to do.\n')
173 while len(all) > DESIRED_HWM:
175 part1 = [name for sz,name in all[:len(all)-DESIRED_LWM+1]]
176 part2 = all[len(all)-DESIRED_LWM+1:]
177 all = list(do_midx_group(path, part1)) + part2
178 if len(all) > DESIRED_HWM:
179 debug1('\nStill too many indexes (%d > %d). Merging again.\n'
180 % (len(all), DESIRED_HWM))
184 if not existed.get(name):
188 def do_midx_group(outdir, infiles):
189 groups = list(_group(infiles, opt.max_files))
191 for n,sublist in enumerate(groups):
193 gprefix = 'Group %d: ' % (n+1)
194 rv = _do_midx(path, None, sublist, gprefix)
201 o = options.Options(optspec)
202 (opt, flags, extra) = o.parse(sys.argv[1:])
204 if extra and (opt.auto or opt.force):
205 o.fatal("you can't use -f/-a and also provide filenames")
207 git.check_repo_or_die()
209 if opt.max_files < 0:
210 opt.max_files = max_files()
211 assert(opt.max_files >= 5)
214 do_midx(git.repo('objects/pack'), opt.output, extra, '')
215 elif opt.auto or opt.force:
219 paths = [git.repo('objects/pack')]
220 paths += glob.glob(git.repo('index-cache/*/.'))
222 debug1('midx: scanning %s\n' % path)
226 o.fatal("you must use -f or -a or provide input filenames")