From 773bdb943e1f73dbacfcb768409bfaba98ee6d3f Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 28 Oct 2020 17:11:30 +0100 Subject: [PATCH] Drops QueryOption parameter from the query method Introduces a query_opt method for that --- js/src/store.rs | 7 ++----- lib/src/lib.rs | 4 ++-- lib/src/sparql/mod.rs | 27 ++++++++++++++++----------- lib/src/sparql/model.rs | 19 +++++++++---------- lib/src/sparql/service.rs | 4 ++-- lib/src/store/memory.rs | 18 ++++++++++++++---- lib/src/store/rocksdb.rs | 12 ++++++++++-- lib/src/store/sled.rs | 12 ++++++++++-- lib/src/store/sophia.rs | 6 ++---- python/src/memory_store.rs | 6 +----- python/src/sled_store.rs | 6 +----- server/src/main.rs | 5 ++--- testsuite/src/sparql_evaluator.rs | 4 ++-- wikibase/src/main.rs | 5 ++--- 14 files changed, 75 insertions(+), 60 deletions(-) diff --git a/js/src/store.rs b/js/src/store.rs index 0fdf4728..952aa423 100644 --- a/js/src/store.rs +++ b/js/src/store.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 { - 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(); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index bd1ec770..b1cc7668 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -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>::Ok(()) diff --git a/lib/src/sparql/mod.rs b/lib/src/sparql/mod.rs index 838c1c71..58c252d8 100644 --- a/lib/src/sparql/mod.rs +++ b/lib/src/sparql/mod.rs @@ -90,7 +90,11 @@ pub(crate) fn evaluate_query( } } -/// 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>, @@ -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 } } diff --git a/lib/src/sparql/model.rs b/lib/src/sparql/model.rs index ab8d62fc..bb9ef758 100644 --- a/lib/src/sparql/model.rs +++ b/lib/src/sparql/model.rs @@ -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>::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>::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>::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?); /// } diff --git a/lib/src/sparql/service.rs b/lib/src/sparql/service.rs index 54d0c92d..a82a4ba4 100644 --- a/lib/src/sparql/service.rs +++ b/lib/src/sparql/service.rs @@ -29,7 +29,7 @@ use std::error::Error; /// /// fn handle(&self,service_name: NamedNode, query: Query) -> Result { /// 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 { ?s ?p ?o } }", /// QueryOptions::default().with_service_handler(service) /// )? { diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index 9eafc116..09fb8846 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -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>::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>::Ok(()) @@ -113,6 +115,14 @@ impl MemoryStore { pub fn query( &self, query: impl TryInto>, + ) -> Result { + 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>, options: QueryOptions, ) -> Result { evaluate_query(self.clone(), query, options) diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index 20f1e333..c6a21d4d 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -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>, + ) -> Result { + 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>, options: QueryOptions, ) -> Result { evaluate_query(self.clone(), query, options) diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index 74a3808c..b5bcf2ba 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -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>, + ) -> Result { + 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>, options: QueryOptions, ) -> Result { evaluate_query(self.clone(), query, options) diff --git a/lib/src/store/sophia.rs b/lib/src/store/sophia.rs index 3d99b1e5..9c7e6283 100644 --- a/lib/src/store/sophia.rs +++ b/lib/src/store/sophia.rs @@ -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>; macro_rules! sparql_to_hashset { ($store: ident, $err_map: ident, $sparql: expr) => {{ (|| -> Result, 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() diff --git a/python/src/memory_store.rs b/python/src/memory_store.rs index 1d97c8e7..0b1b0eb5 100644 --- a/python/src/memory_store.rs +++ b/python/src/memory_store.rs @@ -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) } diff --git a/python/src/sled_store.rs b/python/src/sled_store.rs index a9274db4..6fb39753 100644 --- a/python/src/sled_store.rs +++ b/python/src/sled_store.rs @@ -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) } diff --git a/server/src/main.rs b/server/src/main.rs index 450d1959..ee89e808 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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( diff --git a/testsuite/src/sparql_evaluator.rs b/testsuite/src/sparql_evaluator.rs index ac2ddc65..2e6597ad 100644 --- a/testsuite/src/sparql_evaluator.rs +++ b/testsuite/src/sparql_evaluator.rs @@ -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()), ) diff --git a/wikibase/src/main.rs b/wikibase/src/main.rs index 74cb90b2..f74a8407 100644 --- a/wikibase/src/main.rs +++ b/wikibase/src/main.rs @@ -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,