Uses From for term encoding without insertion

Makes code simpler
pull/171/head
Tpt 4 years ago
parent 0b278a88a3
commit 0071f82662
  1. 23
      lib/src/sparql/dataset.rs
  2. 10
      lib/src/sparql/update.rs
  3. 2
      lib/src/storage/binary_encoder.rs
  4. 187
      lib/src/storage/numeric_encoder.rs
  5. 25
      lib/src/store.rs

@ -1,9 +1,6 @@
use crate::sparql::algebra::QueryDataset; use crate::sparql::algebra::QueryDataset;
use crate::sparql::EvaluationError; use crate::sparql::EvaluationError;
use crate::storage::numeric_encoder::{ use crate::storage::numeric_encoder::{EncodedQuad, EncodedTerm, StrContainer, StrHash, StrLookup};
get_encoded_graph_name, get_encoded_subject, EncodedQuad, EncodedTerm, StrContainer, StrHash,
StrLookup,
};
use crate::storage::Storage; use crate::storage::Storage;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
@ -19,18 +16,12 @@ pub(crate) struct DatasetView {
impl DatasetView { impl DatasetView {
pub fn new(storage: Storage, dataset: &QueryDataset) -> Result<Self, EvaluationError> { pub fn new(storage: Storage, dataset: &QueryDataset) -> Result<Self, EvaluationError> {
let dataset = EncodedDatasetSpec { let dataset = EncodedDatasetSpec {
default: dataset.default_graph_graphs().map(|graphs| { default: dataset
graphs .default_graph_graphs()
.iter() .map(|graphs| graphs.iter().map(|g| g.as_ref().into()).collect::<Vec<_>>()),
.map(|g| get_encoded_graph_name(g.as_ref())) named: dataset
.collect::<Vec<_>>() .available_named_graphs()
}), .map(|graphs| graphs.iter().map(|g| g.as_ref().into()).collect::<Vec<_>>()),
named: dataset.available_named_graphs().map(|graphs| {
graphs
.iter()
.map(|g| get_encoded_subject(g.as_ref()))
.collect::<Vec<_>>()
}),
}; };
Ok(Self { Ok(Self {
storage, storage,

@ -10,8 +10,7 @@ use crate::sparql::plan_builder::PlanBuilder;
use crate::sparql::{EvaluationError, UpdateOptions}; use crate::sparql::{EvaluationError, UpdateOptions};
use crate::storage::io::load_graph; use crate::storage::io::load_graph;
use crate::storage::numeric_encoder::{ use crate::storage::numeric_encoder::{
get_encoded_literal, get_encoded_named_node, EncodedQuad, EncodedTerm, EncodedTriple, EncodedQuad, EncodedTerm, EncodedTriple, StrLookup, WriteEncoder,
StrLookup, WriteEncoder,
}; };
use crate::storage::Storage; use crate::storage::Storage;
use http::header::{ACCEPT, CONTENT_TYPE, USER_AGENT}; use http::header::{ACCEPT, CONTENT_TYPE, USER_AGENT};
@ -645,11 +644,11 @@ impl<'a> SimpleUpdateEvaluator<'a> {
} }
fn encode_named_node_for_deletion(&self, term: &NamedNode) -> EncodedTerm { fn encode_named_node_for_deletion(&self, term: &NamedNode) -> EncodedTerm {
get_encoded_named_node(NamedNodeRef::new_unchecked(&term.iri)) NamedNodeRef::new_unchecked(&term.iri).into()
} }
fn encode_literal_for_deletion(&self, term: &Literal) -> EncodedTerm { fn encode_literal_for_deletion(&self, term: &Literal) -> EncodedTerm {
get_encoded_literal(match term { match term {
Literal::Simple { value } => LiteralRef::new_simple_literal(value), Literal::Simple { value } => LiteralRef::new_simple_literal(value),
Literal::LanguageTaggedString { value, language } => { Literal::LanguageTaggedString { value, language } => {
LiteralRef::new_language_tagged_literal_unchecked(value, language) LiteralRef::new_language_tagged_literal_unchecked(value, language)
@ -657,7 +656,8 @@ impl<'a> SimpleUpdateEvaluator<'a> {
Literal::Typed { value, datatype } => { Literal::Typed { value, datatype } => {
LiteralRef::new_typed_literal(value, NamedNodeRef::new_unchecked(&datatype.iri)) LiteralRef::new_typed_literal(value, NamedNodeRef::new_unchecked(&datatype.iri))
} }
}) }
.into()
} }
fn encode_triple_for_deletion(&self, triple: &GroundTriple) -> EncodedTerm { fn encode_triple_for_deletion(&self, triple: &GroundTriple) -> EncodedTerm {

@ -738,7 +738,7 @@ mod tests {
]; ];
for term in terms { for term in terms {
let encoded = store.encode_term(term.as_ref()).unwrap(); let encoded = store.encode_term(term.as_ref()).unwrap();
assert_eq!(encoded, get_encoded_term(term.as_ref())); assert_eq!(encoded, term.as_ref().into());
assert_eq!(term, store.decode_term(&encoded).unwrap()); assert_eq!(term, store.decode_term(&encoded).unwrap());
let mut buffer = Vec::new(); let mut buffer = Vec::new();

@ -481,80 +481,33 @@ impl From<EncodedTriple> for EncodedTerm {
} }
} }
#[derive(Eq, PartialEq, Debug, Clone, Hash)] impl From<NamedNodeRef<'_>> for EncodedTerm {
pub struct EncodedTriple { fn from(named_node: NamedNodeRef<'_>) -> Self {
pub subject: EncodedTerm, Self::NamedNode {
pub predicate: EncodedTerm,
pub object: EncodedTerm,
}
impl EncodedTriple {
pub fn new(subject: EncodedTerm, predicate: EncodedTerm, object: EncodedTerm) -> Self {
Self {
subject,
predicate,
object,
}
}
}
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct EncodedQuad {
pub subject: EncodedTerm,
pub predicate: EncodedTerm,
pub object: EncodedTerm,
pub graph_name: EncodedTerm,
}
impl EncodedQuad {
pub fn new(
subject: EncodedTerm,
predicate: EncodedTerm,
object: EncodedTerm,
graph_name: EncodedTerm,
) -> Self {
Self {
subject,
predicate,
object,
graph_name,
}
}
}
pub(crate) trait StrLookup {
type Error: Error + Into<EvaluationError> + 'static;
fn get_str(&self, key: &StrHash) -> Result<Option<String>, Self::Error>;
fn contains_str(&self, key: &StrHash) -> Result<bool, Self::Error>;
}
pub(crate) trait StrContainer: StrLookup {
fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, Self::Error>;
}
pub(crate) fn get_encoded_named_node(named_node: NamedNodeRef<'_>) -> EncodedTerm {
EncodedTerm::NamedNode {
iri_id: StrHash::new(named_node.as_str()), iri_id: StrHash::new(named_node.as_str()),
} }
}
} }
pub(crate) fn get_encoded_blank_node(blank_node: BlankNodeRef<'_>) -> EncodedTerm { impl From<BlankNodeRef<'_>> for EncodedTerm {
fn from(blank_node: BlankNodeRef<'_>) -> Self {
if let Some(id) = blank_node.id() { if let Some(id) = blank_node.id() {
EncodedTerm::NumericalBlankNode { id } Self::NumericalBlankNode { id }
} else { } else {
let id = blank_node.as_str(); let id = blank_node.as_str();
if let Ok(id) = id.try_into() { if let Ok(id) = id.try_into() {
EncodedTerm::SmallBlankNode(id) Self::SmallBlankNode(id)
} else { } else {
EncodedTerm::BigBlankNode { Self::BigBlankNode {
id_id: StrHash::new(id), id_id: StrHash::new(id),
} }
} }
} }
}
} }
pub(crate) fn get_encoded_literal(literal: LiteralRef<'_>) -> EncodedTerm { impl From<LiteralRef<'_>> for EncodedTerm {
fn from(literal: LiteralRef<'_>) -> Self {
let value = literal.value(); let value = literal.value();
let datatype = literal.datatype().as_str(); let datatype = literal.datatype().as_str();
let native_encoding = match datatype { let native_encoding = match datatype {
@ -624,7 +577,9 @@ pub(crate) fn get_encoded_literal(literal: LiteralRef<'_>) -> EncodedTerm {
"http://www.w3.org/2001/XMLSchema#yearMonthDuration" => { "http://www.w3.org/2001/XMLSchema#yearMonthDuration" => {
parse_year_month_duration_str(value) parse_year_month_duration_str(value)
} }
"http://www.w3.org/2001/XMLSchema#dayTimeDuration" => parse_day_time_duration_str(value), "http://www.w3.org/2001/XMLSchema#dayTimeDuration" => {
parse_day_time_duration_str(value)
}
_ => None, _ => None,
}; };
match native_encoding { match native_encoding {
@ -643,52 +598,118 @@ pub(crate) fn get_encoded_literal(literal: LiteralRef<'_>) -> EncodedTerm {
} }
} }
} }
}
} }
pub(crate) fn get_encoded_subject(term: SubjectRef<'_>) -> EncodedTerm { impl From<SubjectRef<'_>> for EncodedTerm {
fn from(term: SubjectRef<'_>) -> Self {
match term { match term {
SubjectRef::NamedNode(named_node) => get_encoded_named_node(named_node), SubjectRef::NamedNode(named_node) => named_node.into(),
SubjectRef::BlankNode(blank_node) => get_encoded_blank_node(blank_node), SubjectRef::BlankNode(blank_node) => blank_node.into(),
SubjectRef::Triple(triple) => { SubjectRef::Triple(triple) => triple.as_ref().into(),
EncodedTerm::Triple(Rc::new(get_encoded_triple(triple.as_ref())))
} }
} }
} }
pub(crate) fn get_encoded_term(term: TermRef<'_>) -> EncodedTerm { impl From<TermRef<'_>> for EncodedTerm {
fn from(term: TermRef<'_>) -> Self {
match term { match term {
TermRef::NamedNode(named_node) => get_encoded_named_node(named_node), TermRef::NamedNode(named_node) => named_node.into(),
TermRef::BlankNode(blank_node) => get_encoded_blank_node(blank_node), TermRef::BlankNode(blank_node) => blank_node.into(),
TermRef::Literal(literal) => get_encoded_literal(literal), TermRef::Literal(literal) => literal.into(),
TermRef::Triple(triple) => { TermRef::Triple(triple) => triple.as_ref().into(),
EncodedTerm::Triple(Rc::new(get_encoded_triple(triple.as_ref())))
} }
} }
} }
pub(crate) fn get_encoded_graph_name(name: GraphNameRef<'_>) -> EncodedTerm { impl From<GraphNameRef<'_>> for EncodedTerm {
fn from(name: GraphNameRef<'_>) -> Self {
match name { match name {
GraphNameRef::NamedNode(named_node) => get_encoded_named_node(named_node), GraphNameRef::NamedNode(named_node) => named_node.into(),
GraphNameRef::BlankNode(blank_node) => get_encoded_blank_node(blank_node), GraphNameRef::BlankNode(blank_node) => blank_node.into(),
GraphNameRef::DefaultGraph => EncodedTerm::DefaultGraph, GraphNameRef::DefaultGraph => EncodedTerm::DefaultGraph,
} }
}
}
impl From<TripleRef<'_>> for EncodedTerm {
fn from(triple: TripleRef<'_>) -> Self {
EncodedTerm::Triple(Rc::new(triple.into()))
}
}
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct EncodedTriple {
pub subject: EncodedTerm,
pub predicate: EncodedTerm,
pub object: EncodedTerm,
}
impl EncodedTriple {
pub fn new(subject: EncodedTerm, predicate: EncodedTerm, object: EncodedTerm) -> Self {
Self {
subject,
predicate,
object,
}
}
} }
pub(crate) fn get_encoded_triple(quad: TripleRef<'_>) -> EncodedTriple { impl From<TripleRef<'_>> for EncodedTriple {
fn from(triple: TripleRef<'_>) -> Self {
EncodedTriple { EncodedTriple {
subject: get_encoded_subject(quad.subject), subject: triple.subject.into(),
predicate: get_encoded_named_node(quad.predicate), predicate: triple.predicate.into(),
object: get_encoded_term(quad.object), object: triple.object.into(),
}
}
}
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct EncodedQuad {
pub subject: EncodedTerm,
pub predicate: EncodedTerm,
pub object: EncodedTerm,
pub graph_name: EncodedTerm,
}
impl EncodedQuad {
pub fn new(
subject: EncodedTerm,
predicate: EncodedTerm,
object: EncodedTerm,
graph_name: EncodedTerm,
) -> Self {
Self {
subject,
predicate,
object,
graph_name,
}
} }
} }
pub(crate) fn get_encoded_quad(quad: QuadRef<'_>) -> EncodedQuad { impl From<QuadRef<'_>> for EncodedQuad {
fn from(quad: QuadRef<'_>) -> Self {
EncodedQuad { EncodedQuad {
subject: get_encoded_subject(quad.subject), subject: quad.subject.into(),
predicate: get_encoded_named_node(quad.predicate), predicate: quad.predicate.into(),
object: get_encoded_term(quad.object), object: quad.object.into(),
graph_name: get_encoded_graph_name(quad.graph_name), graph_name: quad.graph_name.into(),
} }
}
}
pub(crate) trait StrLookup {
type Error: Error + Into<EvaluationError> + 'static;
fn get_str(&self, key: &StrHash) -> Result<Option<String>, Self::Error>;
fn contains_str(&self, key: &StrHash) -> Result<bool, Self::Error>;
}
pub(crate) trait StrContainer: StrLookup {
fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, Self::Error>;
} }
/// Encodes a term and insert strings if needed /// Encodes a term and insert strings if needed

@ -31,10 +31,7 @@ use crate::sparql::{
UpdateOptions, UpdateOptions,
}; };
use crate::storage::io::{dump_dataset, dump_graph, load_dataset, load_graph}; use crate::storage::io::{dump_dataset, dump_graph, load_dataset, load_graph};
use crate::storage::numeric_encoder::{ use crate::storage::numeric_encoder::{Decoder, EncodedQuad, EncodedTerm, WriteEncoder};
get_encoded_graph_name, get_encoded_named_node, get_encoded_quad, get_encoded_subject,
get_encoded_term, Decoder, WriteEncoder,
};
pub use crate::storage::ConflictableTransactionError; pub use crate::storage::ConflictableTransactionError;
pub use crate::storage::TransactionError; pub use crate::storage::TransactionError;
pub use crate::storage::UnabortableTransactionError; pub use crate::storage::UnabortableTransactionError;
@ -166,10 +163,10 @@ impl Store {
) -> QuadIter { ) -> QuadIter {
QuadIter { QuadIter {
iter: self.storage.quads_for_pattern( iter: self.storage.quads_for_pattern(
subject.map(get_encoded_subject).as_ref(), subject.map(EncodedTerm::from).as_ref(),
predicate.map(get_encoded_named_node).as_ref(), predicate.map(EncodedTerm::from).as_ref(),
object.map(get_encoded_term).as_ref(), object.map(EncodedTerm::from).as_ref(),
graph_name.map(get_encoded_graph_name).as_ref(), graph_name.map(EncodedTerm::from).as_ref(),
), ),
storage: self.storage.clone(), storage: self.storage.clone(),
} }
@ -182,7 +179,7 @@ impl Store {
/// Checks if this store contains a given quad /// Checks if this store contains a given quad
pub fn contains<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> { pub fn contains<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> {
let quad = get_encoded_quad(quad.into()); let quad = EncodedQuad::from(quad.into());
self.storage.contains(&quad) self.storage.contains(&quad)
} }
@ -375,7 +372,7 @@ impl Store {
/// It might leave the store in a bad state if a crash happens during the removal. /// It might leave the store in a bad state if a crash happens during the removal.
/// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that. /// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that.
pub fn remove<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> { pub fn remove<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> {
let quad = get_encoded_quad(quad.into()); let quad = EncodedQuad::from(quad.into());
self.storage.remove(&quad) self.storage.remove(&quad)
} }
@ -469,7 +466,7 @@ impl Store {
&self, &self,
graph_name: impl Into<SubjectRef<'a>>, graph_name: impl Into<SubjectRef<'a>>,
) -> Result<bool, io::Error> { ) -> Result<bool, io::Error> {
let graph_name = get_encoded_subject(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.contains_named_graph(&graph_name) self.storage.contains_named_graph(&graph_name)
} }
@ -518,7 +515,7 @@ impl Store {
&self, &self,
graph_name: impl Into<GraphNameRef<'a>>, graph_name: impl Into<GraphNameRef<'a>>,
) -> Result<(), io::Error> { ) -> Result<(), io::Error> {
let graph_name = get_encoded_graph_name(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.clear_graph(&graph_name) self.storage.clear_graph(&graph_name)
} }
@ -546,7 +543,7 @@ impl Store {
&self, &self,
graph_name: impl Into<SubjectRef<'a>>, graph_name: impl Into<SubjectRef<'a>>,
) -> Result<bool, io::Error> { ) -> Result<bool, io::Error> {
let graph_name = get_encoded_subject(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.remove_named_graph(&graph_name) self.storage.remove_named_graph(&graph_name)
} }
@ -700,7 +697,7 @@ impl Transaction<'_> {
&self, &self,
quad: impl Into<QuadRef<'a>>, quad: impl Into<QuadRef<'a>>,
) -> Result<bool, UnabortableTransactionError> { ) -> Result<bool, UnabortableTransactionError> {
let quad = get_encoded_quad(quad.into()); let quad = EncodedQuad::from(quad.into());
self.storage.remove(&quad) self.storage.remove(&quad)
} }

Loading…
Cancel
Save