[PATCH 1 of 5] rank: naive rank property computation and retrieval

pacien pacien.trangirard at pacien.net
Mon Jan 31 14:03:44 UTC 2022


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at octobus.net>
# Date 1643365981 -3600
#      Fri Jan 28 11:33:01 2022 +0100
# Node ID ff043e0e1aacc81f8d5238f89bed5a6b355ba9cf
# Parent  4afb9627dc77264d3a8b0c9c1ccc8bff9ffd6faa
# EXP-Topic cl2-rank
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.

The value is computed in a naive way from the definition of the rank. This will
be replaced by a more efficient version later on.

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -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,
@@ -872,6 +874,20 @@
 
         return len(self.revision(rev, raw=False))
 
+    def fast_rank(self, rev):
+        """The "rank" of the revision if available, None otherwise
+
+        The rank of a revision is the size of sub-graph it defines as a head.
+        In other words, the rank og X i sthe size of `ancestors(X)` (X
+        included).
+
+        Some variant of revlog persist this value and make it available.
+        """
+        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:
@@ -2472,6 +2488,11 @@
             # than ones we manually add.
             sidedata_offset = 0
 
+        # maybe the index.append should compute it when applicable instead
+        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,
@@ -2486,6 +2507,7 @@
             sidedata_offset=sidedata_offset,
             sidedata_compressed_length=len(serialized_sidedata),
             sidedata_compression_mode=sidedata_compression_mode,
+            rank=rank,
         )
 
         self.index.append(e)


More information about the Mercurial-devel mailing list