use std::path::PathBuf;
use bytesize::ByteSize;
use heed::MdbError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum LmdbLayerError {
#[error("Cannot create LMDB map of size {0}: larger than this system's addressable memory.")]
MapSizeUnrepresentable(ByteSize),
#[error("Could not create LMDB environment directory {path}: {source}")]
CreateDir {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("Could not open LMDB environment at {path}: {source}")]
Open {
path: PathBuf,
#[source]
source: heed::Error,
},
#[error("LMDB transaction error: {0}")]
Txn(#[source] heed::Error),
#[error("LMDB read error: {0}")]
Read(#[source] heed::Error),
#[error("LMDB write error: {0}")]
Write(#[source] heed::Error),
#[error("Could not open LMDB database {name:?}: {source}")]
OpenDb {
name: String,
#[source]
source: heed::Error,
},
#[error(
"LMDB map is full (capacity {capacity}). The map size is a compile-time constant per \
store; increase it and rebuild."
)]
MapFull { capacity: ByteSize },
#[error("Could not copy LMDB environment to {dst}: {source}")]
Copy {
dst: PathBuf,
#[source]
source: heed::Error,
},
}
impl LmdbLayerError {
pub fn is_map_full(&self) -> bool {
matches!(
self,
LmdbLayerError::Write(heed::Error::Mdb(MdbError::MapFull))
| LmdbLayerError::Txn(heed::Error::Mdb(MdbError::MapFull))
)
}
pub fn is_env_already_opened(&self) -> bool {
matches!(
self,
LmdbLayerError::Open {
source: heed::Error::EnvAlreadyOpened,
..
}
)
}
}