changeset 45534:4f11a67a12fb

hg-core: add `Revlog.get_node_rev` Find the revision of a node given its full hash. Differential Revision: https://phab.mercurial-scm.org/D9012
author Antoine Cezar <antoine.cezar@octobus.net>
date Fri, 18 Sep 2020 16:52:08 +0200
parents 89ac95bd4993
children 72b7d58d6e35
files rust/hg-core/src/revlog/revlog.rs
diffstat 1 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/revlog.rs	Wed Sep 09 14:50:58 2020 +0200
+++ b/rust/hg-core/src/revlog/revlog.rs	Fri Sep 18 16:52:08 2020 +0200
@@ -79,6 +79,32 @@
         })
     }
 
+    /// Return number of entries of the `Revlog`.
+    pub fn len(&self) -> usize {
+        self.index().len()
+    }
+
+    /// Returns `true` if the `Revlog` has zero `entries`.
+    pub fn is_empty(&self) -> bool {
+        self.index().is_empty()
+    }
+
+    /// Return the full data associated to a node.
+    #[timed]
+    pub fn get_node_rev(&self, node: &[u8]) -> Result<Revision, RevlogError> {
+        let index = self.index();
+        // This is brute force. But it is fast enough for now.
+        // Optimization will come later.
+        for rev in (0..self.len() as Revision).rev() {
+            let index_entry =
+                index.get_entry(rev).ok_or(RevlogError::Corrupted)?;
+            if node == index_entry.hash() {
+                return Ok(rev);
+            }
+        }
+        Err(RevlogError::InvalidRevision)
+    }
+
     /// Return the full data associated to a revision.
     ///
     /// All entries required to build the final data out of deltas will be