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> {
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<StrHash> {

@ -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<StrHash, Self::Error> {
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)
}
}
}

@ -697,14 +697,13 @@ impl<'a> WithStoreError for &'a MemoryStore {
impl StrLookup for MemoryStore {
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)
}
}
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<StrHash, Infallible> {
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<StrHash, Infallible> {
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<StrHash, Infallible> {
let key = StrHash::new(value);
self.strings.push((key, value.to_owned()));
Ok(())
Ok(key)
}
}

@ -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<StrHash, Self::Error>;
}
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<StrHash, Infallible> {
let key = StrHash::new(value);
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,
named_node: rio::NamedNode<'_>,
) -> Result<EncodedTerm, Self::Error> {
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<EncodedTerm, Self::Error> {
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<S: StrContainer> Encoder for S {
literal: rio::Literal<'_>,
) -> Result<EncodedTerm, Self::Error> {
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<S: StrContainer> 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)?,
},
}
}
})

@ -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<StrHash, Infallible> {
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<StrHash, io::Error> {
Ok(self.inner.insert_str(value).unwrap_infallible())
}
}

@ -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<StrHash, io::Error> {
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<StrHash, SledUnabortableTransactionError> {
let key = StrHash::new(value);
self.id2str.insert(key.to_be_bytes().as_ref(), value)?;
Ok(())
Ok(key)
}
}

Loading…
Cancel
Save