index: split DefaultIndexStoreError::Io variant, extract save helper

Since OpStoreError can also include io::Error, it doesn't make much sense to
have Io variant at this level. Let's split it to context-specific errors, and
extract helper method that maps io::Error.
This commit is contained in:
Yuya Nishihara 2023-12-17 11:48:35 +09:00
parent bf4a4e70b1
commit b733d52557

View file

@ -46,8 +46,13 @@ pub enum IndexLoadError {
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum DefaultIndexStoreError { pub enum DefaultIndexStoreError {
#[error(transparent)] #[error("Failed to associate commit index file with a operation {op_id:?}: {source}")]
Io(#[from] io::Error), AssociateIndex {
op_id: OperationId,
source: io::Error,
},
#[error("Failed to write commit index file: {0}")]
SaveIndex(#[source] io::Error),
#[error(transparent)] #[error(transparent)]
OpStore(#[from] OpStoreError), OpStore(#[from] OpStoreError),
} }
@ -176,8 +181,7 @@ impl DefaultIndexStore {
mutable_index.add_commit(commit); mutable_index.add_commit(commit);
} }
let index_file = mutable_index.save_in(self.dir.clone())?; let index_file = self.save_mutable_index(mutable_index, operation.id())?;
self.associate_file_with_operation(&index_file, operation.id())?;
tracing::info!( tracing::info!(
?index_file, ?index_file,
commits_count = commits.len(), commits_count = commits.len(),
@ -187,6 +191,22 @@ impl DefaultIndexStore {
Ok(index_file) Ok(index_file)
} }
fn save_mutable_index(
&self,
mutable_index: DefaultMutableIndex,
op_id: &OperationId,
) -> Result<Arc<ReadonlyIndexSegment>, DefaultIndexStoreError> {
let index_segment = mutable_index
.save_in(self.dir.clone())
.map_err(DefaultIndexStoreError::SaveIndex)?;
self.associate_file_with_operation(&index_segment, op_id)
.map_err(|source| DefaultIndexStoreError::AssociateIndex {
op_id: op_id.to_owned(),
source,
})?;
Ok(index_segment)
}
/// Records a link from the given operation to the this index version. /// Records a link from the given operation to the this index version.
fn associate_file_with_operation( fn associate_file_with_operation(
&self, &self,
@ -248,18 +268,9 @@ impl IndexStore for DefaultIndexStore {
.into_any() .into_any()
.downcast::<DefaultMutableIndex>() .downcast::<DefaultMutableIndex>()
.expect("index to merge in must be a DefaultMutableIndex"); .expect("index to merge in must be a DefaultMutableIndex");
let index_segment = index.save_in(self.dir.clone()).map_err(|err| { let index_segment = self
IndexWriteError(format!("Failed to write commit index file: {err}").into()) .save_mutable_index(*index, op_id)
})?; .map_err(|err| IndexWriteError(err.into()))?;
self.associate_file_with_operation(&index_segment, op_id)
.map_err(|err| {
IndexWriteError(
format!(
"Failed to associate commit index file with a operation {op_id:?}: {err}"
)
.into(),
)
})?;
Ok(Box::new(DefaultReadonlyIndex::from_segment(index_segment))) Ok(Box::new(DefaultReadonlyIndex::from_segment(index_segment)))
} }
} }