changeset 52821:6222326bd13e

rust-pyo3-revlog: _index_computephasesmapsets It is not the right time to fix the "ugly hack", but perhaps we could do better with constants, or even have `hg-core` export the whole of `IDX_TO_PHASE_NUM` as a constant. Otherwise, this is much more natural than the original code in `hg-cpython`.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Tue, 24 Dec 2024 14:37:33 +0100
parents 0fc15732fe7b
children 6bd11e3e05d6
files rust/hg-pyo3/src/revision.rs rust/hg-pyo3/src/revlog/mod.rs
diffstat 2 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/revision.rs	Tue Dec 24 14:34:23 2024 +0100
+++ b/rust/hg-pyo3/src/revision.rs	Tue Dec 24 14:37:33 2024 +0100
@@ -117,7 +117,6 @@
     Ok(PyList::new(py, revs.into_iter().map(PyRevision::from))?.unbind())
 }
 
-#[allow(dead_code)]
 pub fn revs_py_set<U>(
     py: Python<'_>,
     revs: impl IntoIterator<Item = Revision, IntoIter = U>,
--- a/rust/hg-pyo3/src/revlog/mod.rs	Tue Dec 24 14:34:23 2024 +0100
+++ b/rust/hg-pyo3/src/revlog/mod.rs	Tue Dec 24 14:37:33 2024 +0100
@@ -16,7 +16,7 @@
 };
 use pyo3_sharedref::{PyShareable, SharedByPyObject};
 
-use std::collections::HashSet;
+use std::collections::{HashMap, HashSet};
 use std::sync::{
     atomic::{AtomicUsize, Ordering},
     RwLock, RwLockReadGuard, RwLockWriteGuard,
@@ -25,7 +25,9 @@
 use hg::{
     errors::HgError,
     revlog::{
-        index::{Index, RevisionDataParams, SnapshotsCache, INDEX_ENTRY_SIZE},
+        index::{
+            Index, Phase, RevisionDataParams, SnapshotsCache, INDEX_ENTRY_SIZE,
+        },
         inner_revlog::InnerRevlog as CoreInnerRevlog,
         nodemap::{NodeMap, NodeMapError, NodeTree as CoreNodeTree},
         options::RevlogOpenOptions,
@@ -44,7 +46,7 @@
     node::{node_from_py_bytes, node_prefix_from_py_bytes, py_node_for_rev},
     revision::{
         check_revision, rev_pyiter_collect, rev_pyiter_collect_or_else,
-        revs_py_list, PyRevision,
+        revs_py_list, revs_py_set, PyRevision,
     },
     store::PyFnCache,
     util::{new_submodule, take_buffer_with_slice},
@@ -445,6 +447,39 @@
         Ok(PyBytes::new(slf.py(), &packed).unbind())
     }
 
+    /// compute phases
+    fn _index_computephasesmapsets(
+        slf: &Bound<'_, Self>,
+        py: Python<'_>,
+        roots: &Bound<'_, PyDict>,
+    ) -> PyResult<Py<PyTuple>> {
+        let (len, phase_maps) = Self::with_index_read(slf, |idx| {
+            let extracted_roots: PyResult<HashMap<Phase, Vec<Revision>>> =
+                roots
+                    .iter()
+                    .map(|(phase, revs)| {
+                        let phase = Phase::try_from(phase.extract::<usize>()?)
+                            .map_err(|_| revlog_error_bare())?;
+                        let revs: Vec<Revision> =
+                            rev_pyiter_collect(&revs, idx)?;
+                        Ok((phase, revs))
+                    })
+                    .collect();
+            idx.compute_phases_map_sets(extracted_roots?)
+                .map_err(graph_error)
+        })?;
+        // Ugly hack, but temporary (!)
+        const IDX_TO_PHASE_NUM: [usize; 4] = [1, 2, 32, 96];
+        let py_phase_maps = PyDict::new(py);
+        for (i, roots) in phase_maps.into_iter().enumerate() {
+            py_phase_maps.set_item(
+                IDX_TO_PHASE_NUM[i],
+                revs_py_set(py, roots)?.into_any(),
+            )?;
+        }
+        Ok((len, py_phase_maps).into_pyobject(py)?.unbind())
+    }
+
     /// reachableroots
     #[pyo3(signature = (*args))]
     fn _index_reachableroots2(