Patchwork D10610: revlog: only use the `_indexfp` method for read operation

login
register
mail settings
Submitter phabricator
Date May 3, 2021, 12:07 p.m.
Message ID <differential-rev-PHID-DREV-thy7dn52qsydrd6rxdwa-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48930/
State New
Headers show

Comments

phabricator - May 3, 2021, 12:07 p.m.
marmoute created this revision.
Herald added a reviewer: indygreg.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This will avoid "other" code to not overlook `_writing` usage. We introduces
  private method dedicated to writing to make use the right option are always
  used.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10610

AFFECTED FILES
  mercurial/revlog.py

CHANGE DETAILS




To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -613,14 +613,33 @@ 
         engine = util.compengines[self._compengine]
         return engine.revlogcompressor(self._compengineopts)
 
-    def _indexfp(self, mode=b'r'):
+    def _indexfp(self):
         """file object for the revlog's index file"""
-        args = {'mode': mode}
-        if mode != b'r':
-            args['checkambig'] = self._checkambig
-        if mode == b'w':
-            args['atomictemp'] = True
-        return self.opener(self._indexfile, **args)
+        return self.opener(self._indexfile, mode=b"r")
+
+    def __index_write_fp(self):
+        # You should not use this directly and use `_writing` instead
+        try:
+            f = self.opener(
+                self._indexfile, mode=b"r+", checkambig=self._checkambig
+            )
+            f.seek(0, os.SEEK_END)
+            return f
+        except IOError as inst:
+            if inst.errno != errno.ENOENT:
+                raise
+            return self.opener(
+                self._indexfile, mode=b"w+", checkambig=self._checkambig
+            )
+
+    def __index_new_fp(self):
+        # You should not use this unless you are upgrading from inline revlog
+        return self.opener(
+            self._indexfile,
+            mode=b"w",
+            checkambig=self._checkambig,
+            atomictemp=True,
+        )
 
     def _datafp(self, mode=b'r'):
         """file object for the revlog's data file"""
@@ -1990,14 +2009,14 @@ 
         new_dfh = self._datafp(b'w+')
         new_dfh.truncate(0)  # drop any potentially existing data
         try:
-            with self._indexfp(b'r') as read_ifh:
+            with self._indexfp() as read_ifh:
                 for r in self:
                     new_dfh.write(self._getsegmentforrevs(r, r, df=read_ifh)[1])
                     if troffset <= self.start(r):
                         trindex = r
                 new_dfh.flush()
 
-            with self.opener(self._indexfile, mode=b'w', atomictemp=True) as fp:
+            with self.__index_new_fp() as fp:
                 self._format_flags &= ~FLAG_INLINE_DATA
                 self._inline = False
                 for i in self:
@@ -2016,7 +2035,7 @@ 
 
             if existing_handles:
                 # switched from inline to conventional reopen the index
-                ifh = self._indexfp(b"r+")
+                ifh = self.__index_write_fp()
                 self._writinghandles = (ifh, new_dfh)
                 new_dfh = None
         finally:
@@ -2047,13 +2066,7 @@ 
                 transaction.add(self._datafile, dsize)
             try:
                 isize = r * self.index.entry_size
-                try:
-                    ifh = self._indexfp(b"r+")
-                    ifh.seek(0, os.SEEK_END)
-                except IOError as inst:
-                    if inst.errno != errno.ENOENT:
-                        raise
-                    ifh = self._indexfp(b"w+")
+                ifh = self.__index_write_fp()
                 if self._inline:
                     transaction.add(self._indexfile, dsize + isize)
                 else: