Adds a special EncodedTerm for the default graph

pull/10/head
Tpt 6 years ago
parent 439719732c
commit b01054a747
  1. 94
      src/store/numeric_encoder.rs
  2. 20
      src/store/rocksdb/storage.rs
  3. 87
      src/store/store.rs

@ -16,14 +16,17 @@ pub trait BytesStore {
fn get_bytes(&self, id: u64) -> Result<Option<Self::BytesOutput>>;
}
const TYPE_NOTHING_ID: u8 = 0;
const TYPE_DEFAULT_GRAPH_ID: u8 = 0;
const TYPE_NAMED_NODE_ID: u8 = 1;
const TYPE_BLANK_NODE_ID: u8 = 2;
const TYPE_LANG_STRING_LITERAL_ID: u8 = 3;
const TYPE_TYPED_LITERAL_ID: u8 = 4;
pub static ENCODED_DEFAULT_GRAPH: EncodedTerm = EncodedTerm::DefaultGraph {};
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum EncodedTerm {
DefaultGraph {},
NamedNode { iri_id: u64 },
BlankNode(Uuid),
LangStringLiteral { value_id: u64, language_id: u64 },
@ -33,6 +36,7 @@ pub enum EncodedTerm {
impl EncodedTerm {
fn type_id(&self) -> u8 {
match self {
EncodedTerm::DefaultGraph { .. } => TYPE_DEFAULT_GRAPH_ID,
EncodedTerm::NamedNode { .. } => TYPE_NAMED_NODE_ID,
EncodedTerm::BlankNode(_) => TYPE_BLANK_NODE_ID,
EncodedTerm::LangStringLiteral { .. } => TYPE_LANG_STRING_LITERAL_ID,
@ -46,12 +50,11 @@ pub struct EncodedQuad {
pub subject: EncodedTerm,
pub predicate: EncodedTerm,
pub object: EncodedTerm,
pub graph_name: Option<EncodedTerm>,
pub graph_name: EncodedTerm,
}
pub trait TermReader {
fn read_term(&mut self) -> Result<EncodedTerm>;
fn read_optional_term(&mut self) -> Result<Option<EncodedTerm>>;
fn read_spog_quad(&mut self) -> Result<EncodedQuad>;
fn read_posg_quad(&mut self) -> Result<EncodedQuad>;
fn read_ospg_quad(&mut self) -> Result<EncodedQuad>;
@ -59,16 +62,25 @@ pub trait TermReader {
impl<R: Read> TermReader for R {
fn read_term(&mut self) -> Result<EncodedTerm> {
let type_id = self.read_u8()?;
read_term_after_type(self, type_id)
}
fn read_optional_term(&mut self) -> Result<Option<EncodedTerm>> {
let type_id = self.read_u8()?;
if type_id == 0 {
Ok(None)
} else {
Ok(Some(read_term_after_type(self, type_id)?))
match self.read_u8()? {
TYPE_DEFAULT_GRAPH_ID => Ok(EncodedTerm::DefaultGraph {}),
TYPE_NAMED_NODE_ID => Ok(EncodedTerm::NamedNode {
iri_id: self.read_u64::<NetworkEndian>()?,
}),
TYPE_BLANK_NODE_ID => {
let mut uuid_buffer = [0 as u8; 16];
self.read_exact(&mut uuid_buffer)?;
Ok(EncodedTerm::BlankNode(Uuid::from_bytes(&uuid_buffer)?))
}
TYPE_LANG_STRING_LITERAL_ID => Ok(EncodedTerm::LangStringLiteral {
language_id: self.read_u64::<NetworkEndian>()?,
value_id: self.read_u64::<NetworkEndian>()?,
}),
TYPE_TYPED_LITERAL_ID => Ok(EncodedTerm::TypedLiteral {
datatype_id: self.read_u64::<NetworkEndian>()?,
value_id: self.read_u64::<NetworkEndian>()?,
}),
_ => Err("the term buffer has an invalid type id".into()),
}
}
@ -76,7 +88,7 @@ impl<R: Read> TermReader for R {
let subject = self.read_term()?;
let predicate = self.read_term()?;
let object = self.read_term()?;
let graph_name = self.read_optional_term()?;
let graph_name = self.read_term()?;
Ok(EncodedQuad {
subject,
predicate,
@ -89,7 +101,7 @@ impl<R: Read> TermReader for R {
let predicate = self.read_term()?;
let object = self.read_term()?;
let subject = self.read_term()?;
let graph_name = self.read_optional_term()?;
let graph_name = self.read_term()?;
Ok(EncodedQuad {
subject,
predicate,
@ -102,7 +114,7 @@ impl<R: Read> TermReader for R {
let object = self.read_term()?;
let subject = self.read_term()?;
let predicate = self.read_term()?;
let graph_name = self.read_optional_term()?;
let graph_name = self.read_term()?;
Ok(EncodedQuad {
subject,
predicate,
@ -112,31 +124,8 @@ impl<R: Read> TermReader for R {
}
}
fn read_term_after_type(reader: &mut impl Read, type_id: u8) -> Result<EncodedTerm> {
match type_id {
TYPE_NAMED_NODE_ID => Ok(EncodedTerm::NamedNode {
iri_id: reader.read_u64::<NetworkEndian>()?,
}),
TYPE_BLANK_NODE_ID => {
let mut uuid_buffer = [0 as u8; 16];
reader.read_exact(&mut uuid_buffer)?;
Ok(EncodedTerm::BlankNode(Uuid::from_bytes(&uuid_buffer)?))
}
TYPE_LANG_STRING_LITERAL_ID => Ok(EncodedTerm::LangStringLiteral {
language_id: reader.read_u64::<NetworkEndian>()?,
value_id: reader.read_u64::<NetworkEndian>()?,
}),
TYPE_TYPED_LITERAL_ID => Ok(EncodedTerm::TypedLiteral {
datatype_id: reader.read_u64::<NetworkEndian>()?,
value_id: reader.read_u64::<NetworkEndian>()?,
}),
_ => Err("the term buffer has an invalid type id".into()),
}
}
pub trait TermWriter {
fn write_term(&mut self, term: &EncodedTerm) -> Result<()>;
fn write_optional_term(&mut self, term: &Option<EncodedTerm>) -> Result<()>;
fn write_spog_quad(&mut self, quad: &EncodedQuad) -> Result<()>;
fn write_posg_quad(&mut self, quad: &EncodedQuad) -> Result<()>;
fn write_ospg_quad(&mut self, quad: &EncodedQuad) -> Result<()>;
@ -146,6 +135,7 @@ impl<R: Write> TermWriter for R {
fn write_term(&mut self, term: &EncodedTerm) -> Result<()> {
self.write_u8(term.type_id())?;
match term {
EncodedTerm::DefaultGraph {} => {}
EncodedTerm::NamedNode { iri_id } => self.write_u64::<NetworkEndian>(*iri_id)?,
EncodedTerm::BlankNode(id) => self.write_all(id.as_bytes())?,
EncodedTerm::LangStringLiteral {
@ -166,18 +156,11 @@ impl<R: Write> TermWriter for R {
Ok(())
}
fn write_optional_term(&mut self, term: &Option<EncodedTerm>) -> Result<()> {
match term {
Some(term) => self.write_term(term),
None => Ok(self.write_u8(TYPE_NOTHING_ID)?),
}
}
fn write_spog_quad(&mut self, quad: &EncodedQuad) -> Result<()> {
self.write_term(&quad.subject)?;
self.write_term(&quad.predicate)?;
self.write_term(&quad.object)?;
self.write_optional_term(&quad.graph_name)?;
self.write_term(&quad.graph_name)?;
Ok(())
}
@ -185,7 +168,7 @@ impl<R: Write> TermWriter for R {
self.write_term(&quad.predicate)?;
self.write_term(&quad.object)?;
self.write_term(&quad.subject)?;
self.write_optional_term(&quad.graph_name)?;
self.write_term(&quad.graph_name)?;
Ok(())
}
@ -193,7 +176,7 @@ impl<R: Write> TermWriter for R {
self.write_term(&quad.object)?;
self.write_term(&quad.subject)?;
self.write_term(&quad.predicate)?;
self.write_optional_term(&quad.graph_name)?;
self.write_term(&quad.graph_name)?;
Ok(())
}
}
@ -252,8 +235,8 @@ impl<S: BytesStore> Encoder<S> {
predicate: self.encode_named_node(quad.predicate())?,
object: self.encode_term(quad.object())?,
graph_name: match quad.graph_name() {
Some(graph_name) => Some(self.encode_named_or_blank_node(&graph_name)?),
None => None,
Some(graph_name) => self.encode_named_or_blank_node(&graph_name)?,
None => ENCODED_DEFAULT_GRAPH.clone(),
},
})
}
@ -261,18 +244,19 @@ impl<S: BytesStore> Encoder<S> {
pub fn encode_triple_in_graph(
&self,
triple: &Triple,
graph_name: Option<EncodedTerm>,
graph_name: &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,
graph_name: graph_name.clone(),
})
}
pub fn decode_term(&self, encoded: &EncodedTerm) -> Result<Term> {
match encoded {
EncodedTerm::DefaultGraph {} => Err("The default graph tag is not a valid term".into()),
EncodedTerm::NamedNode { iri_id } => {
Ok(NamedNode::from(self.decode_url_value(*iri_id)?).into())
}
@ -324,8 +308,8 @@ impl<S: BytesStore> Encoder<S> {
self.decode_named_node(&encoded.predicate)?,
self.decode_term(&encoded.object)?,
match encoded.graph_name {
Some(ref graph_name) => Some(self.decode_named_or_blank_node(&graph_name)?),
None => None,
EncodedTerm::DefaultGraph {} => None,
ref graph_name => Some(self.decode_named_or_blank_node(graph_name)?),
},
))
}

@ -230,7 +230,7 @@ impl EncodedQuadsStore for RocksDbStore {
fn quads_for_graph(
&self,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<SPOGIndexIterator>> {
Ok(InGraphQuadsIterator {
iter: self.quads()?,
@ -241,7 +241,7 @@ impl EncodedQuadsStore for RocksDbStore {
fn quads_for_subject_graph(
&self,
subject: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject(subject)?,
@ -253,7 +253,7 @@ impl EncodedQuadsStore for RocksDbStore {
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<SPOGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject_predicate(subject, predicate)?,
@ -265,7 +265,7 @@ impl EncodedQuadsStore for RocksDbStore {
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_subject_object(subject, object)?,
@ -276,7 +276,7 @@ impl EncodedQuadsStore for RocksDbStore {
fn quads_for_predicate_graph(
&self,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_predicate(predicate)?,
@ -288,7 +288,7 @@ impl EncodedQuadsStore for RocksDbStore {
&self,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<POSGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_predicate_object(predicate, object)?,
@ -299,7 +299,7 @@ impl EncodedQuadsStore for RocksDbStore {
fn quads_for_object_graph(
&self,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<InGraphQuadsIterator<FilteringEncodedQuadsIterator<OSPGIndexIterator>>> {
Ok(InGraphQuadsIterator {
iter: self.quads_for_object(object)?,
@ -363,7 +363,7 @@ struct EncodedQuadPattern {
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<Option<EncodedTerm>>,
graph_name: Option<EncodedTerm>,
}
impl EncodedQuadPattern {
@ -371,7 +371,7 @@ impl EncodedQuadPattern {
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<Option<EncodedTerm>>,
graph_name: Option<EncodedTerm>,
) -> Self {
Self {
subject,
@ -508,7 +508,7 @@ impl<I: Iterator<Item = Result<EncodedQuad>>> Iterator for FilteringEncodedQuads
pub struct InGraphQuadsIterator<I: Iterator<Item = Result<EncodedQuad>>> {
iter: I,
graph_name: Option<EncodedTerm>,
graph_name: EncodedTerm,
}
impl<I: Iterator<Item = Result<EncodedQuad>>> Iterator for InGraphQuadsIterator<I> {

@ -1,10 +1,7 @@
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::numeric_encoder::*;
use store::Dataset;
use store::Graph;
use store::NamedGraph;
@ -60,42 +57,39 @@ pub trait EncodedQuadsStore: BytesStore + Sized {
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_graph(&self, graph_name: &EncodedTerm) -> Result<Self::QuadsForGraphIterator>;
fn quads_for_subject_graph(
&self,
subject: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForSubjectGraphIterator>;
fn quads_for_subject_predicate_graph(
&self,
subject: &EncodedTerm,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForSubjectPredicateGraphIterator>;
fn quads_for_subject_object_graph(
&self,
subject: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForSubjectObjectGraphIterator>;
fn quads_for_predicate_graph(
&self,
predicate: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForPredicateGraphIterator>;
fn quads_for_predicate_object_graph(
&self,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForPredicateObjectGraphIterator>;
fn quads_for_object_graph(
&self,
object: &EncodedTerm,
graph_name: &Option<EncodedTerm>,
graph_name: &EncodedTerm,
) -> Result<Self::QuadsForObjectGraphIterator>;
fn contains(&self, quad: &EncodedQuad) -> Result<bool>;
fn insert(&self, quad: &EncodedQuad) -> Result<()>;
@ -132,7 +126,7 @@ impl<S: EncodedQuadsStore> Dataset for StoreDataset<S> {
Ok(StoreNamedGraph {
store: self.store.clone(),
name: name.clone(),
encoded_name: Some(self.store.encoder().encode_named_or_blank_node(name)?),
encoded_name: self.store.encoder().encode_named_or_blank_node(name)?,
})
}
@ -279,7 +273,7 @@ impl<S: EncodedQuadsStore> Dataset for StoreDataset<S> {
pub struct StoreNamedGraph<S: EncodedQuadsStore> {
store: Arc<S>,
name: NamedOrBlankNode,
encoded_name: Option<EncodedTerm>,
encoded_name: EncodedTerm,
}
impl<S: EncodedQuadsStore> Graph for StoreNamedGraph<S> {
@ -390,7 +384,7 @@ impl<S: EncodedQuadsStore> Graph for StoreNamedGraph<S> {
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
.encode_triple_in_graph(triple, &self.encoded_name)?,
)
}
@ -399,7 +393,7 @@ impl<S: EncodedQuadsStore> Graph for StoreNamedGraph<S> {
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
.encode_triple_in_graph(triple, &self.encoded_name)?,
)
}
@ -408,7 +402,7 @@ impl<S: EncodedQuadsStore> Graph for StoreNamedGraph<S> {
&self
.store
.encoder()
.encode_triple_in_graph(triple, self.encoded_name.clone())?,
.encode_triple_in_graph(triple, &self.encoded_name)?,
)
}
@ -448,7 +442,7 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
fn triples(&self) -> Result<TriplesIterator<S::QuadsForGraphIterator, S>> {
Ok(TriplesIterator {
iter: self.store.quads_for_graph(&None)?,
iter: self.store.quads_for_graph(&ENCODED_DEFAULT_GRAPH)?,
store: self.store.clone(),
})
}
@ -459,9 +453,10 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
) -> 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)?,
iter: self.store.quads_for_subject_graph(
&encoder.encode_named_or_blank_node(subject)?,
&ENCODED_DEFAULT_GRAPH,
)?,
store: self.store.clone(),
})
}
@ -475,7 +470,7 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
iter: self.store.quads_for_subject_predicate_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_named_node(predicate)?,
&None,
&ENCODED_DEFAULT_GRAPH,
)?,
store: self.store.clone(),
})
@ -490,7 +485,7 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
iter: self.store.quads_for_subject_object_graph(
&encoder.encode_named_or_blank_node(subject)?,
&encoder.encode_term(object)?,
&None,
&ENCODED_DEFAULT_GRAPH,
)?,
store: self.store.clone(),
})
@ -501,9 +496,10 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
) -> 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)?,
iter: self.store.quads_for_predicate_graph(
&encoder.encode_named_node(predicate)?,
&ENCODED_DEFAULT_GRAPH,
)?,
store: self.store.clone(),
})
}
@ -517,7 +513,7 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
iter: self.store.quads_for_predicate_object_graph(
&encoder.encode_named_node(predicate)?,
&encoder.encode_term(object)?,
&None,
&ENCODED_DEFAULT_GRAPH,
)?,
store: self.store.clone(),
})
@ -530,32 +526,47 @@ impl<S: EncodedQuadsStore> Graph for StoreDefaultGraph<S> {
Ok(TriplesIterator {
iter: self
.store
.quads_for_object_graph(&encoder.encode_term(object)?, &None)?,
.quads_for_object_graph(&encoder.encode_term(object)?, &ENCODED_DEFAULT_GRAPH)?,
store: self.store.clone(),
})
}
fn contains(&self, triple: &Triple) -> Result<bool> {
self.store
.contains(&self.store.encoder().encode_triple_in_graph(triple, None)?)
self.store.contains(
&self
.store
.encoder()
.encode_triple_in_graph(triple, &ENCODED_DEFAULT_GRAPH)?,
)
}
fn insert(&self, triple: &Triple) -> Result<()> {
self.store
.insert(&self.store.encoder().encode_triple_in_graph(triple, None)?)
self.store.insert(
&self
.store
.encoder()
.encode_triple_in_graph(triple, &ENCODED_DEFAULT_GRAPH)?,
)
}
fn remove(&self, triple: &Triple) -> Result<()> {
self.store
.remove(&self.store.encoder().encode_triple_in_graph(triple, None)?)
self.store.remove(
&self
.store
.encoder()
.encode_triple_in_graph(triple, &ENCODED_DEFAULT_GRAPH)?,
)
}
fn len(&self) -> Result<usize> {
Ok(self.store.quads_for_graph(&None)?.count())
Ok(self.store.quads_for_graph(&ENCODED_DEFAULT_GRAPH)?.count())
}
fn is_empty(&self) -> Result<bool> {
Ok(self.store.quads_for_graph(&None)?.any(|_| true))
Ok(self
.store
.quads_for_graph(&ENCODED_DEFAULT_GRAPH)?
.any(|_| true))
}
}

Loading…
Cancel
Save