From patchwork Mon May 3 12:07:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D10595: revlog: move index reading logic in a dedicated method From: phabricator X-Patchwork-Id: 48915 Message-Id: To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Mon, 3 May 2021 12:07:43 +0000 marmoute created this revision. Herald added a reviewer: indygreg. Herald added a reviewer: hg-reviewers. Herald added a subscriber: mercurial-patches. REVISION SUMMARY They are multiple motivation to do it: - The logic is complicated enough to deserver its own method. - We will need to reuse this once we put a docket in use. - This split the actual reading from the processing of the read data better. REPOSITORY rHG Mercurial BRANCH default REVISION DETAIL https://phab.mercurial-scm.org/D10595 AFFECTED FILES mercurial/revlog.py CHANGE DETAILS To: marmoute, indygreg, #hg-reviewers Cc: mercurial-patches, mercurial-devel diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -446,6 +446,24 @@ force_nodemap = opts.get(b'devel-force-nodemap', False) return newversionflags, mmapindexthreshold, force_nodemap + def _get_data(self, filepath, mmap_threshold): + """return a file content with or without mmap + + If the file is missing return the empty string""" + try: + with self.opener(filepath) as fp: + if mmap_threshold is not None: + file_size = self.opener.fstat(fp).st_size + if file_size >= mmap_threshold: + # TODO: should .close() to release resources without + # relying on Python GC + return util.buffer(util.mmapread(fp)) + return fp.read() + except IOError as inst: + if inst.errno != errno.ENOENT: + raise + return b'' + def _loadindex(self): newversionflags, mmapindexthreshold, force_nodemap = self._init_opts() @@ -465,26 +483,11 @@ indexdata = b'' self._initempty = True - try: - with self._indexfp() as f: - if ( - mmapindexthreshold is not None - and self.opener.fstat(f).st_size >= mmapindexthreshold - ): - # TODO: should .close() to release resources without - # relying on Python GC - indexdata = util.buffer(util.mmapread(f)) - else: - indexdata = f.read() - if len(indexdata) > 0: - versionflags = INDEX_HEADER.unpack(indexdata[:4])[0] - self._initempty = False - else: - versionflags = newversionflags - except IOError as inst: - if inst.errno != errno.ENOENT: - raise - + indexdata = self._get_data(self._indexfile, mmapindexthreshold) + if len(indexdata) > 0: + versionflags = INDEX_HEADER.unpack(indexdata[:4])[0] + self._initempty = False + else: versionflags = newversionflags flags = self._format_flags = versionflags & ~0xFFFF