Skip to content

Commit

Permalink
index: split DefaultIndexStoreError::Io variant, extract save helper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yuja committed Dec 17, 2023
1 parent 68b5c70 commit 2e85430
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/src/default_index/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ pub enum IndexLoadError {

#[derive(Debug, Error)]
pub enum DefaultIndexStoreError {
#[error(transparent)]
Io(#[from] io::Error),
#[error("Failed to associate commit index file with a operation {op_id:?}: {source}")]
AssociateIndex {
op_id: OperationId,
source: io::Error,
},
#[error("Failed to write commit index file: {0}")]
SaveIndex(#[source] io::Error),
#[error(transparent)]
OpStore(#[from] OpStoreError),
}
Expand Down Expand Up @@ -176,8 +181,7 @@ impl DefaultIndexStore {
mutable_index.add_commit(commit);
}

let index_file = mutable_index.save_in(self.dir.clone())?;
self.associate_file_with_operation(&index_file, operation.id())?;
let index_file = self.save_mutable_index(mutable_index, operation.id())?;
tracing::info!(
?index_file,
commits_count = commits.len(),
Expand All @@ -187,6 +191,22 @@ impl DefaultIndexStore {
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.
fn associate_file_with_operation(
&self,
Expand Down Expand Up @@ -248,18 +268,9 @@ impl IndexStore for DefaultIndexStore {
.into_any()
.downcast::<DefaultMutableIndex>()
.expect("index to merge in must be a DefaultMutableIndex");
let index_segment = index.save_in(self.dir.clone()).map_err(|err| {
IndexWriteError(format!("Failed to write commit index file: {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(),
)
})?;
let index_segment = self
.save_mutable_index(*index, op_id)
.map_err(|err| IndexWriteError(err.into()))?;
Ok(Box::new(DefaultReadonlyIndex::from_segment(index_segment)))
}
}

0 comments on commit 2e85430

Please sign in to comment.