From d537a54f0b903291568f47b86e5b3b3327b4c113 Mon Sep 17 00:00:00 2001 From: Tpt Date: Thu, 6 Dec 2018 11:49:23 +0100 Subject: [PATCH] Improves EncodedStore::quads_for_pattern signature --- lib/src/sparql/eval.rs | 130 +++++++++++++------------------ lib/src/store/encoded.rs | 82 +++++++++---------- lib/src/store/numeric_encoder.rs | 14 ++++ 3 files changed, 106 insertions(+), 120 deletions(-) diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index 31f425a2..dcfdf69c 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -108,83 +108,63 @@ impl SimpleEvaluator { self.eval_plan(&*child, from) .flat_map(move |tuple| match tuple { Ok(tuple) => { - let iter: EncodedTuplesIterator = match eval - .store - .quads_for_pattern( - get_pattern_value(&subject, &tuple), - get_pattern_value(&predicate, &tuple), - get_pattern_value(&object, &tuple), - get_pattern_value(&graph_name, &tuple), - ) { - Ok(mut iter) => { - if subject.is_var() && subject == predicate { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.subject == quad.predicate, - })) - } - if subject.is_var() && subject == object { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.subject == quad.object, - })) - } - if predicate.is_var() && predicate == object { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.predicate == quad.object, - })) - } - if graph_name.is_var() { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => { - quad.graph_name != ENCODED_DEFAULT_GRAPH - } - })); - if graph_name == subject { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.graph_name == quad.subject, - })) - } - if graph_name == predicate { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.graph_name == quad.predicate, - })) - } - if graph_name == object { - iter = Box::new(iter.filter(|quad| match quad { - Err(_) => true, - Ok(quad) => quad.graph_name == quad.object, - })) - } - } - Box::new(iter.map(move |quad| { - let quad = quad?; - let mut new_tuple = tuple.clone(); - put_pattern_value( - &subject, - quad.subject, - &mut new_tuple, - ); - put_pattern_value( - &predicate, - quad.predicate, - &mut new_tuple, - ); - put_pattern_value(&object, quad.object, &mut new_tuple); - put_pattern_value( - &graph_name, - quad.graph_name, - &mut new_tuple, - ); - Ok(new_tuple) + let mut iter = eval.store.quads_for_pattern( + get_pattern_value(&subject, &tuple), + get_pattern_value(&predicate, &tuple), + get_pattern_value(&object, &tuple), + get_pattern_value(&graph_name, &tuple), + ); + if subject.is_var() && subject == predicate { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.subject == quad.predicate, + })) + } + if subject.is_var() && subject == object { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.subject == quad.object, + })) + } + if predicate.is_var() && predicate == object { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.predicate == quad.object, + })) + } + if graph_name.is_var() { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.graph_name != ENCODED_DEFAULT_GRAPH, + })); + if graph_name == subject { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.graph_name == quad.subject, })) } - Err(error) => Box::new(once(Err(error))), - }; + if graph_name == predicate { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.graph_name == quad.predicate, + })) + } + if graph_name == object { + iter = Box::new(iter.filter(|quad| match quad { + Err(_) => true, + Ok(quad) => quad.graph_name == quad.object, + })) + } + } + let iter: EncodedTuplesIterator = Box::new(iter.map(move |quad| { + let quad = quad?; + let mut new_tuple = tuple.clone(); + put_pattern_value(&subject, quad.subject, &mut new_tuple); + put_pattern_value(&predicate, quad.predicate, &mut new_tuple); + put_pattern_value(&object, quad.object, &mut new_tuple); + put_pattern_value(&graph_name, quad.graph_name, &mut new_tuple); + Ok(new_tuple) + })); iter } Err(error) => Box::new(once(Err(error))), diff --git a/lib/src/store/encoded.rs b/lib/src/store/encoded.rs index cb19d853..0ba1372c 100644 --- a/lib/src/store/encoded.rs +++ b/lib/src/store/encoded.rs @@ -6,7 +6,6 @@ use std::iter::FromIterator; use std::iter::Iterator; use std::sync::Arc; use store::numeric_encoder::*; -use url::Url; use Result; /// Defines the Store traits that is used to have efficient binary storage @@ -28,8 +27,8 @@ pub trait EncodedQuadsStore: StringStore + Sized + 'static { type QuadsForPredicateObjectGraphIterator: Iterator> + 'static; type QuadsForObjectGraphIterator: Iterator> + 'static; - fn encoder(&self) -> Encoder> { - Encoder::new(DelegatingStringStore(&self)) + fn encoder(&self) -> Encoder<&Self> { + Encoder::new(&self) } fn quads(&self) -> Result; @@ -103,74 +102,83 @@ pub trait EncodedQuadsStore: StringStore + Sized + 'static { predicate: Option, object: Option, graph_name: Option, - ) -> Result>>> { - Ok(match subject { + ) -> Box>> { + match subject { Some(subject) => match predicate { Some(predicate) => match object { Some(object) => match graph_name { Some(graph_name) => { let quad = EncodedQuad::new(subject, predicate, object, graph_name); - if self.contains(&quad)? { - Box::new(once(Ok(quad))) - } else { - Box::new(empty()) + match self.contains(&quad) { + Ok(true) => Box::new(once(Ok(quad))), + Ok(false) => Box::new(empty()), + Err(error) => Box::new(once(Err(error))), } } - None => Box::new( - self.quads_for_subject_predicate_object(subject, predicate, object)?, + None => wrap_error( + self.quads_for_subject_predicate_object(subject, predicate, object), ), }, None => match graph_name { - Some(graph_name) => Box::new( - self.quads_for_subject_predicate_graph(subject, predicate, graph_name)?, + Some(graph_name) => wrap_error( + self.quads_for_subject_predicate_graph(subject, predicate, graph_name), ), - None => Box::new(self.quads_for_subject_predicate(subject, predicate)?), + None => wrap_error(self.quads_for_subject_predicate(subject, predicate)), }, }, None => match object { Some(object) => match graph_name { - Some(graph_name) => Box::new( - self.quads_for_subject_object_graph(subject, object, graph_name)?, + Some(graph_name) => wrap_error( + self.quads_for_subject_object_graph(subject, object, graph_name), ), - None => Box::new(self.quads_for_subject_object(subject, object)?), + None => wrap_error(self.quads_for_subject_object(subject, object)), }, None => match graph_name { Some(graph_name) => { - Box::new(self.quads_for_subject_graph(subject, graph_name)?) + wrap_error(self.quads_for_subject_graph(subject, graph_name)) } - None => Box::new(self.quads_for_subject(subject)?), + None => wrap_error(self.quads_for_subject(subject)), }, }, }, None => match predicate { Some(predicate) => match object { Some(object) => match graph_name { - Some(graph_name) => Box::new( - self.quads_for_predicate_object_graph(predicate, object, graph_name)?, + Some(graph_name) => wrap_error( + self.quads_for_predicate_object_graph(predicate, object, graph_name), ), - None => Box::new(self.quads_for_predicate_object(predicate, object)?), + None => wrap_error(self.quads_for_predicate_object(predicate, object)), }, None => match graph_name { Some(graph_name) => { - Box::new(self.quads_for_predicate_graph(predicate, graph_name)?) + wrap_error(self.quads_for_predicate_graph(predicate, graph_name)) } - None => Box::new(self.quads_for_predicate(predicate)?), + None => wrap_error(self.quads_for_predicate(predicate)), }, }, None => match object { Some(object) => match graph_name { Some(graph_name) => { - Box::new(self.quads_for_object_graph(object, graph_name)?) + wrap_error(self.quads_for_object_graph(object, graph_name)) } - None => Box::new(self.quads_for_object(object)?), + None => wrap_error(self.quads_for_object(object)), }, None => match graph_name { - Some(graph_name) => Box::new(self.quads_for_graph(graph_name)?), - None => Box::new(self.quads()?), + Some(graph_name) => wrap_error(self.quads_for_graph(graph_name)), + None => wrap_error(self.quads()), }, }, }, - }) + } + } +} + +fn wrap_error> + 'static>( + iter: Result, +) -> Box>> { + match iter { + Ok(iter) => Box::new(iter), + Err(error) => Box::new(once(Err(error))), } } @@ -866,22 +874,6 @@ impl fmt::Display for StoreUnionGraph { } } -pub struct DelegatingStringStore<'a, S: 'a + StringStore + Sized>(&'a S); - -impl<'a, S: StringStore> StringStore for DelegatingStringStore<'a, S> { - fn insert_str(&self, value: &str) -> Result { - self.0.insert_str(value) - } - - fn get_str(&self, id: u64) -> Result { - self.0.get_str(id) - } - - fn get_url(&self, id: u64) -> Result { - self.0.get_url(id) - } -} - pub struct QuadsIterator>, S: EncodedQuadsStore> { iter: I, store: Arc, diff --git a/lib/src/store/numeric_encoder.rs b/lib/src/store/numeric_encoder.rs index 90740698..fadecd80 100644 --- a/lib/src/store/numeric_encoder.rs +++ b/lib/src/store/numeric_encoder.rs @@ -54,6 +54,20 @@ pub trait StringStore { } } +impl<'a, S: StringStore> StringStore for &'a S { + fn insert_str(&self, value: &str) -> Result { + (*self).insert_str(value) + } + + fn get_str(&self, id: u64) -> Result { + (*self).get_str(id) + } + + fn get_url(&self, id: u64) -> Result { + (*self).get_url(id) + } +} + pub struct MemoryStringStore { id2str: RwLock>, str2id: RwLock>,