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::store::numeric_encoder::{
EncodedQuad, EncodedTerm, Encoder, MemoryStrStore, StrContainer, StrHash, StrLookup,
ENCODED_DEFAULT_GRAPH,
WithStoreError, ENCODED_DEFAULT_GRAPH,
};
use crate::store::ReadableEncodedStore;
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()))
}
impl<S: ReadableEncodedStore> WithStoreError for DatasetView<S> {
type Error = S::Error;
}
impl<S: ReadableEncodedStore> StrLookup for DatasetView<S> {
type Error = <S as StrLookup>::Error;
fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error> {
if let Some(value) = self.extra.borrow().get_str(id).unwrap_infallible() {
Ok(Some(value))
@ -647,9 +648,11 @@ struct DatasetViewStrContainer<'a, S: ReadableEncodedStore> {
extra: RefMut<'a, MemoryStrStore>,
}
impl<'a, S: ReadableEncodedStore> StrContainer for DatasetViewStrContainer<'a, S> {
type Error = <S as StrLookup>::Error;
impl<'a, S: ReadableEncodedStore> WithStoreError for DatasetViewStrContainer<'a, S> {
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> {
if self.store.get_str(key)?.is_none() {
self.extra.insert_str(key, value).unwrap();

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

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

@ -853,15 +853,15 @@ pub fn write_term(sink: &mut Vec<u8>, term: EncodedTerm) {
}
}
pub(crate) trait StrLookup {
type Error: Error + Into<io::Error>;
pub(crate) trait WithStoreError {
type Error: Error + Into<io::Error> + 'static;
}
pub(crate) trait StrLookup: WithStoreError {
fn get_str(&self, id: StrHash) -> Result<Option<String>, Self::Error>;
}
pub(crate) trait StrContainer {
type Error: Error + Into<io::Error>;
pub(crate) trait StrContainer: WithStoreError {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Self::Error>;
/// 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;
}
impl StrLookup for MemoryStrStore {
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())
@ -911,17 +913,13 @@ impl StrLookup for MemoryStrStore {
}
impl StrContainer for MemoryStrStore {
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(())
}
}
pub(crate) trait Encoder {
type Error: Error + Into<io::Error>;
pub(crate) trait Encoder: WithStoreError {
fn encode_named_node(&mut self, named_node: &NamedNode) -> Result<EncodedTerm, Self::Error> {
self.encode_rio_named_node(named_node.into())
}
@ -1051,8 +1049,6 @@ pub(crate) trait Encoder {
}
impl<S: StrContainer> Encoder for S {
type Error = S::Error;
fn encode_rio_named_node(
&mut self,
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;
}
impl StrLookup for RocksDbStore {
fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> {
self.db
.get_cf(self.id2str_cf(), &id.to_be_bytes())
@ -481,7 +483,6 @@ impl StrLookup for RocksDbStore {
}
impl ReadableEncodedStore for RocksDbStore {
type Error = io::Error;
type QuadsIter = DecodingIndexIterator;
fn encoded_quads_for_pattern(
@ -622,9 +623,11 @@ struct BatchWriter<'a> {
buffer: Vec<u8>,
}
impl StrContainer for BatchWriter<'_> {
impl WithStoreError for BatchWriter<'_> {
type Error = Infallible;
}
impl StrContainer for BatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.batch
.put_cf(self.store.id2str_cf(), &key.to_be_bytes(), value);
@ -633,8 +636,6 @@ impl StrContainer for BatchWriter<'_> {
}
impl WritableEncodedStore for BatchWriter<'_> {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
write_spog_quad(&mut self.buffer, quad);
self.batch.put_cf(self.store.spog_cf(), &self.buffer, &[]);
@ -702,17 +703,17 @@ struct AutoBatchWriter<'a> {
inner: BatchWriter<'a>,
}
impl StrContainer for AutoBatchWriter<'_> {
impl WithStoreError for AutoBatchWriter<'_> {
type Error = io::Error;
}
impl StrContainer for AutoBatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> {
Ok(self.inner.insert_str(key, value)?)
}
}
impl WritableEncodedStore for AutoBatchWriter<'_> {
type Error = io::Error;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
self.inner.insert_encoded(quad)?;
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;
}
impl StrLookup for SledStore {
fn get_str(&self, id: StrHash) -> Result<Option<String>, io::Error> {
self.id2str
.get(id.to_be_bytes())?
@ -398,7 +400,6 @@ impl StrLookup for SledStore {
}
impl ReadableEncodedStore for SledStore {
type Error = io::Error;
type QuadsIter = DecodingQuadIterator;
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;
}
impl<'a> StrContainer for DirectWriter<'a> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> {
self.store
.id2str
@ -495,8 +498,6 @@ impl<'a> StrContainer for DirectWriter<'a> {
}
impl<'a> WritableEncodedStore for DirectWriter<'a> {
type Error = io::Error;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
write_spog_quad(&mut self.buffer, quad);
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;
}
impl<'a> StrContainer for BatchWriter<'a> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> {
self.id2str.insert(key.to_be_bytes().as_ref(), value);
Ok(())
@ -590,8 +593,6 @@ impl<'a> StrContainer for BatchWriter<'a> {
}
impl<'a> WritableEncodedStore for BatchWriter<'a> {
type Error = Infallible;
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> {
write_spog_quad(&mut self.buffer, quad);
self.quads.insert(self.buffer.as_slice(), &[]);

Loading…
Cancel
Save