op_store: propagate initialization error

This commit is contained in:
Yuya Nishihara 2025-02-17 17:35:24 +09:00
parent caaa2ab201
commit 4ed0e1330a
2 changed files with 24 additions and 7 deletions

View File

@ -157,7 +157,9 @@ pub enum RepoInitError {
impl ReadonlyRepo { impl ReadonlyRepo {
pub fn default_op_store_initializer() -> &'static OpStoreInitializer<'static> { pub fn default_op_store_initializer() -> &'static OpStoreInitializer<'static> {
&|_settings, store_path, root_data| Ok(Box::new(SimpleOpStore::init(store_path, root_data))) &|_settings, store_path, root_data| {
Ok(Box::new(SimpleOpStore::init(store_path, root_data)?))
}
} }
pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer<'static> { pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer<'static> {

View File

@ -32,6 +32,7 @@ use prost::Message;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use thiserror::Error; use thiserror::Error;
use crate::backend::BackendInitError;
use crate::backend::CommitId; use crate::backend::CommitId;
use crate::backend::MillisSinceEpoch; use crate::backend::MillisSinceEpoch;
use crate::backend::Timestamp; use crate::backend::Timestamp;
@ -64,6 +65,17 @@ use crate::op_store::WorkspaceId;
const OPERATION_ID_LENGTH: usize = 64; const OPERATION_ID_LENGTH: usize = 64;
const VIEW_ID_LENGTH: usize = 64; const VIEW_ID_LENGTH: usize = 64;
/// Error that may occur during [`SimpleOpStore`] initialization.
#[derive(Debug, Error)]
#[error("Failed to initialize simple operation store")]
pub struct SimpleOpStoreInitError(#[from] pub PathError);
impl From<SimpleOpStoreInitError> for BackendInitError {
fn from(err: SimpleOpStoreInitError) -> Self {
BackendInitError(err.into())
}
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("Failed to read {kind} with ID {id}")] #[error("Failed to read {kind} with ID {id}")]
struct DecodeError { struct DecodeError {
@ -92,11 +104,14 @@ impl SimpleOpStore {
"simple_op_store" "simple_op_store"
} }
/// Creates an empty OpStore, panics if it already exists /// Creates an empty OpStore. Returns error if it already exists.
pub fn init(store_path: &Path, root_data: RootOperationData) -> Self { pub fn init(
store_path: &Path,
root_data: RootOperationData,
) -> Result<Self, SimpleOpStoreInitError> {
let store = Self::new(store_path, root_data); let store = Self::new(store_path, root_data);
store.init_base_dirs().unwrap(); // TODO: propagate error store.init_base_dirs()?;
store Ok(store)
} }
/// Load an existing OpStore /// Load an existing OpStore
@ -794,7 +809,7 @@ mod tests {
let root_data = RootOperationData { let root_data = RootOperationData {
root_commit_id: CommitId::from_hex("000000"), root_commit_id: CommitId::from_hex("000000"),
}; };
let store = SimpleOpStore::init(temp_dir.path(), root_data); let store = SimpleOpStore::init(temp_dir.path(), root_data).unwrap();
let view = create_view(); let view = create_view();
let view_id = store.write_view(&view).unwrap(); let view_id = store.write_view(&view).unwrap();
let read_view = store.read_view(&view_id).unwrap(); let read_view = store.read_view(&view_id).unwrap();
@ -807,7 +822,7 @@ mod tests {
let root_data = RootOperationData { let root_data = RootOperationData {
root_commit_id: CommitId::from_hex("000000"), root_commit_id: CommitId::from_hex("000000"),
}; };
let store = SimpleOpStore::init(temp_dir.path(), root_data); let store = SimpleOpStore::init(temp_dir.path(), root_data).unwrap();
let operation = create_operation(); let operation = create_operation();
let op_id = store.write_operation(&operation).unwrap(); let op_id = store.write_operation(&operation).unwrap();
let read_operation = store.read_operation(&op_id).unwrap(); let read_operation = store.read_operation(&op_id).unwrap();