D6233: rust-discovery: implementing and exposing stats()

gracinet (Georges Racinet) phabricator at mercurial-scm.org
Wed Apr 17 08:17:17 EDT 2019


gracinet updated this revision to Diff 14795.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6233?vs=14745&id=14795

REVISION DETAIL
  https://phab.mercurial-scm.org/D6233

AFFECTED FILES
  rust/hg-core/src/discovery.rs
  rust/hg-cpython/src/discovery.rs
  tests/test-rust-discovery.py

CHANGE DETAILS

diff --git a/tests/test-rust-discovery.py b/tests/test-rust-discovery.py
--- a/tests/test-rust-discovery.py
+++ b/tests/test-rust-discovery.py
@@ -82,6 +82,14 @@
 
         self.assertEqual(disco.commonheads(), {1})
 
+    def testaddmissingsstats(self):
+        idx = self.parseindex()
+        disco = PartialDiscovery(idx, [3])
+        self.assertIsNone(disco.stats()['undecided'], None)
+
+        disco.addmissings([2])
+        self.assertEqual(disco.stats()['undecided'], 2)
+
     def testaddinfocommonfirst(self):
         idx = self.parseindex()
         disco = PartialDiscovery(idx, [3])
diff --git a/rust/hg-cpython/src/discovery.rs b/rust/hg-cpython/src/discovery.rs
--- a/rust/hg-cpython/src/discovery.rs
+++ b/rust/hg-cpython/src/discovery.rs
@@ -14,7 +14,9 @@
 
 use crate::conversion::{py_set, rev_pyiter_collect};
 use cindex::Index;
-use cpython::{ObjectProtocol, PyDict, PyModule, PyObject, PyResult, Python};
+use cpython::{
+    ObjectProtocol, PyDict, PyModule, PyObject, PyResult, Python, ToPyObject,
+};
 use exceptions::GraphError;
 use hg::discovery::PartialDiscovery as CorePartialDiscovery;
 use hg::Revision;
@@ -82,6 +84,15 @@
         Ok(self.inner(py).borrow().is_complete())
     }
 
+    def stats(&self) -> PyResult<PyDict> {
+        let stats = self.inner(py).borrow().stats();
+        let as_dict: PyDict = PyDict::new(py);
+        as_dict.set_item(py, "undecided",
+                         stats.undecided.map(|l| l.to_py_object(py))
+                              .unwrap_or_else(|| py.None()))?;
+        Ok(as_dict)
+    }
+
     def commonheads(&self) -> PyResult<PyObject> {
         py_set(
             py,
diff --git a/rust/hg-core/src/discovery.rs b/rust/hg-core/src/discovery.rs
--- a/rust/hg-core/src/discovery.rs
+++ b/rust/hg-core/src/discovery.rs
@@ -23,6 +23,10 @@
     missing: HashSet<Revision>,
 }
 
+pub struct DiscoveryStats {
+    pub undecided: Option<usize>,
+}
+
 impl<G: Graph + Clone> PartialDiscovery<G> {
     /// Create a PartialDiscovery object, with the intent
     /// of comparing our `::<target_heads>` revset to the contents of another
@@ -119,6 +123,13 @@
             Some(self.common.missing_ancestors(tgt)?.into_iter().collect());
         Ok(())
     }
+
+    /// Provide statistics about the current state of the discovery process
+    pub fn stats(&self) -> DiscoveryStats {
+        DiscoveryStats {
+            undecided: self.undecided.as_ref().map(|s| s.len()),
+        }
+    }
 }
 
 #[cfg(test)]
@@ -161,6 +172,7 @@
         let mut disco = full_disco();
         assert_eq!(disco.undecided, None);
         assert!(!disco.has_info());
+        assert_eq!(disco.stats().undecided, None);
 
         disco.add_common_revisions(vec![11, 12])?;
         assert!(disco.has_info());
@@ -172,6 +184,7 @@
         assert_eq!(disco.undecided, None);
         disco.ensure_undecided()?;
         assert_eq!(sorted_undecided(&disco), vec![5, 8, 10, 13]);
+        assert_eq!(disco.stats().undecided, Some(4));
         Ok(())
     }
 



To: gracinet, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list