ok/jj
1
0
Fork 0
forked from mirrors/jj

index: propagate DefaultIndexStore::init/reinit() errors

This commit is contained in:
Yuya Nishihara 2023-12-17 18:50:53 +09:00
parent 955f6e356a
commit 3abe6be384
4 changed files with 28 additions and 15 deletions

View file

@ -237,7 +237,9 @@ fn cmd_debug_reindex(
let default_index_store: Option<&DefaultIndexStore> = let default_index_store: Option<&DefaultIndexStore> =
repo.index_store().as_any().downcast_ref(); repo.index_store().as_any().downcast_ref();
if let Some(default_index_store) = default_index_store { if let Some(default_index_store) = default_index_store {
default_index_store.reinit(); default_index_store
.reinit()
.map_err(|err| CommandError::InternalError(err.to_string()))?;
let repo = repo.reload_at(repo.operation())?; let repo = repo.reload_at(repo.operation())?;
let default_index: &DefaultReadonlyIndex = repo let default_index: &DefaultReadonlyIndex = repo
.readonly_index() .readonly_index()

View file

@ -28,7 +28,7 @@ pub use self::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError};
pub use self::rev_walk::{ pub use self::rev_walk::{
RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange, RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange,
}; };
pub use self::store::{DefaultIndexStore, DefaultIndexStoreError}; pub use self::store::{DefaultIndexStore, DefaultIndexStoreError, DefaultIndexStoreInitError};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -26,10 +26,10 @@ use thiserror::Error;
use super::mutable::DefaultMutableIndex; use super::mutable::DefaultMutableIndex;
use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment}; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment};
use crate::backend::{BackendError, CommitId, ObjectId}; use crate::backend::{BackendError, BackendInitError, CommitId, ObjectId};
use crate::commit::CommitByCommitterTimestamp; use crate::commit::CommitByCommitterTimestamp;
use crate::dag_walk; use crate::dag_walk;
use crate::file_util::persist_content_addressed_temp_file; use crate::file_util::{persist_content_addressed_temp_file, IoResultExt as _, PathError};
use crate::index::{ use crate::index::{
Index, IndexReadError, IndexStore, IndexWriteError, MutableIndex, ReadonlyIndex, Index, IndexReadError, IndexStore, IndexWriteError, MutableIndex, ReadonlyIndex,
}; };
@ -37,6 +37,17 @@ use crate::op_store::{OpStoreError, OperationId};
use crate::operation::Operation; use crate::operation::Operation;
use crate::store::Store; use crate::store::Store;
/// Error that may occur during `DefaultIndexStore` initialization.
#[derive(Debug, Error)]
#[error("Failed to initialize index store: {0}")]
pub struct DefaultIndexStoreInitError(#[from] pub PathError);
impl From<DefaultIndexStoreInitError> for BackendInitError {
fn from(err: DefaultIndexStoreInitError) -> Self {
BackendInitError(err.into())
}
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum DefaultIndexStoreError { pub enum DefaultIndexStoreError {
#[error( #[error(
@ -69,11 +80,12 @@ impl DefaultIndexStore {
"default" "default"
} }
pub fn init(dir: &Path) -> Self { pub fn init(dir: &Path) -> Result<Self, DefaultIndexStoreInitError> {
std::fs::create_dir(dir.join("operations")).unwrap(); let op_dir = dir.join("operations");
DefaultIndexStore { std::fs::create_dir(&op_dir).context(&op_dir)?;
Ok(DefaultIndexStore {
dir: dir.to_owned(), dir: dir.to_owned(),
} })
} }
pub fn load(dir: &Path) -> DefaultIndexStore { pub fn load(dir: &Path) -> DefaultIndexStore {
@ -82,10 +94,11 @@ impl DefaultIndexStore {
} }
} }
pub fn reinit(&self) { pub fn reinit(&self) -> Result<(), DefaultIndexStoreInitError> {
let op_dir = self.dir.join("operations"); let op_dir = self.dir.join("operations");
std::fs::remove_dir_all(&op_dir).unwrap(); std::fs::remove_dir_all(&op_dir).context(&op_dir)?;
std::fs::create_dir(op_dir).unwrap(); std::fs::create_dir(&op_dir).context(&op_dir)?;
Ok(())
} }
fn load_index_at_operation( fn load_index_at_operation(
@ -256,9 +269,7 @@ impl IndexStore for DefaultIndexStore {
// we just reindex. // we just reindex.
// TODO: Move this message to a callback or something. // TODO: Move this message to a callback or something.
println!("The index was corrupt (maybe the format has changed). Reindexing..."); println!("The index was corrupt (maybe the format has changed). Reindexing...");
// TODO: propagate error self.reinit().map_err(|err| IndexReadError(err.into()))?;
std::fs::remove_dir_all(self.dir.join("operations")).unwrap();
std::fs::create_dir(self.dir.join("operations")).unwrap();
self.index_at_operation(store, op) self.index_at_operation(store, op)
} }
result => result, result => result,

View file

@ -132,7 +132,7 @@ impl ReadonlyRepo {
} }
pub fn default_index_store_initializer() -> &'static IndexStoreInitializer<'static> { pub fn default_index_store_initializer() -> &'static IndexStoreInitializer<'static> {
&|_settings, store_path| Ok(Box::new(DefaultIndexStore::init(store_path))) &|_settings, store_path| Ok(Box::new(DefaultIndexStore::init(store_path)?))
} }
pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer<'static> { pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer<'static> {