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) {
Ok(column_family)
} 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));
}
}
Err(CorruptionError::msg(format!("Column family {name} does not exist")).into())
Err(CorruptionError::from_missing_column_family_name(name).into())
}
#[must_use]

@ -1,5 +1,7 @@
use crate::io::{ParseError, RdfFormat};
use crate::storage::numeric_encoder::EncodedTerm;
use oxiri::IriParseError;
use oxrdf::TermRef;
use std::error::Error;
use std::io;
@ -45,6 +47,18 @@ impl CorruptionError {
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.
#[inline]
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 {
insert_str(iri_id, node.as_str())
} else {
Err(
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
)
Err(CorruptionError::from_encoded_term(encoded, &term).into())
}
}
TermRef::BlankNode(node) => match encoded {
EncodedTerm::BigBlankNode { id_id } => insert_str(id_id, node.as_str()),
EncodedTerm::SmallBlankNode(..) | EncodedTerm::NumericalBlankNode { .. } => Ok(()),
_ => Err(
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
),
_ => Err(CorruptionError::from_encoded_term(encoded, &term).into()),
},
TermRef::Literal(literal) => match encoded {
EncodedTerm::BigStringLiteral { value_id }
@ -736,10 +730,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
if let Some(language) = literal.language() {
insert_str(language_id, language)
} else {
Err(CorruptionError::new(format!(
"Invalid term encoding {encoded:?} for {term}"
))
.into())
Err(CorruptionError::from_encoded_term(encoded, &term).into())
}
}
EncodedTerm::BigBigLangStringLiteral {
@ -750,10 +741,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
if let Some(language) = literal.language() {
insert_str(language_id, language)
} else {
Err(CorruptionError::new(format!(
"Invalid term encoding {encoded:?} for {term}"
))
.into())
Err(CorruptionError::from_encoded_term(encoded, &term).into())
}
}
EncodedTerm::SmallTypedLiteral { datatype_id, .. } => {
@ -784,10 +772,7 @@ pub fn insert_term<F: FnMut(&StrHash, &str) -> Result<(), StorageError>>(
| EncodedTerm::DurationLiteral(..)
| EncodedTerm::YearMonthDurationLiteral(..)
| EncodedTerm::DayTimeDurationLiteral(..) => Ok(()),
_ => Err(
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
),
_ => Err(CorruptionError::from_encoded_term(encoded, &term).into()),
},
TermRef::Triple(triple) => {
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)
} else {
Err(
CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}"))
.into(),
)
Err(CorruptionError::from_encoded_term(encoded, &term).into())
}
}
}

Loading…
Cancel
Save