From ce2b05b62bcfe7ffe60b3901f58e05410c127ffc Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 22 Jul 2020 22:43:40 +0200 Subject: [PATCH] Makes TermReader use IoError --- lib/src/store/numeric_encoder.rs | 37 +++++++++++++++++--------------- lib/src/store/rocksdb.rs | 2 +- lib/src/store/sled.rs | 4 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/src/store/numeric_encoder.rs b/lib/src/store/numeric_encoder.rs index fed6e981..80a91bee 100644 --- a/lib/src/store/numeric_encoder.rs +++ b/lib/src/store/numeric_encoder.rs @@ -12,7 +12,7 @@ use siphasher::sip128::{Hasher128, SipHasher24}; use std::collections::HashMap; use std::hash::Hash; use std::hash::Hasher; -use std::io::{Cursor, Read}; +use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult}; use std::mem::size_of; use std::str; @@ -607,17 +607,17 @@ impl From<&Quad> for EncodedQuad { } pub trait TermReader { - fn read_term(&mut self) -> Result; - fn read_spog_quad(&mut self) -> Result; - fn read_posg_quad(&mut self) -> Result; - fn read_ospg_quad(&mut self) -> Result; - fn read_gspo_quad(&mut self) -> Result; - fn read_gpos_quad(&mut self) -> Result; - fn read_gosp_quad(&mut self) -> Result; + fn read_term(&mut self) -> IoResult; + fn read_spog_quad(&mut self) -> IoResult; + fn read_posg_quad(&mut self) -> IoResult; + fn read_ospg_quad(&mut self) -> IoResult; + fn read_gspo_quad(&mut self) -> IoResult; + fn read_gpos_quad(&mut self) -> IoResult; + fn read_gosp_quad(&mut self) -> IoResult; } impl TermReader for R { - fn read_term(&mut self) -> Result { + fn read_term(&mut self) -> IoResult { let mut type_buffer = [0]; self.read_exact(&mut type_buffer)?; match type_buffer[0] { @@ -730,11 +730,14 @@ impl TermReader for R { DayTimeDuration::from_be_bytes(buffer), )) } - _ => Err(Error::msg("the term buffer has an invalid type id")), + _ => Err(IoError::new( + ErrorKind::InvalidData, + "the term buffer has an invalid type id", + )), } } - fn read_spog_quad(&mut self) -> Result { + fn read_spog_quad(&mut self) -> IoResult { let subject = self.read_term()?; let predicate = self.read_term()?; let object = self.read_term()?; @@ -747,7 +750,7 @@ impl TermReader for R { }) } - fn read_posg_quad(&mut self) -> Result { + fn read_posg_quad(&mut self) -> IoResult { let predicate = self.read_term()?; let object = self.read_term()?; let subject = self.read_term()?; @@ -760,7 +763,7 @@ impl TermReader for R { }) } - fn read_ospg_quad(&mut self) -> Result { + fn read_ospg_quad(&mut self) -> IoResult { let object = self.read_term()?; let subject = self.read_term()?; let predicate = self.read_term()?; @@ -773,7 +776,7 @@ impl TermReader for R { }) } - fn read_gspo_quad(&mut self) -> Result { + fn read_gspo_quad(&mut self) -> IoResult { let graph_name = self.read_term()?; let subject = self.read_term()?; let predicate = self.read_term()?; @@ -786,7 +789,7 @@ impl TermReader for R { }) } - fn read_gpos_quad(&mut self) -> Result { + fn read_gpos_quad(&mut self) -> IoResult { let graph_name = self.read_term()?; let predicate = self.read_term()?; let object = self.read_term()?; @@ -799,7 +802,7 @@ impl TermReader for R { }) } - fn read_gosp_quad(&mut self) -> Result { + fn read_gosp_quad(&mut self) -> IoResult { let graph_name = self.read_term()?; let object = self.read_term()?; let subject = self.read_term()?; @@ -906,7 +909,7 @@ pub enum QuadEncoding { } impl QuadEncoding { - pub fn decode(self, buffer: &[u8]) -> Result { + pub fn decode(self, buffer: &[u8]) -> IoResult { let mut cursor = Cursor::new(&buffer); match self { QuadEncoding::SPOG => cursor.read_spog_quad(), diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index a580b908..488740c5 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -759,7 +759,7 @@ impl<'a> Iterator for DecodingIndexIterator<'a> { fn next(&mut self) -> Option> { if let Some(key) = self.iter.key() { if key.starts_with(&self.prefix) { - let result = self.encoding.decode(key); + let result = self.encoding.decode(key).map_err(crate::Error::from); self.iter.next(); Some(result) } else { diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index d3a5bbb3..40f5f963 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -4,7 +4,7 @@ use crate::model::*; use crate::sparql::{GraphPattern, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::store::numeric_encoder::*; use crate::store::{load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore}; -use crate::{DatasetSyntax, GraphSyntax, Result}; +use crate::{DatasetSyntax, Error, GraphSyntax, Result}; use sled::{Config, Iter, Tree}; use std::io::BufRead; use std::path::Path; @@ -556,7 +556,7 @@ impl Iterator for DecodingQuadIterator { fn next(&mut self) -> Option> { Some(match self.iter.next()? { - Ok((encoded, _)) => self.order.decode(&encoded), + Ok((encoded, _)) => self.order.decode(&encoded).map_err(Error::from), Err(error) => Err(error.into()), }) }