cache: introduce a changelogsourcebase class
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 22 Nov 2017 13:40:47 +0100
changeset 3238 88f11b9881b2
parent 3237 9a2cc4687cb9
child 3239 0c8c7b5274a5
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. This code was headed for core during the 4.3 cycle but never made it there. So we starts with a copy in the evolve repository.
hgext3rd/evolve/genericcaches.py
--- a/hgext3rd/evolve/genericcaches.py	Wed Nov 22 13:40:05 2017 +0100
+++ b/hgext3rd/evolve/genericcaches.py	Wed Nov 22 13:40:47 2017 +0100
@@ -10,6 +10,7 @@
 import struct
 
 from mercurial 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)