comparison mercurial/pure/parsers.py @ 47034:0d8ff1f4ab0c

revlog: add a `entry_binary` method on index The revlog index is already responsible for unpacking the binary entry, it would be simpler to make it responsible for packing them. In practice the C version of the index is already doing this internally. We introduce a "entry_binary" method that return the binary version of an existing revision. The method currently need to also take the revlog header to deal with the "first revision" special case. We will introduce further refactor in a later changeset to split that logic out. Differential Revision: https://phab.mercurial-scm.org/D10508
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 08 Apr 2021 00:01:11 +0200
parents d55b71393907
children d57386e5c80e
comparison
equal deleted inserted replaced
47033:824ee4aaa09b 47034:0d8ff1f4ab0c
125 r = self.index_format.unpack(data) 125 r = self.index_format.unpack(data)
126 if self._lgt and i == 0: 126 if self._lgt and i == 0:
127 r = (offset_type(0, gettype(r[0])),) + r[1:] 127 r = (offset_type(0, gettype(r[0])),) + r[1:]
128 return r 128 return r
129 129
130 def entry_binary(self, rev, header):
131 """return the raw binary string representing a revision"""
132 entry = self[rev]
133 p = revlog_constants.INDEX_ENTRY_V1.pack(*entry)
134 if rev == 0:
135 v_fmt = revlog_constants.INDEX_HEADER
136 v_bin = v_fmt.pack(header)
137 p = v_bin + p[v_fmt.size :]
138 return p
139
130 140
131 class IndexObject(BaseIndexObject): 141 class IndexObject(BaseIndexObject):
132 def __init__(self, data): 142 def __init__(self, data):
133 assert len(data) % self.entry_size == 0 143 assert len(data) % self.entry_size == 0, (
144 len(data),
145 self.entry_size,
146 len(data) % self.entry_size,
147 )
134 self._data = data 148 self._data = data
135 self._lgt = len(data) // self.entry_size 149 self._lgt = len(data) // self.entry_size
136 self._extra = [] 150 self._extra = []
137 151
138 def _calculate_index(self, i): 152 def _calculate_index(self, i):
269 new = old[:64] + packed + old[64 + packed_size :] 283 new = old[:64] + packed + old[64 + packed_size :]
270 self._extra[i - self._lgt] = new 284 self._extra[i - self._lgt] = new
271 else: 285 else:
272 msg = b"cannot rewrite entries outside of this transaction" 286 msg = b"cannot rewrite entries outside of this transaction"
273 raise KeyError(msg) 287 raise KeyError(msg)
288
289 def entry_binary(self, rev, header):
290 """return the raw binary string representing a revision"""
291 entry = self[rev]
292 p = revlog_constants.INDEX_ENTRY_V2.pack(*entry)
293 if rev == 0:
294 v_fmt = revlog_constants.INDEX_HEADER
295 v_bin = v_fmt.pack(header)
296 p = v_bin + p[v_fmt.size :]
297 return p
274 298
275 299
276 class IndexObject2(Index2Mixin, IndexObject): 300 class IndexObject2(Index2Mixin, IndexObject):
277 pass 301 pass
278 302