Adds nice traits for Graph and Dataset

pull/10/head
Tpt 6 years ago
parent 7ace14916c
commit 08f1a68c44
  1. 127
      src/store/mod.rs
  2. 33
      src/store/numeric_encoder.rs
  3. 87
      src/store/rocksdb/mod.rs
  4. 273
      src/store/rocksdb/storage.rs
  5. 701
      src/store/store.rs

@ -2,3 +2,130 @@ pub mod isomorphism;
pub mod memory;
mod numeric_encoder;
pub mod rocksdb;
mod store;
use errors::*;
use model::*;
pub trait Graph {
type TriplesIterator: Iterator<Item = Result<Triple>>;
type TriplesForSubjectIterator: Iterator<Item = Result<Triple>>;
type TriplesForSubjectPredicateIterator: Iterator<Item = Result<Triple>>;
type TriplesForSubjectObjectIterator: Iterator<Item = Result<Triple>>;
type TriplesForPredicateIterator: Iterator<Item = Result<Triple>>;
type TriplesForPredicateObjectIterator: Iterator<Item = Result<Triple>>;
type TriplesForObjectIterator: Iterator<Item = Result<Triple>>;
fn iter(&self) -> Result<Self::TriplesIterator> {
self.triples()
}
fn triples(&self) -> Result<Self::TriplesIterator>;
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::TriplesForSubjectIterator>;
fn triples_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::TriplesForSubjectPredicateIterator>;
fn triples_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<Self::TriplesForSubjectObjectIterator>;
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<Self::TriplesForPredicateIterator>;
fn triples_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::TriplesForPredicateObjectIterator>;
fn triples_for_object(&self, object: &Term) -> Result<Self::TriplesForObjectIterator>;
fn contains(&self, triple: &Triple) -> Result<bool>;
fn insert(&self, triple: &Triple) -> Result<()>;
fn remove(&self, triple: &Triple) -> Result<()>;
}
pub trait NamedGraph: Graph {
fn name(&self) -> &NamedOrBlankNode;
}
pub trait Dataset {
type NamedGraph: NamedGraph;
type DefaultGraph: Graph;
type UnionGraph: Graph;
type QuadsIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForObjectIterator: Iterator<Item = Result<Quad>>;
fn named_graph(&self, name: &NamedOrBlankNode) -> Result<Self::NamedGraph>;
fn default_graph(&self) -> Self::DefaultGraph;
fn union_graph(&self) -> Self::UnionGraph;
fn iter(&self) -> Result<Self::QuadsIterator> {
self.quads()
}
fn quads(&self) -> Result<Self::QuadsIterator>;
fn quads_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::QuadsForSubjectIterator>;
fn quads_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::QuadsForSubjectPredicateIterator>;
fn quads_for_subject_predicate_object(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForSubjectPredicateObjectIterator>;
fn quads_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<Self::QuadsForSubjectObjectIterator>;
fn quads_for_predicate(&self, predicate: &NamedNode)
-> Result<Self::QuadsForPredicateIterator>;
fn quads_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForPredicateObjectIterator>;
fn quads_for_object(&self, object: &Term) -> Result<Self::QuadsForObjectIterator>;
fn contains(&self, quad: &Quad) -> Result<bool>;
fn insert(&self, quad: &Quad) -> Result<()>;
fn remove(&self, quad: &Quad) -> Result<()>;
}

@ -12,8 +12,8 @@ use uuid::Uuid;
pub trait BytesStore {
type BytesOutput: Deref<Target = [u8]>;
fn put(&self, value: &[u8]) -> Result<u64>;
fn get(&self, id: u64) -> Result<Option<Self::BytesOutput>>;
fn insert_bytes(&self, value: &[u8]) -> Result<u64>;
fn get_bytes(&self, id: u64) -> Result<Option<Self::BytesOutput>>;
}
const TYPE_NOTHING_ID: u8 = 0;
@ -258,6 +258,19 @@ impl<S: BytesStore> Encoder<S> {
})
}
pub fn encode_triple_in_graph(
&self,
triple: &Triple,
graph_name: Option<EncodedTerm>,
) -> Result<EncodedQuad> {
Ok(EncodedQuad {
subject: self.encode_named_or_blank_node(triple.subject())?,
predicate: self.encode_named_node(triple.predicate())?,
object: self.encode_term(triple.object())?,
graph_name,
})
}
pub fn decode_term(&self, encoded: &EncodedTerm) -> Result<Term> {
match encoded {
EncodedTerm::NamedNode { iri_id } => {
@ -297,6 +310,14 @@ impl<S: BytesStore> Encoder<S> {
}
}
pub fn decode_triple(&self, encoded: &EncodedQuad) -> Result<Triple> {
Ok(Triple::new(
self.decode_named_or_blank_node(&encoded.subject)?,
self.decode_named_node(&encoded.predicate)?,
self.decode_term(&encoded.object)?,
))
}
pub fn decode_quad(&self, encoded: &EncodedQuad) -> Result<Quad> {
Ok(Quad::new(
self.decode_named_or_blank_node(&encoded.subject)?,
@ -310,7 +331,7 @@ impl<S: BytesStore> Encoder<S> {
}
fn encode_str_value(&self, text: &str) -> Result<u64> {
self.string_store.put(text.as_bytes())
self.string_store.insert_bytes(text.as_bytes())
}
fn decode_url_value(&self, id: u64) -> Result<Url> {
@ -325,7 +346,7 @@ impl<S: BytesStore> Encoder<S> {
fn decode_value(&self, id: u64) -> Result<S::BytesOutput> {
self.string_store
.get(id)?
.get_bytes(id)?
.ok_or("value not found in the dictionary".into())
}
}
@ -352,7 +373,7 @@ mod test {
impl BytesStore for MemoryBytesStore {
type BytesOutput = Vec<u8>;
fn put(&self, value: &[u8]) -> Result<u64> {
fn insert_bytes(&self, value: &[u8]) -> Result<u64> {
let mut str2id = self.str2id.borrow_mut();
let mut id2str = self.id2str.borrow_mut();
let id = str2id.entry(value.to_vec()).or_insert_with(|| {
@ -363,7 +384,7 @@ mod test {
Ok(*id)
}
fn get(&self, id: u64) -> Result<Option<Vec<u8>>> {
fn get_bytes(&self, id: u64) -> Result<Option<Vec<u8>>> {
Ok(self.id2str.borrow().get(&id).map(|s| s.to_owned()))
}
}

@ -1,95 +1,14 @@
mod storage;
use errors::*;
use model::*;
use std::path::Path;
use store::numeric_encoder::EncodedQuad;
use store::numeric_encoder::Encoder;
use store::rocksdb::storage::*;
use store::store::StoreDataset;
pub struct RocksDbDataset {
store: RocksDbStore,
}
pub type RocksDbDataset = StoreDataset<RocksDbStore>;
impl RocksDbDataset {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
store: RocksDbStore::open(path)?,
})
}
fn graph(&self, name: &NamedOrBlankNode) -> RocksDbGraph {
RocksDbGraph {
store: &self.store,
name: name.clone(),
}
}
fn default_graph(&self) -> RocksDbDefaultGraph {
RocksDbDefaultGraph { store: &self.store }
}
fn union_graph(&self) -> RocksDbUnionGraph {
RocksDbUnionGraph { store: &self.store }
}
pub fn iter(&self) -> Result<QuadsIterator<SPOGIndexIterator>> {
Ok(QuadsIterator {
iter: self.store.quads()?,
encoder: self.store.encoder(),
})
}
pub fn quads_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<QuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>> {
Ok(QuadsIterator {
iter: self
.store
.quads_for_subject(self.store.encoder().encode_named_or_blank_node(subject)?)?,
encoder: self.store.encoder(),
})
}
pub fn contains(&self, quad: &Quad) -> Result<bool> {
self.store
.contains(&self.store.encoder().encode_quad(quad)?)
}
pub fn insert(&self, quad: &Quad) -> Result<()> {
self.store.insert(&self.store.encoder().encode_quad(quad)?)
}
pub fn remove(&self, quad: &Quad) -> Result<()> {
self.store.remove(&self.store.encoder().encode_quad(quad)?)
}
}
struct RocksDbGraph<'a> {
store: &'a RocksDbStore,
name: NamedOrBlankNode, //TODO: better storage
}
struct RocksDbDefaultGraph<'a> {
store: &'a RocksDbStore,
}
struct RocksDbUnionGraph<'a> {
store: &'a RocksDbStore,
}
pub struct QuadsIterator<'a, I: Iterator<Item = Result<EncodedQuad>>> {
iter: I,
encoder: Encoder<RocksDbBytesStore<'a>>,
}
impl<'a, I: Iterator<Item = Result<EncodedQuad>>> Iterator for QuadsIterator<'a, I> {
type Item = Result<Quad>;
fn next(&mut self) -> Option<Result<Quad>> {
self.iter
.next()
.map(|k| k.and_then(|quad| self.encoder.decode_quad(&quad)))
Ok(Self::new_from_store(RocksDbStore::open(path)?))
}
}

@ -11,6 +11,7 @@ use std::path::Path;
use std::str;
use std::sync::Mutex;
use store::numeric_encoder::*;
use store::store::EncodedQuadsStore;
use utils::from_bytes;
use utils::from_bytes_slice;
use utils::to_bytes;
@ -60,101 +61,260 @@ impl RocksDbStore {
ospg_cf,
})
}
}
impl BytesStore for RocksDbStore {
type BytesOutput = DBVector;
pub fn encoder(&self) -> Encoder<RocksDbBytesStore> {
Encoder::new(RocksDbBytesStore(&self))
fn insert_bytes(&self, value: &[u8]) -> Result<u64> {
Ok(match self.db.get_cf(self.str2id_cf, value)? {
Some(id) => from_bytes_slice(&id),
None => {
let id = self
.str_id_counter
.lock()
.unwrap()
.get_and_increment(&self.db)? as u64;
let id_bytes = to_bytes(id);
let mut batch = WriteBatch::default();
batch.put_cf(self.id2str_cf, &id_bytes, value)?;
batch.put_cf(self.str2id_cf, value, &id_bytes)?;
self.db.write(batch)?;
id
}
})
}
fn get_bytes(&self, id: u64) -> Result<Option<DBVector>> {
Ok(self.db.get_cf(self.id2str_cf, &to_bytes(id))?)
}
}
pub fn quads(&self) -> Result<SPOGIndexIterator> {
impl EncodedQuadsStore for RocksDbStore {
type QuadsIterator = SPOGIndexIterator;
type QuadsForSubjectIterator = FilteringEncodedQuadsIterator<SPOGIndexIterator>;
type QuadsForSubjectPredicateIterator = FilteringEncodedQuadsIterator<SPOGIndexIterator>;
type QuadsForSubjectPredicateObjectIterator = FilteringEncodedQuadsIterator<SPOGIndexIterator>;
type QuadsForSubjectObjectIterator = FilteringEncodedQuadsIterator<OSPGIndexIterator>;
type QuadsForPredicateIterator = FilteringEncodedQuadsIterator<POSGIndexIterator>;
type QuadsForPredicateObjectIterator = FilteringEncodedQuadsIterator<POSGIndexIterator>;
type QuadsForObjectIterator = FilteringEncodedQuadsIterator<OSPGIndexIterator>;
type QuadsForGraphIterator = InGraphQuadsIterator<SPOGIndexIterator>;
type QuadsForSubjectGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>;
type QuadsForSubjectPredicateGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>;
type QuadsForSubjectObjectGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>;
type QuadsForPredicateGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>;
type QuadsForPredicateObjectGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>;
type QuadsForObjectGraphIterator =
InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>;
fn quads(&self) -> Result<SPOGIndexIterator> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek_to_first();
Ok(SPOGIndexIterator { iter })
}
pub fn quads_for_subject(
fn quads_for_subject(
&self,
subject: EncodedTerm,
subject: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<SPOGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek(&encode_term(&subject)?);
iter.seek(&encode_term(subject)?);
Ok(FilteringEncodedQuadsIterator {
iter: SPOGIndexIterator { iter },
filter: EncodedQuadPattern::new(Some(subject), None, None, None),
filter: EncodedQuadPattern::new(Some(subject.clone()), None, None, None),
})
}
pub fn quads_for_subject_predicate(
fn quads_for_subject_predicate(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<SPOGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek(&encode_term_pair(&subject, &predicate)?);
iter.seek(&encode_term_pair(subject, predicate)?);
Ok(FilteringEncodedQuadsIterator {
iter: SPOGIndexIterator { iter },
filter: EncodedQuadPattern::new(Some(subject), Some(predicate), None, None),
filter: EncodedQuadPattern::new(
Some(subject.clone()),
Some(predicate.clone()),
None,
None,
),
})
}
pub fn quads_for_subject_predicate_object(
fn quads_for_subject_predicate_object(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
object: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<SPOGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek(&encode_term_triple(&subject, &predicate, &object)?);
iter.seek(&encode_term_triple(subject, predicate, object)?);
Ok(FilteringEncodedQuadsIterator {
iter: SPOGIndexIterator { iter },
filter: EncodedQuadPattern::new(Some(subject), Some(predicate), Some(object), None),
filter: EncodedQuadPattern::new(
Some(subject.clone()),
Some(predicate.clone()),
Some(object.clone()),
None,
),
})
}
fn quads_for_subject_object(
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<OSPGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek(&encode_term_pair(object, subject)?);
Ok(FilteringEncodedQuadsIterator {
iter: OSPGIndexIterator { iter },
filter: EncodedQuadPattern::new(
Some(subject.clone()),
None,
Some(object.clone()),
None,
),
})
}
pub fn quads_for_predicate(
fn quads_for_predicate(
&self,
predicate: EncodedTerm,
predicate: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<POSGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.posg_cf)?;
iter.seek(&encode_term(&predicate)?);
iter.seek(&encode_term(predicate)?);
Ok(FilteringEncodedQuadsIterator {
iter: POSGIndexIterator { iter },
filter: EncodedQuadPattern::new(None, Some(predicate), None, None),
filter: EncodedQuadPattern::new(None, Some(predicate.clone()), None, None),
})
}
pub fn quads_for_predicate_object(
fn quads_for_predicate_object(
&self,
predicate: EncodedTerm,
object: EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<POSGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.spog_cf)?;
iter.seek(&encode_term_pair(&predicate, &object)?);
iter.seek(&encode_term_pair(predicate, object)?);
Ok(FilteringEncodedQuadsIterator {
iter: POSGIndexIterator { iter },
filter: EncodedQuadPattern::new(None, Some(predicate), Some(object), None),
filter: EncodedQuadPattern::new(
None,
Some(predicate.clone()),
Some(object.clone()),
None,
),
})
}
pub fn quads_for_object(
fn quads_for_object(
&self,
object: EncodedTerm,
object: &EncodedTerm,
) -> Result<FilteringEncodedQuadsIterator<OSPGIndexIterator>> {
let mut iter = self.db.raw_iterator_cf(self.ospg_cf)?;
iter.seek(&encode_term(&object)?);
Ok(FilteringEncodedQuadsIterator {
iter: OSPGIndexIterator { iter },
filter: EncodedQuadPattern::new(None, None, Some(object), None),
filter: EncodedQuadPattern::new(None, None, Some(object.clone()), None),
})
}
pub fn contains(&self, quad: &EncodedQuad) -> Result<bool> {
fn quads_for_graph(
&self,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<SPOGIndexIterator>> {
Ok(InGraphQuadsIterator {
iter: self.quads()?,
graph_name: graph_name.clone(),
})
}
fn quads_for_subject_graph(
&self,
subject: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject(subject)?,
graph_name: graph_name.clone(),
})
}
fn quads_for_subject_predicate_graph(
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject_predicate(subject, predicate)?,
graph_name: graph_name.clone(),
})
}
fn quads_for_subject_object_graph(
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject_object(subject, object)?,
graph_name: graph_name.clone(),
})
}
fn quads_for_predicate_graph(
&self,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_predicate(predicate)?,
graph_name: graph_name.clone(),
})
}
fn quads_for_predicate_object_graph(
&self,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_predicate_object(predicate, object)?,
graph_name: graph_name.clone(),
})
}
fn quads_for_object_graph(
&self,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_object(object)?,
graph_name: graph_name.clone(),
})
}
fn contains(&self, quad: &EncodedQuad) -> Result<bool> {
Ok(self
.db
.get_cf(self.spog_cf, &encode_spog_quad(quad)?)?
.is_some())
}
pub fn insert(&self, quad: &EncodedQuad) -> Result<()> {
fn insert(&self, quad: &EncodedQuad) -> Result<()> {
let mut batch = WriteBatch::default();
batch.put_cf(self.spog_cf, &encode_spog_quad(quad)?, &EMPTY_BUF)?;
batch.put_cf(self.posg_cf, &encode_posg_quad(quad)?, &EMPTY_BUF)?;
@ -162,7 +322,7 @@ impl RocksDbStore {
Ok(self.db.write(batch)?) //TODO: check what's going on if the key already exists
}
pub fn remove(&self, quad: &EncodedQuad) -> Result<()> {
fn remove(&self, quad: &EncodedQuad) -> Result<()> {
let mut batch = WriteBatch::default();
batch.delete_cf(self.spog_cf, &encode_spog_quad(quad)?)?;
batch.delete_cf(self.posg_cf, &encode_posg_quad(quad)?)?;
@ -176,36 +336,6 @@ pub fn get_cf(db: &DB, name: &str) -> Result<ColumnFamily> {
.ok_or_else(|| "column family not found".into())
}
pub struct RocksDbBytesStore<'a>(&'a RocksDbStore);
impl<'a> BytesStore for RocksDbBytesStore<'a> {
type BytesOutput = DBVector;
fn put(&self, value: &[u8]) -> Result<u64> {
Ok(match self.0.db.get_cf(self.0.str2id_cf, value)? {
Some(id) => from_bytes_slice(&id),
None => {
let id = self
.0
.str_id_counter
.lock()
.unwrap()
.get_and_increment(&self.0.db)? as u64;
let id_bytes = to_bytes(id);
let mut batch = WriteBatch::default();
batch.put_cf(self.0.id2str_cf, &id_bytes, value)?;
batch.put_cf(self.0.str2id_cf, value, &id_bytes)?;
self.0.db.write(batch)?;
id
}
})
}
fn get(&self, id: u64) -> Result<Option<DBVector>> {
Ok(self.0.db.get_cf(self.0.id2str_cf, &to_bytes(id))?)
}
}
struct RocksDBCounter {
name: &'static str,
}
@ -375,3 +505,20 @@ impl<I: Iterator<Item = Result<EncodedQuad>>> Iterator for FilteringEncodedQuads
})
}
}
pub struct InGraphQuadsIterator<I: Iterator<Item = Result<EncodedQuad>>> {
iter: I,
graph_name: Option<EncodedTerm>,
}
impl<I: Iterator<Item = Result<EncodedQuad>>> Iterator for InGraphQuadsIterator<I> {
type Item = Result<EncodedQuad>;
fn next(&mut self) -> Option<Result<EncodedQuad>> {
let graph_name = &self.graph_name;
self.iter.find(|quad| match quad {
Ok(quad) => graph_name == &quad.graph_name,
Err(_) => true,
})
}
}

@ -0,0 +1,701 @@
use errors::*;
use model::*;
use std::sync::Arc;
use store::numeric_encoder::BytesStore;
use store::numeric_encoder::EncodedQuad;
use store::numeric_encoder::EncodedTerm;
use store::numeric_encoder::Encoder;
use store::Dataset;
use store::Graph;
use store::NamedGraph;
/// Defines the Store traits that is used to have efficient binary storage
pub trait EncodedQuadsStore: BytesStore + Sized {
type QuadsIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectPredicateIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectPredicateObjectIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectObjectIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForPredicateIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForPredicateObjectIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForObjectIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectPredicateGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForSubjectObjectGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForPredicateGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForPredicateObjectGraphIterator: Iterator<Item = Result<EncodedQuad>>;
type QuadsForObjectGraphIterator: Iterator<Item = Result<EncodedQuad>>;
fn encoder(&self) -> Encoder<DelegatingBytesStore<Self>> {
Encoder::new(DelegatingBytesStore(&self))
}
fn quads(&self) -> Result<Self::QuadsIterator>;
fn quads_for_subject(&self, subject: &EncodedTerm) -> Result<Self::QuadsForSubjectIterator>;
fn quads_for_subject_predicate(
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
) -> Result<Self::QuadsForSubjectPredicateIterator>;
fn quads_for_subject_predicate_object(
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> Result<Self::QuadsForSubjectPredicateObjectIterator>;
fn quads_for_subject_object(
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
) -> Result<Self::QuadsForSubjectObjectIterator>;
fn quads_for_predicate(
&self,
predicate: &EncodedTerm,
) -> Result<Self::QuadsForPredicateIterator>;
fn quads_for_predicate_object(
&self,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> Result<Self::QuadsForPredicateObjectIterator>;
fn quads_for_object(&self, object: &EncodedTerm) -> Result<Self::QuadsForObjectIterator>;
fn quads_for_graph(
&self,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForGraphIterator>;
fn quads_for_subject_graph(
&self,
subject: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForSubjectGraphIterator>;
fn quads_for_subject_predicate_graph(
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForSubjectPredicateGraphIterator>;
fn quads_for_subject_object_graph(
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForSubjectObjectGraphIterator>;
fn quads_for_predicate_graph(
&self,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForPredicateGraphIterator>;
fn quads_for_predicate_object_graph(
&self,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForPredicateObjectGraphIterator>;
fn quads_for_object_graph(
&self,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
) -> Result<Self::QuadsForObjectGraphIterator>;
fn contains(&self, quad: &EncodedQuad) -> Result<bool>;
fn insert(&self, quad: &EncodedQuad) -> Result<()>;
fn remove(&self, quad: &EncodedQuad) -> Result<()>;
}
pub struct StoreDataset<S: EncodedQuadsStore> {
store: Arc<S>,
}
impl<S: EncodedQuadsStore> StoreDataset<S> {
pub fn new_from_store(store: S) -> Self {
Self {
store: Arc::new(store),
}
}
}
impl<S: EncodedQuadsStore> Dataset for StoreDataset<S> {
type NamedGraph = StoreNamedGraph<S>;
type DefaultGraph = StoreDefaultGraph<S>;
type UnionGraph = StoreUnionGraph<S>;
type QuadsIterator = QuadsIterator<S::QuadsIterator, S>;
type QuadsForSubjectIterator = QuadsIterator<S::QuadsForSubjectIterator, S>;
type QuadsForSubjectPredicateIterator = QuadsIterator<S::QuadsForSubjectPredicateIterator, S>;
type QuadsForSubjectPredicateObjectIterator =
QuadsIterator<S::QuadsForSubjectPredicateObjectIterator, S>;
type QuadsForSubjectObjectIterator = QuadsIterator<S::QuadsForSubjectObjectIterator, S>;
type QuadsForPredicateIterator = QuadsIterator<S::QuadsForPredicateIterator, S>;
type QuadsForPredicateObjectIterator = QuadsIterator<S::QuadsForPredicateObjectIterator, S>;
type QuadsForObjectIterator = QuadsIterator<S::QuadsForObjectIterator, S>;
fn named_graph(&self, name: &NamedOrBlankNode) -> Result<StoreNamedGraph<S>> {
Ok(StoreNamedGraph {
store: self.store.clone(),
name: name.clone(),
encoded_name: Some(self.store.encoder().encode_named_or_blank_node(name)?),
})
}
fn default_graph(&self) -> StoreDefaultGraph<S> {
StoreDefaultGraph {
store: self.store.clone(),
}
}
fn union_graph(&self) -> StoreUnionGraph<S> {
StoreUnionGraph {
store: self.store.clone(),
}
}
fn quads(&self) -> Result<QuadsIterator<S::QuadsIterator, S>> {
Ok(QuadsIterator {
iter: self.store.quads()?,
store: self.store.clone(),
})
}
fn quads_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<QuadsIterator<S::QuadsForSubjectIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self
.store
.quads_for_subject(&encoder.encode_named_or_blank_node(subject)?)?,
store: self.store.clone(),
})
}
fn quads_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<QuadsIterator<S::QuadsForSubjectPredicateIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self.store.quads_for_subject_predicate(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
)?,
store: self.store.clone(),
})
}
fn quads_for_subject_predicate_object(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
object: &Term,
) -> Result<QuadsIterator<S::QuadsForSubjectPredicateObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self.store.quads_for_subject_predicate_object(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
)?,
store: self.store.clone(),
})
}
fn quads_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<QuadsIterator<S::QuadsForSubjectObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self.store.quads_for_subject_object(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_term(object)?,
)?,
store: self.store.clone(),
})
}
fn quads_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<QuadsIterator<S::QuadsForPredicateIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self
.store
.quads_for_predicate(&encoder.encode_named_node(predicate)?)?,
store: self.store.clone(),
})
}
fn quads_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<QuadsIterator<S::QuadsForPredicateObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self.store.quads_for_predicate_object(
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
)?,
store: self.store.clone(),
})
}
fn quads_for_object(
&self,
object: &Term,
) -> Result<QuadsIterator<S::QuadsForObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(QuadsIterator {
iter: self.store.quads_for_object(&encoder.encode_term(object)?)?,
store: self.store.clone(),
})
}
fn contains(&self, quad: &Quad) -> Result<bool> {
self.store
.contains(&self.store.encoder().encode_quad(quad)?)
}
fn insert(&self, quad: &Quad) -> Result<()> {
self.store.insert(&self.store.encoder().encode_quad(quad)?)
}
fn remove(&self, quad: &Quad) -> Result<()> {
self.store.remove(&self.store.encoder().encode_quad(quad)?)
}
}
pub struct StoreNamedGraph<S: EncodedQuadsStore> {
store: Arc<S>,
name: NamedOrBlankNode,
encoded_name: Option<EncodedTerm>,
}
impl<S: EncodedQuadsStore> Graph for StoreNamedGraph<S> {
type TriplesIterator = TriplesIterator<S::QuadsForGraphIterator, S>;
type TriplesForSubjectIterator = TriplesIterator<S::QuadsForSubjectGraphIterator, S>;
type TriplesForSubjectPredicateIterator =
TriplesIterator<S::QuadsForSubjectPredicateGraphIterator, S>;
type TriplesForSubjectObjectIterator =
TriplesIterator<S::QuadsForSubjectObjectGraphIterator, S>;
type TriplesForPredicateIterator = TriplesIterator<S::QuadsForPredicateGraphIterator, S>;
type TriplesForPredicateObjectIterator =
TriplesIterator<S::QuadsForPredicateObjectGraphIterator, S>;
type TriplesForObjectIterator = TriplesIterator<S::QuadsForObjectGraphIterator, S>;
fn triples(&self) -> Result<TriplesIterator<S::QuadsForGraphIterator, S>> {
Ok(TriplesIterator {
iter: self.store.quads_for_graph(&self.encoded_name)?,
store: self.store.clone(),
})
}
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<TriplesIterator<S::QuadsForSubjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_graph(
&encoder.encode_named_or_blank_node(subject)?,
&self.encoded_name,
)?,
store: self.store.clone(),
})
}
fn triples_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForSubjectPredicateGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_predicate_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
&self.encoded_name,
)?,
store: self.store.clone(),
})
}
fn triples_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForSubjectObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_object_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_term(object)?,
&self.encoded_name,
)?,
store: self.store.clone(),
})
}
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForPredicateGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_predicate_graph(
&encoder.encode_named_node(predicate)?,
&self.encoded_name,
)?,
store: self.store.clone(),
})
}
fn triples_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForPredicateObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_predicate_object_graph(
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
&self.encoded_name,
)?,
store: self.store.clone(),
})
}
fn triples_for_object(
&self,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_object_graph(&encoder.encode_term(object)?, &self.encoded_name)?,
store: self.store.clone(),
})
}
fn contains(&self, triple: &Triple) -> Result<bool> {
self.store.contains(
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
)
}
fn insert(&self, triple: &Triple) -> Result<()> {
self.store.insert(
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
)
}
fn remove(&self, triple: &Triple) -> Result<()> {
self.store.remove(
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
)
}
}
impl<S: EncodedQuadsStore> NamedGraph for StoreNamedGraph<S> {
fn name(&self) -> &NamedOrBlankNode {
&self.name
}
}
pub struct StoreDefaultGraph<S: EncodedQuadsStore> {
store: Arc<S>,
}
impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
type TriplesIterator = TriplesIterator<S::QuadsForGraphIterator, S>;
type TriplesForSubjectIterator = TriplesIterator<S::QuadsForSubjectGraphIterator, S>;
type TriplesForSubjectPredicateIterator =
TriplesIterator<S::QuadsForSubjectPredicateGraphIterator, S>;
type TriplesForSubjectObjectIterator =
TriplesIterator<S::QuadsForSubjectObjectGraphIterator, S>;
type TriplesForPredicateIterator = TriplesIterator<S::QuadsForPredicateGraphIterator, S>;
type TriplesForPredicateObjectIterator =
TriplesIterator<S::QuadsForPredicateObjectGraphIterator, S>;
type TriplesForObjectIterator = TriplesIterator<S::QuadsForObjectGraphIterator, S>;
fn triples(&self) -> Result<TriplesIterator<S::QuadsForGraphIterator, S>> {
Ok(TriplesIterator {
iter: self.store.quads_for_graph(&None)?,
store: self.store.clone(),
})
}
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<TriplesIterator<S::QuadsForSubjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_subject_graph(&encoder.encode_named_or_blank_node(subject)?, &None)?,
store: self.store.clone(),
})
}
fn triples_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForSubjectPredicateGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_predicate_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
&None,
)?,
store: self.store.clone(),
})
}
fn triples_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForSubjectObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_object_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_term(object)?,
&None,
)?,
store: self.store.clone(),
})
}
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForPredicateGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_predicate_graph(&encoder.encode_named_node(predicate)?, &None)?,
store: self.store.clone(),
})
}
fn triples_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForPredicateObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_predicate_object_graph(
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
&None,
)?,
store: self.store.clone(),
})
}
fn triples_for_object(
&self,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForObjectGraphIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_object_graph(&encoder.encode_term(object)?, &None)?,
store: self.store.clone(),
})
}
fn contains(&self, triple: &Triple) -> Result<bool> {
self.store
.contains(&self.store.encoder().encode_triple_in_graph(triple, None)?)
}
fn insert(&self, triple: &Triple) -> Result<()> {
self.store
.insert(&self.store.encoder().encode_triple_in_graph(triple, None)?)
}
fn remove(&self, triple: &Triple) -> Result<()> {
self.store
.remove(&self.store.encoder().encode_triple_in_graph(triple, None)?)
}
}
pub struct StoreUnionGraph<S: EncodedQuadsStore> {
store: Arc<S>,
}
impl<S: EncodedQuadsStore> Graph for StoreUnionGraph<S> {
type TriplesIterator = TriplesIterator<S::QuadsIterator, S>;
type TriplesForSubjectIterator = TriplesIterator<S::QuadsForSubjectIterator, S>;
type TriplesForSubjectPredicateIterator =
TriplesIterator<S::QuadsForSubjectPredicateIterator, S>;
type TriplesForSubjectObjectIterator = TriplesIterator<S::QuadsForSubjectObjectIterator, S>;
type TriplesForPredicateIterator = TriplesIterator<S::QuadsForPredicateIterator, S>;
type TriplesForPredicateObjectIterator = TriplesIterator<S::QuadsForPredicateObjectIterator, S>;
type TriplesForObjectIterator = TriplesIterator<S::QuadsForObjectIterator, S>;
fn triples(&self) -> Result<TriplesIterator<S::QuadsIterator, S>> {
Ok(TriplesIterator {
iter: self.store.quads()?,
store: self.store.clone(),
})
}
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<TriplesIterator<S::QuadsForSubjectIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_subject(&encoder.encode_named_or_blank_node(subject)?)?,
store: self.store.clone(),
})
}
fn triples_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForSubjectPredicateIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_predicate(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
)?,
store: self.store.clone(),
})
}
fn triples_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForSubjectObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_subject_object(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_term(object)?,
)?,
store: self.store.clone(),
})
}
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<TriplesIterator<S::QuadsForPredicateIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self
.store
.quads_for_predicate(&encoder.encode_named_node(predicate)?)?,
store: self.store.clone(),
})
}
fn triples_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForPredicateObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_predicate_object(
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
)?,
store: self.store.clone(),
})
}
fn triples_for_object(
&self,
object: &Term,
) -> Result<TriplesIterator<S::QuadsForObjectIterator, S>> {
let encoder = self.store.encoder();
Ok(TriplesIterator {
iter: self.store.quads_for_object(&encoder.encode_term(object)?)?,
store: self.store.clone(),
})
}
fn contains(&self, triple: &Triple) -> Result<bool> {
let encoder = self.store.encoder();
Ok(self
.store
.quads_for_subject_predicate_object(
&encoder.encode_named_or_blank_node(triple.subject())?,
&encoder.encode_named_node(triple.predicate())?,
&encoder.encode_term(triple.object())?,
)?
.any(|_| true))
}
fn insert(&self, triple: &Triple) -> Result<()> {
unimplemented!()
}
fn remove(&self, triple: &Triple) -> Result<()> {
unimplemented!()
}
}
pub struct DelegatingBytesStore<'a, S: 'a + BytesStore + Sized>(&'a S);
impl<'a, S: BytesStore> BytesStore for DelegatingBytesStore<'a, S> {
type BytesOutput = S::BytesOutput;
fn insert_bytes(&self, value: &[u8]) -> Result<u64> {
self.0.insert_bytes(value)
}
fn get_bytes(&self, id: u64) -> Result<Option<S::BytesOutput>> {
self.0.get_bytes(id)
}
}
pub struct QuadsIterator<I: Iterator<Item = Result<EncodedQuad>>, S: EncodedQuadsStore> {
iter: I,
store: Arc<S>,
}
impl<I: Iterator<Item = Result<EncodedQuad>>, S: EncodedQuadsStore> Iterator
for QuadsIterator<I, S>
{
type Item = Result<Quad>;
fn next(&mut self) -> Option<Result<Quad>> {
self.iter
.next()
.map(|k| k.and_then(|quad| self.store.encoder().decode_quad(&quad)))
}
}
pub struct TriplesIterator<I: Iterator<Item = Result<EncodedQuad>>, S: EncodedQuadsStore> {
iter: I,
store: Arc<S>,
}
impl<I: Iterator<Item = Result<EncodedQuad>>, S: EncodedQuadsStore> Iterator
for TriplesIterator<I, S>
{
type Item = Result<Triple>;
fn next(&mut self) -> Option<Result<Triple>> {
self.iter
.next()
.map(|k| k.and_then(|quad| self.store.encoder().decode_triple(&quad)))
}
}
Loading…
Cancel
Save