Adds QueryTriplesIterator

pull/46/head
Tpt 5 years ago
parent 3e11180405
commit 35fd315585
  1. 28
      lib/src/sparql/eval.rs
  2. 1
      lib/src/sparql/mod.rs
  3. 47
      lib/src/sparql/model.rs
  4. 5
      python/src/store_utils.rs

@ -89,22 +89,26 @@ impl<S: ReadableEncodedStore + 'static> SimpleEvaluator<S> {
construct: Rc<Vec<TripleTemplate>>, construct: Rc<Vec<TripleTemplate>>,
) -> Result<QueryResult> { ) -> Result<QueryResult> {
let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len()); let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len());
Ok(QueryResult::Graph(Box::new(ConstructIterator { Ok(QueryResult::Graph(QueryTriplesIterator {
eval: self.clone(), iter: Box::new(ConstructIterator {
iter: self.eval_plan(plan, from), eval: self.clone(),
template: construct, iter: self.eval_plan(plan, from),
buffered_results: Vec::default(), template: construct,
bnodes: Vec::default(), buffered_results: Vec::default(),
}))) bnodes: Vec::default(),
}),
}))
} }
pub fn evaluate_describe_plan(&self, plan: &PlanNode) -> Result<QueryResult> { pub fn evaluate_describe_plan(&self, plan: &PlanNode) -> Result<QueryResult> {
let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len()); let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len());
Ok(QueryResult::Graph(Box::new(DescribeIterator { Ok(QueryResult::Graph(QueryTriplesIterator {
eval: self.clone(), iter: Box::new(DescribeIterator {
iter: self.eval_plan(plan, from), eval: self.clone(),
quads: Box::new(empty()), iter: self.eval_plan(plan, from),
}))) quads: Box::new(empty()),
}),
}))
} }
fn eval_plan(&self, node: &PlanNode, from: EncodedTuple) -> EncodedTuplesIterator { fn eval_plan(&self, node: &PlanNode, from: EncodedTuple) -> EncodedTuplesIterator {

@ -21,6 +21,7 @@ use crate::Result;
pub use crate::sparql::model::QuerySolution; pub use crate::sparql::model::QuerySolution;
pub use crate::sparql::model::QuerySolutionsIterator; pub use crate::sparql::model::QuerySolutionsIterator;
pub use crate::sparql::model::QueryTriplesIterator;
#[deprecated(note = "Please directly use QuerySolutionsIterator type instead")] #[deprecated(note = "Please directly use QuerySolutionsIterator type instead")]
pub type BindingsIterator<'a> = QuerySolutionsIterator; pub type BindingsIterator<'a> = QuerySolutionsIterator;
pub use crate::sparql::model::QueryResult; pub use crate::sparql::model::QueryResult;

@ -19,7 +19,7 @@ pub enum QueryResult {
/// Result of a [ASK](https://www.w3.org/TR/sparql11-query/#ask) query /// Result of a [ASK](https://www.w3.org/TR/sparql11-query/#ask) query
Boolean(bool), Boolean(bool),
/// Results of a [CONSTRUCT](https://www.w3.org/TR/sparql11-query/#construct) or [DESCRIBE](https://www.w3.org/TR/sparql11-query/#describe) query /// Results of a [CONSTRUCT](https://www.w3.org/TR/sparql11-query/#construct) or [DESCRIBE](https://www.w3.org/TR/sparql11-query/#describe) query
Graph(Box<dyn Iterator<Item = Result<Triple>>>), Graph(QueryTriplesIterator),
} }
impl QueryResult { impl QueryResult {
@ -112,11 +112,9 @@ impl QueryResult {
} }
} }
impl<E: Into<Error>, I: Iterator<Item = std::result::Result<Triple, E>> + 'static> From<I> impl From<QuerySolutionsIterator> for QueryResult {
for QueryResult fn from(value: QuerySolutionsIterator) -> Self {
{ QueryResult::Solutions(value)
fn from(iter: I) -> Self {
QueryResult::Graph(Box::new(iter.map(|e| e.map_err(|e| e.into()))))
} }
} }
@ -365,6 +363,43 @@ impl VariableSolutionIndex for Variable {
} }
} }
/// An iterator over the triples that compose a graph solution
///
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::sparql::{QueryResult, QueryOptions};
///
/// let store = MemoryStore::new();
/// if let QueryResult::Graph(triples) = store.query("CONSTRUCT WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// for triple in triples {
/// println!("{}", triple?);
/// }
/// }
/// # oxigraph::Result::Ok(())
/// ```
pub struct QueryTriplesIterator {
pub(crate) iter: Box<dyn Iterator<Item = Result<Triple>>>,
}
impl Iterator for QueryTriplesIterator {
type Item = Result<Triple>;
fn next(&mut self) -> Option<Result<Triple>> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
where
G: FnMut(Acc, Self::Item) -> Acc,
{
self.iter.fold(init, |acc, elt| g(acc, elt))
}
}
/// A SPARQL query variable /// A SPARQL query variable
/// ///
/// ``` /// ```

@ -1,7 +1,6 @@
use crate::model::*; use crate::model::*;
use oxigraph::model::*; use oxigraph::model::*;
use oxigraph::sparql::{QueryResult, QuerySolution, QuerySolutionsIterator}; use oxigraph::sparql::{QueryResult, QuerySolution, QuerySolutionsIterator, QueryTriplesIterator};
use oxigraph::Result;
use pyo3::exceptions::{IOError, TypeError, ValueError}; use pyo3::exceptions::{IOError, TypeError, ValueError};
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::{PyIterProtocol, PyMappingProtocol, PyNativeType, PyObjectProtocol}; use pyo3::{PyIterProtocol, PyMappingProtocol, PyNativeType, PyObjectProtocol};
@ -121,7 +120,7 @@ impl PyIterProtocol for QuerySolutionIter {
#[pyclass(unsendable)] #[pyclass(unsendable)]
pub struct TripleResultIter { pub struct TripleResultIter {
inner: Box<dyn Iterator<Item = Result<Triple>>>, inner: QueryTriplesIterator,
} }
#[pyproto] #[pyproto]

Loading…
Cancel
Save