diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index cd90bfe5..c2dc2f93 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -1605,9 +1605,7 @@ impl SimpleEvaluator { } fn build_string_id(&self, value: &str) -> Option { - let value_id = StrHash::new(value); - self.dataset.encoder().insert_str(value_id, value).ok()?; - Some(value_id) + self.dataset.encoder().insert_str(value).ok() } fn build_language_id(&self, value: EncodedTerm) -> Option { diff --git a/lib/src/sparql/plan.rs b/lib/src/sparql/plan.rs index 48f89be3..b08d7a66 100644 --- a/lib/src/sparql/plan.rs +++ b/lib/src/sparql/plan.rs @@ -652,12 +652,12 @@ impl<'a, S: ReadableEncodedStore> WithStoreError for DatasetViewStrContainer<'a, } impl<'a, S: ReadableEncodedStore> StrContainer for DatasetViewStrContainer<'a, S> { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Self::Error> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); if self.store.get_str(key)?.is_none() { - self.extra.insert_str(key, value).unwrap(); - Ok(()) + Ok(self.extra.insert_str(value).unwrap_infallible()) } else { - Ok(()) + Ok(key) } } } diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index ab10e739..b120cbc9 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -697,14 +697,13 @@ impl<'a> WithStoreError for &'a MemoryStore { impl StrLookup for MemoryStore { fn get_str(&self, id: StrHash) -> Result, Infallible> { - //TODO: avoid copy by adding a lifetime limit to get_str self.indexes().get_str(id) } } impl<'a> StrContainer for &'a MemoryStore { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { - self.indexes_mut().insert_str(key, value) + fn insert_str(&mut self, value: &str) -> Result { + self.indexes_mut().insert_str(value) } } @@ -748,9 +747,10 @@ impl StrLookup for MemoryStoreIndexes { } impl StrContainer for MemoryStoreIndexes { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.id2str.entry(key).or_insert_with(|| value.to_owned()); - Ok(()) + Ok(key) } } @@ -1043,9 +1043,10 @@ impl WithStoreError for MemoryTransaction<'_> { } impl StrContainer for MemoryTransaction<'_> { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.strings.push((key, value.to_owned())); - Ok(()) + Ok(key) } } diff --git a/lib/src/store/numeric_encoder.rs b/lib/src/store/numeric_encoder.rs index c954e08a..2f488220 100644 --- a/lib/src/store/numeric_encoder.rs +++ b/lib/src/store/numeric_encoder.rs @@ -773,7 +773,7 @@ pub(crate) trait StrLookup: WithStoreError { } pub(crate) trait StrContainer: WithStoreError { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Self::Error>; + fn insert_str(&mut self, value: &str) -> Result; } pub struct MemoryStrStore { @@ -800,9 +800,10 @@ impl StrLookup for MemoryStrStore { } impl StrContainer for MemoryStrStore { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.id2str.entry(key).or_insert_with(|| value.to_owned()); - Ok(()) + Ok(key) } } @@ -940,19 +941,18 @@ impl Encoder for S { &mut self, named_node: rio::NamedNode<'_>, ) -> Result { - let iri_id = StrHash::new(named_node.iri); - self.insert_str(iri_id, named_node.iri)?; - Ok(EncodedTerm::NamedNode { iri_id }) + Ok(EncodedTerm::NamedNode { + iri_id: self.insert_str(named_node.iri)?, + }) } fn encode_blank_node(&mut self, blank_node: &BlankNode) -> Result { if let Some(id) = blank_node.id() { Ok(EncodedTerm::InlineBlankNode { id }) } else { - let id = blank_node.as_str(); - let id_id = StrHash::new(id); - self.insert_str(id_id, id)?; - Ok(EncodedTerm::NamedBlankNode { id_id }) + Ok(EncodedTerm::NamedBlankNode { + id_id: self.insert_str(blank_node.as_str())?, + }) } } @@ -975,29 +975,21 @@ impl Encoder for S { literal: rio::Literal<'_>, ) -> Result { Ok(match literal { - rio::Literal::Simple { value } => { - let value_id = StrHash::new(value); - self.insert_str(value_id, value)?; - EncodedTerm::StringLiteral { value_id } - } + rio::Literal::Simple { value } => EncodedTerm::StringLiteral { + value_id: self.insert_str(value)?, + }, rio::Literal::LanguageTaggedString { value, language } => { - let value_id = StrHash::new(value); - self.insert_str(value_id, value)?; - let language_id = StrHash::new(language); - self.insert_str(language_id, language)?; EncodedTerm::LangStringLiteral { - value_id, - language_id, + value_id: self.insert_str(value)?, + language_id: self.insert_str(language)?, } } rio::Literal::Typed { value, datatype } => { match match datatype.iri { "http://www.w3.org/2001/XMLSchema#boolean" => parse_boolean_str(value), - "http://www.w3.org/2001/XMLSchema#string" => { - let value_id = StrHash::new(value); - self.insert_str(value_id, value)?; - Some(EncodedTerm::StringLiteral { value_id }) - } + "http://www.w3.org/2001/XMLSchema#string" => Some(EncodedTerm::StringLiteral { + value_id: self.insert_str(value)?, + }), "http://www.w3.org/2001/XMLSchema#float" => parse_float_str(value), "http://www.w3.org/2001/XMLSchema#double" => parse_double_str(value), "http://www.w3.org/2001/XMLSchema#integer" @@ -1032,16 +1024,10 @@ impl Encoder for S { _ => None, } { Some(v) => v, - None => { - let value_id = StrHash::new(value); - self.insert_str(value_id, value)?; - let datatype_id = StrHash::new(datatype.iri); - self.insert_str(datatype_id, datatype.iri)?; - EncodedTerm::TypedLiteral { - value_id, - datatype_id, - } - } + None => EncodedTerm::TypedLiteral { + value_id: self.insert_str(value)?, + datatype_id: self.insert_str(datatype.iri)?, + }, } } }) diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index d8dc698a..e5c2fbb3 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -624,10 +624,11 @@ impl WithStoreError for BatchWriter<'_> { } impl StrContainer for BatchWriter<'_> { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.batch .put_cf(self.store.id2str_cf(), &key.to_be_bytes(), value); - Ok(()) + Ok(key) } } @@ -704,9 +705,8 @@ impl WithStoreError for AutoBatchWriter<'_> { } impl StrContainer for AutoBatchWriter<'_> { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { - self.inner.insert_str(key, value).unwrap_infallible(); - Ok(()) + fn insert_str(&mut self, value: &str) -> Result { + Ok(self.inner.insert_str(value).unwrap_infallible()) } } diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index 812d0eb4..b61e8ca2 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -496,9 +496,10 @@ impl<'a> WithStoreError for &'a SledStore { } impl<'a> StrContainer for &'a SledStore { - fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.id2str.insert(key.to_be_bytes().as_ref(), value)?; - Ok(()) + Ok(key) } } @@ -634,13 +635,10 @@ impl<'a> WithStoreError for &'a SledTransaction<'a> { } impl<'a> StrContainer for &'a SledTransaction<'a> { - fn insert_str( - &mut self, - key: StrHash, - value: &str, - ) -> Result<(), SledUnabortableTransactionError> { + fn insert_str(&mut self, value: &str) -> Result { + let key = StrHash::new(value); self.id2str.insert(key.to_be_bytes().as_ref(), value)?; - Ok(()) + Ok(key) } }