Adds QueryTriplesIterator

pull/46/head
Tpt 4 years ago
parent 3e11180405
commit 35fd315585
  1. 12
      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>>,
) -> Result<QueryResult> {
let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len());
Ok(QueryResult::Graph(Box::new(ConstructIterator {
Ok(QueryResult::Graph(QueryTriplesIterator {
iter: Box::new(ConstructIterator {
eval: self.clone(),
iter: self.eval_plan(plan, from),
template: construct,
buffered_results: Vec::default(),
bnodes: Vec::default(),
})))
}),
}))
}
pub fn evaluate_describe_plan(&self, plan: &PlanNode) -> Result<QueryResult> {
let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len());
Ok(QueryResult::Graph(Box::new(DescribeIterator {
Ok(QueryResult::Graph(QueryTriplesIterator {
iter: Box::new(DescribeIterator {
eval: self.clone(),
iter: self.eval_plan(plan, from),
quads: Box::new(empty()),
})))
}),
}))
}
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::QuerySolutionsIterator;
pub use crate::sparql::model::QueryTriplesIterator;
#[deprecated(note = "Please directly use QuerySolutionsIterator type instead")]
pub type BindingsIterator<'a> = QuerySolutionsIterator;
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
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
Graph(Box<dyn Iterator<Item = Result<Triple>>>),
Graph(QueryTriplesIterator),
}
impl QueryResult {
@ -112,11 +112,9 @@ impl QueryResult {
}
}
impl<E: Into<Error>, I: Iterator<Item = std::result::Result<Triple, E>> + 'static> From<I>
for QueryResult
{
fn from(iter: I) -> Self {
QueryResult::Graph(Box::new(iter.map(|e| e.map_err(|e| e.into()))))
impl From<QuerySolutionsIterator> for QueryResult {
fn from(value: QuerySolutionsIterator) -> Self {
QueryResult::Solutions(value)
}
}
@ -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
///
/// ```

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

Loading…
Cancel
Save