Makes encoded term not copy

pull/171/head
Tpt 4 years ago
parent e6e83ff369
commit e2bbc29198
  1. 36
      lib/src/sparql/dataset.rs
  2. 560
      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(
&self,
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
subject: Option<&EncodedTerm>,
predicate: Option<&EncodedTerm>,
object: Option<&EncodedTerm>,
graph_name: Option<&EncodedTerm>,
) -> impl Iterator<Item = Result<EncodedQuad, EvaluationError>> + 'static {
self.storage
.quads_for_pattern(subject, predicate, object, graph_name)
@ -54,10 +54,10 @@ impl DatasetView {
#[allow(clippy::needless_collect)]
pub fn encoded_quads_for_pattern(
&self,
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
subject: Option<&EncodedTerm>,
predicate: Option<&EncodedTerm>,
object: Option<&EncodedTerm>,
graph_name: Option<&EncodedTerm>,
) -> Box<dyn Iterator<Item = Result<EncodedQuad, EvaluationError>>> {
if let Some(graph_name) = graph_name {
if graph_name.is_default_graph() {
@ -69,7 +69,7 @@ impl DatasetView {
subject,
predicate,
object,
Some(default_graph_graphs[0]),
Some(&default_graph_graphs[0]),
)
.map(|quad| {
let quad = quad?;
@ -89,7 +89,7 @@ impl DatasetView {
subject,
predicate,
object,
Some(*graph_name),
Some(graph_name),
)
})
.collect::<Vec<_>>();
@ -110,7 +110,7 @@ impl DatasetView {
.dataset
.named
.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(
subject,
@ -129,7 +129,7 @@ impl DatasetView {
subject,
predicate,
object,
Some(*graph_name),
Some(graph_name),
)
})
.collect::<Vec<_>>();
@ -149,25 +149,25 @@ impl DatasetView {
impl StrLookup for DatasetView {
type Error = EvaluationError;
fn get_str(&self, key: StrHash) -> Result<Option<String>, EvaluationError> {
Ok(if let Some(value) = self.extra.borrow().get(&key) {
fn get_str(&self, key: &StrHash) -> Result<Option<String>, EvaluationError> {
Ok(if let Some(value) = self.extra.borrow().get(key) {
Some(value.clone())
} else {
self.storage.get_str(key)?
})
}
fn contains_str(&self, key: StrHash) -> Result<bool, EvaluationError> {
Ok(self.extra.borrow().contains_key(&key) || self.storage.contains_str(key)?)
fn contains_str(&self, key: &StrHash) -> Result<bool, EvaluationError> {
Ok(self.extra.borrow().contains_key(key) || self.storage.contains_str(key)?)
}
}
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)? {
Ok(false)
} else {
match self.extra.borrow_mut().entry(key) {
match self.extra.borrow_mut().entry(*key) {
Entry::Occupied(_) => Ok(false),
Entry::Vacant(entry) => {
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 {
Constant(EncodedTerm),
Variable(usize),
@ -460,14 +460,14 @@ pub enum Comparator {
Desc(PlanExpression),
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct TripleTemplate {
pub subject: TripleTemplateValue,
pub predicate: TripleTemplateValue,
pub object: TripleTemplateValue,
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum TripleTemplateValue {
Constant(EncodedTerm),
BlankNode(usize),
@ -494,8 +494,8 @@ impl EncodedTuple {
self.inner.get(index).map_or(false, Option::is_some)
}
pub fn get(&self, index: usize) -> Option<EncodedTerm> {
self.inner.get(index).cloned().unwrap_or(None)
pub fn get(&self, index: usize) -> Option<&EncodedTerm> {
self.inner.get(index).unwrap_or(&None).as_ref()
}
pub fn iter(&self) -> impl Iterator<Item = Option<EncodedTerm>> + '_ {
@ -526,7 +526,7 @@ impl EncodedTuple {
return None;
}
}
None => result[key] = Some(*self_value),
None => result[key] = Some(self_value.clone()),
}
}
}
@ -541,7 +541,7 @@ impl EncodedTuple {
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(
pattern,
&mut variables,
PatternValue::Constant(EncodedTerm::DefaultGraph),
&PatternValue::Constant(EncodedTerm::DefaultGraph),
)?;
Ok((plan, variables))
}
@ -40,7 +40,7 @@ impl<'a> PlanBuilder<'a> {
&mut self,
pattern: &GraphPattern,
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
) -> Result<PlanNode, EvaluationError> {
Ok(match pattern {
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)?,
path: Rc::new(self.build_for_path(path)?),
object: self.pattern_value_from_term_or_variable(object, variables)?,
graph_name,
graph_name: graph_name.clone(),
},
GraphPattern::Join { left, right } => {
//TODO: improve
@ -69,7 +69,7 @@ impl<'a> PlanBuilder<'a> {
subject: self.pattern_value_from_term_or_variable(subject, variables)?,
path: Rc::new(self.build_for_path(path)?),
object: self.pattern_value_from_term_or_variable(object, variables)?,
graph_name,
graph_name: graph_name.clone(),
}
} else {
PlanNode::Join {
@ -128,7 +128,7 @@ impl<'a> PlanBuilder<'a> {
GraphPattern::Graph { graph_name, inner } => {
let graph_name =
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 {
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(
inner,
&mut inner_variables,
inner_graph_name,
&inner_graph_name,
)?),
key_mapping: Rc::new(
by.iter()
@ -230,7 +230,7 @@ impl<'a> PlanBuilder<'a> {
child: Rc::new(self.build_for_graph_pattern(
inner,
&mut inner_variables,
inner_graph_name,
&inner_graph_name,
)?),
mapping: Rc::new(
projection
@ -276,7 +276,7 @@ impl<'a> PlanBuilder<'a> {
&mut self,
p: &[TriplePattern],
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
) -> Result<PlanNode, EvaluationError> {
let mut plan = PlanNode::Init;
for pattern in sort_bgp(p) {
@ -286,7 +286,7 @@ impl<'a> PlanBuilder<'a> {
predicate: self
.pattern_value_from_named_node_or_variable(&pattern.predicate, variables)?,
object: self.pattern_value_from_term_or_variable(&pattern.object, variables)?,
graph_name,
graph_name: graph_name.clone(),
}
}
Ok(plan)
@ -334,7 +334,7 @@ impl<'a> PlanBuilder<'a> {
&mut self,
expression: &Expression,
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
) -> Result<PlanExpression, EvaluationError> {
Ok(match expression {
Expression::NamedNode(node) => PlanExpression::Constant(self.build_named_node(node)?),
@ -738,7 +738,7 @@ impl<'a> PlanBuilder<'a> {
parameters: &[Expression],
constructor: impl Fn(Box<PlanExpression>) -> PlanExpression,
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
name: &'static str,
) -> Result<PlanExpression, EvaluationError> {
if parameters.len() == 1 {
@ -759,7 +759,7 @@ impl<'a> PlanBuilder<'a> {
&mut self,
l: &[Expression],
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
) -> Result<Vec<PlanExpression>, EvaluationError> {
l.iter()
.map(|e| self.build_for_expression(e, variables, graph_name))
@ -839,7 +839,7 @@ impl<'a> PlanBuilder<'a> {
&mut self,
aggregate: &AggregationFunction,
variables: &mut Vec<Variable>,
graph_name: PatternValue,
graph_name: &PatternValue,
) -> Result<PlanAggregation, EvaluationError> {
match aggregate {
AggregationFunction::Count { expr, distinct } => Ok(PlanAggregation {
@ -958,14 +958,14 @@ impl<'a> PlanBuilder<'a> {
fn convert_pattern_value_id(
&self,
from_value: PatternValue,
from_value: &PatternValue,
from: &[Variable],
to: &mut Vec<Variable>,
) -> PatternValue {
match from_value {
PatternValue::Constant(v) => PatternValue::Constant(v),
PatternValue::Constant(v) => PatternValue::Constant(v.clone()),
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> {
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 {
Ok(())
} else {
@ -224,7 +224,7 @@ impl<'a> SimpleUpdateEvaluator<'a> {
)))
}
} else {
self.storage.insert_named_graph(encoded_graph_name)?;
self.storage.insert_named_graph(&encoded_graph_name)?;
Ok(())
}
}
@ -233,8 +233,8 @@ impl<'a> SimpleUpdateEvaluator<'a> {
match graph {
GraphTarget::NamedNode(graph_name) => {
let graph_name = self.encode_named_node_for_deletion(graph_name);
if self.storage.contains_named_graph(graph_name)? {
Ok(self.storage.clear_graph(graph_name)?)
if self.storage.contains_named_graph(&graph_name)? {
Ok(self.storage.clear_graph(&graph_name)?)
} else if silent {
Ok(())
} 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 => {
// TODO: optimize?
for graph in self.storage.named_graphs() {
self.storage.clear_graph(graph?)?;
self.storage.clear_graph(&graph?)?;
}
Ok(())
}
GraphTarget::AllGraphs => {
// TODO: optimize?
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 {
GraphTarget::NamedNode(graph_name) => {
let graph_name = self.encode_named_node_for_deletion(graph_name);
if self.storage.contains_named_graph(graph_name)? {
self.storage.remove_named_graph(graph_name)?;
if self.storage.contains_named_graph(&graph_name)? {
self.storage.remove_named_graph(&graph_name)?;
Ok(())
} else if silent {
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 => {
// TODO: optimize?
for graph in self.storage.named_graphs() {
self.storage.remove_named_graph(graph?)?;
self.storage.remove_named_graph(&graph?)?;
}
Ok(())
}
@ -436,7 +440,7 @@ impl<'a> SimpleUpdateEvaluator<'a> {
.position(|v2| v == v2)
.and_then(|i| values.get(i))
{
Some(*term)
Some(term.clone())
} else {
None
}

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

@ -52,7 +52,7 @@ where
.map_err(StoreOrParseError::Store)?;
parser.parse_all(&mut move |t| {
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)?;
storage.insert(&quad).map_err(StoreOrParseError::Store)?;
Ok(())

@ -74,7 +74,7 @@ impl Storage {
for quad in this.quads() {
let quad = quad?;
if !quad.graph_name.is_default_graph() {
this.insert_named_graph(quad.graph_name)?;
this.insert_named_graph(&quad.graph_name)?;
}
}
version = 1;
@ -168,10 +168,10 @@ impl Storage {
pub fn quads_for_pattern(
&self,
subject: Option<EncodedTerm>,
predicate: Option<EncodedTerm>,
object: Option<EncodedTerm>,
graph_name: Option<EncodedTerm>,
subject: Option<&EncodedTerm>,
predicate: Option<&EncodedTerm>,
object: Option<&EncodedTerm>,
graph_name: Option<&EncodedTerm>,
) -> ChainedDecodingQuadIterator {
match subject {
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(
self.dspo_quads(encode_term(subject)),
self.spog_quads(encode_term(subject)),
@ -245,8 +245,8 @@ impl Storage {
fn quads_for_subject_predicate(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair(
self.dspo_quads(encode_term_pair(subject, predicate)),
@ -256,9 +256,9 @@ impl Storage {
fn quads_for_subject_predicate_object(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
object: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair(
self.dspo_quads(encode_term_triple(subject, predicate, object)),
@ -268,8 +268,8 @@ impl Storage {
fn quads_for_subject_object(
&self,
subject: EncodedTerm,
object: EncodedTerm,
subject: &EncodedTerm,
object: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair(
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(
self.dpos_quads(encode_term(predicate)),
self.posg_quads(encode_term(predicate)),
@ -286,8 +286,8 @@ impl Storage {
fn quads_for_predicate_object(
&self,
predicate: EncodedTerm,
object: EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::pair(
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(
self.dosp_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() {
self.dspo_quads(Vec::default())
} else {
@ -312,8 +312,8 @@ impl Storage {
fn quads_for_subject_graph(
&self,
subject: EncodedTerm,
graph_name: EncodedTerm,
subject: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term(subject))
@ -324,9 +324,9 @@ impl Storage {
fn quads_for_subject_predicate_graph(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
graph_name: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term_pair(subject, predicate))
@ -337,10 +337,10 @@ impl Storage {
fn quads_for_subject_predicate_object_graph(
&self,
subject: EncodedTerm,
predicate: EncodedTerm,
object: EncodedTerm,
graph_name: EncodedTerm,
subject: &EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dspo_quads(encode_term_triple(subject, predicate, object))
@ -351,9 +351,9 @@ impl Storage {
fn quads_for_subject_object_graph(
&self,
subject: EncodedTerm,
object: EncodedTerm,
graph_name: EncodedTerm,
subject: &EncodedTerm,
object: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dosp_quads(encode_term_pair(object, subject))
@ -364,8 +364,8 @@ impl Storage {
fn quads_for_predicate_graph(
&self,
predicate: EncodedTerm,
graph_name: EncodedTerm,
predicate: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dpos_quads(encode_term(predicate))
@ -376,9 +376,9 @@ impl Storage {
fn quads_for_predicate_object_graph(
&self,
predicate: EncodedTerm,
object: EncodedTerm,
graph_name: EncodedTerm,
predicate: &EncodedTerm,
object: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
self.dpos_quads(encode_term_pair(predicate, object))
@ -389,8 +389,8 @@ impl Storage {
fn quads_for_object_graph(
&self,
object: EncodedTerm,
graph_name: EncodedTerm,
object: &EncodedTerm,
graph_name: &EncodedTerm,
) -> ChainedDecodingQuadIterator {
ChainedDecodingQuadIterator::new(if graph_name.is_default_graph() {
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))?)
}
@ -503,7 +503,7 @@ impl Storage {
self.gosp.insert(buffer.as_slice(), &[])?;
buffer.clear();
write_term(&mut buffer, quad.graph_name);
write_term(&mut buffer, &quad.graph_name);
self.graphs.insert(&buffer, &[])?;
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())
}
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() {
self.dspo.clear()?;
self.dpos.clear()?;
@ -581,7 +581,7 @@ impl Storage {
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) {
self.remove(&quad?)?;
}
@ -603,7 +603,7 @@ impl Storage {
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
.get(key.to_be_bytes())?
.map(|v| String::from_utf8(v.to_vec()))
@ -611,11 +611,11 @@ impl Storage {
.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())?)
}
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())
}
}
@ -748,7 +748,7 @@ impl<'a> StorageTransaction<'a> {
self.gosp.insert(buffer.as_slice(), &[])?;
buffer.clear();
write_term(&mut buffer, quad.graph_name);
write_term(&mut buffer, &quad.graph_name);
self.graphs.insert(buffer.as_slice(), &[])?;
buffer.clear();
}
@ -811,12 +811,12 @@ impl<'a> StorageTransaction<'a> {
pub fn insert_named_graph(
&self,
graph_name: EncodedTerm,
graph_name: &EncodedTerm,
) -> Result<bool, UnabortableTransactionError> {
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
.get(key.to_be_bytes())?
.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)))
}
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())
}
pub fn insert_str(
&self,
key: StrHash,
key: &StrHash,
value: &str,
) -> Result<bool, UnabortableTransactionError> {
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 {
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)
}
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)
}
}
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)
}
}
@ -1010,17 +1010,17 @@ impl StrContainer for Storage {
impl<'a> StrLookup for StorageTransaction<'a> {
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)
}
fn contains_str(&self, key: StrHash) -> Result<bool, UnabortableTransactionError> {
fn contains_str(&self, key: &StrHash) -> Result<bool, UnabortableTransactionError> {
self.contains_str(key)
}
}
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)
}
}

@ -16,7 +16,7 @@ use std::hash::Hash;
use std::hash::Hasher;
use std::{fmt, io, str};
#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
#[repr(transparent)]
pub struct StrHash {
hash: u128,
@ -44,7 +44,7 @@ impl StrHash {
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub enum EncodedTerm {
DefaultGraph,
NamedNode {
@ -337,8 +337,8 @@ impl EncodedTerm {
}
pub fn on_each_id<E>(
self,
mut callback: impl FnMut(StrHash) -> Result<(), E>,
&self,
mut callback: impl FnMut(&StrHash) -> Result<(), E>,
) -> Result<(), E> {
match self {
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 subject: EncodedTerm,
pub predicate: EncodedTerm,
@ -489,13 +489,13 @@ impl EncodedQuad {
pub(crate) trait StrLookup {
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 {
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 {
EncodedTerm::NamedNode {
@ -899,7 +899,7 @@ pub(crate) trait WriteEncoder: StrContainer {
impl<S: StrContainer> WriteEncoder for S {
fn encode_str(&self, value: &str) -> Result<StrHash, Self::Error> {
let key = StrHash::new(value);
self.insert_str(key, value)?;
self.insert_str(&key, value)?;
Ok(key)
}
}
@ -976,11 +976,11 @@ pub fn parse_day_time_duration_str(value: &str) -> Option<EncodedTerm> {
}
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(
&self,
encoded: EncodedTerm,
encoded: &EncodedTerm,
) -> Result<NamedOrBlankNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? {
Term::NamedNode(named_node) => Ok(named_node.into()),
@ -993,7 +993,7 @@ pub(crate) trait Decoder: StrLookup {
fn decode_named_node(
&self,
encoded: EncodedTerm,
encoded: &EncodedTerm,
) -> Result<NamedNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? {
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>> {
Ok(Quad::new(
self.decode_named_or_blank_node(encoded.subject)?,
self.decode_named_node(encoded.predicate)?,
self.decode_term(encoded.object)?,
match encoded.graph_name {
self.decode_named_or_blank_node(&encoded.subject)?,
self.decode_named_node(&encoded.predicate)?,
self.decode_term(&encoded.object)?,
match &encoded.graph_name {
EncodedTerm::DefaultGraph => None,
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 {
fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, DecoderError<Self::Error>> {
fn decode_term(&self, encoded: &EncodedTerm) -> Result<Term, DecoderError<Self::Error>> {
match encoded {
EncodedTerm::DefaultGraph => Err(DecoderError::Decoder {
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 } => {
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::BigBlankNode { id_id } => {
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 } => {
Ok(Literal::new_simple_literal(get_required_str(self, value_id)?).into())
}
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 } => {
Ok(Literal::new_language_tagged_literal_unchecked(
value,
*value,
get_required_str(self, language_id)?,
)
.into())
@ -1058,7 +1052,7 @@ impl<S: StrLookup> Decoder for S {
EncodedTerm::BigSmallLangStringLiteral { value_id, language } => {
Ok(Literal::new_language_tagged_literal_unchecked(
get_required_str(self, value_id)?,
language,
*language,
)
.into())
}
@ -1072,7 +1066,7 @@ impl<S: StrLookup> Decoder for S {
.into()),
EncodedTerm::SmallTypedLiteral { value, datatype_id } => {
Ok(Literal::new_typed_literal(
value,
*value,
NamedNode::new_unchecked(get_required_str(self, datatype_id)?),
)
.into())
@ -1085,29 +1079,29 @@ impl<S: StrLookup> Decoder for S {
NamedNode::new_unchecked(get_required_str(self, datatype_id)?),
)
.into()),
EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::FloatLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::IntegerLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DecimalLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DateTimeLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DateLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::TimeLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::GYearMonthLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::GYearLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::GMonthDayLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::GDayLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::GMonthLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DurationLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::YearMonthDurationLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DayTimeDurationLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::FloatLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::IntegerLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DecimalLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DateTimeLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DateLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::TimeLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GYearMonthLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GYearLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GMonthDayLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GDayLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::GMonthLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DurationLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::YearMonthDurationLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DayTimeDurationLiteral(value) => Ok(Literal::from(*value).into()),
}
}
}
fn get_required_str<L: StrLookup>(
lookup: &L,
id: StrHash,
id: &StrHash,
) -> Result<String, DecoderError<L::Error>> {
lookup
.get_str(id)

@ -171,10 +171,10 @@ impl Store {
) -> QuadIter {
QuadIter {
iter: self.storage.quads_for_pattern(
subject.map(get_encoded_named_or_blank_node),
predicate.map(get_encoded_named_node),
object.map(get_encoded_term),
graph_name.map(get_encoded_graph_name),
subject.map(get_encoded_named_or_blank_node).as_ref(),
predicate.map(get_encoded_named_node).as_ref(),
object.map(get_encoded_term).as_ref(),
graph_name.map(get_encoded_graph_name).as_ref(),
),
storage: self.storage.clone(),
}
@ -475,7 +475,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> {
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
@ -498,7 +498,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> {
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.
@ -524,7 +524,7 @@ impl Store {
graph_name: impl Into<GraphNameRef<'a>>,
) -> Result<(), io::Error> {
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.
@ -552,7 +552,7 @@ impl Store {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> {
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.
@ -717,7 +717,7 @@ impl Transaction<'_> {
graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, UnabortableTransactionError> {
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>> {
Some(
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