Relaxes error bound from Into<Io::Error> to Into<EvaluationError>

pull/46/head
Tpt 4 years ago
parent 81743f464e
commit 3d07160167
  1. 42
      lib/src/error.rs
  2. 16
      lib/src/sparql/error.rs
  3. 13
      lib/src/sparql/plan.rs
  4. 16
      lib/src/store/memory.rs
  5. 80
      lib/src/store/mod.rs
  6. 6
      lib/src/store/numeric_encoder.rs
  7. 17
      lib/src/store/rocksdb.rs
  8. 16
      lib/src/store/sled.rs

@ -1,17 +1,12 @@
use std::convert::Infallible;
use std::error::Error;
use std::{fmt, io};
use std::io;
//TODO: convert to "!" when "never_type" is going to be stabilized
#[allow(clippy::empty_enum)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub(crate) enum Infallible {}
impl Error for Infallible {}
/// Traits that allows unwrapping only infallible results
pub(crate) trait UnwrapInfallible {
type Value;
impl fmt::Display for Infallible {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {}
}
fn unwrap_infallible(self) -> Self::Value;
}
impl<T> UnwrapInfallible for Result<T, Infallible> {
@ -25,31 +20,6 @@ impl<T> UnwrapInfallible for Result<T, Infallible> {
}
}
/// Traits that allows unwrapping only infallible results
pub(crate) trait UnwrapInfallible {
type Value;
fn unwrap_infallible(self) -> Self::Value;
}
impl From<std::convert::Infallible> for Infallible {
fn from(error: std::convert::Infallible) -> Self {
match error {}
}
}
impl From<Infallible> for std::io::Error {
fn from(error: Infallible) -> Self {
match error {}
}
}
impl From<Infallible> for std::convert::Infallible {
fn from(error: Infallible) -> Self {
match error {}
}
}
pub(crate) fn invalid_data_error(error: impl Into<Box<dyn Error + Send + Sync>>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, error)
}

@ -1,6 +1,7 @@
use crate::error::Infallible;
use crate::error::invalid_data_error;
use crate::sparql::ParseError;
use crate::store::numeric_encoder::DecoderError;
use std::convert::Infallible;
use std::error;
use std::fmt;
use std::io;
@ -91,12 +92,6 @@ impl From<Infallible> for EvaluationError {
}
}
impl From<std::convert::Infallible> for EvaluationError {
fn from(error: std::convert::Infallible) -> Self {
match error {}
}
}
impl From<ParseError> for EvaluationError {
fn from(error: ParseError) -> Self {
Self::Parsing(error)
@ -109,8 +104,11 @@ impl From<io::Error> for EvaluationError {
}
}
impl<E: Into<io::Error>> From<DecoderError<E>> for EvaluationError {
impl<E: Into<EvaluationError>> From<DecoderError<E>> for EvaluationError {
fn from(error: DecoderError<E>) -> Self {
io::Error::from(error).into()
match error {
DecoderError::Store(error) => error.into(),
DecoderError::Decoder { msg } => invalid_data_error(msg).into(),
}
}
}

@ -9,7 +9,6 @@ use crate::store::numeric_encoder::{
use crate::store::ReadableEncodedStore;
use std::cell::{RefCell, RefMut};
use std::collections::BTreeSet;
use std::io;
use std::rc::Rc;
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
@ -584,7 +583,7 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
) -> Box<dyn Iterator<Item = Result<EncodedQuad, EvaluationError>>> {
if graph_name == None {
Box::new(
map_io_err(
map_iter_err(
self.store
.encoded_quads_for_pattern(subject, predicate, object, None),
)
@ -595,7 +594,7 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
)
} else if graph_name == Some(EncodedTerm::DefaultGraph) && self.default_graph_as_union {
Box::new(
map_io_err(
map_iter_err(
self.store
.encoded_quads_for_pattern(subject, predicate, object, None),
)
@ -610,7 +609,7 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
}),
)
} else {
Box::new(map_io_err(self.store.encoded_quads_for_pattern(
Box::new(map_iter_err(self.store.encoded_quads_for_pattern(
subject, predicate, object, graph_name,
)))
}
@ -624,10 +623,10 @@ impl<S: ReadableEncodedStore> DatasetView<S> {
}
}
fn map_io_err<'a, T>(
iter: impl Iterator<Item = Result<T, impl Into<io::Error>>> + 'a,
fn map_iter_err<'a, T>(
iter: impl Iterator<Item = Result<T, impl Into<EvaluationError>>> + 'a,
) -> impl Iterator<Item = Result<T, EvaluationError>> + 'a {
iter.map(|e| e.map_err(|e| e.into().into()))
iter.map(|e| e.map_err(|e| e.into()))
}
impl<S: ReadableEncodedStore> WithStoreError for DatasetView<S> {

@ -1,6 +1,6 @@
//! In-memory store.
use crate::error::{Infallible, UnwrapInfallible};
use crate::error::UnwrapInfallible;
use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*;
use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery};
@ -10,7 +10,7 @@ use crate::store::{
};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::convert::{Infallible, TryInto};
use std::hash::{BuildHasherDefault, Hash, Hasher};
use std::io::{BufRead, Write};
use std::iter::FromIterator;
@ -276,7 +276,8 @@ impl MemoryStore {
base_iri: Option<&str>,
) -> Result<(), io::Error> {
let mut store = self;
load_graph(&mut store, reader, format, to_graph_name, base_iri)
load_graph(&mut store, reader, format, to_graph_name, base_iri)?;
Ok(())
}
/// Loads a dataset file (i.e. quads) into the store.
@ -309,7 +310,8 @@ impl MemoryStore {
base_iri: Option<&str>,
) -> Result<(), io::Error> {
let mut store = self;
load_dataset(&mut store, reader, format, base_iri)
load_dataset(&mut store, reader, format, base_iri)?;
Ok(())
}
/// Adds a quad to this store.
@ -977,7 +979,8 @@ impl<'a> MemoryTransaction<'a> {
to_graph_name: &GraphName,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_graph(self, reader, format, to_graph_name, base_iri)
load_graph(self, reader, format, to_graph_name, base_iri)?;
Ok(())
}
/// Loads a dataset file (i.e. quads) into the store during the transaction.
@ -1006,7 +1009,8 @@ impl<'a> MemoryTransaction<'a> {
format: DatasetFormat,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_dataset(self, reader, format, base_iri)
load_dataset(self, reader, format, base_iri)?;
Ok(())
}
/// Adds a quad to this store during the transaction.

@ -28,7 +28,7 @@ use rio_turtle::{
};
use rio_xml::{RdfXmlError, RdfXmlFormatter, RdfXmlParser};
use std::collections::HashMap;
use std::error::Error;
use std::convert::Infallible;
use std::io;
use std::io::{BufRead, Write};
use std::iter::Iterator;
@ -57,7 +57,7 @@ fn load_graph<S: WritableEncodedStore>(
format: GraphFormat,
to_graph_name: &GraphName,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
) -> Result<(), StoreOrParseError<S::Error, io::Error>> {
let base_iri = base_iri.unwrap_or("");
match format {
GraphFormat::NTriples => {
@ -76,26 +76,30 @@ fn load_from_triple_parser<S: WritableEncodedStore, P: TriplesParser>(
store: &mut S,
parser: Result<P, P::Error>,
to_graph_name: &GraphName,
) -> Result<(), io::Error>
) -> Result<(), StoreOrParseError<S::Error, io::Error>>
where
IoOrParseError<P::Error>: From<P::Error>,
StoreOrParseError<S::Error, P::Error>: From<P::Error>,
P::Error: Send + Sync + 'static,
{
let mut parser = parser.map_err(invalid_input_error)?;
let mut bnode_map = HashMap::default();
let to_graph_name = store
.encode_graph_name(to_graph_name)
.map_err(|e| e.into())?;
let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |t| {
.map_err(StoreOrParseError::Store)?;
parser
.parse_all(&mut move |t| {
let quad = store
.encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map)
.map_err(|e| IoOrParseError::Io(e.into()))?;
.map_err(StoreOrParseError::Store)?;
store
.insert_encoded(&quad)
.map_err(|e| IoOrParseError::Io(e.into()))?;
.map_err(StoreOrParseError::Store)?;
Ok(())
});
Ok(result?)
})
.map_err(|e| match e {
StoreOrParseError::Store(e) => StoreOrParseError::Store(e),
StoreOrParseError::Parse(e) => StoreOrParseError::Parse(invalid_data_error(e)),
})
}
fn dump_graph(
@ -138,7 +142,7 @@ fn load_dataset<S: WritableEncodedStore>(
reader: impl BufRead,
format: DatasetFormat,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
) -> Result<(), StoreOrParseError<S::Error, io::Error>> {
let base_iri = base_iri.unwrap_or("");
match format {
DatasetFormat::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)),
@ -149,23 +153,27 @@ fn load_dataset<S: WritableEncodedStore>(
fn load_from_quad_parser<S: WritableEncodedStore, P: QuadsParser>(
store: &mut S,
parser: Result<P, P::Error>,
) -> Result<(), io::Error>
) -> Result<(), StoreOrParseError<S::Error, io::Error>>
where
IoOrParseError<P::Error>: From<P::Error>,
StoreOrParseError<S::Error, P::Error>: From<P::Error>,
P::Error: Send + Sync + 'static,
{
let mut parser = parser.map_err(invalid_input_error)?;
let mut bnode_map = HashMap::default();
let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |q| {
parser
.parse_all(&mut move |q| {
let quad = store
.encode_rio_quad(q, &mut bnode_map)
.map_err(|e| IoOrParseError::Io(e.into()))?;
.map_err(StoreOrParseError::Store)?;
store
.insert_encoded(&quad)
.map_err(|e| IoOrParseError::Io(e.into()))?;
.map_err(StoreOrParseError::Store)?;
Ok(())
});
Ok(result?)
})
.map_err(|e| match e {
StoreOrParseError::Store(e) => StoreOrParseError::Store(e),
StoreOrParseError::Parse(e) => StoreOrParseError::Parse(invalid_data_error(e)),
})
}
fn dump_dataset(
@ -192,34 +200,42 @@ fn dump_dataset(
Ok(())
}
enum IoOrParseError<E: Sized> {
Io(io::Error),
Parse(E),
enum StoreOrParseError<S, P> {
Store(S),
Parse(P),
}
impl<E: Sized> From<io::Error> for IoOrParseError<E> {
fn from(error: io::Error) -> Self {
Self::Io(error)
impl<S> From<TurtleError> for StoreOrParseError<S, TurtleError> {
fn from(error: TurtleError) -> Self {
Self::Parse(error)
}
}
impl From<TurtleError> for IoOrParseError<TurtleError> {
fn from(error: TurtleError) -> Self {
impl<S> From<RdfXmlError> for StoreOrParseError<S, RdfXmlError> {
fn from(error: RdfXmlError) -> Self {
Self::Parse(error)
}
}
impl From<RdfXmlError> for IoOrParseError<RdfXmlError> {
fn from(error: RdfXmlError) -> Self {
impl<S> From<io::Error> for StoreOrParseError<S, io::Error> {
fn from(error: io::Error) -> Self {
Self::Parse(error)
}
}
impl<E: Sized + Error + Send + Sync + 'static> From<IoOrParseError<E>> for io::Error {
fn from(error: IoOrParseError<E>) -> Self {
impl<P: Into<io::Error>> From<StoreOrParseError<io::Error, P>> for io::Error {
fn from(error: StoreOrParseError<io::Error, P>) -> Self {
match error {
StoreOrParseError::Store(error) => error,
StoreOrParseError::Parse(error) => error.into(),
}
}
}
impl<P: Into<io::Error>> From<StoreOrParseError<Infallible, P>> for io::Error {
fn from(error: StoreOrParseError<Infallible, P>) -> Self {
match error {
IoOrParseError::Io(error) => error,
IoOrParseError::Parse(error) => invalid_data_error(error),
StoreOrParseError::Store(error) => match error {},
StoreOrParseError::Parse(error) => error.into(),
}
}
}

@ -1,12 +1,14 @@
#![allow(clippy::unreadable_literal)]
use crate::error::{invalid_data_error, Infallible};
use crate::error::invalid_data_error;
use crate::model::xsd::*;
use crate::model::*;
use crate::sparql::EvaluationError;
use rand::random;
use rio_api::model as rio;
use siphasher::sip128::{Hasher128, SipHasher24};
use std::collections::HashMap;
use std::convert::Infallible;
use std::error::Error;
use std::hash::Hash;
use std::hash::Hasher;
@ -763,7 +765,7 @@ pub fn write_term(sink: &mut Vec<u8>, term: EncodedTerm) {
}
pub(crate) trait WithStoreError {
type Error: Error + Into<io::Error> + 'static;
type Error: Error + Into<EvaluationError> + 'static;
}
pub(crate) trait StrLookup: WithStoreError {

@ -1,6 +1,6 @@
//! Store based on the [RocksDB](https://rocksdb.org/) key-value database.
use crate::error::{invalid_data_error, Infallible, UnwrapInfallible};
use crate::error::{invalid_data_error, UnwrapInfallible};
use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*;
use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery};
@ -9,7 +9,7 @@ use crate::store::{
dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore,
};
use rocksdb::*;
use std::convert::TryInto;
use std::convert::{Infallible, TryInto};
use std::io;
use std::io::{BufRead, Cursor, Write};
use std::mem::{take, transmute};
@ -576,7 +576,8 @@ impl RocksDbTransaction<'_> {
to_graph_name: &GraphName,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_graph(&mut self.inner, reader, syntax, to_graph_name, base_iri)
load_graph(&mut self.inner, reader, syntax, to_graph_name, base_iri)?;
Ok(())
}
/// Loads a dataset file (i.e. quads) into the store. into the store during the transaction.
@ -595,7 +596,8 @@ impl RocksDbTransaction<'_> {
format: DatasetFormat,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_dataset(&mut self.inner, reader, format, base_iri)
load_dataset(&mut self.inner, reader, format, base_iri)?;
Ok(())
}
/// Adds a quad to this store during the transaction.
@ -703,18 +705,19 @@ impl WithStoreError for AutoBatchWriter<'_> {
impl StrContainer for AutoBatchWriter<'_> {
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> {
Ok(self.inner.insert_str(key, value)?)
self.inner.insert_str(key, value).unwrap_infallible();
Ok(())
}
}
impl WritableEncodedStore for AutoBatchWriter<'_> {
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
self.inner.insert_encoded(quad)?;
self.inner.insert_encoded(quad).unwrap_infallible();
self.apply_if_big()
}
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> {
self.inner.remove_encoded(quad)?;
self.inner.remove_encoded(quad).unwrap_infallible();
self.apply_if_big()
}
}

@ -1,6 +1,6 @@
//! Store based on the [Sled](https://sled.rs/) key-value database.
use crate::error::{invalid_data_error, Infallible, UnwrapInfallible};
use crate::error::{invalid_data_error, UnwrapInfallible};
use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*;
use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery};
@ -9,7 +9,7 @@ use crate::store::{
dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore,
};
use sled::{Batch, Config, Iter, Tree};
use std::convert::TryInto;
use std::convert::{Infallible, TryInto};
use std::io::{BufRead, Cursor, Write};
use std::path::Path;
use std::{fmt, io, str};
@ -184,7 +184,8 @@ impl SledStore {
format,
to_graph_name,
base_iri,
)
)?;
Ok(())
}
/// Loads a dataset file (i.e. quads) into the store.
@ -203,7 +204,8 @@ impl SledStore {
format: DatasetFormat,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_dataset(&mut DirectWriter::new(self), reader, format, base_iri)
load_dataset(&mut DirectWriter::new(self), reader, format, base_iri)?;
Ok(())
}
/// Adds a quad to this store.
@ -671,7 +673,8 @@ impl SledTransaction<'_> {
to_graph_name: &GraphName,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_graph(&mut self.inner, reader, format, to_graph_name, base_iri)
load_graph(&mut self.inner, reader, format, to_graph_name, base_iri)?;
Ok(())
}
/// Loads a dataset file (i.e. quads) into the store. into the store during the transaction.
@ -690,7 +693,8 @@ impl SledTransaction<'_> {
format: DatasetFormat,
base_iri: Option<&str>,
) -> Result<(), io::Error> {
load_dataset(&mut self.inner, reader, format, base_iri)
load_dataset(&mut self.inner, reader, format, base_iri)?;
Ok(())
}
/// Adds a quad to this store during the transaction.

Loading…
Cancel
Save