From 90d4baae2a64659d39a4265719092fdadf1b639e Mon Sep 17 00:00:00 2001 From: Tpt Date: Sun, 26 Jul 2020 12:12:58 +0200 Subject: [PATCH] Makes ReadableEncodedStore::encoded_quads_for_pattern static --- lib/src/sparql/plan.rs | 12 ++-- lib/src/store/memory.rs | 44 ++++++++++--- lib/src/store/mod.rs | 8 ++- lib/src/store/rocksdb.rs | 128 +++++++++++++++++------------------- lib/src/store/sled.rs | 138 ++++++++++++++++++--------------------- 5 files changed, 172 insertions(+), 158 deletions(-) diff --git a/lib/src/sparql/plan.rs b/lib/src/sparql/plan.rs index 5fcf9c67..9601e021 100644 --- a/lib/src/sparql/plan.rs +++ b/lib/src/sparql/plan.rs @@ -573,13 +573,13 @@ impl DatasetView { } } - pub fn quads_for_pattern<'a>( - &'a self, + pub fn quads_for_pattern( + &self, subject: Option, predicate: Option, object: Option, graph_name: Option, - ) -> Box> + 'a> { + ) -> Box>> { if graph_name == None { Box::new( self.store @@ -604,8 +604,10 @@ impl DatasetView { }), ) } else { - self.store - .encoded_quads_for_pattern(subject, predicate, object, graph_name) + Box::new( + self.store + .encoded_quads_for_pattern(subject, predicate, object, graph_name), + ) } } diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index 2f3524b0..8eab9117 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -5,7 +5,7 @@ use crate::model::*; use crate::sparql::{GraphPattern, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::store::numeric_encoder::*; use crate::store::{load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore}; -use crate::{DatasetSyntax, GraphSyntax}; +use crate::{DatasetSyntax, Error, GraphSyntax}; use std::collections::hash_map::DefaultHasher; use std::collections::{HashMap, HashSet}; use std::convert::Infallible; @@ -15,6 +15,7 @@ use std::io::BufRead; use std::iter::FromIterator; use std::mem::size_of; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; +use std::vec::IntoIter; /// In-memory store. /// It encodes a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) and allows to query and update it using SPARQL. @@ -641,18 +642,20 @@ impl StrContainer for MemoryStoreIndexes { } impl<'a> ReadableEncodedStore for MemoryStore { - fn encoded_quads_for_pattern<'b>( - &'b self, + type QuadsIter = EncodedQuadsIter; + + fn encoded_quads_for_pattern( + &self, subject: Option, predicate: Option, object: Option, graph_name: Option, - ) -> Box> + 'b> { - Box::new( - self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) - .into_iter() - .map(Ok), - ) + ) -> EncodedQuadsIter { + EncodedQuadsIter { + iter: self + .encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) + .into_iter(), + } } } @@ -1018,6 +1021,29 @@ impl fmt::Display for MemoryStore { } } +pub(crate) struct EncodedQuadsIter { + iter: IntoIter, +} + +impl Iterator for EncodedQuadsIter { + type Item = Result; + + fn next(&mut self) -> Option> { + self.iter.next().map(Ok) + } + + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } + + fn fold(self, init: Acc, mut g: G) -> Acc + where + G: FnMut(Acc, Self::Item) -> Acc, + { + self.iter.fold(init, |acc, elt| g(acc, Ok(elt))) + } +} + // Isomorphism implementation fn iso_canonicalize(g: &MemoryStore) -> Vec> { diff --git a/lib/src/store/mod.rs b/lib/src/store/mod.rs index 0341f95c..6b71fc1a 100644 --- a/lib/src/store/mod.rs +++ b/lib/src/store/mod.rs @@ -27,13 +27,15 @@ use std::io::BufRead; use std::iter::Iterator; pub(crate) trait ReadableEncodedStore: StrLookup { - fn encoded_quads_for_pattern<'a>( - &'a self, + type QuadsIter: Iterator> + 'static; + + fn encoded_quads_for_pattern( + &self, subject: Option, predicate: Option, object: Option, graph_name: Option, - ) -> Box> + 'a>; + ) -> Self::QuadsIter; } pub(crate) trait WritableEncodedStore: StrContainer { diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index 9181de9c..df87ecee 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -273,69 +273,6 @@ impl RocksDbStore { Ok(self.db.get_pinned_cf(self.spog_cf(), &buffer)?.is_some()) } - fn encoded_quads_for_pattern( - &self, - subject: Option, - predicate: Option, - object: Option, - graph_name: Option, - ) -> DecodingIndexIterator { - match subject { - Some(subject) => match predicate { - Some(predicate) => match object { - Some(object) => match graph_name { - Some(graph_name) => self - .spog_quads(encode_term_quad(subject, predicate, object, graph_name)), - None => self.quads_for_subject_predicate_object(subject, predicate, object), - }, - None => match graph_name { - Some(graph_name) => { - self.quads_for_subject_predicate_graph(subject, predicate, graph_name) - } - None => self.quads_for_subject_predicate(subject, predicate), - }, - }, - None => match object { - Some(object) => match graph_name { - Some(graph_name) => { - self.quads_for_subject_object_graph(subject, object, graph_name) - } - None => self.quads_for_subject_object(subject, object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_subject_graph(subject, graph_name), - None => self.quads_for_subject(subject), - }, - }, - }, - None => match predicate { - Some(predicate) => match object { - Some(object) => match graph_name { - Some(graph_name) => { - self.quads_for_predicate_object_graph(predicate, object, graph_name) - } - - None => self.quads_for_predicate_object(predicate, object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_predicate_graph(predicate, graph_name), - None => self.quads_for_predicate(predicate), - }, - }, - None => match object { - Some(object) => match graph_name { - Some(graph_name) => self.quads_for_object_graph(object, graph_name), - None => self.quads_for_object(object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_graph(graph_name), - None => self.quads(), - }, - }, - }, - } - } - fn quads(&self) -> DecodingIndexIterator { self.spog_quads(Vec::default()) } @@ -503,14 +440,69 @@ impl StrLookup for RocksDbStore { } impl ReadableEncodedStore for RocksDbStore { - fn encoded_quads_for_pattern<'a>( - &'a self, + type QuadsIter = DecodingIndexIterator; + + fn encoded_quads_for_pattern( + &self, subject: Option, predicate: Option, object: Option, graph_name: Option, - ) -> Box> + 'a> { - Box::new(self.encoded_quads_for_pattern(subject, predicate, object, graph_name)) + ) -> DecodingIndexIterator { + match subject { + Some(subject) => match predicate { + Some(predicate) => match object { + Some(object) => match graph_name { + Some(graph_name) => self + .spog_quads(encode_term_quad(subject, predicate, object, graph_name)), + None => self.quads_for_subject_predicate_object(subject, predicate, object), + }, + None => match graph_name { + Some(graph_name) => { + self.quads_for_subject_predicate_graph(subject, predicate, graph_name) + } + None => self.quads_for_subject_predicate(subject, predicate), + }, + }, + None => match object { + Some(object) => match graph_name { + Some(graph_name) => { + self.quads_for_subject_object_graph(subject, object, graph_name) + } + None => self.quads_for_subject_object(subject, object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_subject_graph(subject, graph_name), + None => self.quads_for_subject(subject), + }, + }, + }, + None => match predicate { + Some(predicate) => match object { + Some(object) => match graph_name { + Some(graph_name) => { + self.quads_for_predicate_object_graph(predicate, object, graph_name) + } + + None => self.quads_for_predicate_object(predicate, object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_predicate_graph(predicate, graph_name), + None => self.quads_for_predicate(predicate), + }, + }, + None => match object { + Some(object) => match graph_name { + Some(graph_name) => self.quads_for_object_graph(object, graph_name), + None => self.quads_for_object(object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_graph(graph_name), + None => self.quads(), + }, + }, + }, + } } } @@ -753,7 +745,7 @@ impl StaticDBRowIterator { } } -struct DecodingIndexIterator { +pub(crate) struct DecodingIndexIterator { iter: StaticDBRowIterator, prefix: Vec, encoding: QuadEncoding, diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index 8288b4f0..d75a501c 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -127,7 +127,7 @@ impl SledStore { let object = object.map(|o| o.into()); let graph_name = graph_name.map(|g| g.into()); let this = self.clone(); - self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) + self.encoded_quads_for_pattern(subject, predicate, object, graph_name) .map(move |quad| this.decode_quad(&quad?)) } @@ -220,73 +220,6 @@ impl SledStore { Ok(self.quads.contains_key(buffer)?) } - fn encoded_quads_for_pattern_inner( - &self, - subject: Option, - predicate: Option, - object: Option, - graph_name: Option, - ) -> DecodingQuadIterator { - match subject { - Some(subject) => match predicate { - Some(predicate) => match object { - Some(object) => match graph_name { - Some(graph_name) => self.inner_quads(encode_term_quad( - SPOG_PREFIX, - subject, - predicate, - object, - graph_name, - )), - None => self.quads_for_subject_predicate_object(subject, predicate, object), - }, - None => match graph_name { - Some(graph_name) => { - self.quads_for_subject_predicate_graph(subject, predicate, graph_name) - } - None => self.quads_for_subject_predicate(subject, predicate), - }, - }, - None => match object { - Some(object) => match graph_name { - Some(graph_name) => { - self.quads_for_subject_object_graph(subject, object, graph_name) - } - None => self.quads_for_subject_object(subject, object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_subject_graph(subject, graph_name), - None => self.quads_for_subject(subject), - }, - }, - }, - None => match predicate { - Some(predicate) => match object { - Some(object) => match graph_name { - Some(graph_name) => { - self.quads_for_predicate_object_graph(predicate, object, graph_name) - } - None => self.quads_for_predicate_object(predicate, object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_predicate_graph(predicate, graph_name), - None => self.quads_for_predicate(predicate), - }, - }, - None => match object { - Some(object) => match graph_name { - Some(graph_name) => self.quads_for_object_graph(object, graph_name), - None => self.quads_for_object(object), - }, - None => match graph_name { - Some(graph_name) => self.quads_for_graph(graph_name), - None => self.quads(), - }, - }, - }, - } - } - fn quads(&self) -> DecodingQuadIterator { self.inner_quads(&[SPOG_PREFIX]) } @@ -430,14 +363,73 @@ impl StrLookup for SledStore { } impl ReadableEncodedStore for SledStore { - fn encoded_quads_for_pattern<'a>( - &'a self, + type QuadsIter = DecodingQuadIterator; + + fn encoded_quads_for_pattern( + &self, subject: Option, predicate: Option, object: Option, graph_name: Option, - ) -> Box> + 'a> { - Box::new(self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name)) + ) -> DecodingQuadIterator { + match subject { + Some(subject) => match predicate { + Some(predicate) => match object { + Some(object) => match graph_name { + Some(graph_name) => self.inner_quads(encode_term_quad( + SPOG_PREFIX, + subject, + predicate, + object, + graph_name, + )), + None => self.quads_for_subject_predicate_object(subject, predicate, object), + }, + None => match graph_name { + Some(graph_name) => { + self.quads_for_subject_predicate_graph(subject, predicate, graph_name) + } + None => self.quads_for_subject_predicate(subject, predicate), + }, + }, + None => match object { + Some(object) => match graph_name { + Some(graph_name) => { + self.quads_for_subject_object_graph(subject, object, graph_name) + } + None => self.quads_for_subject_object(subject, object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_subject_graph(subject, graph_name), + None => self.quads_for_subject(subject), + }, + }, + }, + None => match predicate { + Some(predicate) => match object { + Some(object) => match graph_name { + Some(graph_name) => { + self.quads_for_predicate_object_graph(predicate, object, graph_name) + } + None => self.quads_for_predicate_object(predicate, object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_predicate_graph(predicate, graph_name), + None => self.quads_for_predicate(predicate), + }, + }, + None => match object { + Some(object) => match graph_name { + Some(graph_name) => self.quads_for_object_graph(object, graph_name), + None => self.quads_for_object(object), + }, + None => match graph_name { + Some(graph_name) => self.quads_for_graph(graph_name), + None => self.quads(), + }, + }, + }, + } } } @@ -723,7 +715,7 @@ fn encode_term_quad( vec } -struct DecodingQuadIterator { +pub(crate) struct DecodingQuadIterator { iter: Iter, }