Patchwork D6425: rust-discovery: exposing sampling to python

login
register
mail settings
Submitter phabricator
Date May 22, 2019, 4:50 p.m.
Message ID <differential-rev-PHID-DREV-cekirzqidm7rh76qv4cn-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/40185/
State Superseded
Headers show

Comments

phabricator - May 22, 2019, 4:50 p.m.
gracinet created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  rust/hg-cpython/src/discovery.rs

CHANGE DETAILS




To: gracinet, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel
phabricator - May 22, 2019, 4:54 p.m.
gracinet added a comment.


  @kevincox this used to be https://phab.mercurial-scm.org/D6261

REPOSITORY
  rHG Mercurial

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

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

Patch

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
@@ -15,7 +15,7 @@ 
 use crate::conversion::{py_set, rev_pyiter_collect};
 use cindex::Index;
 use cpython::{
-    ObjectProtocol, PyDict, PyModule, PyObject, PyResult, Python,
+    ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python,
     PythonObject, ToPyObject,
 };
 use exceptions::GraphError;
@@ -103,6 +103,32 @@ 
                 .map_err(|e| GraphError::pynew(py, e))?
         )
     }
+
+    def takefullsample(&self, _headrevs: PyObject,
+                       size: usize) -> PyResult<PyObject> {
+        let mut inner = self.inner(py).borrow_mut();
+        let sample = inner.take_full_sample(size)
+            .map_err(|e| GraphError::pynew(py, e))?;
+        let as_vec: Vec<PyObject> = sample
+            .iter()
+            .map(|rev| rev.to_py_object(py).into_object())
+            .collect();
+        Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
+    }
+
+    def takequicksample(&self, headrevs: PyObject,
+                        size: usize) -> PyResult<PyObject> {
+        let mut inner = self.inner(py).borrow_mut();
+        let revsvec: Vec<Revision> = rev_pyiter_collect(py, &headrevs)?;
+        let sample = inner.take_quick_sample(revsvec, size)
+            .map_err(|e| GraphError::pynew(py, e))?;
+        let as_vec: Vec<PyObject> = sample
+            .iter()
+            .map(|rev| rev.to_py_object(py).into_object())
+            .collect();
+        Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
+    }
+
 });
 
 /// Create the module, with __package__ given from parent