| 1 |
//! Discovery of common node sets |
| 2 |
use std::collections::HashSet; |
| 3 |
|
| 4 |
use hg::discovery::PartialDiscovery as CorePartialDiscovery; |
| 5 |
use hg::Revision; |
| 6 |
use pyo3::intern; |
| 7 |
use pyo3::pyclass; |
| 8 |
use pyo3::pymethods; |
| 9 |
use pyo3::types::PyAnyMethods; |
| 10 |
use pyo3::types::PyDict; |
| 11 |
use pyo3::types::PyModule; |
| 12 |
use pyo3::types::PyModuleMethods; |
| 13 |
use pyo3::types::PyTuple; |
| 14 |
use pyo3::Bound; |
| 15 |
use pyo3::Py; |
| 16 |
use pyo3::PyAny; |
| 17 |
use pyo3::PyResult; |
| 18 |
use pyo3::Python; |
| 19 |
use pyo3_sharedref::SharedByPyObject; |
| 20 |
|
| 21 |
use crate::exceptions::GraphError; |
| 22 |
use crate::revision::rev_pyiter_collect; |
| 23 |
use crate::revision::PyRevision; |
| 24 |
use crate::revlog::PySharedIndex; |
| 25 |
use crate::utils::new_submodule; |
| 26 |
use crate::utils::py_rust_index_to_graph; |
| 27 |
|
| 28 |
#[pyclass] |
| 29 |
struct PartialDiscovery { |
| 30 |
inner: SharedByPyObject<CorePartialDiscovery<PySharedIndex>>, |
| 31 |
idx: SharedByPyObject<PySharedIndex>, |
| 32 |
} |
| 33 |
|
| 34 |
#[pymethods] |
| 35 |
impl PartialDiscovery { |
| 36 |
#[pyo3(signature = (repo, targetheads, respectsize, randomize=true))] |
| 37 |
#[new] |
| 38 |
fn new( |
| 39 |
py: Python, |
| 40 |
repo: &Bound<'_, PyAny>, |
| 41 |
targetheads: &Bound<'_, PyAny>, |
| 42 |
respectsize: bool, |
| 43 |
randomize: bool, |
| 44 |
) -> PyResult<Self> { |
| 45 |
let index = repo |
| 46 |
.getattr(intern!(py, "changelog"))? |
| 47 |
.getattr(intern!(py, "index"))?; |
| 48 |
let cloned_index = py_rust_index_to_graph(&index.clone())?; |
| 49 |
let index = py_rust_index_to_graph(&index)?; |
| 50 |
|
| 51 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 52 |
// `SharedByPyObject` |
| 53 |
let target_heads = { |
| 54 |
let borrowed_idx = unsafe { index.try_borrow(py)? }; |
| 55 |
rev_pyiter_collect(targetheads, &*borrowed_idx)? |
| 56 |
}; |
| 57 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 58 |
// `SharedByPyObject` |
| 59 |
let lazy_disco = unsafe { |
| 60 |
index.map(py, |idx| { |
| 61 |
CorePartialDiscovery::new( |
| 62 |
idx, |
| 63 |
target_heads, |
| 64 |
respectsize, |
| 65 |
randomize, |
| 66 |
) |
| 67 |
}) |
| 68 |
}; |
| 69 |
Ok(Self { inner: lazy_disco, idx: cloned_index }) |
| 70 |
} |
| 71 |
|
| 72 |
fn addcommons( |
| 73 |
&mut self, |
| 74 |
py: Python, |
| 75 |
commons: &Bound<'_, PyAny>, |
| 76 |
) -> PyResult<Py<PyAny>> { |
| 77 |
let commons = self.pyiter_to_vec(commons)?; |
| 78 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 79 |
// `SharedByPyObject` |
| 80 |
let mut inner = unsafe { self.inner.try_borrow_mut(py)? }; |
| 81 |
inner.add_common_revisions(commons).map_err(GraphError::from_hg)?; |
| 82 |
Ok(py.None()) |
| 83 |
} |
| 84 |
|
| 85 |
fn addmissings( |
| 86 |
&mut self, |
| 87 |
py: Python, |
| 88 |
missings: &Bound<'_, PyAny>, |
| 89 |
) -> PyResult<Py<PyAny>> { |
| 90 |
let missings = self.pyiter_to_vec(missings)?; |
| 91 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 92 |
// `SharedByPyObject` |
| 93 |
let mut inner = unsafe { self.inner.try_borrow_mut(py)? }; |
| 94 |
inner.add_missing_revisions(missings).map_err(GraphError::from_hg)?; |
| 95 |
Ok(py.None()) |
| 96 |
} |
| 97 |
|
| 98 |
fn addinfo( |
| 99 |
&mut self, |
| 100 |
py: Python, |
| 101 |
sample: &Bound<'_, PyAny>, |
| 102 |
) -> PyResult<Py<PyAny>> { |
| 103 |
let mut missing: Vec<Revision> = vec![]; |
| 104 |
let mut common: Vec<Revision> = vec![]; |
| 105 |
for info in sample.try_iter()? { |
| 106 |
// info is a pair (Revision, bool) |
| 107 |
let info = info?; |
| 108 |
let info = info.cast::<PyTuple>()?; |
| 109 |
let rev: PyRevision = info.get_item(0)?.extract()?; |
| 110 |
// This is fine since we're just using revisions as integers |
| 111 |
// for the purposes of discovery |
| 112 |
let rev = Revision(rev.0); |
| 113 |
let known: bool = info.get_item(1)?.extract()?; |
| 114 |
if known { |
| 115 |
common.push(rev); |
| 116 |
} else { |
| 117 |
missing.push(rev); |
| 118 |
} |
| 119 |
} |
| 120 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 121 |
// `SharedByPyObject` |
| 122 |
let mut inner = unsafe { self.inner.try_borrow_mut(py)? }; |
| 123 |
inner.add_common_revisions(common).map_err(GraphError::from_hg)?; |
| 124 |
inner.add_missing_revisions(missing).map_err(GraphError::from_hg)?; |
| 125 |
Ok(py.None()) |
| 126 |
} |
| 127 |
|
| 128 |
fn hasinfo(&self, py: Python<'_>) -> PyResult<bool> { |
| 129 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 130 |
// `SharedByPyObject` |
| 131 |
let inner = unsafe { self.inner.try_borrow(py)? }; |
| 132 |
Ok(inner.has_info()) |
| 133 |
} |
| 134 |
|
| 135 |
fn iscomplete(&self, py: Python<'_>) -> PyResult<bool> { |
| 136 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 137 |
// `SharedByPyObject` |
| 138 |
let inner = unsafe { self.inner.try_borrow(py)? }; |
| 139 |
Ok(inner.is_complete()) |
| 140 |
} |
| 141 |
|
| 142 |
fn stats(&self, py: Python<'_>) -> PyResult<Py<PyDict>> { |
| 143 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 144 |
// `SharedByPyObject` |
| 145 |
let inner = unsafe { self.inner.try_borrow(py)? }; |
| 146 |
let stats = inner.stats(); |
| 147 |
let as_dict = PyDict::new(py); |
| 148 |
as_dict.set_item("undecided", stats.undecided)?; |
| 149 |
Ok(as_dict.unbind()) |
| 150 |
} |
| 151 |
|
| 152 |
fn commonheads(&self, py: Python<'_>) -> PyResult<HashSet<PyRevision>> { |
| 153 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 154 |
// `SharedByPyObject` |
| 155 |
let inner = unsafe { self.inner.try_borrow(py)? }; |
| 156 |
let common_heads = inner.common_heads().map_err(GraphError::from_hg)?; |
| 157 |
Ok(common_heads.into_iter().map(Into::into).collect()) |
| 158 |
} |
| 159 |
|
| 160 |
fn takefullsample( |
| 161 |
&mut self, |
| 162 |
py: Python, |
| 163 |
_headrevs: &Bound<'_, PyAny>, |
| 164 |
size: usize, |
| 165 |
) -> PyResult<Py<PyTuple>> { |
| 166 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 167 |
// `SharedByPyObject` |
| 168 |
let mut inner = unsafe { self.inner.try_borrow_mut(py)? }; |
| 169 |
let sample = |
| 170 |
inner.take_full_sample(size).map_err(GraphError::from_hg)?; |
| 171 |
let as_pyrevision = sample.into_iter().map(|rev| PyRevision(rev.0)); |
| 172 |
Ok(PyTuple::new(py, as_pyrevision)?.unbind()) |
| 173 |
} |
| 174 |
|
| 175 |
fn takequicksample( |
| 176 |
&mut self, |
| 177 |
py: Python, |
| 178 |
headrevs: &Bound<'_, PyAny>, |
| 179 |
size: usize, |
| 180 |
) -> PyResult<Py<PyTuple>> { |
| 181 |
let revs = self.pyiter_to_vec(headrevs)?; |
| 182 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 183 |
// `SharedByPyObject` |
| 184 |
let mut inner = unsafe { self.inner.try_borrow_mut(py)? }; |
| 185 |
let sample = |
| 186 |
inner.take_quick_sample(revs, size).map_err(GraphError::from_hg)?; |
| 187 |
let as_pyrevision = sample.into_iter().map(|rev| PyRevision(rev.0)); |
| 188 |
Ok(PyTuple::new(py, as_pyrevision)?.unbind()) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl PartialDiscovery { |
| 193 |
/// Convert a Python iterator of revisions into a vector |
| 194 |
fn pyiter_to_vec( |
| 195 |
&self, |
| 196 |
iter: &Bound<'_, PyAny>, |
| 197 |
) -> PyResult<Vec<Revision>> { |
| 198 |
// Safety: we don't leak any reference derived form the "faked" one in |
| 199 |
// `SharedByPyObject` |
| 200 |
let index = unsafe { self.idx.try_borrow(iter.py())? }; |
| 201 |
rev_pyiter_collect(iter, &*index) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
pub fn init_module<'py>( |
| 206 |
py: Python<'py>, |
| 207 |
package: &str, |
| 208 |
) -> PyResult<Bound<'py, PyModule>> { |
| 209 |
let m = new_submodule(py, package, "discovery")?; |
| 210 |
m.add_class::<PartialDiscovery>()?; |
| 211 |
Ok(m) |
| 212 |
} |