Makes encoded term not copy

pull/171/head
Tpt 4 years ago
parent e6e83ff369
commit e2bbc29198
  1. 36
      lib/src/sparql/dataset.rs
  2. 608
      lib/src/sparql/eval.rs
  3. 14
      lib/src/sparql/plan.rs
  4. 32
      lib/src/sparql/plan_builder.rs
  5. 30
      lib/src/sparql/update.rs
  6. 98
      lib/src/storage/binary_encoder.rs
  7. 2
      lib/src/storage/io.rs
  8. 112
      lib/src/storage/mod.rs
  9. 90
      lib/src/storage/numeric_encoder.rs
  10. 20
      lib/src/store.rs

@ -41,10 +41,10 @@ impl DatasetView {
fn store_encoded_quads_for_pattern( fn store_encoded_quads_for_pattern(
&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>,
) -> impl Iterator<Item = Result<EncodedQuad, EvaluationError>> + 'static { ) -> impl Iterator<Item = Result<EncodedQuad, EvaluationError>> + 'static {
self.storage self.storage
.quads_for_pattern(subject, predicate, object, graph_name) .quads_for_pattern(subject, predicate, object, graph_name)
@ -54,10 +54,10 @@ impl DatasetView {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
pub fn encoded_quads_for_pattern( pub fn encoded_quads_for_pattern(
&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, EvaluationError>>> { ) -> Box<dyn Iterator<Item = Result<EncodedQuad, EvaluationError>>> {
if let Some(graph_name) = graph_name { if let Some(graph_name) = graph_name {
if graph_name.is_default_graph() { if graph_name.is_default_graph() {
@ -69,7 +69,7 @@ impl DatasetView {
subject, subject,
predicate, predicate,
object, object,
Some(default_graph_graphs[0]), Some(&default_graph_graphs[0]),
) )
.map(|quad| { .map(|quad| {
let quad = quad?; let quad = quad?;
@ -89,7 +89,7 @@ impl DatasetView {
subject, subject,
predicate, predicate,
object, object,
Some(*graph_name), Some(graph_name),
) )
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -110,7 +110,7 @@ impl DatasetView {
.dataset .dataset
.named .named
.as_ref() .as_ref()
.map_or(true, |d| d.contains(&graph_name)) .map_or(true, |d| d.contains(graph_name))
{ {
Box::new(self.store_encoded_quads_for_pattern( Box::new(self.store_encoded_quads_for_pattern(
subject, subject,
@ -129,7 +129,7 @@ impl DatasetView {
subject, subject,
predicate, predicate,
object, object,
Some(*graph_name), Some(graph_name),
) )
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -149,25 +149,25 @@ impl DatasetView {
impl StrLookup for DatasetView { impl StrLookup for DatasetView {
type Error = EvaluationError; type Error = EvaluationError;
fn get_str(&self, key: StrHash) -> Result<Option<String>, EvaluationError> { fn get_str(&self, key: &StrHash) -> Result<Option<String>, EvaluationError> {
Ok(if let Some(value) = self.extra.borrow().get(&key) { Ok(if let Some(value) = self.extra.borrow().get(key) {
Some(value.clone()) Some(value.clone())
} else { } else {
self.storage.get_str(key)? self.storage.get_str(key)?
}) })
} }
fn contains_str(&self, key: StrHash) -> Result<bool, EvaluationError> { fn contains_str(&self, key: &StrHash) -> Result<bool, EvaluationError> {
Ok(self.extra.borrow().contains_key(&key) || self.storage.contains_str(key)?) Ok(self.extra.borrow().contains_key(key) || self.storage.contains_str(key)?)
} }
} }
impl StrContainer for DatasetView { impl StrContainer for DatasetView {
fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, EvaluationError> { fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, EvaluationError> {
if self.storage.contains_str(key)? { if self.storage.contains_str(key)? {
Ok(false) Ok(false)
} else { } else {
match self.extra.borrow_mut().entry(key) { match self.extra.borrow_mut().entry(*key) {
Entry::Occupied(_) => Ok(false), Entry::Occupied(_) => Ok(false),
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
entry.insert(value.to_owned()); entry.insert(value.to_owned());

File diff suppressed because it is too large Load Diff

@ -193,7 +193,7 @@ impl PlanNode {
} }
} }
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum PatternValue { pub enum PatternValue {
Constant(EncodedTerm), Constant(EncodedTerm),
Variable(usize), Variable(usize),
@ -460,14 +460,14 @@ pub enum Comparator {
Desc(PlanExpression), Desc(PlanExpression),
} }
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct TripleTemplate { pub struct TripleTemplate {
pub subject: TripleTemplateValue, pub subject: TripleTemplateValue,
pub predicate: TripleTemplateValue, pub predicate: TripleTemplateValue,
pub object: TripleTemplateValue, pub object: TripleTemplateValue,
} }
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum TripleTemplateValue { pub enum TripleTemplateValue {
Constant(EncodedTerm), Constant(EncodedTerm),
BlankNode(usize), BlankNode(usize),
@ -494,8 +494,8 @@ impl EncodedTuple {
self.inner.get(index).map_or(false, Option::is_some) self.inner.get(index).map_or(false, Option::is_some)
} }
pub fn get(&self, index: usize) -> Option<EncodedTerm> { pub fn get(&self, index: usize) -> Option<&EncodedTerm> {
self.inner.get(index).cloned().unwrap_or(None) self.inner.get(index).unwrap_or(&None).as_ref()
} }
pub fn iter(&self) -> impl Iterator<Item = Option<EncodedTerm>> + '_ { pub fn iter(&self) -> impl Iterator<Item = Option<EncodedTerm>> + '_ {
@ -526,7 +526,7 @@ impl EncodedTuple {
return None; return None;
} }
} }
None => result[key] = Some(*self_value), None => result[key] = Some(self_value.clone()),
} }
} }
} }
@ -541,7 +541,7 @@ impl EncodedTuple {
return None; return None;
} }
} }
None => result[key] = Some(*other_value), None => result[key] = Some(other_value.clone()),
} }
} }
} }

@ -23,7 +23,7 @@ impl<'a> PlanBuilder<'a> {
let plan = PlanBuilder { dataset }.build_for_graph_pattern( let plan = PlanBuilder { dataset }.build_for_graph_pattern(
pattern, pattern,
&mut variables, &mut variables,
PatternValue::Constant(EncodedTerm::DefaultGraph), &PatternValue::Constant(EncodedTerm::DefaultGraph),
)?; )?;
Ok((plan, variables)) Ok((plan, variables))
} }
@ -40,7 +40,7 @@ impl<'a> PlanBuilder<'a> {
&mut self, &mut self,
pattern: &GraphPattern, pattern: &GraphPattern,
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
) -> Result<PlanNode, EvaluationError> { ) -> Result<PlanNode, EvaluationError> {
Ok(match pattern { Ok(match pattern {
GraphPattern::Bgp(p) => self.build_for_bgp(p, variables, graph_name)?, GraphPattern::Bgp(p) => self.build_for_bgp(p, variables, graph_name)?,
@ -53,7 +53,7 @@ impl<'a> PlanBuilder<'a> {
subject: self.pattern_value_from_term_or_variable(subject, variables)?, subject: self.pattern_value_from_term_or_variable(subject, variables)?,
path: Rc::new(self.build_for_path(path)?), path: Rc::new(self.build_for_path(path)?),
object: self.pattern_value_from_term_or_variable(object, variables)?, object: self.pattern_value_from_term_or_variable(object, variables)?,
graph_name, graph_name: graph_name.clone(),
}, },
GraphPattern::Join { left, right } => { GraphPattern::Join { left, right } => {
//TODO: improve //TODO: improve
@ -69,7 +69,7 @@ impl<'a> PlanBuilder<'a> {
subject: self.pattern_value_from_term_or_variable(subject, variables)?, subject: self.pattern_value_from_term_or_variable(subject, variables)?,
path: Rc::new(self.build_for_path(path)?), path: Rc::new(self.build_for_path(path)?),
object: self.pattern_value_from_term_or_variable(object, variables)?, object: self.pattern_value_from_term_or_variable(object, variables)?,
graph_name, graph_name: graph_name.clone(),
} }
} else { } else {
PlanNode::Join { PlanNode::Join {
@ -128,7 +128,7 @@ impl<'a> PlanBuilder<'a> {
GraphPattern::Graph { graph_name, inner } => { GraphPattern::Graph { graph_name, inner } => {
let graph_name = let graph_name =
self.pattern_value_from_named_node_or_variable(graph_name, variables)?; self.pattern_value_from_named_node_or_variable(graph_name, variables)?;
self.build_for_graph_pattern(inner, variables, graph_name)? self.build_for_graph_pattern(inner, variables, &graph_name)?
} }
GraphPattern::Extend { inner, var, expr } => PlanNode::Extend { GraphPattern::Extend { inner, var, expr } => PlanNode::Extend {
child: Rc::new(self.build_for_graph_pattern(inner, variables, graph_name)?), child: Rc::new(self.build_for_graph_pattern(inner, variables, graph_name)?),
@ -174,7 +174,7 @@ impl<'a> PlanBuilder<'a> {
child: Rc::new(self.build_for_graph_pattern( child: Rc::new(self.build_for_graph_pattern(
inner, inner,
&mut inner_variables, &mut inner_variables,
inner_graph_name, &inner_graph_name,
)?), )?),
key_mapping: Rc::new( key_mapping: Rc::new(
by.iter() by.iter()
@ -230,7 +230,7 @@ impl<'a> PlanBuilder<'a> {
child: Rc::new(self.build_for_graph_pattern( child: Rc::new(self.build_for_graph_pattern(
inner, inner,
&mut inner_variables, &mut inner_variables,
inner_graph_name, &inner_graph_name,
)?), )?),
mapping: Rc::new( mapping: Rc::new(
projection projection
@ -276,7 +276,7 @@ impl<'a> PlanBuilder<'a> {
&mut self, &mut self,
p: &[TriplePattern], p: &[TriplePattern],
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
) -> Result<PlanNode, EvaluationError> { ) -> Result<PlanNode, EvaluationError> {
let mut plan = PlanNode::Init; let mut plan = PlanNode::Init;
for pattern in sort_bgp(p) { for pattern in sort_bgp(p) {
@ -286,7 +286,7 @@ impl<'a> PlanBuilder<'a> {
predicate: self predicate: self
.pattern_value_from_named_node_or_variable(&pattern.predicate, variables)?, .pattern_value_from_named_node_or_variable(&pattern.predicate, variables)?,
object: self.pattern_value_from_term_or_variable(&pattern.object, variables)?, object: self.pattern_value_from_term_or_variable(&pattern.object, variables)?,
graph_name, graph_name: graph_name.clone(),
} }
} }
Ok(plan) Ok(plan)
@ -334,7 +334,7 @@ impl<'a> PlanBuilder<'a> {
&mut self, &mut self,
expression: &Expression, expression: &Expression,
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
) -> Result<PlanExpression, EvaluationError> { ) -> Result<PlanExpression, EvaluationError> {
Ok(match expression { Ok(match expression {
Expression::NamedNode(node) => PlanExpression::Constant(self.build_named_node(node)?), Expression::NamedNode(node) => PlanExpression::Constant(self.build_named_node(node)?),
@ -738,7 +738,7 @@ impl<'a> PlanBuilder<'a> {
parameters: &[Expression], parameters: &[Expression],
constructor: impl Fn(Box<PlanExpression>) -> PlanExpression, constructor: impl Fn(Box<PlanExpression>) -> PlanExpression,
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
name: &'static str, name: &'static str,
) -> Result<PlanExpression, EvaluationError> { ) -> Result<PlanExpression, EvaluationError> {
if parameters.len() == 1 { if parameters.len() == 1 {
@ -759,7 +759,7 @@ impl<'a> PlanBuilder<'a> {
&mut self, &mut self,
l: &[Expression], l: &[Expression],
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
) -> Result<Vec<PlanExpression>, EvaluationError> { ) -> Result<Vec<PlanExpression>, EvaluationError> {
l.iter() l.iter()
.map(|e| self.build_for_expression(e, variables, graph_name)) .map(|e| self.build_for_expression(e, variables, graph_name))
@ -839,7 +839,7 @@ impl<'a> PlanBuilder<'a> {
&mut self, &mut self,
aggregate: &AggregationFunction, aggregate: &AggregationFunction,
variables: &mut Vec<Variable>, variables: &mut Vec<Variable>,
graph_name: PatternValue, graph_name: &PatternValue,
) -> Result<PlanAggregation, EvaluationError> { ) -> Result<PlanAggregation, EvaluationError> {
match aggregate { match aggregate {
AggregationFunction::Count { expr, distinct } => Ok(PlanAggregation { AggregationFunction::Count { expr, distinct } => Ok(PlanAggregation {
@ -958,14 +958,14 @@ impl<'a> PlanBuilder<'a> {
fn convert_pattern_value_id( fn convert_pattern_value_id(
&self, &self,
from_value: PatternValue, from_value: &PatternValue,
from: &[Variable], from: &[Variable],
to: &mut Vec<Variable>, to: &mut Vec<Variable>,
) -> PatternValue { ) -> PatternValue {
match from_value { match from_value {
PatternValue::Constant(v) => PatternValue::Constant(v), PatternValue::Constant(v) => PatternValue::Constant(v.clone()),
PatternValue::Variable(from_id) => { PatternValue::Variable(from_id) => {
PatternValue::Variable(self.convert_variable_id(from_id, from, to)) PatternValue::Variable(self.convert_variable_id(*from_id, from, to))
} }
} }
} }

@ -214,7 +214,7 @@ impl<'a> SimpleUpdateEvaluator<'a> {
fn eval_create(&mut self, graph: &NamedNode, silent: bool) -> Result<(), EvaluationError> { fn eval_create(&mut self, graph: &NamedNode, silent: bool) -> Result<(), EvaluationError> {
let encoded_graph_name = self.encode_named_node_for_insertion(graph)?; let encoded_graph_name = self.encode_named_node_for_insertion(graph)?;
if self.storage.contains_named_graph(encoded_graph_name)? { if self.storage.contains_named_graph(&encoded_graph_name)? {
if silent { if silent {
Ok(()) Ok(())
} else { } else {
@ -224,7 +224,7 @@ impl<'a> SimpleUpdateEvaluator<'a> {
))) )))
} }
} else { } else {
self.storage.insert_named_graph(encoded_graph_name)?; self.storage.insert_named_graph(&encoded_graph_name)?;
Ok(()) Ok(())
} }
} }
@ -233,8 +233,8 @@ impl<'a> SimpleUpdateEvaluator<'a> {
match graph { match graph {
GraphTarget::NamedNode(graph_name) => { GraphTarget::NamedNode(graph_name) => {
let graph_name = self.encode_named_node_for_deletion(graph_name); let graph_name = self.encode_named_node_for_deletion(graph_name);
if self.storage.contains_named_graph(graph_name)? { if self.storage.contains_named_graph(&graph_name)? {
Ok(self.storage.clear_graph(graph_name)?) Ok(self.storage.clear_graph(&graph_name)?)
} else if silent { } else if silent {
Ok(()) Ok(())
} else { } else {
@ -244,20 +244,22 @@ impl<'a> SimpleUpdateEvaluator<'a> {
))) )))
} }
} }
GraphTarget::DefaultGraph => Ok(self.storage.clear_graph(EncodedTerm::DefaultGraph)?), GraphTarget::DefaultGraph => {
Ok(self.storage.clear_graph(&EncodedTerm::DefaultGraph)?)
}
GraphTarget::NamedGraphs => { GraphTarget::NamedGraphs => {
// TODO: optimize? // TODO: optimize?
for graph in self.storage.named_graphs() { for graph in self.storage.named_graphs() {
self.storage.clear_graph(graph?)?; self.storage.clear_graph(&graph?)?;
} }
Ok(()) Ok(())
} }
GraphTarget::AllGraphs => { GraphTarget::AllGraphs => {
// TODO: optimize? // TODO: optimize?
for graph in self.storage.named_graphs() { for graph in self.storage.named_graphs() {
self.storage.clear_graph(graph?)?; self.storage.clear_graph(&graph?)?;
} }
Ok(self.storage.clear_graph(EncodedTerm::DefaultGraph)?) Ok(self.storage.clear_graph(&EncodedTerm::DefaultGraph)?)
} }
} }
} }
@ -266,8 +268,8 @@ impl<'a> SimpleUpdateEvaluator<'a> {
match graph { match graph {
GraphTarget::NamedNode(graph_name) => { GraphTarget::NamedNode(graph_name) => {
let graph_name = self.encode_named_node_for_deletion(graph_name); let graph_name = self.encode_named_node_for_deletion(graph_name);
if self.storage.contains_named_graph(graph_name)? { if self.storage.contains_named_graph(&graph_name)? {
self.storage.remove_named_graph(graph_name)?; self.storage.remove_named_graph(&graph_name)?;
Ok(()) Ok(())
} else if silent { } else if silent {
Ok(()) Ok(())
@ -278,11 +280,13 @@ impl<'a> SimpleUpdateEvaluator<'a> {
))) )))
} }
} }
GraphTarget::DefaultGraph => Ok(self.storage.clear_graph(EncodedTerm::DefaultGraph)?), GraphTarget::DefaultGraph => {
Ok(self.storage.clear_graph(&EncodedTerm::DefaultGraph)?)
}
GraphTarget::NamedGraphs => { GraphTarget::NamedGraphs => {
// TODO: optimize? // TODO: optimize?
for graph in self.storage.named_graphs() { for graph in self.storage.named_graphs() {
self.storage.remove_named_graph(graph?)?; self.storage.remove_named_graph(&graph?)?;
} }
Ok(()) Ok(())
} }
@ -436,7 +440,7 @@ impl<'a> SimpleUpdateEvaluator<'a> {
.position(|v2| v == v2) .position(|v2| v == v2)
.and_then(|i| values.get(i)) .and_then(|i| values.get(i))
{ {
Some(*term) Some(term.clone())
} else { } else {
None None
} }

@ -402,79 +402,79 @@ impl<R: Read> TermReader for R {
} }
pub fn write_spog_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_spog_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
} }
pub fn write_posg_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_posg_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
} }
pub fn write_ospg_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_ospg_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
} }
pub fn write_gspo_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_gspo_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
} }
pub fn write_gpos_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_gpos_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
} }
pub fn write_gosp_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_gosp_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.graph_name); write_term(sink, &quad.graph_name);
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
} }
pub fn write_spo_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_spo_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
} }
pub fn write_pos_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_pos_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
} }
pub fn write_osp_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) { pub fn write_osp_quad(sink: &mut Vec<u8>, quad: &EncodedQuad) {
write_term(sink, quad.object); write_term(sink, &quad.object);
write_term(sink, quad.subject); write_term(sink, &quad.subject);
write_term(sink, quad.predicate); write_term(sink, &quad.predicate);
} }
pub fn encode_term(t: EncodedTerm) -> Vec<u8> { pub fn encode_term(t: &EncodedTerm) -> Vec<u8> {
let mut vec = Vec::with_capacity(WRITTEN_TERM_MAX_SIZE); let mut vec = Vec::with_capacity(WRITTEN_TERM_MAX_SIZE);
write_term(&mut vec, t); write_term(&mut vec, t);
vec vec
} }
pub fn encode_term_pair(t1: EncodedTerm, t2: EncodedTerm) -> Vec<u8> { pub fn encode_term_pair(t1: &EncodedTerm, t2: &EncodedTerm) -> Vec<u8> {
let mut vec = Vec::with_capacity(2 * WRITTEN_TERM_MAX_SIZE); let mut vec = Vec::with_capacity(2 * WRITTEN_TERM_MAX_SIZE);
write_term(&mut vec, t1); write_term(&mut vec, t1);
write_term(&mut vec, t2); write_term(&mut vec, t2);
vec vec
} }
pub fn encode_term_triple(t1: EncodedTerm, t2: EncodedTerm, t3: EncodedTerm) -> Vec<u8> { pub fn encode_term_triple(t1: &EncodedTerm, t2: &EncodedTerm, t3: &EncodedTerm) -> Vec<u8> {
let mut vec = Vec::with_capacity(3 * WRITTEN_TERM_MAX_SIZE); let mut vec = Vec::with_capacity(3 * WRITTEN_TERM_MAX_SIZE);
write_term(&mut vec, t1); write_term(&mut vec, t1);
write_term(&mut vec, t2); write_term(&mut vec, t2);
@ -483,10 +483,10 @@ pub fn encode_term_triple(t1: EncodedTerm, t2: EncodedTerm, t3: EncodedTerm) ->
} }
pub fn encode_term_quad( pub fn encode_term_quad(
t1: EncodedTerm, t1: &EncodedTerm,
t2: EncodedTerm, t2: &EncodedTerm,
t3: EncodedTerm, t3: &EncodedTerm,
t4: EncodedTerm, t4: &EncodedTerm,
) -> Vec<u8> { ) -> Vec<u8> {
let mut vec = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE); let mut vec = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE);
write_term(&mut vec, t1); write_term(&mut vec, t1);
@ -496,7 +496,7 @@ pub fn encode_term_quad(
vec vec
} }
pub fn write_term(sink: &mut Vec<u8>, term: EncodedTerm) { pub fn write_term(sink: &mut Vec<u8>, term: &EncodedTerm) {
match term { match term {
EncodedTerm::DefaultGraph => (), EncodedTerm::DefaultGraph => (),
EncodedTerm::NamedNode { iri_id } => { EncodedTerm::NamedNode { iri_id } => {
@ -641,18 +641,18 @@ mod tests {
impl StrLookup for MemoryStrStore { impl StrLookup for MemoryStrStore {
type Error = Infallible; type Error = Infallible;
fn get_str(&self, key: StrHash) -> Result<Option<String>, Infallible> { fn get_str(&self, key: &StrHash) -> Result<Option<String>, Infallible> {
Ok(self.id2str.borrow().get(&key).cloned()) Ok(self.id2str.borrow().get(key).cloned())
} }
fn contains_str(&self, key: StrHash) -> Result<bool, Infallible> { fn contains_str(&self, key: &StrHash) -> Result<bool, Infallible> {
Ok(self.id2str.borrow().contains_key(&key)) Ok(self.id2str.borrow().contains_key(key))
} }
} }
impl StrContainer for MemoryStrStore { impl StrContainer for MemoryStrStore {
fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, Infallible> { fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, Infallible> {
match self.id2str.borrow_mut().entry(key) { match self.id2str.borrow_mut().entry(*key) {
Entry::Occupied(_) => Ok(false), Entry::Occupied(_) => Ok(false),
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
entry.insert(value.to_owned()); entry.insert(value.to_owned());
@ -719,10 +719,10 @@ 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, get_encoded_term(term.as_ref()));
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();
write_term(&mut buffer, encoded); write_term(&mut buffer, &encoded);
assert_eq!(encoded, Cursor::new(&buffer).read_term().unwrap()); assert_eq!(encoded, Cursor::new(&buffer).read_term().unwrap());
} }
} }

@ -52,7 +52,7 @@ where
.map_err(StoreOrParseError::Store)?; .map_err(StoreOrParseError::Store)?;
parser.parse_all(&mut move |t| { parser.parse_all(&mut move |t| {
let quad = storage let quad = storage
.encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map) .encode_rio_triple_in_graph(t, to_graph_name.clone(), &mut bnode_map)
.map_err(StoreOrParseError::Store)?; .map_err(StoreOrParseError::Store)?;
storage.insert(&quad).map_err(StoreOrParseError::Store)?; storage.insert(&quad).map_err(StoreOrParseError::Store)?;
Ok(()) Ok(())

@ -74,7 +74,7 @@ impl Storage {
for quad in this.quads() { for quad in this.quads() {
let quad = quad?; let quad = quad?;
if !quad.graph_name.is_default_graph() { if !quad.graph_name.is_default_graph() {
this.insert_named_graph(quad.graph_name)?; this.insert_named_graph(&quad.graph_name)?;
} }
} }
version = 1; version = 1;
@ -168,10 +168,10 @@ impl Storage {
pub fn quads_for_pattern( pub fn quads_for_pattern(
&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>,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
match subject { match subject {
Some(subject) => match predicate { Some(subject) => match predicate {
@ -236,7 +236,7 @@ impl Storage {
) )
} }
fn quads_for_subject(&self, subject: EncodedTerm) -> ChainedDecodingQuadIterator { fn quads_for_subject(&self, subject: &EncodedTerm) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dspo_quads(encode_term(subject)), self.dspo_quads(encode_term(subject)),
self.spog_quads(encode_term(subject)), self.spog_quads(encode_term(subject)),
@ -245,8 +245,8 @@ impl Storage {
fn quads_for_subject_predicate( fn quads_for_subject_predicate(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
predicate: EncodedTerm, predicate: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dspo_quads(encode_term_pair(subject, predicate)), self.dspo_quads(encode_term_pair(subject, predicate)),
@ -256,9 +256,9 @@ impl Storage {
fn quads_for_subject_predicate_object( fn quads_for_subject_predicate_object(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
predicate: EncodedTerm, predicate: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dspo_quads(encode_term_triple(subject, predicate, object)), self.dspo_quads(encode_term_triple(subject, predicate, object)),
@ -268,8 +268,8 @@ impl Storage {
fn quads_for_subject_object( fn quads_for_subject_object(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dosp_quads(encode_term_pair(object, subject)), self.dosp_quads(encode_term_pair(object, subject)),
@ -277,7 +277,7 @@ impl Storage {
) )
} }
fn quads_for_predicate(&self, predicate: EncodedTerm) -> ChainedDecodingQuadIterator { fn quads_for_predicate(&self, predicate: &EncodedTerm) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dpos_quads(encode_term(predicate)), self.dpos_quads(encode_term(predicate)),
self.posg_quads(encode_term(predicate)), self.posg_quads(encode_term(predicate)),
@ -286,8 +286,8 @@ impl Storage {
fn quads_for_predicate_object( fn quads_for_predicate_object(
&self, &self,
predicate: EncodedTerm, predicate: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dpos_quads(encode_term_pair(predicate, object)), self.dpos_quads(encode_term_pair(predicate, object)),
@ -295,14 +295,14 @@ impl Storage {
) )
} }
fn quads_for_object(&self, object: EncodedTerm) -> ChainedDecodingQuadIterator { fn quads_for_object(&self, object: &EncodedTerm) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair( ChainedDecodingQuadIterator::pair(
self.dosp_quads(encode_term(object)), self.dosp_quads(encode_term(object)),
self.ospg_quads(encode_term(object)), self.ospg_quads(encode_term(object)),
) )
} }
fn quads_for_graph(&self, graph_name: EncodedTerm) -> ChainedDecodingQuadIterator { fn quads_for_graph(&self, graph_name: &EncodedTerm) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(Vec::default()) self.dspo_quads(Vec::default())
} else { } else {
@ -312,8 +312,8 @@ impl Storage {
fn quads_for_subject_graph( fn quads_for_subject_graph(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term(subject)) self.dspo_quads(encode_term(subject))
@ -324,9 +324,9 @@ impl Storage {
fn quads_for_subject_predicate_graph( fn quads_for_subject_predicate_graph(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
predicate: EncodedTerm, predicate: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term_pair(subject, predicate)) self.dspo_quads(encode_term_pair(subject, predicate))
@ -337,10 +337,10 @@ impl Storage {
fn quads_for_subject_predicate_object_graph( fn quads_for_subject_predicate_object_graph(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
predicate: EncodedTerm, predicate: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term_triple(subject, predicate, object)) self.dspo_quads(encode_term_triple(subject, predicate, object))
@ -351,9 +351,9 @@ impl Storage {
fn quads_for_subject_object_graph( fn quads_for_subject_object_graph(
&self, &self,
subject: EncodedTerm, subject: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dosp_quads(encode_term_pair(object, subject)) self.dosp_quads(encode_term_pair(object, subject))
@ -364,8 +364,8 @@ impl Storage {
fn quads_for_predicate_graph( fn quads_for_predicate_graph(
&self, &self,
predicate: EncodedTerm, predicate: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dpos_quads(encode_term(predicate)) self.dpos_quads(encode_term(predicate))
@ -376,9 +376,9 @@ impl Storage {
fn quads_for_predicate_object_graph( fn quads_for_predicate_object_graph(
&self, &self,
predicate: EncodedTerm, predicate: &EncodedTerm,
object: EncodedTerm, object: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dpos_quads(encode_term_pair(predicate, object)) self.dpos_quads(encode_term_pair(predicate, object))
@ -389,8 +389,8 @@ impl Storage {
fn quads_for_object_graph( fn quads_for_object_graph(
&self, &self,
object: EncodedTerm, object: &EncodedTerm,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator { ) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() { ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dosp_quads(encode_term(object)) self.dosp_quads(encode_term(object))
@ -405,7 +405,7 @@ impl Storage {
} }
} }
pub fn contains_named_graph(&self, graph_name: EncodedTerm) -> Result<bool, std::io::Error> { pub fn contains_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> {
Ok(self.graphs.contains_key(&encode_term(graph_name))?) Ok(self.graphs.contains_key(&encode_term(graph_name))?)
} }
@ -503,7 +503,7 @@ impl Storage {
self.gosp.insert(buffer.as_slice(), &[])?; self.gosp.insert(buffer.as_slice(), &[])?;
buffer.clear(); buffer.clear();
write_term(&mut buffer, quad.graph_name); write_term(&mut buffer, &quad.graph_name);
self.graphs.insert(&buffer, &[])?; self.graphs.insert(&buffer, &[])?;
buffer.clear(); buffer.clear();
} }
@ -564,11 +564,11 @@ impl Storage {
} }
} }
pub fn insert_named_graph(&self, graph_name: EncodedTerm) -> Result<bool, std::io::Error> { pub fn insert_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> {
Ok(self.graphs.insert(&encode_term(graph_name), &[])?.is_none()) Ok(self.graphs.insert(&encode_term(graph_name), &[])?.is_none())
} }
pub fn clear_graph(&self, graph_name: EncodedTerm) -> Result<(), std::io::Error> { pub fn clear_graph(&self, graph_name: &EncodedTerm) -> Result<(), std::io::Error> {
if graph_name.is_default_graph() { if graph_name.is_default_graph() {
self.dspo.clear()?; self.dspo.clear()?;
self.dpos.clear()?; self.dpos.clear()?;
@ -581,7 +581,7 @@ impl Storage {
Ok(()) Ok(())
} }
pub fn remove_named_graph(&self, graph_name: EncodedTerm) -> Result<bool, std::io::Error> { pub fn remove_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> {
for quad in self.quads_for_graph(graph_name) { for quad in self.quads_for_graph(graph_name) {
self.remove(&quad?)?; self.remove(&quad?)?;
} }
@ -603,7 +603,7 @@ impl Storage {
Ok(()) Ok(())
} }
pub fn get_str(&self, key: StrHash) -> Result<Option<String>, std::io::Error> { pub fn get_str(&self, key: &StrHash) -> Result<Option<String>, std::io::Error> {
self.id2str self.id2str
.get(key.to_be_bytes())? .get(key.to_be_bytes())?
.map(|v| String::from_utf8(v.to_vec())) .map(|v| String::from_utf8(v.to_vec()))
@ -611,11 +611,11 @@ impl Storage {
.map_err(invalid_data_error) .map_err(invalid_data_error)
} }
pub fn contains_str(&self, key: StrHash) -> Result<bool, std::io::Error> { pub fn contains_str(&self, key: &StrHash) -> Result<bool, std::io::Error> {
Ok(self.id2str.contains_key(key.to_be_bytes())?) Ok(self.id2str.contains_key(key.to_be_bytes())?)
} }
pub fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, std::io::Error> { pub fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, std::io::Error> {
Ok(self.id2str.insert(key.to_be_bytes(), value)?.is_none()) Ok(self.id2str.insert(key.to_be_bytes(), value)?.is_none())
} }
} }
@ -748,7 +748,7 @@ impl<'a> StorageTransaction<'a> {
self.gosp.insert(buffer.as_slice(), &[])?; self.gosp.insert(buffer.as_slice(), &[])?;
buffer.clear(); buffer.clear();
write_term(&mut buffer, quad.graph_name); write_term(&mut buffer, &quad.graph_name);
self.graphs.insert(buffer.as_slice(), &[])?; self.graphs.insert(buffer.as_slice(), &[])?;
buffer.clear(); buffer.clear();
} }
@ -811,12 +811,12 @@ impl<'a> StorageTransaction<'a> {
pub fn insert_named_graph( pub fn insert_named_graph(
&self, &self,
graph_name: EncodedTerm, graph_name: &EncodedTerm,
) -> Result<bool, UnabortableTransactionError> { ) -> Result<bool, UnabortableTransactionError> {
Ok(self.graphs.insert(encode_term(graph_name), &[])?.is_none()) Ok(self.graphs.insert(encode_term(graph_name), &[])?.is_none())
} }
pub fn get_str(&self, key: StrHash) -> Result<Option<String>, UnabortableTransactionError> { pub fn get_str(&self, key: &StrHash) -> Result<Option<String>, UnabortableTransactionError> {
self.id2str self.id2str
.get(key.to_be_bytes())? .get(key.to_be_bytes())?
.map(|v| String::from_utf8(v.to_vec())) .map(|v| String::from_utf8(v.to_vec()))
@ -824,13 +824,13 @@ impl<'a> StorageTransaction<'a> {
.map_err(|e| UnabortableTransactionError::Storage(invalid_data_error(e))) .map_err(|e| UnabortableTransactionError::Storage(invalid_data_error(e)))
} }
pub fn contains_str(&self, key: StrHash) -> Result<bool, UnabortableTransactionError> { pub fn contains_str(&self, key: &StrHash) -> Result<bool, UnabortableTransactionError> {
Ok(self.id2str.get(key.to_be_bytes())?.is_some()) Ok(self.id2str.get(key.to_be_bytes())?.is_some())
} }
pub fn insert_str( pub fn insert_str(
&self, &self,
key: StrHash, key: &StrHash,
value: &str, value: &str,
) -> Result<bool, UnabortableTransactionError> { ) -> Result<bool, UnabortableTransactionError> {
Ok(self.id2str.insert(&key.to_be_bytes(), value)?.is_none()) Ok(self.id2str.insert(&key.to_be_bytes(), value)?.is_none())
@ -992,17 +992,17 @@ impl<T> From<ConflictableTransactionError<T>> for Sled2ConflictableTransactionEr
impl StrLookup for Storage { impl StrLookup for Storage {
type Error = std::io::Error; type Error = std::io::Error;
fn get_str(&self, key: StrHash) -> Result<Option<String>, std::io::Error> { fn get_str(&self, key: &StrHash) -> Result<Option<String>, std::io::Error> {
self.get_str(key) self.get_str(key)
} }
fn contains_str(&self, key: StrHash) -> Result<bool, std::io::Error> { fn contains_str(&self, key: &StrHash) -> Result<bool, std::io::Error> {
self.contains_str(key) self.contains_str(key)
} }
} }
impl StrContainer for Storage { impl StrContainer for Storage {
fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, std::io::Error> { fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, std::io::Error> {
self.insert_str(key, value) self.insert_str(key, value)
} }
} }
@ -1010,17 +1010,17 @@ impl StrContainer for Storage {
impl<'a> StrLookup for StorageTransaction<'a> { impl<'a> StrLookup for StorageTransaction<'a> {
type Error = UnabortableTransactionError; type Error = UnabortableTransactionError;
fn get_str(&self, key: StrHash) -> Result<Option<String>, UnabortableTransactionError> { fn get_str(&self, key: &StrHash) -> Result<Option<String>, UnabortableTransactionError> {
self.get_str(key) self.get_str(key)
} }
fn contains_str(&self, key: StrHash) -> Result<bool, UnabortableTransactionError> { fn contains_str(&self, key: &StrHash) -> Result<bool, UnabortableTransactionError> {
self.contains_str(key) self.contains_str(key)
} }
} }
impl<'a> StrContainer for StorageTransaction<'a> { impl<'a> StrContainer for StorageTransaction<'a> {
fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, UnabortableTransactionError> { fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, UnabortableTransactionError> {
self.insert_str(key, value) self.insert_str(key, value)
} }
} }

@ -16,7 +16,7 @@ use std::hash::Hash;
use std::hash::Hasher; use std::hash::Hasher;
use std::{fmt, io, str}; use std::{fmt, io, str};
#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
#[repr(transparent)] #[repr(transparent)]
pub struct StrHash { pub struct StrHash {
hash: u128, hash: u128,
@ -44,7 +44,7 @@ impl StrHash {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone)]
pub enum EncodedTerm { pub enum EncodedTerm {
DefaultGraph, DefaultGraph,
NamedNode { NamedNode {
@ -337,8 +337,8 @@ impl EncodedTerm {
} }
pub fn on_each_id<E>( pub fn on_each_id<E>(
self, &self,
mut callback: impl FnMut(StrHash) -> Result<(), E>, mut callback: impl FnMut(&StrHash) -> Result<(), E>,
) -> Result<(), E> { ) -> Result<(), E> {
match self { match self {
Self::NamedNode { iri_id } => { Self::NamedNode { iri_id } => {
@ -462,7 +462,7 @@ impl From<DayTimeDuration> for EncodedTerm {
} }
} }
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct EncodedQuad { pub struct EncodedQuad {
pub subject: EncodedTerm, pub subject: EncodedTerm,
pub predicate: EncodedTerm, pub predicate: EncodedTerm,
@ -489,13 +489,13 @@ impl EncodedQuad {
pub(crate) trait StrLookup { pub(crate) trait StrLookup {
type Error: Error + Into<EvaluationError> + 'static; type Error: Error + Into<EvaluationError> + 'static;
fn get_str(&self, key: StrHash) -> Result<Option<String>, Self::Error>; fn get_str(&self, key: &StrHash) -> Result<Option<String>, Self::Error>;
fn contains_str(&self, key: StrHash) -> Result<bool, Self::Error>; fn contains_str(&self, key: &StrHash) -> Result<bool, Self::Error>;
} }
pub(crate) trait StrContainer: StrLookup { pub(crate) trait StrContainer: StrLookup {
fn insert_str(&self, key: StrHash, value: &str) -> Result<bool, Self::Error>; fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, Self::Error>;
} }
pub(crate) fn get_encoded_named_node(named_node: NamedNodeRef<'_>) -> EncodedTerm { pub(crate) fn get_encoded_named_node(named_node: NamedNodeRef<'_>) -> EncodedTerm {
EncodedTerm::NamedNode { EncodedTerm::NamedNode {
@ -899,7 +899,7 @@ pub(crate) trait WriteEncoder: StrContainer {
impl<S: StrContainer> WriteEncoder for S { impl<S: StrContainer> WriteEncoder for S {
fn encode_str(&self, value: &str) -> Result<StrHash, Self::Error> { fn encode_str(&self, value: &str) -> Result<StrHash, Self::Error> {
let key = StrHash::new(value); let key = StrHash::new(value);
self.insert_str(key, value)?; self.insert_str(&key, value)?;
Ok(key) Ok(key)
} }
} }
@ -976,11 +976,11 @@ pub fn parse_day_time_duration_str(value: &str) -> Option<EncodedTerm> {
} }
pub(crate) trait Decoder: StrLookup { pub(crate) trait Decoder: StrLookup {
fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, DecoderError<Self::Error>>; fn decode_term(&self, encoded: &EncodedTerm) -> Result<Term, DecoderError<Self::Error>>;
fn decode_named_or_blank_node( fn decode_named_or_blank_node(
&self, &self,
encoded: EncodedTerm, encoded: &EncodedTerm,
) -> Result<NamedOrBlankNode, DecoderError<Self::Error>> { ) -> Result<NamedOrBlankNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? { match self.decode_term(encoded)? {
Term::NamedNode(named_node) => Ok(named_node.into()), Term::NamedNode(named_node) => Ok(named_node.into()),
@ -993,7 +993,7 @@ pub(crate) trait Decoder: StrLookup {
fn decode_named_node( fn decode_named_node(
&self, &self,
encoded: EncodedTerm, encoded: &EncodedTerm,
) -> Result<NamedNode, DecoderError<Self::Error>> { ) -> Result<NamedNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? { match self.decode_term(encoded)? {
Term::NamedNode(named_node) => Ok(named_node), Term::NamedNode(named_node) => Ok(named_node),
@ -1006,20 +1006,12 @@ pub(crate) trait Decoder: StrLookup {
} }
} }
fn decode_triple(&self, encoded: &EncodedQuad) -> Result<Triple, DecoderError<Self::Error>> {
Ok(Triple::new(
self.decode_named_or_blank_node(encoded.subject)?,
self.decode_named_node(encoded.predicate)?,
self.decode_term(encoded.object)?,
))
}
fn decode_quad(&self, encoded: &EncodedQuad) -> Result<Quad, DecoderError<Self::Error>> { fn decode_quad(&self, encoded: &EncodedQuad) -> Result<Quad, DecoderError<Self::Error>> {
Ok(Quad::new( Ok(Quad::new(
self.decode_named_or_blank_node(encoded.subject)?, self.decode_named_or_blank_node(&encoded.subject)?,
self.decode_named_node(encoded.predicate)?, self.decode_named_node(&encoded.predicate)?,
self.decode_term(encoded.object)?, self.decode_term(&encoded.object)?,
match encoded.graph_name { match &encoded.graph_name {
EncodedTerm::DefaultGraph => None, EncodedTerm::DefaultGraph => None,
graph_name => Some(self.decode_named_or_blank_node(graph_name)?), graph_name => Some(self.decode_named_or_blank_node(graph_name)?),
}, },
@ -1028,7 +1020,7 @@ pub(crate) trait Decoder: StrLookup {
} }
impl<S: StrLookup> Decoder for S { impl<S: StrLookup> Decoder for S {
fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, DecoderError<Self::Error>> { fn decode_term(&self, encoded: &EncodedTerm) -> Result<Term, DecoderError<Self::Error>> {
match encoded { match encoded {
EncodedTerm::DefaultGraph => Err(DecoderError::Decoder { EncodedTerm::DefaultGraph => Err(DecoderError::Decoder {
msg: "The default graph tag is not a valid term".to_owned(), msg: "The default graph tag is not a valid term".to_owned(),
@ -1036,21 +1028,23 @@ impl<S: StrLookup> Decoder for S {
EncodedTerm::NamedNode { iri_id } => { EncodedTerm::NamedNode { iri_id } => {
Ok(NamedNode::new_unchecked(get_required_str(self, iri_id)?).into()) Ok(NamedNode::new_unchecked(get_required_str(self, iri_id)?).into())
} }
EncodedTerm::NumericalBlankNode { id } => Ok(BlankNode::new_from_unique_id(id).into()), EncodedTerm::NumericalBlankNode { id } => Ok(BlankNode::new_from_unique_id(*id).into()),
EncodedTerm::SmallBlankNode(id) => Ok(BlankNode::new_unchecked(id.as_str()).into()), EncodedTerm::SmallBlankNode(id) => Ok(BlankNode::new_unchecked(id.as_str()).into()),
EncodedTerm::BigBlankNode { id_id } => { EncodedTerm::BigBlankNode { id_id } => {
Ok(BlankNode::new_unchecked(get_required_str(self, id_id)?).into()) Ok(BlankNode::new_unchecked(get_required_str(self, id_id)?).into())
} }
EncodedTerm::SmallStringLiteral(value) => Ok(Literal::new_simple_literal(value).into()), EncodedTerm::SmallStringLiteral(value) => {
Ok(Literal::new_simple_literal(*value).into())
}
EncodedTerm::BigStringLiteral { value_id } => { EncodedTerm::BigStringLiteral { value_id } => {
Ok(Literal::new_simple_literal(get_required_str(self, value_id)?).into()) Ok(Literal::new_simple_literal(get_required_str(self, value_id)?).into())
} }
EncodedTerm::SmallSmallLangStringLiteral { value, language } => { EncodedTerm::SmallSmallLangStringLiteral { value, language } => {
Ok(Literal::new_language_tagged_literal_unchecked(value, language).into()) Ok(Literal::new_language_tagged_literal_unchecked(*value, *language).into())
} }
EncodedTerm::SmallBigLangStringLiteral { value, language_id } => { EncodedTerm::SmallBigLangStringLiteral { value, language_id } => {
Ok(Literal::new_language_tagged_literal_unchecked( Ok(Literal::new_language_tagged_literal_unchecked(
value, *value,
get_required_str(self, language_id)?, get_required_str(self, language_id)?,
) )
.into()) .into())
@ -1058,7 +1052,7 @@ impl<S: StrLookup> Decoder for S {
EncodedTerm::BigSmallLangStringLiteral { value_id, language } => { EncodedTerm::BigSmallLangStringLiteral { value_id, language } => {
Ok(Literal::new_language_tagged_literal_unchecked( Ok(Literal::new_language_tagged_literal_unchecked(
get_required_str(self, value_id)?, get_required_str(self, value_id)?,
language, *language,
) )
.into()) .into())
} }
@ -1072,7 +1066,7 @@ impl<S: StrLookup> Decoder for S {
.into()), .into()),
EncodedTerm::SmallTypedLiteral { value, datatype_id } => { EncodedTerm::SmallTypedLiteral { value, datatype_id } => {
Ok(Literal::new_typed_literal( Ok(Literal::new_typed_literal(
value, *value,
NamedNode::new_unchecked(get_required_str(self, datatype_id)?), NamedNode::new_unchecked(get_required_str(self, datatype_id)?),
) )
.into()) .into())
@ -1085,29 +1079,29 @@ impl<S: StrLookup> Decoder for S {
NamedNode::new_unchecked(get_required_str(self, datatype_id)?), NamedNode::new_unchecked(get_required_str(self, datatype_id)?),
) )
.into()), .into()),
EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::FloatLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::FloatLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::IntegerLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::IntegerLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DecimalLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DecimalLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DateTimeLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DateTimeLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DateLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DateLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::TimeLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::TimeLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GYearMonthLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::GYearMonthLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GYearLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::GYearLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GMonthDayLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::GMonthDayLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GDayLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::GDayLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GMonthLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::GMonthLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DurationLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DurationLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::YearMonthDurationLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::YearMonthDurationLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DayTimeDurationLiteral(value) => Ok(Literal::from(value).into()), EncodedTerm::DayTimeDurationLiteral(value) => Ok(Literal::from(*value).into()),
} }
} }
} }
fn get_required_str<L: StrLookup>( fn get_required_str<L: StrLookup>(
lookup: &L, lookup: &L,
id: StrHash, id: &StrHash,
) -> Result<String, DecoderError<L::Error>> { ) -> Result<String, DecoderError<L::Error>> {
lookup lookup
.get_str(id) .get_str(id)

@ -171,10 +171,10 @@ impl Store {
) -> QuadIter { ) -> QuadIter {
QuadIter { QuadIter {
iter: self.storage.quads_for_pattern( iter: self.storage.quads_for_pattern(
subject.map(get_encoded_named_or_blank_node), subject.map(get_encoded_named_or_blank_node).as_ref(),
predicate.map(get_encoded_named_node), predicate.map(get_encoded_named_node).as_ref(),
object.map(get_encoded_term), object.map(get_encoded_term).as_ref(),
graph_name.map(get_encoded_graph_name), graph_name.map(get_encoded_graph_name).as_ref(),
), ),
storage: self.storage.clone(), storage: self.storage.clone(),
} }
@ -475,7 +475,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> Result<bool, io::Error> {
let graph_name = get_encoded_named_or_blank_node(graph_name.into()); let graph_name = get_encoded_named_or_blank_node(graph_name.into());
self.storage.contains_named_graph(graph_name) self.storage.contains_named_graph(&graph_name)
} }
/// Inserts a graph into this store /// Inserts a graph into this store
@ -498,7 +498,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> Result<bool, io::Error> {
let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?; let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?;
self.storage.insert_named_graph(graph_name) self.storage.insert_named_graph(&graph_name)
} }
/// Clears a graph from this store. /// Clears a graph from this store.
@ -524,7 +524,7 @@ impl Store {
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 = get_encoded_graph_name(graph_name.into());
self.storage.clear_graph(graph_name) self.storage.clear_graph(&graph_name)
} }
/// Removes a graph from this store. /// Removes a graph from this store.
@ -552,7 +552,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> Result<bool, io::Error> {
let graph_name = get_encoded_named_or_blank_node(graph_name.into()); let graph_name = get_encoded_named_or_blank_node(graph_name.into());
self.storage.remove_named_graph(graph_name) self.storage.remove_named_graph(&graph_name)
} }
/// Clears the store. /// Clears the store.
@ -717,7 +717,7 @@ impl Transaction<'_> {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, UnabortableTransactionError> { ) -> Result<bool, UnabortableTransactionError> {
let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?; let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?;
self.storage.insert_named_graph(graph_name) self.storage.insert_named_graph(&graph_name)
} }
} }
@ -750,7 +750,7 @@ impl Iterator for GraphNameIter {
fn next(&mut self) -> Option<Result<NamedOrBlankNode, io::Error>> { fn next(&mut self) -> Option<Result<NamedOrBlankNode, io::Error>> {
Some( Some(
self.iter.next()?.and_then(|graph_name| { self.iter.next()?.and_then(|graph_name| {
Ok(self.store.storage.decode_named_or_blank_node(graph_name)?) Ok(self.store.storage.decode_named_or_blank_node(&graph_name)?)
}), }),
) )
} }

Loading…
Cancel
Save