From 31b6e93c6e48eebdaadfb284cbb12376d66f0171 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 17 Dec 2023 12:00:42 +0900 Subject: [PATCH] index: move IndexLoadError to "readonly" module, rename accordingly I thought IndexLoadError and DefaultIndexStoreError would represent "load" and "store" failures respectively, but they aren't. Actually, DefaultIndexStoreError is the store-level error, and IndexLoadError should be wrapped in it. --- lib/src/default_index/mod.rs | 4 ++-- lib/src/default_index/mutable.rs | 7 +++---- lib/src/default_index/readonly.rs | 15 ++++++++++++--- lib/src/default_index/store.rs | 14 +++----------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index b4dc5440d..ce17c5c18 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -24,11 +24,11 @@ mod store; pub use self::composite::{AsCompositeIndex, CompositeIndex, IndexLevelStats, IndexStats}; pub use self::entry::{IndexEntry, IndexPosition}; pub use self::mutable::DefaultMutableIndex; -pub use self::readonly::DefaultReadonlyIndex; +pub use self::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError}; pub use self::rev_walk::{ RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange, }; -pub use self::store::{DefaultIndexStore, DefaultIndexStoreError, IndexLoadError}; +pub use self::store::{DefaultIndexStore, DefaultIndexStoreError}; #[cfg(test)] mod tests { diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index a3a569792..6d22f253c 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -32,8 +32,7 @@ use tempfile::NamedTempFile; use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; -use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; -use super::store::IndexLoadError; +use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment}; use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::commit::Commit; use crate::file_util::persist_content_addressed_temp_file; @@ -302,10 +301,10 @@ impl MutableIndexSegment { change_id_length, ) .map_err(|err| match err { - IndexLoadError::IndexCorrupt(err) => { + ReadonlyIndexLoadError::IndexCorrupt(err) => { panic!("Just-created index file is corrupt: {err}") } - IndexLoadError::IoError(err) => err, + ReadonlyIndexLoadError::IoError(err) => err, }) } } diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 62273a168..ae19d1fe7 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -18,23 +18,32 @@ use std::any::Any; use std::cmp::Ordering; use std::fmt::{Debug, Formatter}; use std::fs::File; +use std::io; use std::io::Read; use std::path::Path; use std::sync::Arc; use byteorder::{LittleEndian, ReadBytesExt}; use smallvec::SmallVec; +use thiserror::Error; use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::mutable::DefaultMutableIndex; -use super::store::IndexLoadError; use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::default_revset_engine; use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::store::Store; +#[derive(Debug, Error)] +pub enum ReadonlyIndexLoadError { + #[error("Index file '{0}' is corrupt.")] + IndexCorrupt(String), + #[error("I/O error while loading index file: {0}")] + IoError(#[from] io::Error), +} + struct CommitGraphEntry<'a> { data: &'a [u8], commit_id_length: usize, @@ -166,7 +175,7 @@ impl ReadonlyIndexSegment { name: String, commit_id_length: usize, change_id_length: usize, - ) -> Result, IndexLoadError> { + ) -> Result, ReadonlyIndexLoadError> { let parent_filename_len = file.read_u32::()?; let num_parent_commits; let maybe_parent_file; @@ -200,7 +209,7 @@ impl ReadonlyIndexSegment { let parent_overflow_size = (num_parent_overflow_entries as usize) * 4; let expected_size = graph_size + lookup_size + parent_overflow_size; if data.len() != expected_size { - return Err(IndexLoadError::IndexCorrupt(name)); + return Err(ReadonlyIndexLoadError::IndexCorrupt(name)); } Ok(Arc::new(ReadonlyIndexSegment { parent_file: maybe_parent_file, diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index 3f8a53c16..3dd870ff9 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -26,7 +26,7 @@ use tempfile::NamedTempFile; use thiserror::Error; use super::mutable::DefaultMutableIndex; -use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; +use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment}; use crate::backend::{CommitId, ObjectId}; use crate::commit::CommitByCommitterTimestamp; use crate::dag_walk; @@ -38,14 +38,6 @@ use crate::op_store::{OpStoreError, OperationId}; use crate::operation::Operation; use crate::store::Store; -#[derive(Error, Debug)] -pub enum IndexLoadError { - #[error("Index file '{0}' is corrupt.")] - IndexCorrupt(String), - #[error("I/O error while loading index file: {0}")] - IoError(#[from] io::Error), -} - #[derive(Debug, Error)] pub enum DefaultIndexStoreError { #[error("Failed to associate commit index file with a operation {op_id:?}: {source}")] @@ -93,7 +85,7 @@ impl DefaultIndexStore { commit_id_length: usize, change_id_length: usize, op_id: &OperationId, - ) -> Result, IndexLoadError> { + ) -> Result, ReadonlyIndexLoadError> { let op_id_file = self.dir.join("operations").join(op_id.hex()); let buf = fs::read(op_id_file).unwrap(); let index_file_id_hex = String::from_utf8(buf).unwrap(); @@ -248,7 +240,7 @@ impl IndexStore for DefaultIndexStore { store.change_id_length(), op.id(), ) { - Err(IndexLoadError::IndexCorrupt(_)) => { + Err(ReadonlyIndexLoadError::IndexCorrupt(_)) => { // If the index was corrupt (maybe it was written in a different format), // we just reindex. // TODO: Move this message to a callback or something.