Makes StrContainer::insert_str return the string ID

pull/46/head
Tpt 4 years ago
parent 69686c8710
commit fcb3a33606
  1. 4
      lib/src/sparql/eval.rs
  2. 8
      lib/src/sparql/plan.rs
  3. 15
      lib/src/store/memory.rs
  4. 58
      lib/src/store/numeric_encoder.rs
  5. 10
      lib/src/store/rocksdb.rs
  6. 14
      lib/src/store/sled.rs

@ -1605,9 +1605,7 @@ impl<S: ReadableEncodedStore + 'static> SimpleEvaluator<S> {
} }
fn build_string_id(&self, value: &str) -> Option<StrHash> { fn build_string_id(&self, value: &str) -> Option<StrHash> {
let value_id = StrHash::new(value); self.dataset.encoder().insert_str(value).ok()
self.dataset.encoder().insert_str(value_id, value).ok()?;
Some(value_id)
} }
fn build_language_id(&self, value: EncodedTerm) -> Option<StrHash> { fn build_language_id(&self, value: EncodedTerm) -> Option<StrHash> {

@ -652,12 +652,12 @@ impl<'a, S: ReadableEncodedStore> WithStoreError for DatasetViewStrContainer<'a,
} }
impl<'a, S: ReadableEncodedStore> StrContainer for DatasetViewStrContainer<'a, S> { 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<StrHash, Self::Error> {
let key = StrHash::new(value);
if self.store.get_str(key)?.is_none() { if self.store.get_str(key)?.is_none() {
self.extra.insert_str(key, value).unwrap(); Ok(self.extra.insert_str(value).unwrap_infallible())
Ok(())
} else { } else {
Ok(()) Ok(key)
} }
} }
} }

@ -697,14 +697,13 @@ impl<'a> WithStoreError for &'a MemoryStore {
impl StrLookup for MemoryStore { impl StrLookup for MemoryStore {
fn get_str(&self, id: StrHash) -> Result<Option<String>, Infallible> { fn get_str(&self, id: StrHash) -> Result<Option<String>, Infallible> {
//TODO: avoid copy by adding a lifetime limit to get_str
self.indexes().get_str(id) self.indexes().get_str(id)
} }
} }
impl<'a> StrContainer for &'a MemoryStore { impl<'a> StrContainer for &'a MemoryStore {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, value: &str) -> Result<StrHash, Infallible> {
self.indexes_mut().insert_str(key, value) self.indexes_mut().insert_str(value)
} }
} }
@ -748,9 +747,10 @@ impl StrLookup for MemoryStoreIndexes {
} }
impl StrContainer 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<StrHash, Infallible> {
let key = StrHash::new(value);
self.id2str.entry(key).or_insert_with(|| value.to_owned()); 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<'_> { impl StrContainer for MemoryTransaction<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, value: &str) -> Result<StrHash, Infallible> {
let key = StrHash::new(value);
self.strings.push((key, value.to_owned())); self.strings.push((key, value.to_owned()));
Ok(()) Ok(key)
} }
} }

@ -773,7 +773,7 @@ pub(crate) trait StrLookup: WithStoreError {
} }
pub(crate) trait StrContainer: 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<StrHash, Self::Error>;
} }
pub struct MemoryStrStore { pub struct MemoryStrStore {
@ -800,9 +800,10 @@ impl StrLookup for MemoryStrStore {
} }
impl StrContainer 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<StrHash, Infallible> {
let key = StrHash::new(value);
self.id2str.entry(key).or_insert_with(|| value.to_owned()); self.id2str.entry(key).or_insert_with(|| value.to_owned());
Ok(()) Ok(key)
} }
} }
@ -940,19 +941,18 @@ impl<S: StrContainer> Encoder for S {
&mut self, &mut self,
named_node: rio::NamedNode<'_>, named_node: rio::NamedNode<'_>,
) -> Result<EncodedTerm, Self::Error> { ) -> Result<EncodedTerm, Self::Error> {
let iri_id = StrHash::new(named_node.iri); Ok(EncodedTerm::NamedNode {
self.insert_str(iri_id, named_node.iri)?; iri_id: self.insert_str(named_node.iri)?,
Ok(EncodedTerm::NamedNode { iri_id }) })
} }
fn encode_blank_node(&mut self, blank_node: &BlankNode) -> Result<EncodedTerm, Self::Error> { fn encode_blank_node(&mut self, blank_node: &BlankNode) -> Result<EncodedTerm, Self::Error> {
if let Some(id) = blank_node.id() { if let Some(id) = blank_node.id() {
Ok(EncodedTerm::InlineBlankNode { id }) Ok(EncodedTerm::InlineBlankNode { id })
} else { } else {
let id = blank_node.as_str(); Ok(EncodedTerm::NamedBlankNode {
let id_id = StrHash::new(id); id_id: self.insert_str(blank_node.as_str())?,
self.insert_str(id_id, id)?; })
Ok(EncodedTerm::NamedBlankNode { id_id })
} }
} }
@ -975,29 +975,21 @@ impl<S: StrContainer> Encoder for S {
literal: rio::Literal<'_>, literal: rio::Literal<'_>,
) -> Result<EncodedTerm, Self::Error> { ) -> Result<EncodedTerm, Self::Error> {
Ok(match literal { Ok(match literal {
rio::Literal::Simple { value } => { rio::Literal::Simple { value } => EncodedTerm::StringLiteral {
let value_id = StrHash::new(value); value_id: self.insert_str(value)?,
self.insert_str(value_id, value)?; },
EncodedTerm::StringLiteral { value_id }
}
rio::Literal::LanguageTaggedString { value, language } => { 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 { EncodedTerm::LangStringLiteral {
value_id, value_id: self.insert_str(value)?,
language_id, language_id: self.insert_str(language)?,
} }
} }
rio::Literal::Typed { value, datatype } => { rio::Literal::Typed { value, datatype } => {
match match datatype.iri { match match datatype.iri {
"http://www.w3.org/2001/XMLSchema#boolean" => parse_boolean_str(value), "http://www.w3.org/2001/XMLSchema#boolean" => parse_boolean_str(value),
"http://www.w3.org/2001/XMLSchema#string" => { "http://www.w3.org/2001/XMLSchema#string" => Some(EncodedTerm::StringLiteral {
let value_id = StrHash::new(value); value_id: self.insert_str(value)?,
self.insert_str(value_id, value)?; }),
Some(EncodedTerm::StringLiteral { value_id })
}
"http://www.w3.org/2001/XMLSchema#float" => parse_float_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#double" => parse_double_str(value),
"http://www.w3.org/2001/XMLSchema#integer" "http://www.w3.org/2001/XMLSchema#integer"
@ -1032,16 +1024,10 @@ impl<S: StrContainer> Encoder for S {
_ => None, _ => None,
} { } {
Some(v) => v, Some(v) => v,
None => { None => EncodedTerm::TypedLiteral {
let value_id = StrHash::new(value); value_id: self.insert_str(value)?,
self.insert_str(value_id, value)?; datatype_id: self.insert_str(datatype.iri)?,
let datatype_id = StrHash::new(datatype.iri); },
self.insert_str(datatype_id, datatype.iri)?;
EncodedTerm::TypedLiteral {
value_id,
datatype_id,
}
}
} }
} }
}) })

@ -624,10 +624,11 @@ impl WithStoreError for BatchWriter<'_> {
} }
impl StrContainer 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<StrHash, Infallible> {
let key = StrHash::new(value);
self.batch self.batch
.put_cf(self.store.id2str_cf(), &key.to_be_bytes(), value); .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<'_> { impl StrContainer for AutoBatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { fn insert_str(&mut self, value: &str) -> Result<StrHash, io::Error> {
self.inner.insert_str(key, value).unwrap_infallible(); Ok(self.inner.insert_str(value).unwrap_infallible())
Ok(())
} }
} }

@ -496,9 +496,10 @@ impl<'a> WithStoreError for &'a SledStore {
} }
impl<'a> StrContainer 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<StrHash, io::Error> {
let key = StrHash::new(value);
self.id2str.insert(key.to_be_bytes().as_ref(), 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> { impl<'a> StrContainer for &'a SledTransaction<'a> {
fn insert_str( fn insert_str(&mut self, value: &str) -> Result<StrHash, SledUnabortableTransactionError> {
&mut self, let key = StrHash::new(value);
key: StrHash,
value: &str,
) -> Result<(), SledUnabortableTransactionError> {
self.id2str.insert(key.to_be_bytes().as_ref(), value)?; self.id2str.insert(key.to_be_bytes().as_ref(), value)?;
Ok(()) Ok(key)
} }
} }

Loading…
Cancel
Save