##// END OF EJS Templates
tests: use sha256line.py instead of /dev/random in test-censor.t (issue6858)...
tests: use sha256line.py instead of /dev/random in test-censor.t (issue6858) Sometimes the systems that run our test suite don't have enough entropy and they cannot produce target file of the expected size using /dev/random, which results in test failures. Switching to /dev/urandom would give us way more available data at the cost of it being less "random", but we don't really need to use entropy for this task at all, since we only care if the file size after compression is big enough to not be stored inline in the revlog. So let's use something that we already have used to generate this kind of data in other tests.

File last commit:

r52146:96e05f1a default
r52255:e7be2ddf stable
Show More
cindex.rs
217 lines | 6.9 KiB | application/rls-services+xml | RustLexer
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 // cindex.rs
//
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Bindings to use the Index defined by the parsers C extension
//!
//! Ideally, we should use an Index entirely implemented in Rust,
//! but this will take some time to get there.
Georges Racinet
rust-index: add `append` method to cindex/Index...
r44991 use cpython::{
revlog: signal which revlog index are compatible with Rust...
r48042 exc::ImportError, exc::TypeError, ObjectProtocol, PyClone, PyErr,
PyObject, PyResult, PyTuple, Python, PythonObject,
Georges Racinet
rust-index: add `append` method to cindex/Index...
r44991 };
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 use hg::revlog::{Node, RevlogIndex};
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 use hg::{BaseRevision, Graph, GraphError, Revision};
Simon Sapin
persistent-nodemap: Fix Rust declarations for Revlog_CAPI signatures...
r47141 use libc::{c_int, ssize_t};
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082
pacien
revlog: implement fast_rank retrieval in C...
r49707 const REVLOG_CABI_VERSION: c_int = 3;
Georges Racinet
revlog-native: introduced ABI version in capsule...
r44523
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 #[repr(C)]
pub struct Revlog_CAPI {
Georges Racinet
revlog-native: introduced ABI version in capsule...
r44523 abi_version: c_int,
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 index_length:
Simon Sapin
persistent-nodemap: Fix Rust declarations for Revlog_CAPI signatures...
r47141 unsafe extern "C" fn(index: *mut revlog_capi::RawPyObject) -> ssize_t,
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 index_node: unsafe extern "C" fn(
index: *mut revlog_capi::RawPyObject,
Simon Sapin
persistent-nodemap: Fix Rust declarations for Revlog_CAPI signatures...
r47141 rev: ssize_t,
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 ) -> *const Node,
pacien
revlog: implement fast_rank retrieval in C...
r49707 fast_rank: unsafe extern "C" fn(
index: *mut revlog_capi::RawPyObject,
rev: ssize_t,
) -> ssize_t,
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 index_parents: unsafe extern "C" fn(
index: *mut revlog_capi::RawPyObject,
rev: c_int,
ps: *mut [c_int; 2],
) -> c_int,
}
py_capsule!(
from mercurial.cext.parsers import revlog_CAPI
as revlog_capi for Revlog_CAPI);
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082
/// A `Graph` backed up by objects and functions from revlog.c
///
/// This implementation of the `Graph` trait, relies on (pointers to)
/// - the C index object (`index` member)
/// - the `index_get_parents()` function (`parents` member)
///
/// # Safety
///
/// The C index itself is mutable, and this Rust exposition is **not
/// protected by the GIL**, meaning that this construct isn't safe with respect
/// to Python threads.
///
/// All callers of this `Index` must acquire the GIL and must not release it
/// while working.
///
/// # TODO find a solution to make it GIL safe again.
///
/// This is non trivial, and can wait until we have a clearer picture with
/// more Rust Mercurial constructs.
///
/// One possibility would be to a `GILProtectedIndex` wrapper enclosing
/// a `Python<'p>` marker and have it be the one implementing the
/// `Graph` trait, but this would mean the `Graph` implementor would become
/// likely to change between subsequent method invocations of the `hg-core`
/// objects (a serious change of the `hg-core` API):
/// either exposing ways to mutate the `Graph`, or making it a non persistent
/// parameter in the relevant methods that need one.
///
/// Another possibility would be to introduce an abstract lock handle into
/// the core API, that would be tied to `GILGuard` / `Python<'p>`
/// in the case of the `cpython` crate bindings yet could leave room for other
/// mechanisms in other contexts.
pub struct Index {
index: PyObject,
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 capi: &'static Revlog_CAPI,
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 }
impl Index {
pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
Georges Racinet
revlog-native: introduced ABI version in capsule...
r44523 let capi = unsafe { revlog_capi::retrieve(py)? };
if capi.abi_version != REVLOG_CABI_VERSION {
return Err(PyErr::new::<ImportError, _>(
py,
format!(
"ABI version mismatch: the C ABI revlog version {} \
does not match the {} expected by Rust hg-cpython",
capi.abi_version, REVLOG_CABI_VERSION
),
));
}
revlog: signal which revlog index are compatible with Rust...
r48042 let compat: u64 = index.getattr(py, "rust_ext_compat")?.extract(py)?;
if compat == 0 {
return Err(PyErr::new::<TypeError, _>(
py,
"index object not compatible with Rust",
));
}
Raphaël Gomès
rust: do a clippy pass...
r45500 Ok(Index { index, capi })
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 }
Georges Racinet
rust-index: add a `inner` method to the Index struct...
r44412
/// return a reference to the CPython Index object in this Struct
pub fn inner(&self) -> &PyObject {
&self.index
}
Georges Racinet
rust-index: add `append` method to cindex/Index...
r44991
pub fn append(&mut self, py: Python, tup: PyTuple) -> PyResult<PyObject> {
self.index.call_method(
py,
"append",
PyTuple::new(py, &[tup.into_object()]),
None,
)
}
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 }
Georges Racinet
rust: core implementation for lazyancestors...
r41084 impl Clone for Index {
fn clone(&self) -> Self {
let guard = Python::acquire_gil();
Index {
index: self.index.clone_ref(guard.python()),
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 capi: self.capi,
Georges Racinet
rust: core implementation for lazyancestors...
r41084 }
}
}
Georges Racinet
rust-index: make it possible to clone the struct referencing the C index...
r44462 impl PyClone for Index {
fn clone_ref(&self, py: Python) -> Self {
Index {
index: self.index.clone_ref(py),
capi: self.capi,
}
}
}
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 impl Graph for Index {
/// wrap a call to the C extern parents function
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
let mut res: [c_int; 2] = [0; 2];
let code = unsafe {
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 (self.capi.index_parents)(
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 self.index.as_ptr(),
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 rev.0 as c_int,
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 &mut res as *mut [c_int; 2],
)
};
match code {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 0 => Ok([Revision(res[0]), Revision(res[1])]),
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 _ => Err(GraphError::ParentOutOfRange(rev)),
}
}
}
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989
pacien
hg-cpython: implement vcsgraph::Graph for our Index...
r49349 impl vcsgraph::graph::Graph for Index {
fn parents(
&self,
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 rev: BaseRevision,
pacien
hg-cpython: implement vcsgraph::Graph for our Index...
r49349 ) -> Result<vcsgraph::graph::Parents, vcsgraph::graph::GraphReadError>
{
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 // FIXME This trait should be reworked to decide between Revision
// and UncheckedRevision, get better errors names, etc.
match Graph::parents(self, Revision(rev)) {
Ok(parents) => {
Ok(vcsgraph::graph::Parents([parents[0].0, parents[1].0]))
}
pacien
hg-cpython: implement vcsgraph::Graph for our Index...
r49349 Err(GraphError::ParentOutOfRange(rev)) => {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev.0))
pacien
hg-cpython: implement vcsgraph::Graph for our Index...
r49349 }
}
}
}
pacien
rust: implement vcsgraph::RankedGraph for Index...
r49708 impl vcsgraph::graph::RankedGraph for Index {
fn rank(
&self,
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 rev: BaseRevision,
pacien
rust: implement vcsgraph::RankedGraph for Index...
r49708 ) -> Result<vcsgraph::graph::Rank, vcsgraph::graph::GraphReadError> {
match unsafe {
(self.capi.fast_rank)(self.index.as_ptr(), rev as ssize_t)
} {
-1 => Err(vcsgraph::graph::GraphReadError::InconsistentGraphData),
rank => Ok(rank as usize),
}
}
}
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 impl RevlogIndex for Index {
/// Note C return type is Py_ssize_t (hence signed), but we shall
/// force it to unsigned, because it's a length
fn len(&self) -> usize {
unsafe { (self.capi.index_length)(self.index.as_ptr()) as usize }
}
Raphaël Gomès
rust: do a clippy pass...
r45500 fn node(&self, rev: Revision) -> Option<&Node> {
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 let raw = unsafe {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 (self.capi.index_node)(self.index.as_ptr(), rev.0 as ssize_t)
Georges Racinet
revlog: using two new functions in C capsule from Rust code...
r44989 };
if raw.is_null() {
None
} else {
// TODO it would be much better for the C layer to give us
// a length, since the hash length will change in the near
// future, but that's probably out of scope for the nodemap
// patch series.
//
// The root of that unsafety relies in the signature of
// `capi.index_node()` itself: returning a `Node` pointer
// whereas it's a `char *` in the C counterpart.
Some(unsafe { &*raw })
}
}
}