diff --git a/src/store/store.rs b/src/store/store.rs index 63e0eb8f..b0c442bd 100644 --- a/src/store/store.rs +++ b/src/store/store.rs @@ -1,6 +1,8 @@ use errors::*; use model::*; use std::fmt; +use std::iter::empty; +use std::iter::once; use std::iter::FromIterator; use std::iter::Iterator; use std::sync::Arc; @@ -9,21 +11,21 @@ use store::numeric_encoder::*; /// Defines the Store traits that is used to have efficient binary storage pub trait EncodedQuadsStore: BytesStore + Sized { - type QuadsIterator: Iterator>; - type QuadsForSubjectIterator: Iterator>; - type QuadsForSubjectPredicateIterator: Iterator>; - type QuadsForSubjectPredicateObjectIterator: Iterator>; - type QuadsForSubjectObjectIterator: Iterator>; - type QuadsForPredicateIterator: Iterator>; - type QuadsForPredicateObjectIterator: Iterator>; - type QuadsForObjectIterator: Iterator>; - type QuadsForGraphIterator: Iterator>; - type QuadsForSubjectGraphIterator: Iterator>; - type QuadsForSubjectPredicateGraphIterator: Iterator>; - type QuadsForSubjectObjectGraphIterator: Iterator>; - type QuadsForPredicateGraphIterator: Iterator>; - type QuadsForPredicateObjectGraphIterator: Iterator>; - type QuadsForObjectGraphIterator: Iterator>; + type QuadsIterator: Iterator> + 'static; + type QuadsForSubjectIterator: Iterator> + 'static; + type QuadsForSubjectPredicateIterator: Iterator> + 'static; + type QuadsForSubjectPredicateObjectIterator: Iterator> + 'static; + type QuadsForSubjectObjectIterator: Iterator> + 'static; + type QuadsForPredicateIterator: Iterator> + 'static; + type QuadsForPredicateObjectIterator: Iterator> + 'static; + type QuadsForObjectIterator: Iterator> + 'static; + type QuadsForGraphIterator: Iterator> + 'static; + type QuadsForSubjectGraphIterator: Iterator> + 'static; + type QuadsForSubjectPredicateGraphIterator: Iterator> + 'static; + type QuadsForSubjectObjectGraphIterator: Iterator> + 'static; + type QuadsForPredicateGraphIterator: Iterator> + 'static; + type QuadsForPredicateObjectGraphIterator: Iterator> + 'static; + type QuadsForObjectGraphIterator: Iterator> + 'static; fn encoder(&self) -> Encoder> { Encoder::new(DelegatingBytesStore(&self)) @@ -94,6 +96,81 @@ pub trait EncodedQuadsStore: BytesStore + Sized { fn contains(&self, quad: &EncodedQuad) -> Result; fn insert(&self, quad: &EncodedQuad) -> Result<()>; fn remove(&self, quad: &EncodedQuad) -> Result<()>; + fn quads_for_pattern( + &self, + subject: Option, + predicate: Option, + object: Option, + graph_name: Option, + ) -> Result>>> { + Ok(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()) + } + } + None => Box::new( + 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)?, + ), + None => Box::new(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)?, + ), + None => Box::new(self.quads_for_subject_object(subject, object)?), + }, + None => match graph_name { + Some(graph_name) => { + Box::new(self.quads_for_subject_graph(subject, graph_name)?) + } + None => Box::new(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)?, + ), + None => Box::new(self.quads_for_predicate_object(predicate, object)?), + }, + None => match graph_name { + Some(graph_name) => { + Box::new(self.quads_for_predicate_graph(predicate, graph_name)?) + } + None => Box::new(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)?) + } + None => Box::new(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()?), + }, + }, + }, + }) + } } pub struct StoreDataset {