Simplify some storage errors and deduplicate them

pull/775/head
Yuri Astrakhan 1 year ago
parent 532a025c8a
commit e92a43b466
  1. 2
      lib/oxigraph/src/storage/backend/fallback.rs
  2. 2
      lib/oxigraph/src/storage/backend/rocksdb.rs
  3. 14
      lib/oxigraph/src/storage/error.rs
  4. 30
      lib/oxigraph/src/storage/numeric_encoder.rs

@ -36,7 +36,7 @@ impl Db {
if self.0.read().unwrap().contains_key(&column_family) { if self.0.read().unwrap().contains_key(&column_family) {
Ok(column_family) Ok(column_family)
} else { } else {
Err(CorruptionError::msg(format!("Column family {name} does not exist")).into()) Err(CorruptionError::from_missing_column_family_name(name).into())
} }
} }

@ -548,7 +548,7 @@ impl Db {
return Ok(ColumnFamily(*cf_handle)); return Ok(ColumnFamily(*cf_handle));
} }
} }
Err(CorruptionError::msg(format!("Column family {name} does not exist")).into()) Err(CorruptionError::from_missing_column_family_name(name).into())
} }
#[must_use] #[must_use]

@ -1,5 +1,7 @@
use crate::io::{ParseError, RdfFormat}; use crate::io::{ParseError, RdfFormat};
use crate::storage::numeric_encoder::EncodedTerm;
use oxiri::IriParseError; use oxiri::IriParseError;
use oxrdf::TermRef;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
@ -45,6 +47,18 @@ impl CorruptionError {
Self::Other(error.into()) Self::Other(error.into())
} }
#[inline]
pub(crate) fn from_encoded_term(encoded: &EncodedTerm, term: &TermRef<'_>) -> Self {
// TODO: eventually use a dedicated error enum value
Self::new(format!("Invalid term encoding {encoded:?} for {term}"))
}
#[inline]
pub(crate) fn from_missing_column_family_name(name: &'static str) -> Self {
// TODO: eventually use a dedicated error enum value
Self::new(format!("Column family {name} does not exist"))
}
/// Builds an error from a printable error message. /// Builds an error from a printable error message.
#[inline] #[inline]
pub(crate) fn msg(msg: impl Into<String>) -> Self { pub(crate) fn msg(msg: impl Into<String>) -> Self {

@ -713,19 +713,13 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
if let EncodedTerm::NamedNode { iri_id } = encoded { if let EncodedTerm::NamedNode { iri_id } = encoded {
insert_str(iri_id, node.as_str()) insert_str(iri_id, node.as_str())
} else { } else {
Err( Err(CorruptionError::from_encoded_term(encoded, &term).into())
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
)
} }
} }
TermRef::BlankNode(node) => match encoded { TermRef::BlankNode(node) => match encoded {
EncodedTerm::BigBlankNode { id_id } => insert_str(id_id, node.as_str()), EncodedTerm::BigBlankNode { id_id } => insert_str(id_id, node.as_str()),
EncodedTerm::SmallBlankNode(..) | EncodedTerm::NumericalBlankNode { .. } => Ok(()), EncodedTerm::SmallBlankNode(..) | EncodedTerm::NumericalBlankNode { .. } => Ok(()),
_ => Err( _ => Err(CorruptionError::from_encoded_term(encoded, &term).into()),
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
),
}, },
TermRef::Literal(literal) => match encoded { TermRef::Literal(literal) => match encoded {
EncodedTerm::BigStringLiteral { value_id } EncodedTerm::BigStringLiteral { value_id }
@ -736,10 +730,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
if let Some(language) = literal.language() { if let Some(language) = literal.language() {
insert_str(language_id, language) insert_str(language_id, language)
} else { } else {
Err(CorruptionError::new(format!( Err(CorruptionError::from_encoded_term(encoded, &term).into())
"Invalid term encoding {encoded:?} for {term}"
))
.into())
} }
} }
EncodedTerm::BigBigLangStringLiteral { EncodedTerm::BigBigLangStringLiteral {
@ -750,10 +741,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
if let Some(language) = literal.language() { if let Some(language) = literal.language() {
insert_str(language_id, language) insert_str(language_id, language)
} else { } else {
Err(CorruptionError::new(format!( Err(CorruptionError::from_encoded_term(encoded, &term).into())
"Invalid term encoding {encoded:?} for {term}"
))
.into())
} }
} }
EncodedTerm::SmallTypedLiteral { datatype_id, .. } => { EncodedTerm::SmallTypedLiteral { datatype_id, .. } => {
@ -784,10 +772,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
| EncodedTerm::DurationLiteral(..) | EncodedTerm::DurationLiteral(..)
| EncodedTerm::YearMonthDurationLiteral(..) | EncodedTerm::YearMonthDurationLiteral(..)
| EncodedTerm::DayTimeDurationLiteral(..) => Ok(()), | EncodedTerm::DayTimeDurationLiteral(..) => Ok(()),
_ => Err( _ => Err(CorruptionError::from_encoded_term(encoded, &term).into()),
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
),
}, },
TermRef::Triple(triple) => { TermRef::Triple(triple) => {
if let EncodedTerm::Triple(encoded) = encoded { if let EncodedTerm::Triple(encoded) = encoded {
@ -799,10 +784,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
)?; )?;
insert_term(triple.object.as_ref(), &encoded.object, insert_str) insert_term(triple.object.as_ref(), &encoded.object, insert_str)
} else { } else {
Err( Err(CorruptionError::from_encoded_term(encoded, &term).into())
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
)
} }
} }
} }

Loading…
Cancel
Save