From face4d637f5442526becb0f3401e1c422f473fcf Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 6 Feb 2021 00:10:27 -0800 Subject: [PATCH] index: define methods from CompositeIndex directly on {Readonly,Mutable}Index This is one step towards making `CompositeIndex` non-public (and maybe deleting it). Next, we'll add an `IndexRef` enum similar to `RepoRef` etc. --- lib/src/index.rs | 92 ++++++++++++++++++++++++++++++++++++++++++------ src/commands.rs | 15 ++------ 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index d0080416b..2036fb8a5 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -143,15 +143,10 @@ fn topo_order_parents_first( let mut commits = vec![]; let mut visited = HashSet::new(); let mut in_parent_file = HashSet::new(); - let parent_file_source = parent_file - .as_ref() - .map(|file| file.as_ref().as_composite()); + let parent_file_source = parent_file.as_ref().map(|file| file.as_ref()); while !work.is_empty() { let commit = work.pop().unwrap(); - if parent_file_source - .as_ref() - .map_or(false, |index| index.has_id(commit.id())) - { + if parent_file_source.map_or(false, |index| index.has_id(commit.id())) { in_parent_file.insert(commit.id().clone()); continue; } else if !visited.insert(commit.id().clone()) { @@ -306,7 +301,6 @@ impl MutableIndex { }; for parent_id in parent_ids { let parent_entry = self - .as_composite() .entry_by_id(&parent_id) .expect("parent commit is not indexed"); entry.generation_number = max( @@ -378,6 +372,45 @@ impl MutableIndex { buf } + + pub fn num_commits(&self) -> u32 { + CompositeIndex(self).num_commits() + } + + pub fn stats(&self) -> IndexStats { + CompositeIndex(self).stats() + } + + pub fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option { + CompositeIndex(self).commit_id_to_pos(commit_id) + } + + pub fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + CompositeIndex(self).resolve_prefix(prefix) + } + + pub fn entry_by_id(&self, commit_id: &CommitId) -> Option { + CompositeIndex(self).entry_by_id(commit_id) + } + + pub fn has_id(&self, commit_id: &CommitId) -> bool { + CompositeIndex(self).has_id(commit_id) + } + + pub fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> bool { + CompositeIndex(self).is_ancestor(ancestor_id, descendant_id) + } + + pub fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk { + CompositeIndex(self).walk_revs(wanted, unwanted) + } + + pub fn heads<'candidates>( + &self, + candidates: impl IntoIterator, + ) -> Vec { + CompositeIndex(self).heads(candidates) + } } trait IndexSegment { @@ -489,7 +522,7 @@ impl<'a> CompositeIndex<'a> { .segment_parent_file() .as_ref() .map_or(PrefixResolution::NoMatch, |file| { - file.as_composite().resolve_prefix(prefix) + file.resolve_prefix(prefix) }); local_match.plus(&parent_match) } @@ -530,7 +563,7 @@ impl<'a> CompositeIndex<'a> { false } - pub fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk { + pub fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk<'a> { let mut rev_walk = RevWalk::new(self.clone()); for pos in wanted.iter().map(|id| self.commit_id_to_pos(id).unwrap()) { rev_walk.add_wanted(pos); @@ -1077,6 +1110,45 @@ impl ReadonlyIndex { CompositeIndex(self) } + pub fn num_commits(&self) -> u32 { + CompositeIndex(self).num_commits() + } + + pub fn stats(&self) -> IndexStats { + CompositeIndex(self).stats() + } + + pub fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option { + CompositeIndex(self).commit_id_to_pos(commit_id) + } + + pub fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + CompositeIndex(self).resolve_prefix(prefix) + } + + pub fn entry_by_id(&self, commit_id: &CommitId) -> Option { + CompositeIndex(self).entry_by_id(commit_id) + } + + pub fn has_id(&self, commit_id: &CommitId) -> bool { + CompositeIndex(self).has_id(commit_id) + } + + pub fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> bool { + CompositeIndex(self).is_ancestor(ancestor_id, descendant_id) + } + + pub fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk { + CompositeIndex(self).walk_revs(wanted, unwanted) + } + + pub fn heads<'candidates>( + &self, + candidates: impl IntoIterator, + ) -> Vec { + CompositeIndex(self).heads(candidates) + } + fn graph_entry(&self, local_pos: u32) -> CommitGraphEntry { let offset = (local_pos as usize) * self.commit_graph_entry_size; CommitGraphEntry { diff --git a/src/commands.rs b/src/commands.rs index aa85572bb..219c992d8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -104,8 +104,7 @@ fn resolve_commit_id_prefix( repo: &ReadonlyRepo, prefix: &HexPrefix, ) -> Result { - let index = repo.index(); - match index.as_composite().resolve_prefix(prefix) { + match repo.index().resolve_prefix(prefix) { PrefixResolution::NoMatch => Err(CommandError::UserError(String::from("No such commit"))), PrefixResolution::AmbiguousMatch => { Err(CommandError::UserError(String::from("Ambiguous prefix"))) @@ -1767,8 +1766,7 @@ fn cmd_debug( writeln!(ui, "{:?}", parse); } else if let Some(_reindex_matches) = sub_matches.subcommand_matches("index") { let repo = get_repo(ui, &matches)?; - let index = repo.index(); - let stats = index.as_composite().stats(); + let stats = repo.index().stats(); writeln!(ui, "Number of commits: {}", stats.num_commits); writeln!(ui, "Number of merges: {}", stats.num_merges); writeln!(ui, "Max generation number: {}", stats.max_generation_number); @@ -1783,11 +1781,7 @@ fn cmd_debug( let mut repo = get_repo(ui, &matches)?; let mut_repo = Arc::get_mut(&mut repo).unwrap(); let index = mut_repo.reindex(); - writeln!( - ui, - "Finished indexing {:?} commits.", - index.as_composite().num_commits() - ); + writeln!(ui, "Finished indexing {:?} commits.", index.num_commits()); } else { panic!("unhandled command: {:#?}", matches); } @@ -1823,7 +1817,6 @@ fn cmd_bench( command_matches.value_of("descendant").unwrap(), )?; let index = repo.index(); - let index = index.as_composite(); let routine = || index.is_ancestor(ancestor_commit.id(), descendant_commit.id()); writeln!(ui, "Result: {:?}", routine()); criterion.bench_function("isancestor", |bencher: &mut criterion::Bencher| { @@ -1837,7 +1830,6 @@ fn cmd_bench( let wanted_commit = resolve_single_rev(ui, mut_repo, command_matches.value_of("wanted").unwrap())?; let index = repo.index(); - let index = index.as_composite(); let routine = || { index .walk_revs( @@ -1854,7 +1846,6 @@ fn cmd_bench( let repo = get_repo(ui, &matches)?; let prefix = HexPrefix::new(command_matches.value_of("prefix").unwrap().to_string()); let index = repo.index(); - let index = index.as_composite(); let routine = || index.resolve_prefix(&prefix); writeln!(ui, "Result: {:?}", routine()); criterion.bench_function("resolveprefix", |bencher: &mut criterion::Bencher| {