Patchwork [02,of,14] cache: introduce a changelogsourcebase class

login
register
mail settings
Submitter Boris Feld
Date July 9, 2017, 5:55 p.m.
Message ID <8b71290526ddb77f157e.1499622914@FB>
Download mbox | patch
Permalink /patch/22162/
State Changes Requested
Headers show

Comments

Boris Feld - July 9, 2017, 5:55 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1499458552 -7200
#      Fri Jul 07 22:15:52 2017 +0200
# Node ID 8b71290526ddb77f157e075191dd748793d85601
# Parent  6edb62505c697329de034c2fdc47befd5896f31f
# EXP-Topic obs-cache
cache: introduce a changelogsourcebase class

This abstract class will help code that need a cache tracking the changelog
content (eg: the branchmap cache). The cache key used is the same as what the
branchmap uses.
Augie Fackler - July 14, 2017, 6:04 p.m.
On Sun, Jul 09, 2017 at 07:55:14PM +0200, Boris Feld wrote:
> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1499458552 -7200
> #      Fri Jul 07 22:15:52 2017 +0200
> # Node ID 8b71290526ddb77f157e075191dd748793d85601
> # Parent  6edb62505c697329de034c2fdc47befd5896f31f
> # EXP-Topic obs-cache
> cache: introduce a changelogsourcebase class

These two are interesting on their own, assuming we've got existing
caches that could be refactored.

>
> This abstract class will help code that need a cache tracking the changelog
> content (eg: the branchmap cache). The cache key used is the same as what the
> branchmap uses.

This sounds like we could get these two in by migrating the branchmap
cache to use them?

>
> diff -r 6edb62505c69 -r 8b71290526dd mercurial/cache.py
> --- a/mercurial/cache.py	Fri Jul 07 22:14:01 2017 +0200
> +++ b/mercurial/cache.py	Fri Jul 07 22:15:52 2017 +0200
> @@ -10,6 +10,7 @@
>  import struct
>
>  from . import (
> +    node,
>      util,
>  )
>
> @@ -125,3 +126,41 @@
>      def _deserializecachekey(self, data):
>          """read the cachekey from bytes"""
>          return self._cachekeystruct.unpack(data)
> +
> +class changelogsourcebase(incrementalcachebase):
> +    """an abstract class for cache sourcing data from the changelog
> +
> +    For this purpose it use a cache key covering changelog content.
> +    The cache key parts are: (tiprev, tipnode)
> +    """
> +
> +    __metaclass__ = abc.ABCMeta
> +
> +    # default key used for an empty cache
> +    emptykey = (0, node.nullid)
> +    _cachekeyspec = 'i20s'
> +    _cachename = None # used for debug message
> +
> +    # Useful "public" function (no need to override them)
> +
> +    def _fetchchangelogdata(self, cachekey, cl):
> +        """use a cachekey to fetch incremental data
> +
> +        Exists as its own method to help subclass to reuse it."""
> +        tiprev = len(cl) - 1
> +        tipnode = cl.node(tiprev)
> +        newkey = (tiprev, tipnode)
> +        tiprev = len(cl) - 1
> +        if newkey == cachekey:
> +            return False, [], newkey
> +        keyrev, keynode = cachekey
> +        if tiprev < keyrev or cl.node(keyrev) != keynode:
> +            revs = ()
> +            if len(cl):
> +                revs = list(cl.revs(stop=tiprev))
> +            return True, revs, newkey
> +        else:
> +            return False, list(cl.revs(start=keyrev + 1, stop=tiprev)), newkey
> +
> +    def _fetchupdatedata(self, repo):
> +        return self._fetchchangelogdata(self._cachekey, repo.changelog)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff -r 6edb62505c69 -r 8b71290526dd mercurial/cache.py
--- a/mercurial/cache.py	Fri Jul 07 22:14:01 2017 +0200
+++ b/mercurial/cache.py	Fri Jul 07 22:15:52 2017 +0200
@@ -10,6 +10,7 @@ 
 import struct
 
 from . import (
+    node,
     util,
 )
 
@@ -125,3 +126,41 @@ 
     def _deserializecachekey(self, data):
         """read the cachekey from bytes"""
         return self._cachekeystruct.unpack(data)
+
+class changelogsourcebase(incrementalcachebase):
+    """an abstract class for cache sourcing data from the changelog
+
+    For this purpose it use a cache key covering changelog content.
+    The cache key parts are: (tiprev, tipnode)
+    """
+
+    __metaclass__ = abc.ABCMeta
+
+    # default key used for an empty cache
+    emptykey = (0, node.nullid)
+    _cachekeyspec = 'i20s'
+    _cachename = None # used for debug message
+
+    # Useful "public" function (no need to override them)
+
+    def _fetchchangelogdata(self, cachekey, cl):
+        """use a cachekey to fetch incremental data
+
+        Exists as its own method to help subclass to reuse it."""
+        tiprev = len(cl) - 1
+        tipnode = cl.node(tiprev)
+        newkey = (tiprev, tipnode)
+        tiprev = len(cl) - 1
+        if newkey == cachekey:
+            return False, [], newkey
+        keyrev, keynode = cachekey
+        if tiprev < keyrev or cl.node(keyrev) != keynode:
+            revs = ()
+            if len(cl):
+                revs = list(cl.revs(stop=tiprev))
+            return True, revs, newkey
+        else:
+            return False, list(cl.revs(start=keyrev + 1, stop=tiprev)), newkey
+
+    def _fetchupdatedata(self, repo):
+        return self._fetchchangelogdata(self._cachekey, repo.changelog)