2 from bup import _helpers
3 from bup.helpers import *
7 extract_bits = _helpers.extract_bits
13 """Wrapper which contains data from multiple index files.
14 Multiple index (.midx) files constitute a wrapper around index (.idx) files
15 and make it possible for bup to expand Git's indexing capabilities to vast
18 def __init__(self, filename):
20 self.force_keep = False
22 assert(filename.endswith('.midx'))
23 self.map = mmap_read(open(filename))
24 if str(self.map[0:4]) != 'MIDX':
25 log('Warning: skipping: invalid MIDX header in %r\n' % filename)
26 self.force_keep = True
27 return self._init_failed()
28 ver = struct.unpack('!I', self.map[4:8])[0]
29 if ver < MIDX_VERSION:
30 log('Warning: ignoring old-style (v%d) midx %r\n'
32 self.force_keep = False # old stuff is boring
33 return self._init_failed()
34 if ver > MIDX_VERSION:
35 log('Warning: ignoring too-new (v%d) midx %r\n'
37 self.force_keep = True # new stuff is exciting
38 return self._init_failed()
40 self.bits = _helpers.firstword(self.map[8:12])
41 self.entries = 2**self.bits
42 self.fanout = buffer(self.map, 12, self.entries*4)
43 self.sha_ofs = 12 + self.entries*4
44 self.nsha = nsha = self._fanget(self.entries-1)
45 self.shatable = buffer(self.map, self.sha_ofs, nsha*20)
46 self.which_ofs = self.sha_ofs + 20*nsha
47 self.whichlist = buffer(self.map, self.which_ofs, nsha*4)
48 self.idxnames = str(self.map[self.which_ofs + 4*nsha:]).split('\0')
53 def _init_failed(self):
56 self.fanout = buffer('\0\0\0\0')
57 self.shatable = buffer('\0'*20)
62 s = self.fanout[start:start+4]
63 return _helpers.firstword(s)
66 return str(self.shatable[i*20:(i+1)*20])
68 def _get_idx_i(self, i):
69 return struct.unpack('!I', self.whichlist[i*4:(i+1)*4])[0]
71 def _get_idxname(self, i):
72 return self.idxnames[self._get_idx_i(i)]
75 if self.map is not None:
79 def exists(self, hash, want_source=False):
80 """Return nonempty if the object exists in the index files."""
81 global _total_searches, _total_steps
84 el = extract_bits(want, self.bits)
86 start = self._fanget(el-1)
87 startv = el << (32-self.bits)
91 end = self._fanget(el)
92 endv = (el+1) << (32-self.bits)
93 _total_steps += 1 # lookup table is a step
94 hashv = _helpers.firstword(hash)
95 #print '(%08x) %08x %08x %08x' % (extract_bits(want, 32), startv, hashv, endv)
98 #print '! %08x %08x %08x %d - %d' % (startv, hashv, endv, start, end)
99 mid = start + (hashv-startv)*(end-start-1)/(endv-startv)
100 #print ' %08x %08x %08x %d %d %d' % (startv, hashv, endv, start, mid, end)
102 #print ' %08x' % self._num(v)
105 startv = _helpers.firstword(v)
108 endv = _helpers.firstword(v)
110 return want_source and self._get_idxname(mid) or True
114 for i in xrange(self._fanget(self.entries-1)):
115 yield buffer(self.shatable, i*20, 20)
118 return int(self._fanget(self.entries-1))