Introduces a WithStoreError trait to share the store error

Avoids a lot of "as" casts
pull/46/head
Tpt 4 years ago
parent 9038ab3921
commit 669d3b1494
  1. 13
      lib/src/sparql/plan.rs
  2. 65
      lib/src/store/memory.rs
  3. 16
      lib/src/store/mod.rs
  4. 22
      lib/src/store/numeric_encoder.rs
  5. 17
      lib/src/store/rocksdb.rs
  6. 17
      lib/src/store/sled.rs

@ -4,7 +4,7 @@ use crate::sparql::error::EvaluationError;
use crate::sparql::model::Variable; use crate::sparql::model::Variable;
use crate::store::numeric_encoder::{ use crate::store::numeric_encoder::{
EncodedQuad, EncodedTerm, Encoder, MemoryStrStore, StrContainer, StrHash, StrLookup, EncodedQuad, EncodedTerm, Encoder, MemoryStrStore, StrContainer, StrHash, StrLookup,
ENCODED_DEFAULT_GRAPH, WithStoreError, ENCODED_DEFAULT_GRAPH,
}; };
use crate::store::ReadableEncodedStore; use crate::store::ReadableEncodedStore;
use std::cell::{RefCell, RefMut}; use std::cell::{RefCell, RefMut};
@ -630,9 +630,10 @@ fn map_io_err<'a, T>(
iter.map(|e| e.map_err(|e| e.into().into())) iter.map(|e| e.map_err(|e| e.into().into()))
} }
impl<S: ReadableEncodedStore> WithStoreError for DatasetView<S> {
type Error = S::Error;
}
impl<S: ReadableEncodedStore> StrLookup for DatasetView<S> { impl<S: ReadableEncodedStore> StrLookup for DatasetView<S> {
type Error = <S as StrLookup>::Error;
fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error> { fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error> {
if let Some(value) = self.extra.borrow().get_str(id).unwrap_infallible() { if let Some(value) = self.extra.borrow().get_str(id).unwrap_infallible() {
Ok(Some(value)) Ok(Some(value))
@ -647,9 +648,11 @@ struct DatasetViewStrContainer<'a, S: ReadableEncodedStore> {
extra: RefMut<'a, MemoryStrStore>, extra: RefMut<'a, MemoryStrStore>,
} }
impl<'a, S: ReadableEncodedStore> StrContainer for DatasetViewStrContainer<'a, S> { impl<'a, S: ReadableEncodedStore> WithStoreError for DatasetViewStrContainer<'a, S> {
type Error = <S as StrLookup>::Error; type Error = S::Error;
}
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, key: StrHash, value: &str) -> Result<(), Self::Error> {
if self.store.get_str(key)?.is_none() { if self.store.get_str(key)?.is_none() {
self.extra.insert_str(key, value).unwrap(); self.extra.insert_str(key, value).unwrap();

@ -76,10 +76,10 @@ impl Default for MemoryStore {
impl MemoryStore { impl MemoryStore {
/// Constructs a new `MemoryStore` /// Constructs a new `MemoryStore`
pub fn new() -> Self { pub fn new() -> Self {
let mut new = Self { let new = Self {
indexes: Arc::new(RwLock::default()), indexes: Arc::new(RwLock::default()),
}; };
new.set_first_strings().unwrap_infallible(); (&new).set_first_strings().unwrap_infallible();
new new
} }
@ -687,51 +687,28 @@ impl MemoryStore {
} }
} }
impl StrLookup for MemoryStore { impl WithStoreError for MemoryStore {
type Error = Infallible; type Error = 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)
}
} }
impl StrLookup for MemoryStoreIndexes { impl<'a> WithStoreError for &'a MemoryStore {
type Error = Infallible; type Error = Infallible;
}
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 //TODO: avoid copy by adding a lifetime limit to get_str
Ok(self.id2str.get(&id).cloned()) self.indexes().get_str(id)
}
}
impl StrContainer for MemoryStore {
type Error = Infallible;
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.indexes_mut().insert_str(key, value)
} }
} }
impl<'a> StrContainer for &'a MemoryStore { impl<'a> StrContainer for &'a MemoryStore {
type Error = Infallible;
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.indexes_mut().insert_str(key, value) self.indexes_mut().insert_str(key, value)
} }
} }
impl StrContainer for MemoryStoreIndexes {
type Error = Infallible;
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.id2str.entry(key).or_insert_with(|| value.to_owned());
Ok(())
}
}
impl<'a> ReadableEncodedStore for MemoryStore { impl<'a> ReadableEncodedStore for MemoryStore {
type Error = Infallible;
type QuadsIter = EncodedQuadsIter; type QuadsIter = EncodedQuadsIter;
fn encoded_quads_for_pattern( fn encoded_quads_for_pattern(
@ -749,9 +726,7 @@ impl<'a> ReadableEncodedStore for MemoryStore {
} }
} }
impl WritableEncodedStore for MemoryStore { impl<'a> WritableEncodedStore for &'a MemoryStore {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
self.indexes_mut().insert_encoded(quad) self.indexes_mut().insert_encoded(quad)
} }
@ -761,21 +736,25 @@ impl WritableEncodedStore for MemoryStore {
} }
} }
impl<'a> WritableEncodedStore for &'a MemoryStore { impl WithStoreError for MemoryStoreIndexes {
type Error = Infallible; type Error = Infallible;
}
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { impl StrLookup for MemoryStoreIndexes {
self.indexes_mut().insert_encoded(quad) fn get_str(&self, id: StrHash) -> Result<Option<String>, Infallible> {
//TODO: avoid copy by adding a lifetime limit to get_str
Ok(self.id2str.get(&id).cloned())
} }
}
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { impl StrContainer for MemoryStoreIndexes {
self.indexes_mut().remove_encoded(quad) fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.id2str.entry(key).or_insert_with(|| value.to_owned());
Ok(())
} }
} }
impl WritableEncodedStore for MemoryStoreIndexes { impl WritableEncodedStore for MemoryStoreIndexes {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
insert_into_quad_map( insert_into_quad_map(
&mut self.gosp, &mut self.gosp,
@ -1057,9 +1036,11 @@ impl<'a> MemoryTransaction<'a> {
} }
} }
impl StrContainer for MemoryTransaction<'_> { impl WithStoreError for MemoryTransaction<'_> {
type Error = Infallible; type Error = Infallible;
}
impl StrContainer for MemoryTransaction<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.strings.push((key, value.to_owned())); self.strings.push((key, value.to_owned()));
Ok(()) Ok(())
@ -1067,8 +1048,6 @@ impl StrContainer for MemoryTransaction<'_> {
} }
impl WritableEncodedStore for MemoryTransaction<'_> { impl WritableEncodedStore for MemoryTransaction<'_> {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
self.ops.push(TransactionOp::Insert(*quad)); self.ops.push(TransactionOp::Insert(*quad));
Ok(()) Ok(())

@ -34,9 +34,7 @@ use std::io::{BufRead, Write};
use std::iter::Iterator; use std::iter::Iterator;
pub(crate) trait ReadableEncodedStore: StrLookup { pub(crate) trait ReadableEncodedStore: StrLookup {
type Error: From<<Self as StrLookup>::Error> + Error + Into<io::Error> + 'static; type QuadsIter: Iterator<Item = Result<EncodedQuad, Self::Error>> + 'static;
type QuadsIter: Iterator<Item = Result<EncodedQuad, <Self as ReadableEncodedStore>::Error>>
+ 'static;
fn encoded_quads_for_pattern( fn encoded_quads_for_pattern(
&self, &self,
@ -48,17 +46,9 @@ pub(crate) trait ReadableEncodedStore: StrLookup {
} }
pub(crate) trait WritableEncodedStore: StrContainer { pub(crate) trait WritableEncodedStore: StrContainer {
type Error: From<<Self as StrContainer>::Error> + Error + Into<io::Error>; fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Self::Error>;
fn insert_encoded( fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Self::Error>;
&mut self,
quad: &EncodedQuad,
) -> Result<(), <Self as WritableEncodedStore>::Error>;
fn remove_encoded(
&mut self,
quad: &EncodedQuad,
) -> Result<(), <Self as WritableEncodedStore>::Error>;
} }
fn load_graph<S: WritableEncodedStore>( fn load_graph<S: WritableEncodedStore>(

@ -853,15 +853,15 @@ pub fn write_term(sink: &mut Vec<u8>, term: EncodedTerm) {
} }
} }
pub(crate) trait StrLookup { pub(crate) trait WithStoreError {
type Error: Error + Into<io::Error>; type Error: Error + Into<io::Error> + 'static;
}
pub(crate) trait StrLookup: WithStoreError {
fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error>; fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error>;
} }
pub(crate) trait StrContainer { pub(crate) trait StrContainer: WithStoreError {
type Error: Error + Into<io::Error>;
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Self::Error>; fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Self::Error>;
/// Should be called when the bytes store is created /// Should be called when the bytes store is created
@ -901,9 +901,11 @@ impl Default for MemoryStrStore {
} }
} }
impl StrLookup for MemoryStrStore { impl WithStoreError for MemoryStrStore {
type Error = Infallible; type Error = Infallible;
}
impl StrLookup for MemoryStrStore {
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 //TODO: avoid copy by adding a lifetime limit to get_str
Ok(self.id2str.get(&id).cloned()) Ok(self.id2str.get(&id).cloned())
@ -911,17 +913,13 @@ impl StrLookup for MemoryStrStore {
} }
impl StrContainer for MemoryStrStore { impl StrContainer for MemoryStrStore {
type Error = Infallible;
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.id2str.entry(key).or_insert_with(|| value.to_owned()); self.id2str.entry(key).or_insert_with(|| value.to_owned());
Ok(()) Ok(())
} }
} }
pub(crate) trait Encoder { pub(crate) trait Encoder: WithStoreError {
type Error: Error + Into<io::Error>;
fn encode_named_node(&mut self, named_node: &NamedNode) -> Result<EncodedTerm, Self::Error> { fn encode_named_node(&mut self, named_node: &NamedNode) -> Result<EncodedTerm, Self::Error> {
self.encode_rio_named_node(named_node.into()) self.encode_rio_named_node(named_node.into())
} }
@ -1051,8 +1049,6 @@ pub(crate) trait Encoder {
} }
impl<S: StrContainer> Encoder for S { impl<S: StrContainer> Encoder for S {
type Error = S::Error;
fn encode_rio_named_node( fn encode_rio_named_node(
&mut self, &mut self,
named_node: rio::NamedNode<'_>, named_node: rio::NamedNode<'_>,

@ -467,9 +467,11 @@ impl fmt::Display for RocksDbStore {
} }
} }
impl StrLookup for RocksDbStore { impl WithStoreError for RocksDbStore {
type Error = io::Error; type Error = io::Error;
}
impl StrLookup for RocksDbStore {
fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> { fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> {
self.db self.db
.get_cf(self.id2str_cf(), &id.to_be_bytes()) .get_cf(self.id2str_cf(), &id.to_be_bytes())
@ -481,7 +483,6 @@ impl StrLookup for RocksDbStore {
} }
impl ReadableEncodedStore for RocksDbStore { impl ReadableEncodedStore for RocksDbStore {
type Error = io::Error;
type QuadsIter = DecodingIndexIterator; type QuadsIter = DecodingIndexIterator;
fn encoded_quads_for_pattern( fn encoded_quads_for_pattern(
@ -622,9 +623,11 @@ struct BatchWriter<'a> {
buffer: Vec<u8>, buffer: Vec<u8>,
} }
impl StrContainer for BatchWriter<'_> { impl WithStoreError for BatchWriter<'_> {
type Error = Infallible; type Error = Infallible;
}
impl StrContainer for BatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
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);
@ -633,8 +636,6 @@ impl StrContainer for BatchWriter<'_> {
} }
impl WritableEncodedStore for BatchWriter<'_> { impl WritableEncodedStore for BatchWriter<'_> {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
write_spog_quad(&mut self.buffer, quad); write_spog_quad(&mut self.buffer, quad);
self.batch.put_cf(self.store.spog_cf(), &self.buffer, &[]); self.batch.put_cf(self.store.spog_cf(), &self.buffer, &[]);
@ -702,17 +703,17 @@ struct AutoBatchWriter<'a> {
inner: BatchWriter<'a>, inner: BatchWriter<'a>,
} }
impl StrContainer for AutoBatchWriter<'_> { impl WithStoreError for AutoBatchWriter<'_> {
type Error = io::Error; type Error = io::Error;
}
impl StrContainer for AutoBatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> {
Ok(self.inner.insert_str(key, value)?) Ok(self.inner.insert_str(key, value)?)
} }
} }
impl WritableEncodedStore for AutoBatchWriter<'_> { impl WritableEncodedStore for AutoBatchWriter<'_> {
type Error = io::Error;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
self.inner.insert_encoded(quad)?; self.inner.insert_encoded(quad)?;
self.apply_if_big() self.apply_if_big()

@ -385,9 +385,11 @@ impl fmt::Display for SledStore {
} }
} }
impl StrLookup for SledStore { impl WithStoreError for SledStore {
type Error = io::Error; type Error = io::Error;
}
impl StrLookup for SledStore {
fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> { fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> {
self.id2str self.id2str
.get(id.to_be_bytes())? .get(id.to_be_bytes())?
@ -398,7 +400,6 @@ impl StrLookup for SledStore {
} }
impl ReadableEncodedStore for SledStore { impl ReadableEncodedStore for SledStore {
type Error = io::Error;
type QuadsIter = DecodingQuadIterator; type QuadsIter = DecodingQuadIterator;
fn encoded_quads_for_pattern( fn encoded_quads_for_pattern(
@ -483,9 +484,11 @@ impl<'a> DirectWriter<'a> {
} }
} }
impl<'a> StrContainer for DirectWriter<'a> { impl WithStoreError for DirectWriter<'_> {
type Error = io::Error; type Error = io::Error;
}
impl<'a> StrContainer for DirectWriter<'a> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> {
self.store self.store
.id2str .id2str
@ -495,8 +498,6 @@ impl<'a> StrContainer for DirectWriter<'a> {
} }
impl<'a> WritableEncodedStore for DirectWriter<'a> { impl<'a> WritableEncodedStore for DirectWriter<'a> {
type Error = io::Error;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
write_spog_quad(&mut self.buffer, quad); write_spog_quad(&mut self.buffer, quad);
self.store.quads.insert(self.buffer.as_slice(), &[])?; self.store.quads.insert(self.buffer.as_slice(), &[])?;
@ -580,9 +581,11 @@ impl<'a> BatchWriter<'a> {
} }
} }
impl<'a> StrContainer for BatchWriter<'a> { impl WithStoreError for BatchWriter<'_> {
type Error = Infallible; type Error = Infallible;
}
impl<'a> StrContainer for BatchWriter<'a> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.id2str.insert(key.to_be_bytes().as_ref(), value); self.id2str.insert(key.to_be_bytes().as_ref(), value);
Ok(()) Ok(())
@ -590,8 +593,6 @@ impl<'a> StrContainer for BatchWriter<'a> {
} }
impl<'a> WritableEncodedStore for BatchWriter<'a> { impl<'a> WritableEncodedStore for BatchWriter<'a> {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
write_spog_quad(&mut self.buffer, quad); write_spog_quad(&mut self.buffer, quad);
self.quads.insert(self.buffer.as_slice(), &[]); self.quads.insert(self.buffer.as_slice(), &[]);

Loading…
Cancel
Save