2021-06-14 22:58:38 +08:00
|
|
|
use std::convert::Infallible;
|
2021-06-10 23:31:08 +08:00
|
|
|
use std::error::Error as StdError;
|
2021-06-14 22:58:38 +08:00
|
|
|
use std::{fmt, io, str};
|
2021-06-09 23:05:46 +08:00
|
|
|
|
2021-06-10 21:55:22 +08:00
|
|
|
use heed::{MdbError, Error as HeedError};
|
2021-06-14 22:58:38 +08:00
|
|
|
use rayon::ThreadPoolBuildError;
|
2021-06-10 23:31:08 +08:00
|
|
|
use serde_json::{Map, Value};
|
|
|
|
|
2021-06-14 22:58:38 +08:00
|
|
|
use crate::search::ParserRule;
|
2021-06-10 23:31:08 +08:00
|
|
|
use crate::{DocumentId, FieldId};
|
2021-06-10 21:55:22 +08:00
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
pub type Object = Map<String, Value>;
|
2021-06-09 23:05:46 +08:00
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
#[derive(Debug)]
|
2021-06-09 23:05:46 +08:00
|
|
|
pub enum Error {
|
|
|
|
InternalError(InternalError),
|
|
|
|
IoError(io::Error),
|
|
|
|
UserError(UserError),
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
#[derive(Debug)]
|
2021-06-09 23:05:46 +08:00
|
|
|
pub enum InternalError {
|
2021-06-14 22:58:38 +08:00
|
|
|
DatabaseClosing,
|
2021-06-10 23:31:08 +08:00
|
|
|
DatabaseMissingEntry { db_name: &'static str, key: Option<&'static str> },
|
2021-06-09 23:05:46 +08:00
|
|
|
FieldIdMapMissingEntry(FieldIdMapMissingEntry),
|
2021-06-14 22:58:38 +08:00
|
|
|
Fst(fst::Error),
|
|
|
|
GrenadInvalidCompressionType,
|
2021-06-10 23:31:08 +08:00
|
|
|
IndexingMergingKeys { process: &'static str },
|
2021-06-10 21:55:22 +08:00
|
|
|
InvalidDatabaseTyping,
|
2021-06-14 22:58:38 +08:00
|
|
|
RayonThreadPool(ThreadPoolBuildError),
|
|
|
|
SerdeJson(serde_json::Error),
|
|
|
|
Serialization(SerializationError),
|
|
|
|
Store(MdbError),
|
|
|
|
Utf8(str::Utf8Error),
|
2021-06-10 21:55:22 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
#[derive(Debug)]
|
2021-06-10 21:55:22 +08:00
|
|
|
pub enum SerializationError {
|
|
|
|
Decoding { db_name: Option<&'static str> },
|
|
|
|
Encoding { db_name: Option<&'static str> },
|
|
|
|
InvalidNumberSerialization,
|
2021-06-09 23:05:46 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
#[derive(Debug)]
|
2021-06-09 23:05:46 +08:00
|
|
|
pub enum FieldIdMapMissingEntry {
|
2021-06-15 17:10:50 +08:00
|
|
|
FieldId { field_id: FieldId, process: &'static str },
|
|
|
|
FieldName { field_name: String, process: &'static str },
|
2021-06-09 23:05:46 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
#[derive(Debug)]
|
2021-06-09 23:05:46 +08:00
|
|
|
pub enum UserError {
|
2021-06-10 21:55:22 +08:00
|
|
|
AttributeLimitReached,
|
2021-06-14 22:58:38 +08:00
|
|
|
Csv(csv::Error),
|
|
|
|
DatabaseSizeReached,
|
2021-06-10 21:55:22 +08:00
|
|
|
DocumentLimitReached,
|
2021-06-14 22:58:38 +08:00
|
|
|
FilterParsing(pest::error::Error<ParserRule>),
|
2021-06-10 21:55:22 +08:00
|
|
|
InvalidCriterionName { name: String },
|
2021-06-10 23:31:08 +08:00
|
|
|
InvalidDocumentId { document_id: Value },
|
2021-06-14 22:58:38 +08:00
|
|
|
InvalidStoreFile,
|
2021-06-10 21:55:22 +08:00
|
|
|
MissingDocumentId { document: Object },
|
|
|
|
MissingPrimaryKey,
|
|
|
|
NoSpaceLeftOnDevice,
|
2021-06-14 22:58:38 +08:00
|
|
|
SerdeJson(serde_json::Error),
|
|
|
|
UnknownInternalDocumentId { document_id: DocumentId },
|
2021-06-10 21:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(error: io::Error) -> Error {
|
|
|
|
// TODO must be improved and more precise
|
|
|
|
Error::IoError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:38 +08:00
|
|
|
impl From<fst::Error> for Error {
|
|
|
|
fn from(error: fst::Error) -> Error {
|
|
|
|
Error::InternalError(InternalError::Fst(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E> From<grenad::Error<E>> for Error where Error: From<E> {
|
|
|
|
fn from(error: grenad::Error<E>) -> Error {
|
|
|
|
match error {
|
|
|
|
grenad::Error::Io(error) => Error::IoError(error),
|
|
|
|
grenad::Error::Merge(error) => Error::from(error),
|
|
|
|
grenad::Error::InvalidCompressionType => {
|
|
|
|
Error::InternalError(InternalError::GrenadInvalidCompressionType)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<str::Utf8Error> for Error {
|
|
|
|
fn from(error: str::Utf8Error) -> Error {
|
|
|
|
Error::InternalError(InternalError::Utf8(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Infallible> for Error {
|
|
|
|
fn from(_error: Infallible) -> Error {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 21:55:22 +08:00
|
|
|
impl From<HeedError> for Error {
|
|
|
|
fn from(error: HeedError) -> Error {
|
|
|
|
use self::Error::*;
|
|
|
|
use self::InternalError::*;
|
|
|
|
use self::SerializationError::*;
|
|
|
|
use self::UserError::*;
|
2021-06-09 23:05:46 +08:00
|
|
|
|
2021-06-10 21:55:22 +08:00
|
|
|
match error {
|
|
|
|
HeedError::Io(error) => Error::from(error),
|
|
|
|
HeedError::Mdb(MdbError::MapFull) => UserError(DatabaseSizeReached),
|
|
|
|
HeedError::Mdb(MdbError::Invalid) => UserError(InvalidStoreFile),
|
2021-06-14 22:58:38 +08:00
|
|
|
HeedError::Mdb(error) => InternalError(Store(error)),
|
|
|
|
HeedError::Encoding => InternalError(Serialization(Encoding { db_name: None })),
|
|
|
|
HeedError::Decoding => InternalError(Serialization(Decoding { db_name: None })),
|
2021-06-10 21:55:22 +08:00
|
|
|
HeedError::InvalidDatabaseTyping => InternalError(InvalidDatabaseTyping),
|
|
|
|
HeedError::DatabaseClosing => InternalError(DatabaseClosing),
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 23:05:46 +08:00
|
|
|
}
|
2021-06-10 23:31:08 +08:00
|
|
|
|
2021-06-14 22:58:38 +08:00
|
|
|
impl From<ThreadPoolBuildError> for Error {
|
|
|
|
fn from(error: ThreadPoolBuildError) -> Error {
|
|
|
|
Error::InternalError(InternalError::RayonThreadPool(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FieldIdMapMissingEntry> for Error {
|
|
|
|
fn from(error: FieldIdMapMissingEntry) -> Error {
|
|
|
|
Error::InternalError(InternalError::FieldIdMapMissingEntry(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<InternalError> for Error {
|
|
|
|
fn from(error: InternalError) -> Error {
|
|
|
|
Error::InternalError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserError> for Error {
|
|
|
|
fn from(error: UserError) -> Error {
|
|
|
|
Error::UserError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SerializationError> for Error {
|
|
|
|
fn from(error: SerializationError) -> Error {
|
|
|
|
Error::InternalError(InternalError::Serialization(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:31:08 +08:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::InternalError(error) => write!(f, "internal: {}", error),
|
|
|
|
Self::IoError(error) => error.fmt(f),
|
|
|
|
Self::UserError(error) => error.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for Error {}
|
|
|
|
|
|
|
|
impl fmt::Display for InternalError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::DatabaseMissingEntry { db_name, key } => {
|
|
|
|
write!(f, "missing {} in the {} database", key.unwrap_or("key"), db_name)
|
|
|
|
},
|
|
|
|
Self::FieldIdMapMissingEntry(error) => error.fmt(f),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::Fst(error) => error.fmt(f),
|
|
|
|
Self::GrenadInvalidCompressionType => {
|
|
|
|
f.write_str("invalid compression type have been specified to grenad")
|
|
|
|
},
|
2021-06-10 23:31:08 +08:00
|
|
|
Self::IndexingMergingKeys { process } => {
|
|
|
|
write!(f, "invalid merge while processing {}", process)
|
|
|
|
},
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::Serialization(error) => error.fmt(f),
|
2021-06-10 23:31:08 +08:00
|
|
|
Self::InvalidDatabaseTyping => HeedError::InvalidDatabaseTyping.fmt(f),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::RayonThreadPool(error) => error.fmt(f),
|
|
|
|
Self::SerdeJson(error) => error.fmt(f),
|
2021-06-10 23:31:08 +08:00
|
|
|
Self::DatabaseClosing => HeedError::DatabaseClosing.fmt(f),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::Store(error) => error.fmt(f),
|
|
|
|
Self::Utf8(error) => error.fmt(f),
|
2021-06-10 23:31:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for InternalError {}
|
|
|
|
|
|
|
|
impl fmt::Display for UserError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::AttributeLimitReached => f.write_str("maximum number of attributes reached"),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::Csv(error) => error.fmt(f),
|
2021-06-10 23:31:08 +08:00
|
|
|
Self::DocumentLimitReached => f.write_str("maximum number of documents reached"),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::FilterParsing(error) => error.fmt(f),
|
2021-06-10 23:31:08 +08:00
|
|
|
Self::InvalidCriterionName { name } => write!(f, "invalid criterion {}", name),
|
|
|
|
Self::InvalidDocumentId { document_id } => {
|
|
|
|
let json = serde_json::to_string(document_id).unwrap();
|
|
|
|
write!(f, "document identifier is invalid {}", json)
|
|
|
|
},
|
|
|
|
Self::MissingDocumentId { document } => {
|
|
|
|
let json = serde_json::to_string(document).unwrap();
|
|
|
|
write!(f, "document doesn't have an identifier {}", json)
|
|
|
|
},
|
|
|
|
Self::MissingPrimaryKey => f.write_str("missing primary key"),
|
|
|
|
Self::DatabaseSizeReached => f.write_str("database size reached"),
|
|
|
|
// TODO where can we find it instead of writing the text ourselves?
|
|
|
|
Self::NoSpaceLeftOnDevice => f.write_str("no space left on device"),
|
|
|
|
Self::InvalidStoreFile => f.write_str("store file is not a valid database file"),
|
2021-06-14 22:58:38 +08:00
|
|
|
Self::SerdeJson(error) => error.fmt(f),
|
|
|
|
Self::UnknownInternalDocumentId { document_id } => {
|
|
|
|
write!(f, "an unknown internal document id have been used ({})", document_id)
|
|
|
|
},
|
2021-06-10 23:31:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for UserError {}
|
|
|
|
|
|
|
|
impl fmt::Display for FieldIdMapMissingEntry {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2021-06-15 17:10:50 +08:00
|
|
|
Self::FieldId { field_id, process } => {
|
|
|
|
write!(f, "unknown field id {} coming from the {} process", field_id, process)
|
2021-06-10 23:31:08 +08:00
|
|
|
},
|
2021-06-15 17:10:50 +08:00
|
|
|
Self::FieldName { field_name, process } => {
|
|
|
|
write!(f, "unknown field name {} coming from the {} process", field_name, process)
|
2021-06-10 23:31:08 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for FieldIdMapMissingEntry {}
|
|
|
|
|
|
|
|
impl fmt::Display for SerializationError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Decoding { db_name: Some(name) } => {
|
|
|
|
write!(f, "decoding from the {} database failed", name)
|
|
|
|
},
|
|
|
|
Self::Decoding { db_name: None } => f.write_str("decoding failed"),
|
|
|
|
Self::Encoding { db_name: Some(name) } => {
|
|
|
|
write!(f, "encoding into the {} database failed", name)
|
|
|
|
},
|
|
|
|
Self::Encoding { db_name: None } => f.write_str("encoding failed"),
|
|
|
|
Self::InvalidNumberSerialization => f.write_str("number is not a valid finite number"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for SerializationError {}
|