Improves EncodedStore::quads_for_pattern signature

pull/10/head
Tpt 6 years ago
parent d8de4eaf7a
commit d537a54f0b
  1. 130
      lib/src/sparql/eval.rs
  2. 82
      lib/src/store/encoded.rs
  3. 14
      lib/src/store/numeric_encoder.rs

@ -108,83 +108,63 @@ impl<S: EncodedQuadsStore> SimpleEvaluator<S> {
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))),

@ -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<Item = Result<EncodedQuad>> + 'static;
type QuadsForObjectGraphIterator: Iterator<Item = Result<EncodedQuad>> + 'static;
fn encoder(&self) -> Encoder<DelegatingStringStore<Self>> {
Encoder::new(DelegatingStringStore(&self))
fn encoder(&self) -> Encoder<&Self> {
Encoder::new(&self)
}
fn quads(&self) -> Result<Self::QuadsIterator>;
@ -103,74 +102,83 @@ pub trait EncodedQuadsStore: StringStore + Sized + 'static {
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
) -> Result<Box<dyn Iterator<Item = Result<EncodedQuad>>>> {
Ok(match subject {
) -> Box<dyn Iterator<Item = Result<EncodedQuad>>> {
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<E: 'static, I: Iterator<Item = Result<E>> + 'static>(
iter: Result<I>,
) -> Box<dyn Iterator<Item = Result<E>>> {
match iter {
Ok(iter) => Box::new(iter),
Err(error) => Box::new(once(Err(error))),
}
}
@ -866,22 +874,6 @@ impl<S: EncodedQuadsStore> fmt::Display for StoreUnionGraph<S> {
}
}
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<u64> {
self.0.insert_str(value)
}
fn get_str(&self, id: u64) -> Result<String> {
self.0.get_str(id)
}
fn get_url(&self, id: u64) -> Result<Url> {
self.0.get_url(id)
}
}
pub struct QuadsIterator<I: Iterator<Item = Result<EncodedQuad>>, S: EncodedQuadsStore> {
iter: I,
store: Arc<S>,

@ -54,6 +54,20 @@ pub trait StringStore {
}
}
impl<'a, S: StringStore> StringStore for &'a S {
fn insert_str(&self, value: &str) -> Result<u64> {
(*self).insert_str(value)
}
fn get_str(&self, id: u64) -> Result<String> {
(*self).get_str(id)
}
fn get_url(&self, id: u64) -> Result<Url> {
(*self).get_url(id)
}
}
pub struct MemoryStringStore {
id2str: RwLock<Vec<String>>,
str2id: RwLock<BTreeMap<String, u64>>,

Loading…
Cancel
Save