Patchwork [07,of,13] revlog: move index file opening in a method

login
register
mail settings
Submitter Boris Feld
Date Feb. 6, 2018, 1:21 p.m.
Message ID <d238ec45ba25f7c1ea9e.1517923288@FB>
Download mbox | patch
Permalink /patch/27359/
State New
Headers show

Comments

Boris Feld - Feb. 6, 2018, 1:21 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1517847733 -3600
#      Mon Feb 05 17:22:13 2018 +0100
# Node ID d238ec45ba25f7c1ea9ec22aa11a4ec699c72740
# Parent  b192ee27b376523292ec1cf52c49c9382a870658
# EXP-Topic revlog-fp
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r d238ec45ba25
revlog: move index file opening in a method

Having file operation centralized into a single spot help to factor common
logic out (eg: special flag handling according to the mode).

It is also the first step to simplify file handling during batch operation
(eg: revlog cloning). However, that part does not seems to be a hotspot yet.
Yuya Nishihara - Feb. 7, 2018, 2:07 p.m.
On Tue, 06 Feb 2018 14:21:28 +0100, Boris Feld wrote:
> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1517847733 -3600
> #      Mon Feb 05 17:22:13 2018 +0100
> # Node ID d238ec45ba25f7c1ea9ec22aa11a4ec699c72740
> # Parent  b192ee27b376523292ec1cf52c49c9382a870658
> # EXP-Topic revlog-fp
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r d238ec45ba25
> revlog: move index file opening in a method

Queud the series, thanks.

> @@ -682,6 +682,15 @@ class revlog(object):
>      def _compressor(self):
>          return util.compengines[self._compengine].revlogcompressor()
>  
> +    def _indexfp(self, mode='r'):
> +        """file object for the revlog's index file"""
> +        args = {'mode': mode}
> +        if mode != 'r':
> +            args['checkambig'] = self._checkambig
> +        if mode == 'w':
> +            args['atomictemp'] = True
> +        return self.opener(self.indexfile, **args)

Added r'' in flight to get around py3 transformer.

> -        fp = self.opener(self.indexfile, 'w', atomictemp=True,
> -                         checkambig=self._checkambig)
> +        fp = self._indexfp('w')

I don't think it's good idea to make 'w' imply atomictemp=True because here
the atomic-temp semantics is really important, but the series looks good as
a whole.

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -621,7 +621,7 @@  class revlog(object):
         indexdata = ''
         self._initempty = True
         try:
-            f = self.opener(self.indexfile)
+            f = self._indexfp()
             if (mmapindexthreshold is not None and
                     self.opener.fstat(f).st_size >= mmapindexthreshold):
                 indexdata = util.buffer(util.mmapread(f))
@@ -682,6 +682,15 @@  class revlog(object):
     def _compressor(self):
         return util.compengines[self._compengine].revlogcompressor()
 
+    def _indexfp(self, mode='r'):
+        """file object for the revlog's index file"""
+        args = {'mode': mode}
+        if mode != 'r':
+            args['checkambig'] = self._checkambig
+        if mode == 'w':
+            args['atomictemp'] = True
+        return self.opener(self.indexfile, **args)
+
     def _datafp(self, mode='r'):
         """file object for the revlog's data file"""
         return self.opener(self.datafile, mode=mode)
@@ -1498,7 +1507,7 @@  class revlog(object):
             closehandle = False
         else:
             if self._inline:
-                df = self.opener(self.indexfile)
+                df = self._indexfp()
             else:
                 df = self._datafp()
             closehandle = True
@@ -1858,8 +1867,7 @@  class revlog(object):
         finally:
             df.close()
 
-        fp = self.opener(self.indexfile, 'w', atomictemp=True,
-                         checkambig=self._checkambig)
+        fp = self._indexfp('w')
         self.version &= ~FLAG_INLINE_DATA
         self._inline = False
         for i in self:
@@ -1928,7 +1936,7 @@  class revlog(object):
         dfh = None
         if not self._inline:
             dfh = self._datafp("a+")
-        ifh = self.opener(self.indexfile, "a+", checkambig=self._checkambig)
+        ifh = self._indexfp("a+")
         try:
             return self._addrevision(node, rawtext, transaction, link, p1, p2,
                                      flags, cachedelta, ifh, dfh,
@@ -2157,7 +2165,7 @@  class revlog(object):
         end = 0
         if r:
             end = self.end(r - 1)
-        ifh = self.opener(self.indexfile, "a+", checkambig=self._checkambig)
+        ifh = self._indexfp("a+")
         isize = r * self._io.size
         if self._inline:
             transaction.add(self.indexfile, end + isize, r)
@@ -2229,8 +2237,7 @@  class revlog(object):
                     # reopen the index
                     ifh.close()
                     dfh = self._datafp("a+")
-                    ifh = self.opener(self.indexfile, "a+",
-                                      checkambig=self._checkambig)
+                    ifh = self._indexfp("a+")
         finally:
             if dfh:
                 dfh.close()