index: add stub IndexReadError type

This is needed to remove .unwrap()s from DefaultIndexStore.
This commit is contained in:
Yuya Nishihara 2023-12-17 14:47:14 +09:00
parent d49b079494
commit b5de16007e
3 changed files with 22 additions and 4 deletions

View file

@ -31,7 +31,9 @@ use crate::backend::{CommitId, ObjectId};
use crate::commit::CommitByCommitterTimestamp;
use crate::dag_walk;
use crate::file_util::persist_content_addressed_temp_file;
use crate::index::{Index, IndexStore, IndexWriteError, MutableIndex, ReadonlyIndex};
use crate::index::{
Index, IndexReadError, IndexStore, IndexWriteError, MutableIndex, ReadonlyIndex,
};
use crate::op_store::{OpStoreError, OperationId};
use crate::operation::Operation;
use crate::store::Store;
@ -233,7 +235,11 @@ impl IndexStore for DefaultIndexStore {
Self::name()
}
fn get_index_at_op(&self, op: &Operation, store: &Arc<Store>) -> Box<dyn ReadonlyIndex> {
fn get_index_at_op(
&self,
op: &Operation,
store: &Arc<Store>,
) -> Result<Box<dyn ReadonlyIndex>, IndexReadError> {
let op_id_hex = op.id().hex();
let op_id_file = self.dir.join("operations").join(op_id_hex);
let index_segment = if op_id_file.exists() {
@ -256,7 +262,7 @@ impl IndexStore for DefaultIndexStore {
} else {
self.index_at_operation(store, op).unwrap()
};
Box::new(DefaultReadonlyIndex::from_segment(index_segment))
Ok(Box::new(DefaultReadonlyIndex::from_segment(index_segment)))
}
fn write_index(

View file

@ -27,6 +27,11 @@ use crate::operation::Operation;
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store;
/// Error while reading index from the `IndexStore`.
#[derive(Debug, Error)]
#[error(transparent)]
pub struct IndexReadError(pub Box<dyn std::error::Error + Send + Sync>);
/// Error while writing index to the `IndexStore`.
#[derive(Debug, Error)]
#[error(transparent)]
@ -37,7 +42,11 @@ pub trait IndexStore: Send + Sync + Debug {
fn name(&self) -> &str;
fn get_index_at_op(&self, op: &Operation, store: &Arc<Store>) -> Box<dyn ReadonlyIndex>;
fn get_index_at_op(
&self,
op: &Operation,
store: &Arc<Store>,
) -> Result<Box<dyn ReadonlyIndex>, IndexReadError>;
fn write_index(
&self,

View file

@ -253,8 +253,11 @@ impl ReadonlyRepo {
pub fn readonly_index(&self) -> &dyn ReadonlyIndex {
self.index
.get_or_init(|| {
// TODO: somehow propagate error, but it's weird if all callers
// had Result<T, IndexReadError> signature.
self.index_store
.get_index_at_op(&self.operation, &self.store)
.unwrap()
})
.deref()
}