Drops QueryOption parameter from the query method

Introduces a query_opt method for that
pull/69/head
Tpt 4 years ago
parent ece760f0c1
commit 773bdb943e
  1. 7
      js/src/store.rs
  2. 4
      lib/src/lib.rs
  3. 27
      lib/src/sparql/mod.rs
  4. 19
      lib/src/sparql/model.rs
  5. 4
      lib/src/sparql/service.rs
  6. 18
      lib/src/store/memory.rs
  7. 12
      lib/src/store/rocksdb.rs
  8. 12
      lib/src/store/sled.rs
  9. 6
      lib/src/store/sophia.rs
  10. 6
      python/src/memory_store.rs
  11. 6
      python/src/sled_store.rs
  12. 5
      server/src/main.rs
  13. 4
      testsuite/src/sparql_evaluator.rs
  14. 5
      wikibase/src/main.rs

@ -4,7 +4,7 @@ use crate::utils::to_err;
use js_sys::{Array, Map};
use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::model::*;
use oxigraph::sparql::{QueryOptions, QueryResults};
use oxigraph::sparql::QueryResults;
use oxigraph::MemoryStore;
use std::convert::{TryFrom, TryInto};
use std::io::Cursor;
@ -105,10 +105,7 @@ impl JsMemoryStore {
}
pub fn query(&self, query: &str) -> Result<JsValue, JsValue> {
let results = self
.store
.query(query, QueryOptions::default())
.map_err(to_err)?;
let results = self.store.query(query).map_err(to_err)?;
let output = match results {
QueryResults::Solutions(solutions) => {
let results = Array::new();

@ -21,7 +21,7 @@
//! ```
//! use oxigraph::MemoryStore;
//! use oxigraph::model::*;
//! use oxigraph::sparql::{QueryOptions, QueryResults};
//! use oxigraph::sparql::QueryResults;
//!
//! let store = MemoryStore::new();
//!
@ -35,7 +35,7 @@
//! assert_eq!(vec![quad], results);
//!
//! // SPARQL query
//! if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
//! if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
//! assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
//! }
//! # Result::<_,Box<dyn std::error::Error>>::Ok(())

@ -90,7 +90,11 @@ pub(crate) fn evaluate_query<R: ReadableEncodedStore + 'static>(
}
}
/// Options for SPARQL query evaluation
/// Options for SPARQL query evaluation.
///
///
/// If the `"http_client"` optional feature is enabled,
/// a simple HTTP 1.1 client is used to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
#[derive(Clone)]
pub struct QueryOptions {
pub(crate) service_handler: Rc<dyn ServiceHandler<Error = EvaluationError>>,
@ -100,26 +104,27 @@ impl Default for QueryOptions {
#[inline]
fn default() -> Self {
Self {
service_handler: Rc::new(EmptyServiceHandler),
service_handler: if cfg!(feature = "http_client") {
Rc::new(service::SimpleServiceHandler::new())
} else {
Rc::new(EmptyServiceHandler)
},
}
}
}
impl QueryOptions {
/// Use a simple HTTP 1.1 client built into Oxigraph to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
///
/// Requires the `"http_client"` optional feature.
/// Use a given [`ServiceHandler`](trait.ServiceHandler.html) to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
#[inline]
#[cfg(feature = "http_client")]
pub fn with_simple_service_handler(mut self) -> Self {
self.service_handler = Rc::new(service::SimpleServiceHandler::new());
pub fn with_service_handler(mut self, service_handler: impl ServiceHandler + 'static) -> Self {
self.service_handler = Rc::new(ErrorConversionServiceHandler::wrap(service_handler));
self
}
/// Use a given [`ServiceHandler`](trait.ServiceHandler.html) to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
/// Disables the `SERVICE` calls
#[inline]
pub fn with_service_handler(mut self, service_handler: impl ServiceHandler + 'static) -> Self {
self.service_handler = Rc::new(ErrorConversionServiceHandler::wrap(service_handler));
pub fn without_service_handler(mut self) -> Self {
self.service_handler = Rc::new(EmptyServiceHandler);
self
}
}

@ -47,14 +47,14 @@ impl QueryResults {
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryOptions, QueryResultsFormat};
/// use oxigraph::sparql::QueryResultsFormat;
///
/// let store = MemoryStore::new();
/// let ex = NamedNode::new("http://example.com")?;
/// store.insert(Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
///
/// let mut results = Vec::new();
/// store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())?.write(&mut results, QueryResultsFormat::Json)?;
/// store.query("SELECT ?s WHERE { ?s ?p ?o }")?.write(&mut results, QueryResultsFormat::Json)?;
/// assert_eq!(results, "{\"head\":{\"vars\":[\"s\"]},\"results\":{\"bindings\":[{\"s\":{\"type\":\"uri\",\"value\":\"http://example.com\"}}]}}".as_bytes());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
@ -78,7 +78,6 @@ impl QueryResults {
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::io::GraphFormat;
/// use oxigraph::sparql::QueryOptions;
/// use oxigraph::model::*;
/// use std::io::Cursor;
///
@ -88,7 +87,7 @@ impl QueryResults {
/// store.load_graph(Cursor::new(graph), GraphFormat::NTriples, &GraphName::DefaultGraph, None)?;
///
/// let mut results = Vec::new();
/// store.query("CONSTRUCT WHERE { ?s ?p ?o }", QueryOptions::default())?.write_graph(&mut results, GraphFormat::NTriples)?;
/// store.query("CONSTRUCT WHERE { ?s ?p ?o }")?.write_graph(&mut results, GraphFormat::NTriples)?;
/// assert_eq!(results, graph);
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
@ -219,10 +218,10 @@ impl QueryResultsFormat {
///
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::sparql::{QueryResults, QueryOptions};
/// use oxigraph::sparql::QueryResults;
///
/// let store = MemoryStore::new();
/// if let QueryResults::Solutions(solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
/// for solution in solutions {
/// println!("{:?}", solution?.get("s"));
/// }
@ -246,10 +245,10 @@ impl QuerySolutionIter {
///
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::sparql::{QueryResults, QueryOptions, Variable};
/// use oxigraph::sparql::{QueryResults, Variable};
///
/// let store = MemoryStore::new();
/// if let QueryResults::Solutions(solutions) = store.query("SELECT ?s ?o WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(solutions) = store.query("SELECT ?s ?o WHERE { ?s ?p ?o }")? {
/// assert_eq!(solutions.variables(), &[Variable::new("s")?, Variable::new("o")?]);
/// }
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
@ -368,10 +367,10 @@ impl VariableSolutionIndex for Variable {
///
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::sparql::{QueryResults, QueryOptions};
/// use oxigraph::sparql::QueryResults;
///
/// let store = MemoryStore::new();
/// if let QueryResults::Graph(triples) = store.query("CONSTRUCT WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Graph(triples) = store.query("CONSTRUCT WHERE { ?s ?p ?o }")? {
/// for triple in triples {
/// println!("{}", triple?);
/// }

@ -29,7 +29,7 @@ use std::error::Error;
///
/// fn handle(&self,service_name: NamedNode, query: Query) -> Result<QueryResults,EvaluationError> {
/// if service_name == "http://example.com/service" {
/// self.store.query(query, QueryOptions::default())
/// self.store.query(query)
/// } else {
/// panic!()
/// }
@ -41,7 +41,7 @@ use std::error::Error;
/// let ex = NamedNode::new("http://example.com")?;
/// service.store.insert(Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
///
/// if let QueryResults::Solutions(mut solutions) = store.query(
/// if let QueryResults::Solutions(mut solutions) = store.query_opt(
/// "SELECT ?s WHERE { SERVICE <http://example.com/service> { ?s ?p ?o } }",
/// QueryOptions::default().with_service_handler(service)
/// )? {

@ -32,7 +32,7 @@ use std::{fmt, io};
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryResults, QueryOptions};
/// use oxigraph::sparql::QueryResults;
///
/// let store = MemoryStore::new();
///
@ -46,7 +46,7 @@ use std::{fmt, io};
/// assert_eq!(vec![quad], results);
///
/// // SPARQL query
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
/// assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
/// }
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
@ -92,11 +92,13 @@ impl MemoryStore {
/// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/).
///
/// The default query options are used.
///
/// Usage example:
/// ```
/// use oxigraph::MemoryStore;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryResults, QueryOptions};
/// use oxigraph::sparql::QueryResults;
///
/// let store = MemoryStore::new();
///
@ -105,7 +107,7 @@ impl MemoryStore {
/// store.insert(Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
///
/// // SPARQL query
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
/// assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
/// }
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
@ -113,6 +115,14 @@ impl MemoryStore {
pub fn query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
) -> Result<QueryResults, EvaluationError> {
self.query_opt(query, QueryOptions::default())
}
/// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) with some options.
pub fn query_opt(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<QueryResults, EvaluationError> {
evaluate_query(self.clone(), query, options)

@ -34,7 +34,7 @@ use std::{fmt, str};
/// ```
/// use oxigraph::RocksDbStore;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryOptions, QueryResults};
/// use oxigraph::sparql::QueryResults;
/// # use std::fs::remove_dir_all;
///
/// # {
@ -50,7 +50,7 @@ use std::{fmt, str};
/// assert_eq!(vec![quad], results?);
///
/// // SPARQL query
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
/// assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
/// }
/// #
@ -127,6 +127,14 @@ impl RocksDbStore {
pub fn query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
) -> Result<QueryResults, EvaluationError> {
self.query_opt(query, QueryOptions::default())
}
/// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) with some options.
pub fn query_opt(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<QueryResults, EvaluationError> {
evaluate_query(self.clone(), query, options)

@ -36,7 +36,7 @@ use std::{fmt, io, str};
/// Usage example:
/// ```
/// use oxigraph::SledStore;
/// use oxigraph::sparql::{QueryOptions, QueryResults};
/// use oxigraph::sparql::QueryResults;
/// use oxigraph::model::*;
/// # use std::fs::remove_dir_all;
///
@ -53,7 +53,7 @@ use std::{fmt, io, str};
/// assert_eq!(vec![quad], results?);
///
/// // SPARQL query
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())? {
/// if let QueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {
/// assert_eq!(solutions.next().unwrap()?.get("s"), Some(&ex.into()));
/// };
/// #
@ -137,6 +137,14 @@ impl SledStore {
pub fn query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
) -> Result<QueryResults, EvaluationError> {
self.query_opt(query, QueryOptions::default())
}
/// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) with some options.
pub fn query_opt(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions,
) -> Result<QueryResults, EvaluationError> {
evaluate_query(self.clone(), query, options)

@ -1,7 +1,7 @@
//! This crate provides implementation of [Sophia] traits for the `store` module.
//! [Sophia]: https://docs.rs/sophia/latest/sophia/
use crate::model::*;
use crate::sparql::{EvaluationError, QueryOptions, QueryResults};
use crate::sparql::{EvaluationError, QueryResults};
use crate::store::*;
use sophia_api::dataset::*;
use sophia_api::quad::stream::{QuadSource, StreamResult};
@ -24,9 +24,7 @@ type StreamedSophiaQuad<'a> = StreamedQuad<'a, ByValue<SophiaQuad>>;
macro_rules! sparql_to_hashset {
($store: ident, $err_map: ident, $sparql: expr) => {{
(|| -> Result<HashSet<Term>, EvaluationError> {
if let QueryResults::Solutions(solutions) =
$store.query($sparql, QueryOptions::default())?
{
if let QueryResults::Solutions(solutions) = $store.query($sparql)? {
solutions
.map(|r| r.map(|v| v.get(0).unwrap().clone()))
.collect()

@ -3,7 +3,6 @@ use crate::model::*;
use crate::sparql::*;
use crate::store_utils::*;
use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::sparql::QueryOptions;
use oxigraph::store::memory::*;
use pyo3::basic::CompareOp;
use pyo3::exceptions::{PyNotImplementedError, PyValueError};
@ -164,10 +163,7 @@ impl PyMemoryStore {
default_graph,
named_graphs,
)?;
let results = self
.inner
.query(query, QueryOptions::default())
.map_err(map_evaluation_error)?;
let results = self.inner.query(query).map_err(map_evaluation_error)?;
query_results_to_python(py, results)
}

@ -3,7 +3,6 @@ use crate::model::*;
use crate::sparql::*;
use crate::store_utils::*;
use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::sparql::QueryOptions;
use oxigraph::store::sled::*;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::{
@ -178,10 +177,7 @@ impl PySledStore {
default_graph,
named_graphs,
)?;
let results = self
.inner
.query(query, QueryOptions::default())
.map_err(map_evaluation_error)?;
let results = self.inner.query(query).map_err(map_evaluation_error)?;
query_results_to_python(py, results)
}

@ -18,7 +18,7 @@ use async_std::task::{block_on, spawn, spawn_blocking};
use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result, StatusCode};
use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::model::{GraphName, NamedNode, NamedOrBlankNode};
use oxigraph::sparql::{Query, QueryOptions, QueryResults, QueryResultsFormat, Update};
use oxigraph::sparql::{Query, QueryResults, QueryResultsFormat, Update};
use std::io::BufReader;
use std::str::FromStr;
use url::form_urlencoded;
@ -246,8 +246,7 @@ async fn evaluate_sparql_query(
.set_available_named_graphs(named_graph_uris);
}
let options = QueryOptions::default().with_simple_service_handler();
let results = store.query(query, options)?;
let results = store.query(query)?;
//TODO: stream
if let QueryResults::Graph(_) = results {
let format = content_negotiation(

@ -89,7 +89,7 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
test,
error
)),
Ok(query) => match store.query(query, options) {
Ok(query) => match store.query_opt(query, options) {
Err(error) => Err(anyhow!(
"Failure to execute query of {} with error: {}",
test,
@ -272,7 +272,7 @@ impl ServiceHandler for StaticServiceHandler {
format!("Service {} not found", service_name),
)
})?
.query(
.query_opt(
query,
QueryOptions::default().with_service_handler(self.clone()),
)

@ -17,7 +17,7 @@ use async_std::prelude::*;
use async_std::task::{spawn, spawn_blocking};
use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result, StatusCode};
use oxigraph::io::GraphFormat;
use oxigraph::sparql::{Query, QueryOptions, QueryResults, QueryResultsFormat};
use oxigraph::sparql::{Query, QueryResults, QueryResultsFormat};
use oxigraph::RocksDbStore;
use std::str::FromStr;
use std::time::Duration;
@ -183,8 +183,7 @@ async fn evaluate_sparql_query(
if query.dataset().is_default_dataset() {
query.dataset_mut().set_default_graph_as_union();
}
let options = QueryOptions::default().with_simple_service_handler();
let results = store.query(query, options)?;
let results = store.query(query)?;
if let QueryResults::Graph(_) = results {
let format = content_negotiation(
request,

Loading…
Cancel
Save