From c6cd9c450e3a984c3307c30ca3a54e0f52f53ecd Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 14:07:45 +0900 Subject: [PATCH 1/4] index: extract merge_in() function that works on segment types Prepares for splitting MutableIndexImpl into segment and index wrapper types. --- lib/src/default_index_store.rs | 65 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 8429d0f622..7b56ba9e78 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -506,6 +506,39 @@ impl MutableIndexImpl { } } + fn merge_in(&mut self, other: Arc) { + let mut maybe_own_ancestor = self.parent_file.clone(); + let mut maybe_other_ancestor = Some(other); + let mut files_to_add = vec![]; + loop { + if maybe_other_ancestor.is_none() { + break; + } + let other_ancestor = maybe_other_ancestor.as_ref().unwrap(); + if maybe_own_ancestor.is_none() { + files_to_add.push(other_ancestor.clone()); + maybe_other_ancestor = other_ancestor.parent_file.clone(); + continue; + } + let own_ancestor = maybe_own_ancestor.as_ref().unwrap(); + if own_ancestor.name == other_ancestor.name { + break; + } + if own_ancestor.as_composite().num_commits() + < other_ancestor.as_composite().num_commits() + { + files_to_add.push(other_ancestor.clone()); + maybe_other_ancestor = other_ancestor.parent_file.clone(); + } else { + maybe_own_ancestor = own_ancestor.parent_file.clone(); + } + } + + for file in files_to_add.iter().rev() { + self.add_commits_from(file.as_ref()); + } + } + fn serialize(self) -> Vec { assert_eq!(self.graph.len(), self.lookup.len()); @@ -708,37 +741,7 @@ impl MutableIndex for MutableIndexImpl { .as_any() .downcast_ref::() .expect("index to merge in must be a DefaultReadonlyIndex"); - - let mut maybe_own_ancestor = self.parent_file.clone(); - let mut maybe_other_ancestor = Some(other.0.clone()); - let mut files_to_add = vec![]; - loop { - if maybe_other_ancestor.is_none() { - break; - } - let other_ancestor = maybe_other_ancestor.as_ref().unwrap(); - if maybe_own_ancestor.is_none() { - files_to_add.push(other_ancestor.clone()); - maybe_other_ancestor = other_ancestor.parent_file.clone(); - continue; - } - let own_ancestor = maybe_own_ancestor.as_ref().unwrap(); - if own_ancestor.name == other_ancestor.name { - break; - } - if own_ancestor.as_composite().num_commits() - < other_ancestor.as_composite().num_commits() - { - files_to_add.push(other_ancestor.clone()); - maybe_other_ancestor = other_ancestor.parent_file.clone(); - } else { - maybe_own_ancestor = own_ancestor.parent_file.clone(); - } - } - - for file in files_to_add.iter().rev() { - self.add_commits_from(file.as_ref()); - } + self.merge_in(other.0.clone()); } } From d51d7a8796623fd954261560e7ae150e92f169f6 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 14:15:16 +0900 Subject: [PATCH 2/4] index: duplicate add_commit() to MutableIndexImpl --- lib/src/default_index_store.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 7b56ba9e78..c4df8d3ff1 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -465,6 +465,14 @@ impl MutableIndexImpl { CompositeIndex(self) } + fn add_commit(&mut self, commit: &Commit) { + self.add_commit_data( + commit.id().clone(), + commit.change_id().clone(), + commit.parent_ids(), + ); + } + pub(crate) fn add_commit_data( &mut self, commit_id: CommitId, @@ -729,11 +737,7 @@ impl MutableIndex for MutableIndexImpl { } fn add_commit(&mut self, commit: &Commit) { - self.add_commit_data( - commit.id().clone(), - commit.change_id().clone(), - commit.parent_ids(), - ); + self.add_commit(commit); } fn merge_in(&mut self, other: &dyn ReadonlyIndex) { From 52e8c61dce53a7fef034cef1470c3cccb2531d89 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 13:34:32 +0900 Subject: [PATCH 3/4] index: add DefaultMutableIndex wrapper, move Index impls to it The wrapper type isn't needed for the mutable layer, but this mirrors the readonly type structure. Test cases are also migrated to be using the index wrapper so long as we don't have to care for the nesting of the segment files. --- lib/src/default_index_store.rs | 93 +++++++++++++++++++++----------- lib/src/default_revset_engine.rs | 4 +- lib/tests/test_index.rs | 4 +- 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index c4df8d3ff1..51fb3b7e6f 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -248,9 +248,9 @@ impl IndexStore for DefaultIndexStore { ) -> Result, IndexWriteError> { let index = index .into_any() - .downcast::() - .expect("index to merge in must be a MutableIndexImpl"); - let index_segment = index.save_in(self.dir.clone()).map_err(|err| { + .downcast::() + .expect("index to merge in must be a DefaultMutableIndex"); + let index_segment = index.0.save_in(self.dir.clone()).map_err(|err| { IndexWriteError::Other(format!("Failed to write commit index file: {err}")) })?; self.associate_file_with_operation(&index_segment, op_id) @@ -399,7 +399,8 @@ impl ReadonlyIndex for DefaultReadonlyIndex { } fn start_modification(&self) -> Box { - Box::new(MutableIndexImpl::incremental(self.0.clone())) + let mutable_segment = MutableIndexImpl::incremental(self.0.clone()); + Box::new(DefaultMutableIndex(mutable_segment)) } } @@ -426,7 +427,7 @@ struct MutableGraphEntry { parent_positions: SmallIndexPositionsVec, } -pub struct MutableIndexImpl { +struct MutableIndexImpl { parent_file: Option>, num_parent_commits: u32, commit_id_length: usize, @@ -436,7 +437,7 @@ pub struct MutableIndexImpl { } impl MutableIndexImpl { - pub(crate) fn full(commit_id_length: usize, change_id_length: usize) -> Self { + fn full(commit_id_length: usize, change_id_length: usize) -> Self { Self { parent_file: None, num_parent_commits: 0, @@ -461,7 +462,7 @@ impl MutableIndexImpl { } } - pub fn as_composite(&self) -> CompositeIndex { + fn as_composite(&self) -> CompositeIndex { CompositeIndex(self) } @@ -473,13 +474,13 @@ impl MutableIndexImpl { ); } - pub(crate) fn add_commit_data( + fn add_commit_data( &mut self, commit_id: CommitId, change_id: ChangeId, parent_ids: &[CommitId], ) { - if self.has_id(&commit_id) { + if self.as_composite().has_id(&commit_id) { return; } let mut entry = MutableGraphEntry { @@ -685,33 +686,59 @@ impl MutableIndexImpl { } } -impl Index for MutableIndexImpl { +/// In-memory mutable records for the on-disk commit index backend. +pub struct DefaultMutableIndex(MutableIndexImpl); + +impl DefaultMutableIndex { + #[cfg(test)] + pub(crate) fn full(commit_id_length: usize, change_id_length: usize) -> Self { + let mutable_segment = MutableIndexImpl::full(commit_id_length, change_id_length); + DefaultMutableIndex(mutable_segment) + } + + pub fn as_composite(&self) -> CompositeIndex { + self.0.as_composite() + } + + #[cfg(test)] + pub(crate) fn add_commit_data( + &mut self, + commit_id: CommitId, + change_id: ChangeId, + parent_ids: &[CommitId], + ) { + self.0.add_commit_data(commit_id, change_id, parent_ids); + } +} + +impl Index for DefaultMutableIndex { fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize { - CompositeIndex(self).shortest_unique_commit_id_prefix_len(commit_id) + self.as_composite() + .shortest_unique_commit_id_prefix_len(commit_id) } fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - CompositeIndex(self).resolve_prefix(prefix) + self.as_composite().resolve_prefix(prefix) } fn has_id(&self, commit_id: &CommitId) -> bool { - CompositeIndex(self).has_id(commit_id) + self.as_composite().has_id(commit_id) } fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> bool { - CompositeIndex(self).is_ancestor(ancestor_id, descendant_id) + self.as_composite().is_ancestor(ancestor_id, descendant_id) } fn common_ancestors(&self, set1: &[CommitId], set2: &[CommitId]) -> Vec { - CompositeIndex(self).common_ancestors(set1, set2) + self.as_composite().common_ancestors(set1, set2) } fn heads(&self, candidates: &mut dyn Iterator) -> Vec { - CompositeIndex(self).heads(candidates) + self.as_composite().heads(candidates) } fn topo_order(&self, input: &mut dyn Iterator) -> Vec { - CompositeIndex(self).topo_order(input) + self.as_composite().topo_order(input) } fn evaluate_revset<'index>( @@ -719,11 +746,11 @@ impl Index for MutableIndexImpl { expression: &ResolvedExpression, store: &Arc, ) -> Result + 'index>, RevsetEvaluationError> { - CompositeIndex(self).evaluate_revset(expression, store) + self.as_composite().evaluate_revset(expression, store) } } -impl MutableIndex for MutableIndexImpl { +impl MutableIndex for DefaultMutableIndex { fn as_any(&self) -> &dyn Any { self } @@ -737,7 +764,7 @@ impl MutableIndex for MutableIndexImpl { } fn add_commit(&mut self, commit: &Commit) { - self.add_commit(commit); + self.0.add_commit(commit); } fn merge_in(&mut self, other: &dyn ReadonlyIndex) { @@ -745,7 +772,7 @@ impl MutableIndex for MutableIndexImpl { .as_any() .downcast_ref::() .expect("index to merge in must be a DefaultReadonlyIndex"); - self.merge_in(other.0.clone()); + self.0.merge_in(other.0.clone()); } } @@ -2074,7 +2101,7 @@ mod tests { #[should_panic(expected = "parent commit is not indexed")] fn index_missing_parent_commit() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); let id_0 = CommitId::from_hex("000000"); let id_1 = CommitId::from_hex("111111"); index.add_commit_data(id_1, new_change_id(), &[id_0]); @@ -2284,6 +2311,8 @@ mod tests { index.add_commit_data(id_4, new_change_id(), &[]); index.add_commit_data(id_5, new_change_id(), &[]); + let index = index.as_composite(); + // Can find commits given the full hex number assert_eq!( index.resolve_prefix(&HexPrefix::new(&id_0.hex()).unwrap()), @@ -2482,6 +2511,8 @@ mod tests { index.add_commit_data(id_4.clone(), new_change_id(), &[]); index.add_commit_data(id_5.clone(), new_change_id(), &[]); + let index = index.as_composite(); + // Public API: calculate shortest unique prefix len with known commit_id assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_0), 3); assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_1), 3); @@ -2512,7 +2543,7 @@ mod tests { #[test] fn test_is_ancestor() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 5 // |\ // 4 | 3 @@ -2549,7 +2580,7 @@ mod tests { #[test] fn test_common_ancestors() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 5 // |\ // 4 | @@ -2631,7 +2662,7 @@ mod tests { #[test] fn test_common_ancestors_criss_cross() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 3 4 // |X| // 1 2 @@ -2656,7 +2687,7 @@ mod tests { #[test] fn test_common_ancestors_merge_with_ancestor() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 4 5 // |\ /| // 1 2 3 @@ -2683,7 +2714,7 @@ mod tests { #[test] fn test_walk_revs() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 5 // |\ // 4 | 3 @@ -2765,7 +2796,7 @@ mod tests { #[test] fn test_walk_revs_filter_by_generation() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 8 6 // | | // 7 5 @@ -2857,7 +2888,7 @@ mod tests { #[allow(clippy::redundant_clone)] // allow id_n.clone() fn test_walk_revs_filter_by_generation_range_merging() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // Long linear history with some short branches let ids = (0..11) .map(|n| CommitId::from_hex(&format!("{n:06x}"))) @@ -2914,7 +2945,7 @@ mod tests { #[test] fn test_walk_revs_descendants_filtered_by_generation() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 8 6 // | | // 7 5 @@ -3023,7 +3054,7 @@ mod tests { #[test] fn test_heads() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); // 5 // |\ // 4 | 3 diff --git a/lib/src/default_revset_engine.rs b/lib/src/default_revset_engine.rs index 966aafc08f..fceb5bda4e 100644 --- a/lib/src/default_revset_engine.rs +++ b/lib/src/default_revset_engine.rs @@ -900,7 +900,7 @@ fn has_diff_from_parent( mod tests { use super::*; use crate::backend::{ChangeId, CommitId, ObjectId}; - use crate::default_index_store::MutableIndexImpl; + use crate::default_index_store::DefaultMutableIndex; /// Generator of unique 16-byte ChangeId excluding root id fn change_id_generator() -> impl FnMut() -> ChangeId { @@ -911,7 +911,7 @@ mod tests { #[test] fn test_revset_combinator() { let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut index = DefaultMutableIndex::full(3, 16); let id_0 = CommitId::from_hex("000000"); let id_1 = CommitId::from_hex("111111"); let id_2 = CommitId::from_hex("222222"); diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index b6ed8bb5cb..2370169211 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -18,7 +18,7 @@ use jj_lib::backend::CommitId; use jj_lib::commit::Commit; use jj_lib::commit_builder::CommitBuilder; use jj_lib::default_index_store::{ - CompositeIndex, DefaultReadonlyIndex, IndexPosition, MutableIndexImpl, + CompositeIndex, DefaultMutableIndex, DefaultReadonlyIndex, IndexPosition, }; use jj_lib::index::Index as _; use jj_lib::repo::{MutableRepo, ReadonlyRepo, Repo}; @@ -454,7 +454,7 @@ fn as_readonly_composite(repo: &Arc) -> CompositeIndex<'_> { fn as_mutable_composite(repo: &MutableRepo) -> CompositeIndex<'_> { repo.mutable_index() .as_any() - .downcast_ref::() + .downcast_ref::() .unwrap() .as_composite() } From 78a6dd70d2f36df03d08ffeb32fb87db14f51aa9 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 14:40:47 +0900 Subject: [PATCH 4/4] index: rename MutableIndexImpl to MutableIndexSegment --- lib/src/default_index_store.rs | 151 +++++++++++++++++---------------- 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 51fb3b7e6f..16236534cc 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -136,14 +136,14 @@ impl DefaultIndexStore { match parent_op_id { None => { maybe_parent_file = None; - data = MutableIndexImpl::full(commit_id_length, change_id_length); + data = MutableIndexSegment::full(commit_id_length, change_id_length); } Some(parent_op_id) => { let parent_file = self .load_index_at_operation(commit_id_length, change_id_length, &parent_op_id) .unwrap(); maybe_parent_file = Some(parent_file.clone()); - data = MutableIndexImpl::incremental(parent_file) + data = MutableIndexSegment::incremental(parent_file) } } @@ -399,7 +399,7 @@ impl ReadonlyIndex for DefaultReadonlyIndex { } fn start_modification(&self) -> Box { - let mutable_segment = MutableIndexImpl::incremental(self.0.clone()); + let mutable_segment = MutableIndexSegment::incremental(self.0.clone()); Box::new(DefaultMutableIndex(mutable_segment)) } } @@ -427,7 +427,7 @@ struct MutableGraphEntry { parent_positions: SmallIndexPositionsVec, } -struct MutableIndexImpl { +struct MutableIndexSegment { parent_file: Option>, num_parent_commits: u32, commit_id_length: usize, @@ -436,7 +436,7 @@ struct MutableIndexImpl { lookup: BTreeMap, } -impl MutableIndexImpl { +impl MutableIndexSegment { fn full(commit_id_length: usize, change_id_length: usize) -> Self { Self { parent_file: None, @@ -615,7 +615,7 @@ impl MutableIndexImpl { /// If the MutableIndex has more than half the commits of its parent /// ReadonlyIndex, return MutableIndex with the commits from both. This /// is done recursively, so the stack of index files has O(log n) files. - fn maybe_squash_with_ancestors(self) -> MutableIndexImpl { + fn maybe_squash_with_ancestors(self) -> MutableIndexSegment { let mut num_new_commits = self.segment_num_commits(); let mut files_to_squash = vec![]; let mut maybe_parent_file = self.parent_file.clone(); @@ -626,7 +626,7 @@ impl MutableIndexImpl { // TODO: We should probably also squash if the parent file has less than N // commits, regardless of how many (few) are in `self`. if 2 * num_new_commits < parent_file.segment_num_commits() { - squashed = MutableIndexImpl::incremental(parent_file); + squashed = MutableIndexSegment::incremental(parent_file); break; } num_new_commits += parent_file.segment_num_commits(); @@ -634,7 +634,8 @@ impl MutableIndexImpl { maybe_parent_file = parent_file.parent_file.clone(); } None => { - squashed = MutableIndexImpl::full(self.commit_id_length, self.change_id_length); + squashed = + MutableIndexSegment::full(self.commit_id_length, self.change_id_length); break; } } @@ -687,12 +688,12 @@ impl MutableIndexImpl { } /// In-memory mutable records for the on-disk commit index backend. -pub struct DefaultMutableIndex(MutableIndexImpl); +pub struct DefaultMutableIndex(MutableIndexSegment); impl DefaultMutableIndex { #[cfg(test)] pub(crate) fn full(commit_id_length: usize, change_id_length: usize) -> Self { - let mutable_segment = MutableIndexImpl::full(commit_id_length, change_id_length); + let mutable_segment = MutableIndexSegment::full(commit_id_length, change_id_length); DefaultMutableIndex(mutable_segment) } @@ -1703,7 +1704,7 @@ impl IndexSegment for ReadonlyIndexSegment { } } -impl IndexSegment for MutableIndexImpl { +impl IndexSegment for MutableIndexSegment { fn segment_num_parent_commits(&self) -> u32 { self.num_parent_commits } @@ -2034,12 +2035,12 @@ mod tests { #[test_case(true; "file")] fn index_empty(on_disk: bool) { let temp_dir = testutils::new_temp_dir(); - let index = MutableIndexImpl::full(3, 16); + let mutable_segment = MutableIndexSegment::full(3, 16); let index_segment: Box = if on_disk { - let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); + let saved_index = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { - Box::new(index) + Box::new(mutable_segment) }; let index = CompositeIndex(index_segment.as_ref()); @@ -2062,15 +2063,15 @@ mod tests { fn index_root_commit(on_disk: bool) { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); let id_0 = CommitId::from_hex("000000"); let change_id0 = new_change_id(); - index.add_commit_data(id_0.clone(), change_id0.clone(), &[]); + mutable_segment.add_commit_data(id_0.clone(), change_id0.clone(), &[]); let index_segment: Box = if on_disk { - let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); + let saved_index = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { - Box::new(index) + Box::new(mutable_segment) }; let index = CompositeIndex(index_segment.as_ref()); @@ -2114,7 +2115,7 @@ mod tests { fn index_multiple_commits(incremental: bool, on_disk: bool) { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); // 5 // |\ // 4 | 3 @@ -2131,15 +2132,15 @@ mod tests { // TODO: Remove the exception after https://github.com/rust-lang/rust-clippy/issues/10577 // is fixed or file a new bug. let change_id2 = change_id1.clone(); - index.add_commit_data(id_0.clone(), change_id0, &[]); - index.add_commit_data(id_1.clone(), change_id1.clone(), &[id_0.clone()]); - index.add_commit_data(id_2.clone(), change_id2.clone(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_0.clone(), change_id0, &[]); + mutable_segment.add_commit_data(id_1.clone(), change_id1.clone(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_2.clone(), change_id2.clone(), &[id_0.clone()]); // If testing incremental indexing, write the first three commits to one file // now and build the remainder as another segment on top. if incremental { - let initial_file = index.save_in(temp_dir.path().to_owned()).unwrap(); - index = MutableIndexImpl::incremental(initial_file); + let initial_file = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); + mutable_segment = MutableIndexSegment::incremental(initial_file); } let id_3 = CommitId::from_hex("333333"); @@ -2148,14 +2149,14 @@ mod tests { let change_id4 = new_change_id(); let id_5 = CommitId::from_hex("555555"); let change_id5 = change_id3.clone(); - index.add_commit_data(id_3.clone(), change_id3.clone(), &[id_2.clone()]); - index.add_commit_data(id_4.clone(), change_id4, &[id_1.clone()]); - index.add_commit_data(id_5.clone(), change_id5, &[id_4.clone(), id_2.clone()]); + mutable_segment.add_commit_data(id_3.clone(), change_id3.clone(), &[id_2.clone()]); + mutable_segment.add_commit_data(id_4.clone(), change_id4, &[id_1.clone()]); + mutable_segment.add_commit_data(id_5.clone(), change_id5, &[id_4.clone(), id_2.clone()]); let index_segment: Box = if on_disk { - let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); + let saved_index = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { - Box::new(index) + Box::new(mutable_segment) }; let index = CompositeIndex(index_segment.as_ref()); @@ -2226,7 +2227,7 @@ mod tests { fn index_many_parents(on_disk: bool) { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); // 6 // /|\ // / | \ @@ -2243,22 +2244,22 @@ mod tests { let id_4 = CommitId::from_hex("444444"); let id_5 = CommitId::from_hex("555555"); let id_6 = CommitId::from_hex("666666"); - index.add_commit_data(id_0.clone(), new_change_id(), &[]); - index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); - index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]); - index.add_commit_data(id_3.clone(), new_change_id(), &[id_0.clone()]); - index.add_commit_data(id_4.clone(), new_change_id(), &[id_0.clone()]); - index.add_commit_data(id_5.clone(), new_change_id(), &[id_0]); - index.add_commit_data( + mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[id_0.clone()]); + mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[id_0]); + mutable_segment.add_commit_data( id_6.clone(), new_change_id(), &[id_1, id_2, id_3, id_4, id_5], ); let index_segment: Box = if on_disk { - let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); + let saved_index = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { - Box::new(index) + Box::new(mutable_segment) }; let index = CompositeIndex(index_segment.as_ref()); @@ -2290,28 +2291,28 @@ mod tests { fn resolve_prefix() { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); // Create some commits with different various common prefixes. let id_0 = CommitId::from_hex("000000"); let id_1 = CommitId::from_hex("009999"); let id_2 = CommitId::from_hex("055488"); - index.add_commit_data(id_0.clone(), new_change_id(), &[]); - index.add_commit_data(id_1.clone(), new_change_id(), &[]); - index.add_commit_data(id_2.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]); // Write the first three commits to one file and build the remainder on top. - let initial_file = index.save_in(temp_dir.path().to_owned()).unwrap(); - index = MutableIndexImpl::incremental(initial_file); + let initial_file = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); + mutable_segment = MutableIndexSegment::incremental(initial_file); let id_3 = CommitId::from_hex("055444"); let id_4 = CommitId::from_hex("055555"); let id_5 = CommitId::from_hex("033333"); - index.add_commit_data(id_3, new_change_id(), &[]); - index.add_commit_data(id_4, new_change_id(), &[]); - index.add_commit_data(id_5, new_change_id(), &[]); + mutable_segment.add_commit_data(id_3, new_change_id(), &[]); + mutable_segment.add_commit_data(id_4, new_change_id(), &[]); + mutable_segment.add_commit_data(id_5, new_change_id(), &[]); - let index = index.as_composite(); + let index = mutable_segment.as_composite(); // Can find commits given the full hex number assert_eq!( @@ -2362,26 +2363,26 @@ mod tests { fn neighbor_commit_ids() { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); // Create some commits with different various common prefixes. let id_0 = CommitId::from_hex("000001"); let id_1 = CommitId::from_hex("009999"); let id_2 = CommitId::from_hex("055488"); - index.add_commit_data(id_0.clone(), new_change_id(), &[]); - index.add_commit_data(id_1.clone(), new_change_id(), &[]); - index.add_commit_data(id_2.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]); // Write the first three commits to one file and build the remainder on top. - let initial_file = index.save_in(temp_dir.path().to_owned()).unwrap(); - index = MutableIndexImpl::incremental(initial_file.clone()); + let initial_file = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); + mutable_segment = MutableIndexSegment::incremental(initial_file.clone()); let id_3 = CommitId::from_hex("055444"); let id_4 = CommitId::from_hex("055555"); let id_5 = CommitId::from_hex("033333"); - index.add_commit_data(id_3.clone(), new_change_id(), &[]); - index.add_commit_data(id_4.clone(), new_change_id(), &[]); - index.add_commit_data(id_5.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[]); // Local lookup in readonly index, commit_id exists. assert_eq!( @@ -2413,34 +2414,34 @@ mod tests { // Local lookup in mutable index, commit_id exists. id_5 < id_3 < id_4 assert_eq!( - index.segment_commit_id_to_neighbor_positions(&id_5), + mutable_segment.segment_commit_id_to_neighbor_positions(&id_5), (None, Some(IndexPosition(3))), ); assert_eq!( - index.segment_commit_id_to_neighbor_positions(&id_3), + mutable_segment.segment_commit_id_to_neighbor_positions(&id_3), (Some(IndexPosition(5)), Some(IndexPosition(4))), ); assert_eq!( - index.segment_commit_id_to_neighbor_positions(&id_4), + mutable_segment.segment_commit_id_to_neighbor_positions(&id_4), (Some(IndexPosition(3)), None), ); // Local lookup in mutable index, commit_id does not exist. id_5 < id_3 < id_4 assert_eq!( - index.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("033332")), + mutable_segment.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("033332")), (None, Some(IndexPosition(5))), ); assert_eq!( - index.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("033334")), + mutable_segment.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("033334")), (Some(IndexPosition(5)), Some(IndexPosition(3))), ); assert_eq!( - index.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("ffffff")), + mutable_segment.segment_commit_id_to_neighbor_positions(&CommitId::from_hex("ffffff")), (Some(IndexPosition(4)), None), ); // Global lookup, commit_id exists. id_0 < id_1 < id_5 < id_3 < id_2 < id_4 - let composite_index = CompositeIndex(&index); + let composite_index = CompositeIndex(&mutable_segment); assert_eq!( composite_index.resolve_neighbor_commit_ids(&id_0), (None, Some(id_1.clone())), @@ -2490,28 +2491,28 @@ mod tests { fn shortest_unique_commit_id_prefix() { let temp_dir = testutils::new_temp_dir(); let mut new_change_id = change_id_generator(); - let mut index = MutableIndexImpl::full(3, 16); + let mut mutable_segment = MutableIndexSegment::full(3, 16); // Create some commits with different various common prefixes. let id_0 = CommitId::from_hex("000001"); let id_1 = CommitId::from_hex("009999"); let id_2 = CommitId::from_hex("055488"); - index.add_commit_data(id_0.clone(), new_change_id(), &[]); - index.add_commit_data(id_1.clone(), new_change_id(), &[]); - index.add_commit_data(id_2.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]); // Write the first three commits to one file and build the remainder on top. - let initial_file = index.save_in(temp_dir.path().to_owned()).unwrap(); - index = MutableIndexImpl::incremental(initial_file); + let initial_file = mutable_segment.save_in(temp_dir.path().to_owned()).unwrap(); + mutable_segment = MutableIndexSegment::incremental(initial_file); let id_3 = CommitId::from_hex("055444"); let id_4 = CommitId::from_hex("055555"); let id_5 = CommitId::from_hex("033333"); - index.add_commit_data(id_3.clone(), new_change_id(), &[]); - index.add_commit_data(id_4.clone(), new_change_id(), &[]); - index.add_commit_data(id_5.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[]); + mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[]); - let index = index.as_composite(); + let index = mutable_segment.as_composite(); // Public API: calculate shortest unique prefix len with known commit_id assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_0), 3);