rank: naive rank property computation and retrieval
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 28 Jan 2022 11:33:01 +0100
changeset 48761 2e949ede7350
parent 48760 93d6f0e7ba2f
child 48762 d5121df04808
rank: naive rank property computation and retrieval This stores the rank (size of the ancestor set of a revision, including itself) in a changelog field and allows this property to be retrieved. This new property is used as part of stable-range computations, which will be introduced later on. The value is computed in a naive way from the definition of the rank. This will be replaced by a more efficient version subsequently. Differential Revision: https://phab.mercurial-scm.org/D12139
mercurial/revlog.py
--- a/mercurial/revlog.py	Mon Feb 07 22:54:38 2022 -0800
+++ b/mercurial/revlog.py	Fri Jan 28 11:33:01 2022 +0100
@@ -40,11 +40,13 @@
     COMP_MODE_DEFAULT,
     COMP_MODE_INLINE,
     COMP_MODE_PLAIN,
+    ENTRY_RANK,
     FEATURES_BY_VERSION,
     FLAG_GENERALDELTA,
     FLAG_INLINE_DATA,
     INDEX_HEADER,
     KIND_CHANGELOG,
+    RANK_UNKNOWN,
     REVLOGV0,
     REVLOGV1,
     REVLOGV1_FLAGS,
@@ -857,6 +859,22 @@
 
         return len(self.revision(rev))
 
+    def fast_rank(self, rev):
+        """Return the rank of a revision if already known, or None otherwise.
+
+        The rank of a revision is the size of the sub-graph it defines as a
+        head. Equivalently, the rank of a revision `r` is the size of the set
+        `ancestors(r)`, `r` included.
+
+        This method returns the rank retrieved from the revlog in constant
+        time. It makes no attempt at computing unknown values for versions of
+        the revlog which do not persist the rank.
+        """
+        rank = self.index[rev][ENTRY_RANK]
+        if rank == RANK_UNKNOWN:
+            return None
+        return rank
+
     def chainbase(self, rev):
         base = self._chainbasecache.get(rev)
         if base is not None:
@@ -2444,6 +2462,10 @@
             # than ones we manually add.
             sidedata_offset = 0
 
+        rank = RANK_UNKNOWN
+        if self._format_version == CHANGELOGV2:
+            rank = len(list(self.ancestors([p1r, p2r], inclusive=True))) + 1
+
         e = revlogutils.entry(
             flags=flags,
             data_offset=offset,
@@ -2458,6 +2480,7 @@
             sidedata_offset=sidedata_offset,
             sidedata_compressed_length=len(serialized_sidedata),
             sidedata_compression_mode=sidedata_compression_mode,
+            rank=rank,
         )
 
         self.index.append(e)