Makes ReadableEncodedStore::encoded_quads_for_pattern static

pull/46/head
Tpt 4 years ago
parent 2cc5e39f94
commit 90d4baae2a
  1. 10
      lib/src/sparql/plan.rs
  2. 44
      lib/src/store/memory.rs
  3. 8
      lib/src/store/mod.rs
  4. 128
      lib/src/store/rocksdb.rs
  5. 138
      lib/src/store/sled.rs

@ -573,13 +573,13 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
} }
} }
pub fn quads_for_pattern<'a>( pub fn quads_for_pattern(
&'a self, &self,
subject: Option<EncodedTerm>, subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>, predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>, object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>, graph_name: Option<EncodedTerm>,
) -> Box<dyn Iterator<Item = Result<EncodedQuad>> + 'a> { ) -> Box<dyn Iterator<Item = Result<EncodedQuad>>> {
if graph_name == None { if graph_name == None {
Box::new( Box::new(
self.store self.store
@ -604,8 +604,10 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
}), }),
) )
} else { } else {
Box::new(
self.store self.store
.encoded_quads_for_pattern(subject, predicate, object, graph_name) .encoded_quads_for_pattern(subject, predicate, object, graph_name),
)
} }
} }

@ -5,7 +5,7 @@ use crate::model::*;
use crate::sparql::{GraphPattern, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::sparql::{GraphPattern, QueryOptions, QueryResult, SimplePreparedQuery};
use crate::store::numeric_encoder::*; use crate::store::numeric_encoder::*;
use crate::store::{load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore}; 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::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::convert::Infallible; use std::convert::Infallible;
@ -15,6 +15,7 @@ use std::io::BufRead;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::mem::size_of; use std::mem::size_of;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::vec::IntoIter;
/// In-memory store. /// 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. /// 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 { impl<'a> ReadableEncodedStore for MemoryStore {
fn encoded_quads_for_pattern<'b>( type QuadsIter = EncodedQuadsIter;
&'b self,
fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>, subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>, predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>, object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>, graph_name: Option<EncodedTerm>,
) -> Box<dyn Iterator<Item = crate::Result<EncodedQuad>> + 'b> { ) -> EncodedQuadsIter {
Box::new( EncodedQuadsIter {
self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) iter: self
.into_iter() .encoded_quads_for_pattern_inner(subject, predicate, object, graph_name)
.map(Ok), .into_iter(),
) }
} }
} }
@ -1018,6 +1021,29 @@ impl fmt::Display for MemoryStore {
} }
} }
pub(crate) struct EncodedQuadsIter {
iter: IntoIter<EncodedQuad>,
}
impl Iterator for EncodedQuadsIter {
type Item = Result<EncodedQuad, Error>;
fn next(&mut self) -> Option<Result<EncodedQuad, Error>> {
self.iter.next().map(Ok)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn fold<Acc, G>(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 // Isomorphism implementation
fn iso_canonicalize(g: &MemoryStore) -> Vec<Vec<u8>> { fn iso_canonicalize(g: &MemoryStore) -> Vec<Vec<u8>> {

@ -27,13 +27,15 @@ use std::io::BufRead;
use std::iter::Iterator; use std::iter::Iterator;
pub(crate) trait ReadableEncodedStore: StrLookup { pub(crate) trait ReadableEncodedStore: StrLookup {
fn encoded_quads_for_pattern<'a>( type QuadsIter: Iterator<Item = Result<EncodedQuad>> + 'static;
&'a self,
fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>, subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>, predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>, object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>, graph_name: Option<EncodedTerm>,
) -> Box<dyn Iterator<Item = Result<EncodedQuad>> + 'a>; ) -> Self::QuadsIter;
} }
pub(crate) trait WritableEncodedStore: StrContainer { pub(crate) trait WritableEncodedStore: StrContainer {

@ -273,69 +273,6 @@ impl RocksDbStore {
Ok(self.db.get_pinned_cf(self.spog_cf(), &buffer)?.is_some()) Ok(self.db.get_pinned_cf(self.spog_cf(), &buffer)?.is_some())
} }
fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
) -> 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 { fn quads(&self) -> DecodingIndexIterator {
self.spog_quads(Vec::default()) self.spog_quads(Vec::default())
} }
@ -503,14 +440,69 @@ impl StrLookup for RocksDbStore {
} }
impl ReadableEncodedStore for RocksDbStore { impl ReadableEncodedStore for RocksDbStore {
fn encoded_quads_for_pattern<'a>( type QuadsIter = DecodingIndexIterator;
&'a self,
fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>, subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>, predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>, object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>, graph_name: Option<EncodedTerm>,
) -> Box<dyn Iterator<Item = Result<EncodedQuad>> + 'a> { ) -> DecodingIndexIterator {
Box::new(self.encoded_quads_for_pattern(subject, predicate, object, graph_name)) 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, iter: StaticDBRowIterator,
prefix: Vec<u8>, prefix: Vec<u8>,
encoding: QuadEncoding, encoding: QuadEncoding,

@ -127,7 +127,7 @@ impl SledStore {
let object = object.map(|o| o.into()); let object = object.map(|o| o.into());
let graph_name = graph_name.map(|g| g.into()); let graph_name = graph_name.map(|g| g.into());
let this = self.clone(); 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?)) .map(move |quad| this.decode_quad(&quad?))
} }
@ -220,73 +220,6 @@ impl SledStore {
Ok(self.quads.contains_key(buffer)?) Ok(self.quads.contains_key(buffer)?)
} }
fn encoded_quads_for_pattern_inner(
&self,
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
) -> 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 { fn quads(&self) -> DecodingQuadIterator {
self.inner_quads(&[SPOG_PREFIX]) self.inner_quads(&[SPOG_PREFIX])
} }
@ -430,14 +363,73 @@ impl StrLookup for SledStore {
} }
impl ReadableEncodedStore for SledStore { impl ReadableEncodedStore for SledStore {
fn encoded_quads_for_pattern<'a>( type QuadsIter = DecodingQuadIterator;
&'a self,
fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>, subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>, predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>, object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>, graph_name: Option<EncodedTerm>,
) -> Box<dyn Iterator<Item = Result<EncodedQuad>> + 'a> { ) -> DecodingQuadIterator {
Box::new(self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name)) 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 vec
} }
struct DecodingQuadIterator { pub(crate) struct DecodingQuadIterator {
iter: Iter, iter: Iter,
} }

Loading…
Cancel
Save