Uses std::io::Result where relevant

pull/171/head
Tpt 3 years ago
parent 3307ec160e
commit c18281183a
  1. 16
      lib/src/io/read.rs
  2. 12
      lib/src/io/write.rs
  3. 10
      lib/src/model/dataset.rs
  4. 4
      lib/src/model/graph.rs
  5. 4
      lib/src/sparql/csv_results.rs
  6. 8
      lib/src/sparql/json_results.rs
  7. 5
      lib/src/sparql/model.rs
  8. 2
      lib/src/sparql/xml_results.rs
  9. 26
      lib/src/storage/binary_encoder.rs
  10. 8
      lib/src/storage/io.rs
  11. 54
      lib/src/storage/mod.rs
  12. 45
      lib/src/store.rs

@ -66,7 +66,7 @@ impl GraphParser {
} }
/// Executes the parsing itself on a [`BufRead`](std::io::BufRead) implementation and returns an iterator of triples /// Executes the parsing itself on a [`BufRead`](std::io::BufRead) implementation and returns an iterator of triples
pub fn read_triples<R: BufRead>(&self, reader: R) -> Result<TripleReader<R>, io::Error> { pub fn read_triples<R: BufRead>(&self, reader: R) -> io::Result<TripleReader<R>> {
Ok(TripleReader { Ok(TripleReader {
mapper: RioMapper::default(), mapper: RioMapper::default(),
parser: match self.format { parser: match self.format {
@ -113,9 +113,9 @@ enum TripleReaderKind<R: BufRead> {
} }
impl<R: BufRead> Iterator for TripleReader<R> { impl<R: BufRead> Iterator for TripleReader<R> {
type Item = Result<Triple, io::Error>; type Item = io::Result<Triple>;
fn next(&mut self) -> Option<Result<Triple, io::Error>> { fn next(&mut self) -> Option<io::Result<Triple>> {
loop { loop {
if let Some(r) = self.buffer.pop() { if let Some(r) = self.buffer.pop() {
return Some(Ok(r)); return Some(Ok(r));
@ -143,7 +143,7 @@ impl<R: BufRead> TripleReader<R> {
parser: &mut P, parser: &mut P,
buffer: &mut Vec<Triple>, buffer: &mut Vec<Triple>,
mapper: &mut RioMapper, mapper: &mut RioMapper,
) -> Option<Result<(), io::Error>> ) -> Option<io::Result<()>>
where where
io::Error: From<P::Error>, io::Error: From<P::Error>,
{ {
@ -214,7 +214,7 @@ impl DatasetParser {
} }
/// Executes the parsing itself on a [`BufRead`](std::io::BufRead) implementation and returns an iterator of quads /// Executes the parsing itself on a [`BufRead`](std::io::BufRead) implementation and returns an iterator of quads
pub fn read_quads<R: BufRead>(&self, reader: R) -> Result<QuadReader<R>, io::Error> { pub fn read_quads<R: BufRead>(&self, reader: R) -> io::Result<QuadReader<R>> {
Ok(QuadReader { Ok(QuadReader {
mapper: RioMapper::default(), mapper: RioMapper::default(),
parser: match self.format { parser: match self.format {
@ -257,9 +257,9 @@ enum QuadReaderKind<R: BufRead> {
} }
impl<R: BufRead> Iterator for QuadReader<R> { impl<R: BufRead> Iterator for QuadReader<R> {
type Item = Result<Quad, io::Error>; type Item = io::Result<Quad>;
fn next(&mut self) -> Option<Result<Quad, io::Error>> { fn next(&mut self) -> Option<io::Result<Quad>> {
loop { loop {
if let Some(r) = self.buffer.pop() { if let Some(r) = self.buffer.pop() {
return Some(Ok(r)); return Some(Ok(r));
@ -284,7 +284,7 @@ impl<R: BufRead> QuadReader<R> {
parser: &mut P, parser: &mut P,
buffer: &mut Vec<Quad>, buffer: &mut Vec<Quad>,
mapper: &mut RioMapper, mapper: &mut RioMapper,
) -> Option<Result<(), io::Error>> ) -> Option<io::Result<()>>
where where
io::Error: From<P::Error>, io::Error: From<P::Error>,
{ {

@ -43,7 +43,7 @@ impl GraphSerializer {
} }
/// Returns a `TripleWriter` allowing writing triples into the given [`Write`](std::io::Write) implementation /// Returns a `TripleWriter` allowing writing triples into the given [`Write`](std::io::Write) implementation
pub fn triple_writer<W: Write>(&self, writer: W) -> Result<TripleWriter<W>, io::Error> { pub fn triple_writer<W: Write>(&self, writer: W) -> io::Result<TripleWriter<W>> {
Ok(TripleWriter { Ok(TripleWriter {
formatter: match self.format { formatter: match self.format {
GraphFormat::NTriples => TripleWriterKind::NTriples(NTriplesFormatter::new(writer)), GraphFormat::NTriples => TripleWriterKind::NTriples(NTriplesFormatter::new(writer)),
@ -88,7 +88,7 @@ enum TripleWriterKind<W: Write> {
impl<W: Write> TripleWriter<W> { impl<W: Write> TripleWriter<W> {
/// Writes a triple /// Writes a triple
pub fn write<'a>(&mut self, triple: impl Into<TripleRef<'a>>) -> Result<(), io::Error> { pub fn write<'a>(&mut self, triple: impl Into<TripleRef<'a>>) -> io::Result<()> {
let triple = triple.into(); let triple = triple.into();
match &mut self.formatter { match &mut self.formatter {
TripleWriterKind::NTriples(formatter) => formatter.format(&triple.into())?, TripleWriterKind::NTriples(formatter) => formatter.format(&triple.into())?,
@ -99,7 +99,7 @@ impl<W: Write> TripleWriter<W> {
} }
/// Writes the last bytes of the file /// Writes the last bytes of the file
pub fn finish(self) -> Result<(), io::Error> { pub fn finish(self) -> io::Result<()> {
match self.formatter { match self.formatter {
TripleWriterKind::NTriples(formatter) => formatter.finish(), TripleWriterKind::NTriples(formatter) => formatter.finish(),
TripleWriterKind::Turtle(formatter) => formatter.finish()?, TripleWriterKind::Turtle(formatter) => formatter.finish()?,
@ -144,7 +144,7 @@ impl DatasetSerializer {
} }
/// Returns a `QuadWriter` allowing writing triples into the given [`Write`](std::io::Write) implementation /// Returns a `QuadWriter` allowing writing triples into the given [`Write`](std::io::Write) implementation
pub fn quad_writer<W: Write>(&self, writer: W) -> Result<QuadWriter<W>, io::Error> { pub fn quad_writer<W: Write>(&self, writer: W) -> io::Result<QuadWriter<W>> {
Ok(QuadWriter { Ok(QuadWriter {
formatter: match self.format { formatter: match self.format {
DatasetFormat::NQuads => QuadWriterKind::NQuads(NQuadsFormatter::new(writer)), DatasetFormat::NQuads => QuadWriterKind::NQuads(NQuadsFormatter::new(writer)),
@ -188,7 +188,7 @@ enum QuadWriterKind<W: Write> {
impl<W: Write> QuadWriter<W> { impl<W: Write> QuadWriter<W> {
/// Writes a quad /// Writes a quad
pub fn write<'a>(&mut self, quad: impl Into<QuadRef<'a>>) -> Result<(), io::Error> { pub fn write<'a>(&mut self, quad: impl Into<QuadRef<'a>>) -> io::Result<()> {
let quad = quad.into(); let quad = quad.into();
match &mut self.formatter { match &mut self.formatter {
QuadWriterKind::NQuads(formatter) => formatter.format(&quad.into())?, QuadWriterKind::NQuads(formatter) => formatter.format(&quad.into())?,
@ -198,7 +198,7 @@ impl<W: Write> QuadWriter<W> {
} }
/// Writes the last bytes of the file /// Writes the last bytes of the file
pub fn finish(self) -> Result<(), io::Error> { pub fn finish(self) -> io::Result<()> {
match self.formatter { match self.formatter {
QuadWriterKind::NQuads(formatter) => formatter.finish(), QuadWriterKind::NQuads(formatter) => formatter.finish(),
QuadWriterKind::TriG(formatter) => formatter.finish()?, QuadWriterKind::TriG(formatter) => formatter.finish()?,

@ -436,7 +436,7 @@ impl Dataset {
reader: impl BufRead, reader: impl BufRead,
format: DatasetFormat, format: DatasetFormat,
base_iri: Option<&str>, base_iri: Option<&str>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
let mut parser = DatasetParser::from_format(format); let mut parser = DatasetParser::from_format(format);
if let Some(base_iri) = base_iri { if let Some(base_iri) = base_iri {
parser = parser parser = parser
@ -468,7 +468,7 @@ impl Dataset {
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn dump(&self, writer: impl Write, format: DatasetFormat) -> Result<(), io::Error> { pub fn dump(&self, writer: impl Write, format: DatasetFormat) -> io::Result<()> {
let mut writer = DatasetSerializer::from_format(format).quad_writer(writer)?; let mut writer = DatasetSerializer::from_format(format).quad_writer(writer)?;
for t in self { for t in self {
writer.write(t)?; writer.write(t)?;
@ -1265,7 +1265,7 @@ impl<'a> GraphView<'a> {
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn dump(&self, writer: impl Write, format: GraphFormat) -> Result<(), io::Error> { pub fn dump(&self, writer: impl Write, format: GraphFormat) -> io::Result<()> {
let mut writer = GraphSerializer::from_format(format).triple_writer(writer)?; let mut writer = GraphSerializer::from_format(format).triple_writer(writer)?;
for t in self { for t in self {
writer.write(t)?; writer.write(t)?;
@ -1402,7 +1402,7 @@ impl<'a> GraphViewMut<'a> {
reader: impl BufRead, reader: impl BufRead,
format: GraphFormat, format: GraphFormat,
base_iri: Option<&str>, base_iri: Option<&str>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
let mut parser = GraphParser::from_format(format); let mut parser = GraphParser::from_format(format);
if let Some(base_iri) = base_iri { if let Some(base_iri) = base_iri {
parser = parser parser = parser
@ -1538,7 +1538,7 @@ impl<'a> GraphViewMut<'a> {
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn dump(self, writer: impl Write, format: GraphFormat) -> Result<(), io::Error> { pub fn dump(self, writer: impl Write, format: GraphFormat) -> io::Result<()> {
self.read().dump(writer, format) self.read().dump(writer, format)
} }
} }

@ -208,7 +208,7 @@ impl Graph {
reader: impl BufRead, reader: impl BufRead,
format: GraphFormat, format: GraphFormat,
base_iri: Option<&str>, base_iri: Option<&str>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
self.graph_mut().load(reader, format, base_iri) self.graph_mut().load(reader, format, base_iri)
} }
@ -229,7 +229,7 @@ impl Graph {
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn dump(&self, writer: impl Write, format: GraphFormat) -> Result<(), io::Error> { pub fn dump(&self, writer: impl Write, format: GraphFormat) -> io::Result<()> {
self.graph().dump(writer, format) self.graph().dump(writer, format)
} }

@ -178,7 +178,7 @@ fn write_tsv_term<'a>(term: impl Into<TermRef<'a>>, sink: &mut impl Write) -> io
} }
} }
pub fn read_tsv_results(mut source: impl BufRead + 'static) -> Result<QueryResults, io::Error> { pub fn read_tsv_results(mut source: impl BufRead + 'static) -> io::Result<QueryResults> {
let mut buffer = String::new(); let mut buffer = String::new();
// We read the header // We read the header
@ -192,7 +192,7 @@ pub fn read_tsv_results(mut source: impl BufRead + 'static) -> Result<QueryResul
let variables = buffer let variables = buffer
.split('\t') .split('\t')
.map(|v| Variable::from_str(v.trim()).map_err(invalid_data_error)) .map(|v| Variable::from_str(v.trim()).map_err(invalid_data_error))
.collect::<Result<Vec<_>, io::Error>>()?; .collect::<io::Result<Vec<_>>>()?;
Ok(QueryResults::Solutions(QuerySolutionIter::new( Ok(QueryResults::Solutions(QuerySolutionIter::new(
Rc::new(variables), Rc::new(variables),

@ -115,7 +115,7 @@ fn write_json_term(
Ok(()) Ok(())
} }
pub fn read_json_results(source: impl BufRead + 'static) -> Result<QueryResults, io::Error> { pub fn read_json_results(source: impl BufRead + 'static) -> io::Result<QueryResults> {
let mut reader = JsonReader::from_reader(source); let mut reader = JsonReader::from_reader(source);
let mut buffer = Vec::default(); let mut buffer = Vec::default();
let mut variables = None; let mut variables = None;
@ -423,8 +423,7 @@ impl<R: BufRead> ResultsIterator<R> {
Term::Literal(_) => { Term::Literal(_) => {
return Err(invalid_data_error( return Err(invalid_data_error(
"The 'subject' value should not be a literal", "The 'subject' value should not be a literal",
) ))
.into())
} }
}, },
match predicate.ok_or_else(|| { match predicate.ok_or_else(|| {
@ -436,8 +435,7 @@ impl<R: BufRead> ResultsIterator<R> {
_ => { _ => {
return Err(invalid_data_error( return Err(invalid_data_error(
"The 'predicate' value should be a uri", "The 'predicate' value should be a uri",
) ))
.into())
} }
}, },
object.ok_or_else(|| { object.ok_or_else(|| {

@ -23,10 +23,7 @@ pub enum QueryResults {
impl QueryResults { impl QueryResults {
/// Reads a SPARQL query results serialization /// Reads a SPARQL query results serialization
pub fn read( pub fn read(reader: impl BufRead + 'static, format: QueryResultsFormat) -> io::Result<Self> {
reader: impl BufRead + 'static,
format: QueryResultsFormat,
) -> Result<Self, io::Error> {
match format { match format {
QueryResultsFormat::Xml => read_xml_results(reader), QueryResultsFormat::Xml => read_xml_results(reader),
QueryResultsFormat::Json => read_json_results(reader), QueryResultsFormat::Json => read_json_results(reader),

@ -183,7 +183,7 @@ fn write_xml_term(
Ok(()) Ok(())
} }
pub fn read_xml_results(source: impl BufRead + 'static) -> Result<QueryResults, io::Error> { pub fn read_xml_results(source: impl BufRead + 'static) -> io::Result<QueryResults> {
enum State { enum State {
Start, Start,
Sparql, Sparql,

@ -63,7 +63,7 @@ pub enum QuadEncoding {
} }
impl QuadEncoding { impl QuadEncoding {
pub fn decode(self, buffer: &[u8]) -> Result<EncodedQuad, io::Error> { pub fn decode(self, buffer: &[u8]) -> io::Result<EncodedQuad> {
let mut cursor = Cursor::new(&buffer); let mut cursor = Cursor::new(&buffer);
match self { match self {
QuadEncoding::Spog => cursor.read_spog_quad(), QuadEncoding::Spog => cursor.read_spog_quad(),
@ -79,14 +79,14 @@ impl QuadEncoding {
} }
} }
pub fn decode_term(buffer: &[u8]) -> Result<EncodedTerm, io::Error> { pub fn decode_term(buffer: &[u8]) -> io::Result<EncodedTerm> {
Cursor::new(&buffer).read_term() Cursor::new(&buffer).read_term()
} }
pub trait TermReader { pub trait TermReader {
fn read_term(&mut self) -> Result<EncodedTerm, io::Error>; fn read_term(&mut self) -> io::Result<EncodedTerm>;
fn read_spog_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_spog_quad(&mut self) -> io::Result<EncodedQuad> {
let subject = self.read_term()?; let subject = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
@ -99,7 +99,7 @@ pub trait TermReader {
}) })
} }
fn read_posg_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_posg_quad(&mut self) -> io::Result<EncodedQuad> {
let predicate = self.read_term()?; let predicate = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
@ -112,7 +112,7 @@ pub trait TermReader {
}) })
} }
fn read_ospg_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_ospg_quad(&mut self) -> io::Result<EncodedQuad> {
let object = self.read_term()?; let object = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
@ -125,7 +125,7 @@ pub trait TermReader {
}) })
} }
fn read_gspo_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_gspo_quad(&mut self) -> io::Result<EncodedQuad> {
let graph_name = self.read_term()?; let graph_name = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
@ -138,7 +138,7 @@ pub trait TermReader {
}) })
} }
fn read_gpos_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_gpos_quad(&mut self) -> io::Result<EncodedQuad> {
let graph_name = self.read_term()?; let graph_name = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
@ -151,7 +151,7 @@ pub trait TermReader {
}) })
} }
fn read_gosp_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_gosp_quad(&mut self) -> io::Result<EncodedQuad> {
let graph_name = self.read_term()?; let graph_name = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
@ -164,7 +164,7 @@ pub trait TermReader {
}) })
} }
fn read_dspo_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_dspo_quad(&mut self) -> io::Result<EncodedQuad> {
let subject = self.read_term()?; let subject = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
@ -176,7 +176,7 @@ pub trait TermReader {
}) })
} }
fn read_dpos_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_dpos_quad(&mut self) -> io::Result<EncodedQuad> {
let predicate = self.read_term()?; let predicate = self.read_term()?;
let object = self.read_term()?; let object = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
@ -188,7 +188,7 @@ pub trait TermReader {
}) })
} }
fn read_dosp_quad(&mut self) -> Result<EncodedQuad, io::Error> { fn read_dosp_quad(&mut self) -> io::Result<EncodedQuad> {
let object = self.read_term()?; let object = self.read_term()?;
let subject = self.read_term()?; let subject = self.read_term()?;
let predicate = self.read_term()?; let predicate = self.read_term()?;
@ -202,7 +202,7 @@ pub trait TermReader {
} }
impl<R: Read> TermReader for R { impl<R: Read> TermReader for R {
fn read_term(&mut self) -> Result<EncodedTerm, io::Error> { fn read_term(&mut self) -> io::Result<EncodedTerm> {
let mut type_buffer = [0]; let mut type_buffer = [0];
self.read_exact(&mut type_buffer)?; self.read_exact(&mut type_buffer)?;
match type_buffer[0] { match type_buffer[0] {

@ -60,10 +60,10 @@ where
} }
pub fn dump_graph( pub fn dump_graph(
triples: impl Iterator<Item = Result<Triple, io::Error>>, triples: impl Iterator<Item = io::Result<Triple>>,
writer: impl Write, writer: impl Write,
format: GraphFormat, format: GraphFormat,
) -> Result<(), io::Error> { ) -> io::Result<()> {
let mut writer = GraphSerializer::from_format(format).triple_writer(writer)?; let mut writer = GraphSerializer::from_format(format).triple_writer(writer)?;
for triple in triples { for triple in triples {
writer.write(&triple?)?; writer.write(&triple?)?;
@ -106,10 +106,10 @@ where
} }
pub fn dump_dataset( pub fn dump_dataset(
quads: impl Iterator<Item = Result<Quad, io::Error>>, quads: impl Iterator<Item = io::Result<Quad>>,
writer: impl Write, writer: impl Write,
format: DatasetFormat, format: DatasetFormat,
) -> Result<(), io::Error> { ) -> io::Result<()> {
let mut writer = DatasetSerializer::from_format(format).quad_writer(writer)?; let mut writer = DatasetSerializer::from_format(format).quad_writer(writer)?;
for quad in quads { for quad in quads {
writer.write(&quad?)?; writer.write(&quad?)?;

@ -43,15 +43,15 @@ pub struct Storage {
} }
impl Storage { impl Storage {
pub fn new() -> Result<Self, std::io::Error> { pub fn new() -> std::io::Result<Self> {
Self::do_open(&Config::new().temporary(true)) Self::do_open(&Config::new().temporary(true))
} }
pub fn open(path: &Path) -> Result<Self, std::io::Error> { pub fn open(path: &Path) -> std::io::Result<Self> {
Self::do_open(&Config::new().path(path)) Self::do_open(&Config::new().path(path))
} }
fn do_open(config: &Config) -> Result<Self, std::io::Error> { fn do_open(config: &Config) -> std::io::Result<Self> {
let db = config.open()?; let db = config.open()?;
let this = Self { let this = Self {
default: db.clone(), default: db.clone(),
@ -95,7 +95,7 @@ impl Storage {
} }
} }
fn ensure_version(&self) -> Result<u64, std::io::Error> { fn ensure_version(&self) -> std::io::Result<u64> {
Ok(if let Some(version) = self.default.get("oxversion")? { Ok(if let Some(version) = self.default.get("oxversion")? {
let mut buffer = [0; 8]; let mut buffer = [0; 8];
buffer.copy_from_slice(&version); buffer.copy_from_slice(&version);
@ -106,7 +106,7 @@ impl Storage {
}) })
} }
fn set_version(&self, version: u64) -> Result<(), std::io::Error> { fn set_version(&self, version: u64) -> std::io::Result<()> {
self.default.insert("oxversion", &version.to_be_bytes())?; self.default.insert("oxversion", &version.to_be_bytes())?;
Ok(()) Ok(())
} }
@ -155,7 +155,7 @@ impl Storage {
self.gspo.is_empty() && self.dspo.is_empty() self.gspo.is_empty() && self.dspo.is_empty()
} }
pub fn contains(&self, quad: &EncodedQuad) -> Result<bool, std::io::Error> { pub fn contains(&self, quad: &EncodedQuad) -> std::io::Result<bool> {
let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE); let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE);
if quad.graph_name.is_default_graph() { if quad.graph_name.is_default_graph() {
write_spo_quad(&mut buffer, quad); write_spo_quad(&mut buffer, quad);
@ -405,7 +405,7 @@ impl Storage {
} }
} }
pub fn contains_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> { pub fn contains_named_graph(&self, graph_name: &EncodedTerm) -> std::io::Result<bool> {
Ok(self.graphs.contains_key(&encode_term(graph_name))?) Ok(self.graphs.contains_key(&encode_term(graph_name))?)
} }
@ -457,7 +457,7 @@ impl Storage {
} }
} }
pub fn insert(&self, quad: &EncodedQuad) -> Result<bool, std::io::Error> { pub fn insert(&self, quad: &EncodedQuad) -> std::io::Result<bool> {
let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE + 1); let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE + 1);
if quad.graph_name.is_default_graph() { if quad.graph_name.is_default_graph() {
@ -512,7 +512,7 @@ impl Storage {
} }
} }
pub fn remove(&self, quad: &EncodedQuad) -> Result<bool, std::io::Error> { pub fn remove(&self, quad: &EncodedQuad) -> std::io::Result<bool> {
let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE + 1); let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE + 1);
if quad.graph_name.is_default_graph() { if quad.graph_name.is_default_graph() {
@ -564,11 +564,11 @@ impl Storage {
} }
} }
pub fn insert_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> { pub fn insert_named_graph(&self, graph_name: &EncodedTerm) -> std::io::Result<bool> {
Ok(self.graphs.insert(&encode_term(graph_name), &[])?.is_none()) Ok(self.graphs.insert(&encode_term(graph_name), &[])?.is_none())
} }
pub fn clear_graph(&self, graph_name: &EncodedTerm) -> Result<(), std::io::Error> { pub fn clear_graph(&self, graph_name: &EncodedTerm) -> std::io::Result<()> {
if graph_name.is_default_graph() { if graph_name.is_default_graph() {
self.dspo.clear()?; self.dspo.clear()?;
self.dpos.clear()?; self.dpos.clear()?;
@ -581,14 +581,14 @@ impl Storage {
Ok(()) Ok(())
} }
pub fn remove_named_graph(&self, graph_name: &EncodedTerm) -> Result<bool, std::io::Error> { pub fn remove_named_graph(&self, graph_name: &EncodedTerm) -> std::io::Result<bool> {
for quad in self.quads_for_graph(graph_name) { for quad in self.quads_for_graph(graph_name) {
self.remove(&quad?)?; self.remove(&quad?)?;
} }
Ok(self.graphs.remove(&encode_term(graph_name))?.is_some()) Ok(self.graphs.remove(&encode_term(graph_name))?.is_some())
} }
pub fn clear(&self) -> Result<(), std::io::Error> { pub fn clear(&self) -> std::io::Result<()> {
self.dspo.clear()?; self.dspo.clear()?;
self.dpos.clear()?; self.dpos.clear()?;
self.dosp.clear()?; self.dosp.clear()?;
@ -603,17 +603,17 @@ impl Storage {
Ok(()) Ok(())
} }
pub fn flush(&self) -> Result<(), std::io::Error> { pub fn flush(&self) -> std::io::Result<()> {
self.default.flush()?; self.default.flush()?;
Ok(()) Ok(())
} }
pub async fn flush_async(&self) -> Result<(), std::io::Error> { pub async fn flush_async(&self) -> std::io::Result<()> {
self.default.flush_async().await?; self.default.flush_async().await?;
Ok(()) Ok(())
} }
pub fn get_str(&self, key: &StrHash) -> Result<Option<String>, std::io::Error> { pub fn get_str(&self, key: &StrHash) -> std::io::Result<Option<String>> {
self.id2str self.id2str
.get(key.to_be_bytes())? .get(key.to_be_bytes())?
.map(|v| String::from_utf8(v.to_vec())) .map(|v| String::from_utf8(v.to_vec()))
@ -621,11 +621,11 @@ impl Storage {
.map_err(invalid_data_error) .map_err(invalid_data_error)
} }
pub fn contains_str(&self, key: &StrHash) -> Result<bool, std::io::Error> { pub fn contains_str(&self, key: &StrHash) -> std::io::Result<bool> {
Ok(self.id2str.contains_key(key.to_be_bytes())?) Ok(self.id2str.contains_key(key.to_be_bytes())?)
} }
pub fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, std::io::Error> { pub fn insert_str(&self, key: &StrHash, value: &str) -> std::io::Result<bool> {
Ok(self.id2str.insert(key.to_be_bytes(), value)?.is_none()) Ok(self.id2str.insert(key.to_be_bytes(), value)?.is_none())
} }
} }
@ -652,9 +652,9 @@ impl ChainedDecodingQuadIterator {
} }
impl Iterator for ChainedDecodingQuadIterator { impl Iterator for ChainedDecodingQuadIterator {
type Item = Result<EncodedQuad, std::io::Error>; type Item = std::io::Result<EncodedQuad>;
fn next(&mut self) -> Option<Result<EncodedQuad, std::io::Error>> { fn next(&mut self) -> Option<std::io::Result<EncodedQuad>> {
if let Some(result) = self.first.next() { if let Some(result) = self.first.next() {
Some(result) Some(result)
} else if let Some(second) = self.second.as_mut() { } else if let Some(second) = self.second.as_mut() {
@ -671,9 +671,9 @@ pub struct DecodingQuadIterator {
} }
impl Iterator for DecodingQuadIterator { impl Iterator for DecodingQuadIterator {
type Item = Result<EncodedQuad, std::io::Error>; type Item = std::io::Result<EncodedQuad>;
fn next(&mut self) -> Option<Result<EncodedQuad, std::io::Error>> { fn next(&mut self) -> Option<std::io::Result<EncodedQuad>> {
Some(match self.iter.next()? { Some(match self.iter.next()? {
Ok((encoded, _)) => self.encoding.decode(&encoded), Ok((encoded, _)) => self.encoding.decode(&encoded),
Err(error) => Err(error.into()), Err(error) => Err(error.into()),
@ -686,9 +686,9 @@ pub struct DecodingGraphIterator {
} }
impl Iterator for DecodingGraphIterator { impl Iterator for DecodingGraphIterator {
type Item = Result<EncodedTerm, std::io::Error>; type Item = std::io::Result<EncodedTerm>;
fn next(&mut self) -> Option<Result<EncodedTerm, std::io::Error>> { fn next(&mut self) -> Option<std::io::Result<EncodedTerm>> {
Some(match self.iter.next()? { Some(match self.iter.next()? {
Ok((encoded, _)) => decode_term(&encoded), Ok((encoded, _)) => decode_term(&encoded),
Err(error) => Err(error.into()), Err(error) => Err(error.into()),
@ -1002,17 +1002,17 @@ impl<T> From<ConflictableTransactionError<T>> for Sled2ConflictableTransactionEr
impl StrLookup for Storage { impl StrLookup for Storage {
type Error = std::io::Error; type Error = std::io::Error;
fn get_str(&self, key: &StrHash) -> Result<Option<String>, std::io::Error> { fn get_str(&self, key: &StrHash) -> std::io::Result<Option<String>> {
self.get_str(key) self.get_str(key)
} }
fn contains_str(&self, key: &StrHash) -> Result<bool, std::io::Error> { fn contains_str(&self, key: &StrHash) -> std::io::Result<bool> {
self.contains_str(key) self.contains_str(key)
} }
} }
impl StrContainer for Storage { impl StrContainer for Storage {
fn insert_str(&self, key: &StrHash, value: &str) -> Result<bool, std::io::Error> { fn insert_str(&self, key: &StrHash, value: &str) -> std::io::Result<bool> {
self.insert_str(key, value) self.insert_str(key, value)
} }
} }

@ -86,14 +86,14 @@ pub struct Store {
impl Store { impl Store {
/// Creates a temporary [`Store`]() that will be deleted after drop. /// Creates a temporary [`Store`]() that will be deleted after drop.
pub fn new() -> Result<Self, io::Error> { pub fn new() -> io::Result<Self> {
Ok(Self { Ok(Self {
storage: Storage::new()?, storage: Storage::new()?,
}) })
} }
/// Opens a [`Store`]() and creates it if it does not exist yet. /// Opens a [`Store`]() and creates it if it does not exist yet.
pub fn open(path: impl AsRef<Path>) -> Result<Self, io::Error> { pub fn open(path: impl AsRef<Path>) -> io::Result<Self> {
Ok(Self { Ok(Self {
storage: Storage::open(path.as_ref())?, storage: Storage::open(path.as_ref())?,
}) })
@ -178,7 +178,7 @@ impl Store {
} }
/// Checks if this store contains a given quad /// Checks if this store contains a given quad
pub fn contains<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> { pub fn contains<'a>(&self, quad: impl Into<QuadRef<'a>>) -> io::Result<bool> {
let quad = EncodedQuad::from(quad.into()); let quad = EncodedQuad::from(quad.into());
self.storage.contains(&quad) self.storage.contains(&quad)
} }
@ -303,7 +303,7 @@ impl Store {
format: GraphFormat, format: GraphFormat,
to_graph_name: impl Into<GraphNameRef<'a>>, to_graph_name: impl Into<GraphNameRef<'a>>,
base_iri: Option<&str>, base_iri: Option<&str>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
load_graph( load_graph(
&self.storage, &self.storage,
reader, reader,
@ -347,7 +347,7 @@ impl Store {
reader: impl BufRead, reader: impl BufRead,
format: DatasetFormat, format: DatasetFormat,
base_iri: Option<&str>, base_iri: Option<&str>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
load_dataset(&self.storage, reader, format, base_iri)?; load_dataset(&self.storage, reader, format, base_iri)?;
Ok(()) Ok(())
} }
@ -359,7 +359,7 @@ impl Store {
/// This method is optimized for performances and is not atomic. /// This method is optimized for performances and is not atomic.
/// It might leave the store in a bad state if a crash happens during the insertion. /// It might leave the store in a bad state if a crash happens during the insertion.
/// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that. /// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that.
pub fn insert<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> { pub fn insert<'a>(&self, quad: impl Into<QuadRef<'a>>) -> io::Result<bool> {
let quad = self.storage.encode_quad(quad.into())?; let quad = self.storage.encode_quad(quad.into())?;
self.storage.insert(&quad) self.storage.insert(&quad)
} }
@ -371,7 +371,7 @@ impl Store {
/// This method is optimized for performances and is not atomic. /// This method is optimized for performances and is not atomic.
/// It might leave the store in a bad state if a crash happens during the removal. /// It might leave the store in a bad state if a crash happens during the removal.
/// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that. /// Use a (memory greedy) [transaction](Store::transaction()) if you do not want that.
pub fn remove<'a>(&self, quad: impl Into<QuadRef<'a>>) -> Result<bool, io::Error> { pub fn remove<'a>(&self, quad: impl Into<QuadRef<'a>>) -> io::Result<bool> {
let quad = EncodedQuad::from(quad.into()); let quad = EncodedQuad::from(quad.into());
self.storage.remove(&quad) self.storage.remove(&quad)
} }
@ -399,7 +399,7 @@ impl Store {
writer: impl Write, writer: impl Write,
format: GraphFormat, format: GraphFormat,
from_graph_name: impl Into<GraphNameRef<'a>>, from_graph_name: impl Into<GraphNameRef<'a>>,
) -> Result<(), io::Error> { ) -> io::Result<()> {
dump_graph( dump_graph(
self.quads_for_pattern(None, None, None, Some(from_graph_name.into())) self.quads_for_pattern(None, None, None, Some(from_graph_name.into()))
.map(|q| Ok(q?.into())), .map(|q| Ok(q?.into())),
@ -424,7 +424,7 @@ impl Store {
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # std::io::Result::Ok(()) /// # std::io::Result::Ok(())
/// ``` /// ```
pub fn dump_dataset(&self, writer: impl Write, format: DatasetFormat) -> Result<(), io::Error> { pub fn dump_dataset(&self, writer: impl Write, format: DatasetFormat) -> io::Result<()> {
dump_dataset(self.iter(), writer, format) dump_dataset(self.iter(), writer, format)
} }
@ -465,7 +465,7 @@ impl Store {
pub fn contains_named_graph<'a>( pub fn contains_named_graph<'a>(
&self, &self,
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> io::Result<bool> {
let graph_name = EncodedTerm::from(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.contains_named_graph(&graph_name) self.storage.contains_named_graph(&graph_name)
} }
@ -488,7 +488,7 @@ impl Store {
pub fn insert_named_graph<'a>( pub fn insert_named_graph<'a>(
&self, &self,
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> io::Result<bool> {
let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?; let graph_name = self.storage.encode_named_or_blank_node(graph_name.into())?;
self.storage.insert_named_graph(&graph_name) self.storage.insert_named_graph(&graph_name)
} }
@ -511,10 +511,7 @@ impl Store {
/// assert_eq!(1, store.named_graphs().count()); /// assert_eq!(1, store.named_graphs().count());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn clear_graph<'a>( pub fn clear_graph<'a>(&self, graph_name: impl Into<GraphNameRef<'a>>) -> io::Result<()> {
&self,
graph_name: impl Into<GraphNameRef<'a>>,
) -> Result<(), io::Error> {
let graph_name = EncodedTerm::from(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.clear_graph(&graph_name) self.storage.clear_graph(&graph_name)
} }
@ -542,7 +539,7 @@ impl Store {
pub fn remove_named_graph<'a>( pub fn remove_named_graph<'a>(
&self, &self,
graph_name: impl Into<NamedOrBlankNodeRef<'a>>, graph_name: impl Into<NamedOrBlankNodeRef<'a>>,
) -> Result<bool, io::Error> { ) -> io::Result<bool> {
let graph_name = EncodedTerm::from(graph_name.into()); let graph_name = EncodedTerm::from(graph_name.into());
self.storage.remove_named_graph(&graph_name) self.storage.remove_named_graph(&graph_name)
} }
@ -564,7 +561,7 @@ impl Store {
/// assert!(store.is_empty()); /// assert!(store.is_empty());
/// # Result::<_,Box<dyn std::error::Error>>::Ok(()) /// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ``` /// ```
pub fn clear(&self) -> Result<(), io::Error> { pub fn clear(&self) -> io::Result<()> {
self.storage.clear() self.storage.clear()
} }
@ -574,7 +571,7 @@ impl Store {
/// However, calling this method explicitly is still required for Windows and Android. /// However, calling this method explicitly is still required for Windows and Android.
/// ///
/// An [async version](SledStore::flush_async) is also available. /// An [async version](SledStore::flush_async) is also available.
pub fn flush(&self) -> Result<(), io::Error> { pub fn flush(&self) -> io::Result<()> {
self.storage.flush() self.storage.flush()
} }
@ -584,7 +581,7 @@ impl Store {
/// However, calling this method explicitly is still required for Windows and Android. /// However, calling this method explicitly is still required for Windows and Android.
/// ///
/// A [sync version](SledStore::flush) is also available. /// A [sync version](SledStore::flush) is also available.
pub async fn flush_async(&self) -> Result<(), io::Error> { pub async fn flush_async(&self) -> io::Result<()> {
self.storage.flush_async().await self.storage.flush_async().await
} }
} }
@ -740,9 +737,9 @@ pub struct QuadIter {
} }
impl Iterator for QuadIter { impl Iterator for QuadIter {
type Item = Result<Quad, io::Error>; type Item = io::Result<Quad>;
fn next(&mut self) -> Option<Result<Quad, io::Error>> { fn next(&mut self) -> Option<io::Result<Quad>> {
Some(match self.iter.next()? { Some(match self.iter.next()? {
Ok(quad) => self.storage.decode_quad(&quad).map_err(|e| e.into()), Ok(quad) => self.storage.decode_quad(&quad).map_err(|e| e.into()),
Err(error) => Err(error), Err(error) => Err(error),
@ -757,9 +754,9 @@ pub struct GraphNameIter {
} }
impl Iterator for GraphNameIter { impl Iterator for GraphNameIter {
type Item = Result<NamedOrBlankNode, io::Error>; type Item = io::Result<NamedOrBlankNode>;
fn next(&mut self) -> Option<Result<NamedOrBlankNode, io::Error>> { fn next(&mut self) -> Option<io::Result<NamedOrBlankNode>> {
Some( Some(
self.iter.next()?.and_then(|graph_name| { self.iter.next()?.and_then(|graph_name| {
Ok(self.store.storage.decode_named_or_blank_node(&graph_name)?) Ok(self.store.storage.decode_named_or_blank_node(&graph_name)?)
@ -773,7 +770,7 @@ impl Iterator for GraphNameIter {
} }
#[test] #[test]
fn store() -> Result<(), io::Error> { fn store() -> io::Result<()> {
use crate::model::*; use crate::model::*;
let main_s = Subject::from(BlankNode::default()); let main_s = Subject::from(BlankNode::default());

Loading…
Cancel
Save