tests working

pull/11/head
Dustin Whitney 5 years ago
parent 75c786b350
commit 5912e4c40b
  1. 5
      lib/src/lib.rs
  2. 11
      lib/src/repository.rs
  3. 27
      lib/src/sparql/eval.rs
  4. 41
      lib/src/sparql/mod.rs
  5. 5
      lib/src/store/memory.rs
  6. 3
      lib/src/store/mod.rs
  7. 2
      lib/tests/service_test_cases.rs
  8. 2
      lib/tests/sparql_test_cases.rs

@ -27,8 +27,9 @@
//! assert_eq!(vec![quad], results.unwrap()); //! assert_eq!(vec![quad], results.unwrap());
//! //!
//! // SPARQL query //! // SPARQL query
//! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap(); //! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
//! let results = prepared_query.exec().unwrap(); //! let options = QueryOptions::default();
//! let results = prepared_query.exec(&options).unwrap();
//! if let QueryResult::Bindings(results) = results { //! if let QueryResult::Bindings(results) = results {
//! assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); //! assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
//! } //! }

@ -30,8 +30,9 @@ use std::io::BufRead;
/// assert_eq!(vec![quad], results.unwrap()); /// assert_eq!(vec![quad], results.unwrap());
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// } /// }
@ -75,8 +76,9 @@ pub trait RepositoryConnection: Clone {
/// connection.insert(&Quad::new(ex.clone(), ex.clone(), ex.clone(), None)); /// connection.insert(&Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// } /// }
@ -87,6 +89,7 @@ pub trait RepositoryConnection: Clone {
fn prepare_query_from_pattern<'a>( fn prepare_query_from_pattern<'a>(
&'a self, &'a self,
graph_pattern: &'a GraphPattern, graph_pattern: &'a GraphPattern,
base_iri: Option<&str>,
) -> Result<Self::PreparedQuery>; ) -> Result<Self::PreparedQuery>;
/// Retrieves quads with a filter on each quad component /// Retrieves quads with a filter on each quad component

@ -40,15 +40,17 @@ type EncodedTuplesIterator<'a> = Box<dyn Iterator<Item = Result<EncodedTuple>> +
pub struct SimpleEvaluator<S: StoreConnection> { pub struct SimpleEvaluator<S: StoreConnection> {
dataset: DatasetView<S>, dataset: DatasetView<S>,
base_iri: Option<Iri<String>>,
bnodes_map: Mutex<BTreeMap<u128, u128>>, bnodes_map: Mutex<BTreeMap<u128, u128>>,
now: DateTime<FixedOffset>, now: DateTime<FixedOffset>,
} }
impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> { impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
pub fn new(dataset: DatasetView<S>) -> Self { pub fn new(dataset: DatasetView<S>, base_iri: Option<Iri<String>>) -> Self {
Self { Self {
dataset, dataset,
bnodes_map: Mutex::new(BTreeMap::default()), bnodes_map: Mutex::new(BTreeMap::default()),
base_iri,
now: Utc::now().with_timezone(&FixedOffset::east(0)), now: Utc::now().with_timezone(&FixedOffset::east(0)),
} }
} }
@ -948,22 +950,15 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
_ => None, _ => None,
}?; }?;
let iri = self.dataset.get_str(iri_id).ok()??; let iri = self.dataset.get_str(iri_id).ok()??;
match options.base_iri { let base_iri = options.base_iri
None => { .map(|base_iri| Iri::parse(base_iri.to_string()))
Iri::parse(iri).ok()?; .or(self.base_iri.as_ref().map(|iri| Ok(iri.clone())));
Some(EncodedTerm::NamedNode { iri_id }) if let Some(Ok(base_iri)) = base_iri {
}, self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner())
Some(str_iri) => { } else {
match Iri::parse(str_iri) { Iri::parse(iri).ok()?;
Ok(base_iri) => self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner()), Some(EncodedTerm::NamedNode { iri_id })
_ => {
Iri::parse(iri).ok()?;
Some(EncodedTerm::NamedNode { iri_id })
}
}
}
} }
} }
PlanExpression::BNode(id) => match id { PlanExpression::BNode(id) => match id {
Some(id) => { Some(id) => {

@ -18,6 +18,8 @@ use crate::sparql::plan::{DatasetView, PlanNode};
use crate::sparql::plan_builder::PlanBuilder; use crate::sparql::plan_builder::PlanBuilder;
use crate::store::StoreConnection; use crate::store::StoreConnection;
use crate::Result; use crate::Result;
use failure::format_err;
use rio_api::iri::Iri;
use std::fmt; use std::fmt;
pub use crate::sparql::algebra::GraphPattern; pub use crate::sparql::algebra::GraphPattern;
@ -64,31 +66,31 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
QueryVariants::Select { QueryVariants::Select {
algebra, algebra,
dataset: _, dataset: _,
.. base_iri,
} => { } => {
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Select { SimplePreparedQueryAction::Select {
plan, plan,
variables, variables,
evaluator: SimpleEvaluator::new(dataset), evaluator: SimpleEvaluator::new(dataset, base_iri),
} }
} }
QueryVariants::Ask { QueryVariants::Ask {
algebra, algebra,
dataset: _, dataset: _,
.. base_iri,
} => { } => {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Ask { SimplePreparedQueryAction::Ask {
plan, plan,
evaluator: SimpleEvaluator::new(dataset), evaluator: SimpleEvaluator::new(dataset, base_iri),
} }
} }
QueryVariants::Construct { QueryVariants::Construct {
construct, construct,
algebra, algebra,
dataset: _, dataset: _,
.. base_iri,
} => { } => {
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Construct { SimplePreparedQueryAction::Construct {
@ -98,18 +100,18 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
&construct, &construct,
variables, variables,
)?, )?,
evaluator: SimpleEvaluator::new(dataset), evaluator: SimpleEvaluator::new(dataset, base_iri),
} }
} }
QueryVariants::Describe { QueryVariants::Describe {
algebra, algebra,
dataset: _, dataset: _,
.. base_iri,
} => { } => {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Describe { SimplePreparedQueryAction::Describe {
plan, plan,
evaluator: SimpleEvaluator::new(dataset), evaluator: SimpleEvaluator::new(dataset, base_iri),
} }
} }
})) }))
@ -118,14 +120,27 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
pub(crate) fn new_from_pattern( pub(crate) fn new_from_pattern(
connection: S, connection: S,
pattern: &GraphPattern, pattern: &GraphPattern,
base_iri: Option<&'a str>
) -> Result<Self> { ) -> Result<Self> {
let dataset = DatasetView::new(connection); let dataset = DatasetView::new(connection);
let (plan, variables) = PlanBuilder::build(dataset.encoder(), pattern)?; let (plan, variables) = PlanBuilder::build(dataset.encoder(), pattern)?;
Ok(Self(SimplePreparedQueryAction::Select { let base_iri = base_iri.map(|str_iri| Iri::parse(str_iri.to_string()));
plan, match base_iri {
variables, Some(Err(_)) => Err(format_err!("Failed to parse base_iri")),
evaluator: SimpleEvaluator::new(dataset), Some(Ok(base_iri)) =>
})) Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, Some(base_iri)),
})),
None =>
Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, None),
}))
}
} }
} }

@ -29,8 +29,9 @@ use std::sync::{PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};
/// assert_eq!(vec![quad], results.unwrap()); /// assert_eq!(vec![quad], results.unwrap());
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// } /// }

@ -101,8 +101,9 @@ impl<S: StoreConnection> RepositoryConnection for StoreRepositoryConnection<S> {
fn prepare_query_from_pattern<'a>( fn prepare_query_from_pattern<'a>(
&'a self, &'a self,
pattern: &GraphPattern, pattern: &GraphPattern,
base_iri: Option<&'a str>
) -> Result<Self::PreparedQuery> { ) -> Result<Self::PreparedQuery> {
SimplePreparedQuery::new_from_pattern(self.inner.clone(), pattern) //TODO: avoid clone SimplePreparedQuery::new_from_pattern(self.inner.clone(), pattern, base_iri) //TODO: avoid clone
} }
fn load_graph( fn load_graph(

@ -217,7 +217,7 @@ fn query_repository<'a>(repository: MemoryRepository, query: String, options: Qu
fn pattern_repository<'a>(repository: MemoryRepository, pattern: GraphPattern, options: QueryOptions<'a>) -> Result<BindingsIterator<'a>> { fn pattern_repository<'a>(repository: MemoryRepository, pattern: GraphPattern, options: QueryOptions<'a>) -> Result<BindingsIterator<'a>> {
let connection = repository.connection()?; let connection = repository.connection()?;
let prepared_query = connection.prepare_query_from_pattern(&pattern)?; let prepared_query = connection.prepare_query_from_pattern(&pattern, None)?;
let result = prepared_query.exec(&options)?; let result = prepared_query.exec(&options)?;
match result { match result {
QueryResult::Bindings(iterator) => { QueryResult::Bindings(iterator) => {

@ -164,7 +164,7 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
"Failure to parse query of {} with error: {}", "Failure to parse query of {} with error: {}",
test, error test, error
)), )),
Ok(query) => match query.exec(&QueryOptions::default().with_base_iri(&test.query)) { Ok(query) => match query.exec(&QueryOptions::default()) {
Err(error) => Err(format_err!( Err(error) => Err(format_err!(
"Failure to execute query of {} with error: {}", "Failure to execute query of {} with error: {}",
test, error test, error

Loading…
Cancel
Save