Drops prepare_query methods

Not very useful, most of the job could be done by parsing the query first
pull/69/head
Tpt 4 years ago
parent 7a110902d2
commit ece760f0c1
  1. 8
      lib/src/sparql/eval.rs
  2. 159
      lib/src/sparql/mod.rs
  3. 49
      lib/src/store/memory.rs
  4. 31
      lib/src/store/rocksdb.rs
  5. 30
      lib/src/store/sled.rs

@ -92,14 +92,14 @@ where
pub fn evaluate_construct_plan( pub fn evaluate_construct_plan(
&self, &self,
plan: &PlanNode<S::StrId>, plan: &PlanNode<S::StrId>,
construct: Rc<Vec<TripleTemplate<S::StrId>>>, template: Vec<TripleTemplate<S::StrId>>,
) -> Result<QueryResults, EvaluationError> { ) -> Result<QueryResults, EvaluationError> {
let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len()); let from = EncodedTuple::with_capacity(plan.maybe_bound_variables().len());
Ok(QueryResults::Graph(QueryTripleIter { Ok(QueryResults::Graph(QueryTripleIter {
iter: Box::new(ConstructIterator { iter: Box::new(ConstructIterator {
eval: self.clone(), eval: self.clone(),
iter: self.eval_plan(plan, from), iter: self.eval_plan(plan, from),
template: construct, template,
buffered_results: Vec::default(), buffered_results: Vec::default(),
bnodes: Vec::default(), bnodes: Vec::default(),
}), }),
@ -2634,7 +2634,7 @@ where
struct ConstructIterator<S: ReadableEncodedStore + 'static> { struct ConstructIterator<S: ReadableEncodedStore + 'static> {
eval: SimpleEvaluator<S>, eval: SimpleEvaluator<S>,
iter: EncodedTuplesIterator<S::StrId>, iter: EncodedTuplesIterator<S::StrId>,
template: Rc<Vec<TripleTemplate<S::StrId>>>, template: Vec<TripleTemplate<S::StrId>>,
buffered_results: Vec<Result<Triple, EvaluationError>>, buffered_results: Vec<Result<Triple, EvaluationError>>,
bnodes: Vec<EncodedTerm<S::StrId>>, bnodes: Vec<EncodedTerm<S::StrId>>,
} }
@ -2652,7 +2652,7 @@ impl<S: ReadableEncodedStore<Error = EvaluationError> + 'static> Iterator for Co
Ok(tuple) => tuple, Ok(tuple) => tuple,
Err(error) => return Some(Err(error)), Err(error) => return Some(Err(error)),
}; };
for template in self.template.iter() { for template in &self.template {
if let (Some(subject), Some(predicate), Some(object)) = ( if let (Some(subject), Some(predicate), Some(object)) = (
get_triple_template_value(&template.subject, &tuple, &mut self.bnodes), get_triple_template_value(&template.subject, &tuple, &mut self.bnodes),
get_triple_template_value(&template.predicate, &tuple, &mut self.bnodes), get_triple_template_value(&template.predicate, &tuple, &mut self.bnodes),

@ -29,126 +29,63 @@ pub use crate::sparql::model::QuerySolutionIter;
pub use crate::sparql::model::QueryTripleIter; pub use crate::sparql::model::QueryTripleIter;
pub use crate::sparql::model::{Variable, VariableNameParseError}; pub use crate::sparql::model::{Variable, VariableNameParseError};
pub use crate::sparql::parser::ParseError; pub use crate::sparql::parser::ParseError;
use crate::sparql::plan::{PlanNode, TripleTemplate};
use crate::sparql::plan_builder::PlanBuilder; use crate::sparql::plan_builder::PlanBuilder;
pub use crate::sparql::service::ServiceHandler; pub use crate::sparql::service::ServiceHandler;
use crate::sparql::service::{EmptyServiceHandler, ErrorConversionServiceHandler}; use crate::sparql::service::{EmptyServiceHandler, ErrorConversionServiceHandler};
use crate::sparql::update::SimpleUpdateEvaluator; use crate::sparql::update::SimpleUpdateEvaluator;
use crate::store::numeric_encoder::{StrContainer, StrEncodingAware}; use crate::store::numeric_encoder::StrContainer;
use crate::store::{ReadableEncodedStore, StoreOrParseError, WritableEncodedStore}; use crate::store::{ReadableEncodedStore, StoreOrParseError, WritableEncodedStore};
use std::convert::TryInto; use std::convert::TryInto;
use std::io; use std::io;
use std::rc::Rc; use std::rc::Rc;
/// A prepared [SPARQL query](https://www.w3.org/TR/sparql11-query/) pub(crate) fn evaluate_query<R: ReadableEncodedStore + 'static>(
pub(crate) struct SimplePreparedQuery<S: ReadableEncodedStore + 'static>( store: R,
SimplePreparedQueryAction<S>, query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
); options: QueryOptions,
) -> Result<QueryResults, EvaluationError> {
#[derive(Clone)] match query.try_into().map_err(|e| e.into())?.0 {
enum SimplePreparedQueryAction<S: ReadableEncodedStore + 'static> { QueryVariants::Select {
Select { algebra,
plan: Rc<PlanNode<<DatasetView<S> as StrEncodingAware>::StrId>>, base_iri,
variables: Rc<Vec<Variable>>, dataset,
evaluator: SimpleEvaluator<DatasetView<S>>, } => {
}, let dataset = DatasetView::new(store, &dataset)?;
Ask { let (plan, variables) = PlanBuilder::build(&dataset, &algebra)?;
plan: Rc<PlanNode<<DatasetView<S> as StrEncodingAware>::StrId>>, SimpleEvaluator::new(Rc::new(dataset), base_iri, options.service_handler)
evaluator: SimpleEvaluator<DatasetView<S>>, .evaluate_select_plan(&plan, Rc::new(variables))
}, }
Construct { QueryVariants::Ask {
plan: Rc<PlanNode<<DatasetView<S> as StrEncodingAware>::StrId>>, algebra,
construct: Rc<Vec<TripleTemplate<<DatasetView<S> as StrEncodingAware>::StrId>>>, base_iri,
evaluator: SimpleEvaluator<DatasetView<S>>, dataset,
}, } => {
Describe { let dataset = DatasetView::new(store, &dataset)?;
plan: Rc<PlanNode<<DatasetView<S> as StrEncodingAware>::StrId>>, let (plan, _) = PlanBuilder::build(&dataset, &algebra)?;
evaluator: SimpleEvaluator<DatasetView<S>>, SimpleEvaluator::new(Rc::new(dataset), base_iri, options.service_handler)
}, .evaluate_ask_plan(&plan)
} }
QueryVariants::Construct {
impl<S: ReadableEncodedStore + 'static> SimplePreparedQuery<S> { construct,
pub(crate) fn new( algebra,
store: S, base_iri,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>, dataset,
options: QueryOptions, } => {
) -> Result<Self, EvaluationError> { let dataset = DatasetView::new(store, &dataset)?;
Ok(Self(match query.try_into().map_err(|e| e.into())?.0 { let (plan, variables) = PlanBuilder::build(&dataset, &algebra)?;
QueryVariants::Select { let construct = PlanBuilder::build_graph_template(&dataset, &construct, variables)?;
algebra, SimpleEvaluator::new(Rc::new(dataset), base_iri, options.service_handler)
base_iri, .evaluate_construct_plan(&plan, construct)
dataset, }
} => { QueryVariants::Describe {
let dataset = Rc::new(DatasetView::new(store, &dataset)?); algebra,
let (plan, variables) = PlanBuilder::build(dataset.as_ref(), &algebra)?; base_iri,
SimplePreparedQueryAction::Select { dataset,
plan: Rc::new(plan), } => {
variables: Rc::new(variables), let dataset = DatasetView::new(store, &dataset)?;
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler), let (plan, _) = PlanBuilder::build(&dataset, &algebra)?;
} SimpleEvaluator::new(Rc::new(dataset), base_iri, options.service_handler)
} .evaluate_describe_plan(&plan)
QueryVariants::Ask {
algebra,
base_iri,
dataset,
} => {
let dataset = Rc::new(DatasetView::new(store, &dataset)?);
let (plan, _) = PlanBuilder::build(dataset.as_ref(), &algebra)?;
SimplePreparedQueryAction::Ask {
plan: Rc::new(plan),
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler),
}
}
QueryVariants::Construct {
construct,
algebra,
base_iri,
dataset,
} => {
let dataset = Rc::new(DatasetView::new(store, &dataset)?);
let (plan, variables) = PlanBuilder::build(dataset.as_ref(), &algebra)?;
SimplePreparedQueryAction::Construct {
plan: Rc::new(plan),
construct: Rc::new(PlanBuilder::build_graph_template(
dataset.as_ref(),
&construct,
variables,
)?),
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler),
}
}
QueryVariants::Describe {
algebra,
base_iri,
dataset,
} => {
let dataset = Rc::new(DatasetView::new(store, &dataset)?);
let (plan, _) = PlanBuilder::build(dataset.as_ref(), &algebra)?;
SimplePreparedQueryAction::Describe {
plan: Rc::new(plan),
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler),
}
}
}))
}
/// Evaluates the query and returns its results
pub fn exec(&self) -> Result<QueryResults, EvaluationError> {
match &self.0 {
SimplePreparedQueryAction::Select {
plan,
variables,
evaluator,
} => evaluator.evaluate_select_plan(plan, variables.clone()),
SimplePreparedQueryAction::Ask { plan, evaluator } => evaluator.evaluate_ask_plan(plan),
SimplePreparedQueryAction::Construct {
plan,
construct,
evaluator,
} => evaluator.evaluate_construct_plan(plan, construct.clone()),
SimplePreparedQueryAction::Describe { plan, evaluator } => {
evaluator.evaluate_describe_plan(plan)
}
} }
} }
} }

@ -4,8 +4,7 @@ use crate::error::{invalid_input_error, UnwrapInfallible};
use crate::io::{DatasetFormat, DatasetParser, GraphFormat, GraphParser}; use crate::io::{DatasetFormat, DatasetParser, GraphFormat, GraphParser};
use crate::model::*; use crate::model::*;
use crate::sparql::{ use crate::sparql::{
evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, SimplePreparedQuery, evaluate_query, evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, Update,
Update,
}; };
use crate::store::numeric_encoder::{ use crate::store::numeric_encoder::{
Decoder, ReadEncoder, StrContainer, StrEncodingAware, StrId, StrLookup, WriteEncoder, Decoder, ReadEncoder, StrContainer, StrEncodingAware, StrId, StrLookup, WriteEncoder,
@ -116,41 +115,7 @@ impl MemoryStore {
query: impl TryInto<Query, Error = impl Into<EvaluationError>>, query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions, options: QueryOptions,
) -> Result<QueryResults, EvaluationError> { ) -> Result<QueryResults, EvaluationError> {
self.prepare_query(query, options)?.exec() evaluate_query(self.clone(), query, options)
}
/// Prepares a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) and returns an object that could be used to execute it.
/// It is useful if you want to execute multiple times the same SPARQL query.
///
/// Usage example:
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryResults, QueryOptions};
///
/// let store = MemoryStore::new();
///
/// // insertions
/// let ex = NamedNode::new("http://example.com")?;
/// store.insert(Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
///
/// // SPARQL query
/// let prepared_query = store.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())?;
/// if let QueryResults::Solutions(mut solutions) = prepared_query.exec()? {
/// assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
/// }
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
pub fn prepare_query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<MemoryPreparedQuery, EvaluationError> {
Ok(MemoryPreparedQuery(SimplePreparedQuery::new(
self.clone(),
query,
options,
)?))
} }
/// Retrieves quads with a filter on each quad component /// Retrieves quads with a filter on each quad component
@ -1110,16 +1075,6 @@ fn quad_map_flatten<'a, T: Copy>(gspo: &'a QuadMap<T>) -> impl Iterator<Item = (
}) })
} }
/// A prepared [SPARQL query](https://www.w3.org/TR/sparql11-query/) for the [`MemoryStore`](struct.MemoryStore.html).
pub struct MemoryPreparedQuery(SimplePreparedQuery<MemoryStore>);
impl MemoryPreparedQuery {
/// Evaluates the query and returns its results
pub fn exec(&self) -> Result<QueryResults, EvaluationError> {
self.0.exec()
}
}
/// Allows inserting and deleting quads during an ACID transaction with the [`MemoryStore`](struct.MemoryStore.html). /// Allows inserting and deleting quads during an ACID transaction with the [`MemoryStore`](struct.MemoryStore.html).
pub struct MemoryTransaction { pub struct MemoryTransaction {
ops: Vec<TransactionOp>, ops: Vec<TransactionOp>,

@ -4,8 +4,7 @@ use crate::error::invalid_data_error;
use crate::io::{DatasetFormat, GraphFormat}; use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*; use crate::model::*;
use crate::sparql::{ use crate::sparql::{
evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, SimplePreparedQuery, evaluate_query, evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, Update,
Update,
}; };
use crate::store::binary_encoder::*; use crate::store::binary_encoder::*;
use crate::store::numeric_encoder::{ use crate::store::numeric_encoder::{
@ -130,23 +129,7 @@ impl RocksDbStore {
query: impl TryInto<Query, Error = impl Into<EvaluationError>>, query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions, options: QueryOptions,
) -> Result<QueryResults, EvaluationError> { ) -> Result<QueryResults, EvaluationError> {
self.prepare_query(query, options)?.exec() evaluate_query(self.clone(), query, options)
}
/// Prepares a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) and returns an object that could be used to execute it.
/// It is useful if you want to execute multiple times the same SPARQL query.
///
/// See [`MemoryStore`](../memory/struct.MemoryStore.html#method.prepare_query) for a usage example.
pub fn prepare_query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<RocksDbPreparedQuery, EvaluationError> {
Ok(RocksDbPreparedQuery(SimplePreparedQuery::new(
(*self).clone(),
query,
options,
)?))
} }
/// Retrieves quads with a filter on each quad component /// Retrieves quads with a filter on each quad component
@ -749,16 +732,6 @@ impl ReadableEncodedStore for RocksDbStore {
} }
} }
/// A prepared [SPARQL query](https://www.w3.org/TR/sparql11-query/) for the [`RocksDbStore`](struct.RocksDbStore.html).
pub struct RocksDbPreparedQuery(SimplePreparedQuery<RocksDbStore>);
impl RocksDbPreparedQuery {
/// Evaluates the query and returns its results
pub fn exec(&self) -> Result<QueryResults, EvaluationError> {
self.0.exec()
}
}
struct AutoBatchWriter<'a> { struct AutoBatchWriter<'a> {
store: &'a RocksDbStore, store: &'a RocksDbStore,
batch: WriteBatch, batch: WriteBatch,

@ -4,8 +4,7 @@ use crate::error::invalid_data_error;
use crate::io::{DatasetFormat, GraphFormat}; use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*; use crate::model::*;
use crate::sparql::{ use crate::sparql::{
evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, SimplePreparedQuery, evaluate_query, evaluate_update, EvaluationError, Query, QueryOptions, QueryResults, Update,
Update,
}; };
use crate::store::binary_encoder::*; use crate::store::binary_encoder::*;
use crate::store::numeric_encoder::{ use crate::store::numeric_encoder::{
@ -140,22 +139,7 @@ impl SledStore {
query: impl TryInto<Query, Error = impl Into<EvaluationError>>, query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions, options: QueryOptions,
) -> Result<QueryResults, EvaluationError> { ) -> Result<QueryResults, EvaluationError> {
self.prepare_query(query, options)?.exec() evaluate_query(self.clone(), query, options)
}
/// Prepares a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) and returns an object that could be used to execute it.
///
/// See [`MemoryStore`](../memory/struct.MemoryStore.html#method.prepare_query) for a usage example.
pub fn prepare_query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<SledPreparedQuery, EvaluationError> {
Ok(SledPreparedQuery(SimplePreparedQuery::new(
(*self).clone(),
query,
options,
)?))
} }
/// Retrieves quads with a filter on each quad component /// Retrieves quads with a filter on each quad component
@ -1179,16 +1163,6 @@ impl<T> From<SledConflictableTransactionError<T>> for ConflictableTransactionErr
} }
} }
/// A prepared [SPARQL query](https://www.w3.org/TR/sparql11-query/) for the [`SledStore`](struct.SledStore.html).
pub struct SledPreparedQuery(SimplePreparedQuery<SledStore>);
impl SledPreparedQuery {
/// Evaluates the query and returns its results
pub fn exec(&self) -> Result<QueryResults, EvaluationError> {
self.0.exec()
}
}
pub(crate) struct DecodingQuadsIterator { pub(crate) struct DecodingQuadsIterator {
first: DecodingQuadIterator, first: DecodingQuadIterator,
second: Option<DecodingQuadIterator>, second: Option<DecodingQuadIterator>,

Loading…
Cancel
Save