index: add wrapper functions to DefaultMutableIndex to remove pub(super) field

into_segment() could be added instead of save_in(), but I decided to wrap
save_in(). save_in() may squash ancestor files, so it could be considered an
index-level operation.
This commit is contained in:
Yuya Nishihara 2023-12-11 20:06:57 +09:00
parent 5aeeb5f723
commit fbec16b49f
3 changed files with 13 additions and 5 deletions

View File

@ -33,7 +33,6 @@ use smallvec::SmallVec;
pub use self::composite::{CompositeIndex, IndexLevelStats, IndexStats}; pub use self::composite::{CompositeIndex, IndexLevelStats, IndexStats};
pub use self::mutable::DefaultMutableIndex; pub use self::mutable::DefaultMutableIndex;
use self::mutable::MutableIndexSegment;
pub use self::rev_walk::{ pub use self::rev_walk::{
RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange, RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange,
}; };
@ -171,8 +170,7 @@ impl ReadonlyIndex for DefaultReadonlyIndex {
} }
fn start_modification(&self) -> Box<dyn MutableIndex> { fn start_modification(&self) -> Box<dyn MutableIndex> {
let mutable_segment = MutableIndexSegment::incremental(self.0.clone()); Box::new(DefaultMutableIndex::incremental(self.0.clone()))
Box::new(DefaultMutableIndex(mutable_segment))
} }
} }
@ -602,6 +600,7 @@ mod tests {
use smallvec::smallvec_inline; use smallvec::smallvec_inline;
use test_case::test_case; use test_case::test_case;
use super::mutable::MutableIndexSegment;
use super::*; use super::*;
use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::index::Index; use crate::index::Index;

View File

@ -395,7 +395,7 @@ impl IndexSegment for MutableIndexSegment {
} }
/// In-memory mutable records for the on-disk commit index backend. /// In-memory mutable records for the on-disk commit index backend.
pub struct DefaultMutableIndex(pub(super) MutableIndexSegment); pub struct DefaultMutableIndex(MutableIndexSegment);
impl DefaultMutableIndex { impl DefaultMutableIndex {
#[cfg(test)] #[cfg(test)]
@ -404,6 +404,11 @@ impl DefaultMutableIndex {
DefaultMutableIndex(mutable_segment) DefaultMutableIndex(mutable_segment)
} }
pub(super) fn incremental(parent_file: Arc<ReadonlyIndexSegment>) -> Self {
let mutable_segment = MutableIndexSegment::incremental(parent_file);
DefaultMutableIndex(mutable_segment)
}
pub fn as_composite(&self) -> CompositeIndex { pub fn as_composite(&self) -> CompositeIndex {
self.0.as_composite() self.0.as_composite()
} }
@ -417,6 +422,10 @@ impl DefaultMutableIndex {
) { ) {
self.0.add_commit_data(commit_id, change_id, parent_ids); self.0.add_commit_data(commit_id, change_id, parent_ids);
} }
pub(super) fn save_in(self, dir: PathBuf) -> io::Result<Arc<ReadonlyIndexSegment>> {
self.0.save_in(dir)
}
} }
impl Index for DefaultMutableIndex { impl Index for DefaultMutableIndex {

View File

@ -248,7 +248,7 @@ impl IndexStore for DefaultIndexStore {
.into_any() .into_any()
.downcast::<DefaultMutableIndex>() .downcast::<DefaultMutableIndex>()
.expect("index to merge in must be a DefaultMutableIndex"); .expect("index to merge in must be a DefaultMutableIndex");
let index_segment = index.0.save_in(self.dir.clone()).map_err(|err| { let index_segment = index.save_in(self.dir.clone()).map_err(|err| {
IndexWriteError::Other(format!("Failed to write commit index file: {err}")) IndexWriteError::Other(format!("Failed to write commit index file: {err}"))
})?; })?;
self.associate_file_with_operation(&index_segment, op_id) self.associate_file_with_operation(&index_segment, op_id)