2 import sys, math, struct, glob, resource
4 from bup import options, git, midx, _helpers
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 use all existing .midx/.idx files as input
15 f,force merge produce exactly one .midx containing all objects
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
34 merge_into = _helpers.merge_into
37 def _do_midx(outdir, outfilename, infilenames, prefixstr):
41 sum = Sha1('\0'.join(infilenames)).hexdigest()
42 outfilename = '%s/midx-%s.midx' % (outdir, sum)
47 for name in infilenames:
48 ix = git.open_idx(name)
53 isinstance(ix, midx.PackMidx) and ix.which_ofs or 0,
57 allfilenames.append(os.path.basename(n))
59 inp.sort(lambda x,y: cmp(str(y[0][y[2]:y[2]+20]),str(x[0][x[2]:x[2]+20])))
61 if not _first: _first = outdir
62 dirprefix = (_first != outdir) and git.repo_rel(outdir)+': ' or ''
63 log('midx: %s%screating from %d files (%d objects).\n'
64 % (dirprefix, prefixstr, len(infilenames), total))
65 if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
66 or len(infilenames) < 2 \
67 or (opt.force and not total):
68 debug1('midx: nothing to do.\n')
71 pages = int(total/SHA_PER_PAGE) or 1
72 bits = int(math.ceil(math.log(pages, 2)))
74 debug1('midx: table size: %d (%d bits)\n' % (entries*4, bits))
77 os.unlink(outfilename)
80 f = open(outfilename + '.tmp', 'w+b')
82 f.write(struct.pack('!II', midx.MIDX_VERSION, bits))
83 assert(f.tell() == 12)
85 f.truncate(12 + 4*entries + 20*total + 4*total)
87 fmap = mmap_readwrite(f, close=False)
89 count = merge_into(fmap, bits, total, inp)
93 f.seek(0, git.SEEK_END)
94 f.write('\0'.join(allfilenames))
96 os.rename(outfilename + '.tmp', outfilename)
98 # this is just for testing
100 p = midx.PackMidx(outfilename)
101 assert(len(p.idxnames) == len(infilenames))
103 assert(len(p) == total)
104 for pe, e in p, git.idxmerge(inp, final_progress=False):
105 assert(i == pi.next())
108 return total, outfilename
111 def do_midx(outdir, outfilename, infilenames, prefixstr):
112 rv = _do_midx(outdir, outfilename, infilenames, prefixstr)
113 if rv and opt['print']:
117 def do_midx_dir(path):
120 if opt.force and not opt.auto:
121 midxs = [] # don't use existing midx files
123 midxs = glob.glob('%s/*.midx' % path)
126 m = git.open_idx(mname)
127 contents[mname] = [('%s/%s' % (path,i)) for i in m.idxnames]
128 sizes[mname] = len(m)
130 # sort the biggest midxes first, so that we can eliminate smaller
131 # redundant ones that come later in the list
132 midxs.sort(lambda x,y: -cmp(sizes[x], sizes[y]))
136 for iname in contents[mname]:
137 if not already.get(iname):
141 debug1('%r is redundant\n' % mname)
145 midxs = [k for k in midxs if not already.get(k)]
146 idxs = [k for k in glob.glob('%s/*.idx' % path) if not already.get(k)]
149 i = git.open_idx(iname)
150 sizes[iname] = len(i)
152 all = [(sizes[n],n) for n in (midxs + idxs)]
154 # FIXME: what are the optimal values? Does this make sense?
155 DESIRED_HWM = opt.force and 1 or 5
156 DESIRED_LWM = opt.force and 1 or 2
157 existed = dict((name,1) for sz,name in all)
158 debug1('midx: %d indexes; want no more than %d.\n'
159 % (len(all), DESIRED_HWM))
160 if len(all) <= DESIRED_HWM:
161 debug1('midx: nothing to do.\n')
162 while len(all) > DESIRED_HWM:
164 part1 = [name for sz,name in all[:len(all)-DESIRED_LWM+1]]
165 part2 = all[len(all)-DESIRED_LWM+1:]
166 all = list(do_midx_group(path, part1)) + part2
167 if len(all) > DESIRED_HWM:
168 debug1('\nStill too many indexes (%d > %d). Merging again.\n'
169 % (len(all), DESIRED_HWM))
173 if not existed.get(name):
177 def do_midx_group(outdir, infiles):
178 groups = list(_group(infiles, opt.max_files))
180 for n,sublist in enumerate(groups):
182 gprefix = 'Group %d: ' % (n+1)
183 rv = _do_midx(path, None, sublist, gprefix)
190 o = options.Options(optspec)
191 (opt, flags, extra) = o.parse(sys.argv[1:])
193 if extra and (opt.auto or opt.force):
194 o.fatal("you can't use -f/-a and also provide filenames")
196 git.check_repo_or_die()
198 if opt.max_files < 0:
199 opt.max_files = max_files()
200 assert(opt.max_files >= 5)
203 do_midx(git.repo('objects/pack'), opt.output, extra, '')
204 elif opt.auto or opt.force:
205 paths = opt.dir and [opt.dir] or git.all_packdirs()
207 debug1('midx: scanning %s\n' % path)
210 o.fatal("you must use -f or -a or provide input filenames")