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.
This commit is contained in:
Yuya Nishihara 2023-12-17 12:00:42 +09:00
parent b5de16007e
commit 31b6e93c6e
4 changed files with 20 additions and 20 deletions

View File

@ -24,11 +24,11 @@ mod store;
pub use self::composite::{AsCompositeIndex, CompositeIndex, IndexLevelStats, IndexStats}; pub use self::composite::{AsCompositeIndex, CompositeIndex, IndexLevelStats, IndexStats};
pub use self::entry::{IndexEntry, IndexPosition}; pub use self::entry::{IndexEntry, IndexPosition};
pub use self::mutable::DefaultMutableIndex; pub use self::mutable::DefaultMutableIndex;
pub use self::readonly::DefaultReadonlyIndex; 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, IndexLoadError}; pub use self::store::{DefaultIndexStore, DefaultIndexStoreError};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -32,8 +32,7 @@ use tempfile::NamedTempFile;
use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment};
use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec};
use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment};
use super::store::IndexLoadError;
use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::commit::Commit; use crate::commit::Commit;
use crate::file_util::persist_content_addressed_temp_file; use crate::file_util::persist_content_addressed_temp_file;
@ -302,10 +301,10 @@ impl MutableIndexSegment {
change_id_length, change_id_length,
) )
.map_err(|err| match err { .map_err(|err| match err {
IndexLoadError::IndexCorrupt(err) => { ReadonlyIndexLoadError::IndexCorrupt(err) => {
panic!("Just-created index file is corrupt: {err}") panic!("Just-created index file is corrupt: {err}")
} }
IndexLoadError::IoError(err) => err, ReadonlyIndexLoadError::IoError(err) => err,
}) })
} }
} }

View File

@ -18,23 +18,32 @@ use std::any::Any;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::fs::File; use std::fs::File;
use std::io;
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use smallvec::SmallVec; use smallvec::SmallVec;
use thiserror::Error;
use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment};
use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec};
use super::mutable::DefaultMutableIndex; use super::mutable::DefaultMutableIndex;
use super::store::IndexLoadError;
use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::default_revset_engine; use crate::default_revset_engine;
use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex};
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store; 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> { struct CommitGraphEntry<'a> {
data: &'a [u8], data: &'a [u8],
commit_id_length: usize, commit_id_length: usize,
@ -166,7 +175,7 @@ impl ReadonlyIndexSegment {
name: String, name: String,
commit_id_length: usize, commit_id_length: usize,
change_id_length: usize, change_id_length: usize,
) -> Result<Arc<ReadonlyIndexSegment>, IndexLoadError> { ) -> Result<Arc<ReadonlyIndexSegment>, ReadonlyIndexLoadError> {
let parent_filename_len = file.read_u32::<LittleEndian>()?; let parent_filename_len = file.read_u32::<LittleEndian>()?;
let num_parent_commits; let num_parent_commits;
let maybe_parent_file; let maybe_parent_file;
@ -200,7 +209,7 @@ impl ReadonlyIndexSegment {
let parent_overflow_size = (num_parent_overflow_entries as usize) * 4; let parent_overflow_size = (num_parent_overflow_entries as usize) * 4;
let expected_size = graph_size + lookup_size + parent_overflow_size; let expected_size = graph_size + lookup_size + parent_overflow_size;
if data.len() != expected_size { if data.len() != expected_size {
return Err(IndexLoadError::IndexCorrupt(name)); return Err(ReadonlyIndexLoadError::IndexCorrupt(name));
} }
Ok(Arc::new(ReadonlyIndexSegment { Ok(Arc::new(ReadonlyIndexSegment {
parent_file: maybe_parent_file, parent_file: maybe_parent_file,

View File

@ -26,7 +26,7 @@ use tempfile::NamedTempFile;
use thiserror::Error; use thiserror::Error;
use super::mutable::DefaultMutableIndex; use super::mutable::DefaultMutableIndex;
use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment};
use crate::backend::{CommitId, ObjectId}; use crate::backend::{CommitId, ObjectId};
use crate::commit::CommitByCommitterTimestamp; use crate::commit::CommitByCommitterTimestamp;
use crate::dag_walk; use crate::dag_walk;
@ -38,14 +38,6 @@ use crate::op_store::{OpStoreError, OperationId};
use crate::operation::Operation; use crate::operation::Operation;
use crate::store::Store; 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)] #[derive(Debug, Error)]
pub enum DefaultIndexStoreError { pub enum DefaultIndexStoreError {
#[error("Failed to associate commit index file with a operation {op_id:?}: {source}")] #[error("Failed to associate commit index file with a operation {op_id:?}: {source}")]
@ -93,7 +85,7 @@ impl DefaultIndexStore {
commit_id_length: usize, commit_id_length: usize,
change_id_length: usize, change_id_length: usize,
op_id: &OperationId, op_id: &OperationId,
) -> Result<Arc<ReadonlyIndexSegment>, IndexLoadError> { ) -> Result<Arc<ReadonlyIndexSegment>, ReadonlyIndexLoadError> {
let op_id_file = self.dir.join("operations").join(op_id.hex()); let op_id_file = self.dir.join("operations").join(op_id.hex());
let buf = fs::read(op_id_file).unwrap(); let buf = fs::read(op_id_file).unwrap();
let index_file_id_hex = String::from_utf8(buf).unwrap(); let index_file_id_hex = String::from_utf8(buf).unwrap();
@ -248,7 +240,7 @@ impl IndexStore for DefaultIndexStore {
store.change_id_length(), store.change_id_length(),
op.id(), op.id(),
) { ) {
Err(IndexLoadError::IndexCorrupt(_)) => { Err(ReadonlyIndexLoadError::IndexCorrupt(_)) => {
// If the index was corrupt (maybe it was written in a different format), // If the index was corrupt (maybe it was written in a different format),
// we just reindex. // we just reindex.
// TODO: Move this message to a callback or something. // TODO: Move this message to a callback or something.