docket.py
180 lines
| 5.6 KiB
| text/x-python
|
PythonLexer
r48008 | # docket - code related to revlog "docket" | |||
# | ||||
# Copyright 2021 Pierre-Yves David <pierre-yves.david@octobus.net> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
### Revlog docket file | ||||
# | ||||
# The revlog is stored on disk using multiple files: | ||||
# | ||||
# * a small docket file, containing metadata and a pointer, | ||||
# | ||||
# * an index file, containing fixed width information about revisions, | ||||
# | ||||
# * a data file, containing variable width data for these revisions, | ||||
from __future__ import absolute_import | ||||
import struct | ||||
r48015 | from .. import ( | |||
error, | ||||
r48029 | util, | |||
r48015 | ) | |||
r48008 | from . import ( | |||
constants, | ||||
) | ||||
# Docket format | ||||
# | ||||
# * 4 bytes: revlog version | ||||
# | This is mandatory as docket must be compatible with the previous | ||||
# | revlog index header. | ||||
r48016 | # * 8 bytes: size of index-data | |||
# * 8 bytes: pending size of index-data | ||||
# * 8 bytes: size of data | ||||
# * 8 bytes: pending size of data | ||||
r48029 | # * 1 bytes: default compression header | |||
S_HEADER = struct.Struct(constants.INDEX_HEADER.format + 'LLLLc') | ||||
r48008 | ||||
class RevlogDocket(object): | ||||
"""metadata associated with revlog""" | ||||
r48015 | def __init__( | |||
self, | ||||
revlog, | ||||
use_pending=False, | ||||
version_header=None, | ||||
index_end=0, | ||||
pending_index_end=0, | ||||
r48016 | data_end=0, | |||
pending_data_end=0, | ||||
r48029 | default_compression_header=None, | |||
r48015 | ): | |||
r48008 | self._version_header = version_header | |||
r48015 | self._read_only = bool(use_pending) | |||
r48008 | self._dirty = False | |||
self._radix = revlog.radix | ||||
self._path = revlog._docket_file | ||||
self._opener = revlog.opener | ||||
r48016 | # thes asserts should be True as long as we have a single index filename | |||
r48015 | assert index_end <= pending_index_end | |||
r48016 | assert data_end <= pending_data_end | |||
r48015 | self._initial_index_end = index_end | |||
self._pending_index_end = pending_index_end | ||||
r48016 | self._initial_data_end = data_end | |||
self._pending_data_end = pending_data_end | ||||
r48015 | if use_pending: | |||
self._index_end = self._pending_index_end | ||||
r48016 | self._data_end = self._pending_data_end | |||
r48015 | else: | |||
self._index_end = self._initial_index_end | ||||
r48016 | self._data_end = self._initial_data_end | |||
r48029 | self.default_compression_header = default_compression_header | |||
r48008 | ||||
def index_filepath(self): | ||||
"""file path to the current index file associated to this docket""" | ||||
# very simplistic version at first | ||||
return b"%s.idx" % self._radix | ||||
r48012 | @property | |||
def index_end(self): | ||||
return self._index_end | ||||
@index_end.setter | ||||
def index_end(self, new_size): | ||||
if new_size != self._index_end: | ||||
self._index_end = new_size | ||||
self._dirty = True | ||||
r48016 | @property | |||
def data_end(self): | ||||
return self._data_end | ||||
@data_end.setter | ||||
def data_end(self, new_size): | ||||
if new_size != self._data_end: | ||||
self._data_end = new_size | ||||
self._dirty = True | ||||
r48015 | def write(self, transaction, pending=False, stripping=False): | |||
r48008 | """write the modification of disk if any | |||
This make the new content visible to all process""" | ||||
r48015 | if not self._dirty: | |||
return False | ||||
else: | ||||
if self._read_only: | ||||
msg = b'writing read-only docket: %s' | ||||
msg %= self._path | ||||
raise error.ProgrammingError(msg) | ||||
r48012 | if not stripping: | |||
# XXX we could, leverage the docket while stripping. However it | ||||
# is not powerfull enough at the time of this comment | ||||
transaction.addbackup(self._path, location=b'store') | ||||
r48008 | with self._opener(self._path, mode=b'w', atomictemp=True) as f: | |||
r48015 | f.write(self._serialize(pending=pending)) | |||
# if pending we still need to the write final data eventually | ||||
self._dirty = pending | ||||
return True | ||||
r48008 | ||||
r48015 | def _serialize(self, pending=False): | |||
if pending: | ||||
official_index_end = self._initial_index_end | ||||
r48016 | official_data_end = self._initial_data_end | |||
r48015 | else: | |||
official_index_end = self._index_end | ||||
r48016 | official_data_end = self._data_end | |||
r48015 | ||||
# this assert should be True as long as we have a single index filename | ||||
r48016 | assert official_data_end <= self._data_end | |||
r48012 | data = ( | |||
self._version_header, | ||||
r48015 | official_index_end, | |||
r48012 | self._index_end, | |||
r48016 | official_data_end, | |||
self._data_end, | ||||
r48029 | self.default_compression_header, | |||
r48012 | ) | |||
return S_HEADER.pack(*data) | ||||
r48008 | ||||
def default_docket(revlog, version_header): | ||||
"""given a revlog version a new docket object for the given revlog""" | ||||
r48040 | rl_version = version_header & 0xFFFF | |||
if rl_version not in (constants.REVLOGV2, constants.CHANGELOGV2): | ||||
r48008 | return None | |||
r48029 | comp = util.compengines[revlog._compengine].revlogheader() | |||
docket = RevlogDocket( | ||||
revlog, | ||||
version_header=version_header, | ||||
default_compression_header=comp, | ||||
) | ||||
r48008 | docket._dirty = True | |||
return docket | ||||
r48015 | def parse_docket(revlog, data, use_pending=False): | |||
r48008 | """given some docket data return a docket object for the given revlog""" | |||
header = S_HEADER.unpack(data[: S_HEADER.size]) | ||||
r48016 | version_header = header[0] | |||
index_size = header[1] | ||||
pending_index_size = header[2] | ||||
data_size = header[3] | ||||
pending_data_size = header[4] | ||||
r48029 | default_compression_header = header[5] | |||
r48008 | docket = RevlogDocket( | |||
revlog, | ||||
r48015 | use_pending=use_pending, | |||
r48008 | version_header=version_header, | |||
r48012 | index_end=index_size, | |||
r48015 | pending_index_end=pending_index_size, | |||
r48016 | data_end=data_size, | |||
pending_data_end=pending_data_size, | ||||
r48029 | default_compression_header=default_compression_header, | |||
r48008 | ) | |||
return docket | ||||