From 5b401a23b0414c452c9c57b1cf3e9d0f8ace6bc2 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 23 Dec 2023 17:04:36 +0900 Subject: [PATCH] index: rename resolve_prefix() to resolve_commit_id_prefix() I'll probably add change id lookup methods to CompositeIndex. The Index trait won't gain resolve_change_id_prefix(), but I also renamed its resolve_prefix() for consistency. --- cli/src/commands/bench.rs | 2 +- lib/src/default_index/composite.rs | 6 +++--- lib/src/default_index/mod.rs | 18 +++++++++--------- lib/src/default_index/mutable.rs | 26 +++++++++++++------------- lib/src/default_index/readonly.rs | 24 ++++++++++++------------ lib/src/id_prefix.rs | 2 +- lib/src/index.rs | 2 +- lib/src/revset.rs | 4 +++- 8 files changed, 43 insertions(+), 41 deletions(-) diff --git a/cli/src/commands/bench.rs b/cli/src/commands/bench.rs index b5e7180f31..d8cced5330 100644 --- a/cli/src/commands/bench.rs +++ b/cli/src/commands/bench.rs @@ -161,7 +161,7 @@ pub(crate) fn cmd_bench( let workspace_command = command.workspace_helper(ui)?; let prefix = HexPrefix::new(&args.prefix).unwrap(); let index = workspace_command.repo().index(); - let routine = || index.resolve_prefix(&prefix); + let routine = || index.resolve_commit_id_prefix(&prefix); run_bench( ui, &format!("resolveprefix-{}", prefix.hex()), diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index 9779ffb12e..ead3145e98 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -50,7 +50,7 @@ pub(super) trait IndexSegment: Send + Sync { commit_id: &CommitId, ) -> (Option, Option); - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; fn generation_number(&self, local_pos: LocalPosition) -> u32; @@ -324,13 +324,13 @@ impl Index for CompositeIndex<'_> { .unwrap_or(0) } - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { self.ancestor_index_segments() .fold(PrefixResolution::NoMatch, |acc_match, segment| { if acc_match == PrefixResolution::AmbiguousMatch { acc_match // avoid checking the parent file(s) } else { - let local_match = segment.resolve_prefix(prefix); + let local_match = segment.resolve_commit_id_prefix(prefix); acc_match.plus(&local_match) } }) diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index 0e0a6bbebe..6076c62408 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -353,44 +353,44 @@ mod tests { // Can find commits given the full hex number assert_eq!( - index.resolve_prefix(&HexPrefix::new(&id_0.hex()).unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new(&id_0.hex()).unwrap()), PrefixResolution::SingleMatch(id_0) ); assert_eq!( - index.resolve_prefix(&HexPrefix::new(&id_1.hex()).unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new(&id_1.hex()).unwrap()), PrefixResolution::SingleMatch(id_1) ); assert_eq!( - index.resolve_prefix(&HexPrefix::new(&id_2.hex()).unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new(&id_2.hex()).unwrap()), PrefixResolution::SingleMatch(id_2) ); // Test nonexistent commits assert_eq!( - index.resolve_prefix(&HexPrefix::new("ffffff").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("ffffff").unwrap()), PrefixResolution::NoMatch ); assert_eq!( - index.resolve_prefix(&HexPrefix::new("000001").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("000001").unwrap()), PrefixResolution::NoMatch ); // Test ambiguous prefix assert_eq!( - index.resolve_prefix(&HexPrefix::new("0").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("0").unwrap()), PrefixResolution::AmbiguousMatch ); // Test a globally unique prefix in initial part assert_eq!( - index.resolve_prefix(&HexPrefix::new("009").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("009").unwrap()), PrefixResolution::SingleMatch(CommitId::from_hex("009999")) ); // Test a globally unique prefix in incremental part assert_eq!( - index.resolve_prefix(&HexPrefix::new("03").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("03").unwrap()), PrefixResolution::SingleMatch(CommitId::from_hex("033333")) ); // Test a locally unique but globally ambiguous prefix assert_eq!( - index.resolve_prefix(&HexPrefix::new("0554").unwrap()), + index.resolve_commit_id_prefix(&HexPrefix::new("0554").unwrap()), PrefixResolution::AmbiguousMatch ); } diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index ee6a26f1df..5ceda9db11 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -53,7 +53,7 @@ pub(super) struct MutableIndexSegment { commit_id_length: usize, change_id_length: usize, graph: Vec, - lookup: BTreeMap, + commit_lookup: BTreeMap, } impl MutableIndexSegment { @@ -64,7 +64,7 @@ impl MutableIndexSegment { commit_id_length, change_id_length, graph: vec![], - lookup: BTreeMap::new(), + commit_lookup: BTreeMap::new(), } } @@ -78,7 +78,7 @@ impl MutableIndexSegment { commit_id_length, change_id_length, graph: vec![], - lookup: BTreeMap::new(), + commit_lookup: BTreeMap::new(), } } @@ -120,7 +120,7 @@ impl MutableIndexSegment { ); entry.parent_positions.push(parent_entry.position()); } - self.lookup.insert( + self.commit_lookup.insert( entry.commit_id.clone(), IndexPosition(u32::try_from(self.graph.len()).unwrap() + self.num_parent_commits), ); @@ -183,7 +183,7 @@ impl MutableIndexSegment { } fn serialize_local_entries(&self, buf: &mut Vec) { - assert_eq!(self.graph.len(), self.lookup.len()); + assert_eq!(self.graph.len(), self.commit_lookup.len()); let num_commits = u32::try_from(self.graph.len()).unwrap(); buf.extend(num_commits.to_le_bytes()); @@ -222,7 +222,7 @@ impl MutableIndexSegment { buf.extend_from_slice(entry.commit_id.as_bytes()); } - for (commit_id, pos) in &self.lookup { + for (commit_id, pos) in &self.commit_lookup { buf.extend_from_slice(commit_id.as_bytes()); buf.extend(pos.0.to_le_bytes()); } @@ -316,7 +316,7 @@ impl IndexSegment for MutableIndexSegment { } fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option { - self.lookup.get(commit_id).cloned() + self.commit_lookup.get(commit_id).cloned() } fn resolve_neighbor_commit_ids( @@ -324,22 +324,22 @@ impl IndexSegment for MutableIndexSegment { commit_id: &CommitId, ) -> (Option, Option) { let prev_id = self - .lookup + .commit_lookup .range((Bound::Unbounded, Bound::Excluded(commit_id))) .next_back() .map(|(id, _)| id.clone()); let next_id = self - .lookup + .commit_lookup .range((Bound::Excluded(commit_id), Bound::Unbounded)) .next() .map(|(id, _)| id.clone()); (prev_id, next_id) } - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes()); let mut matches = self - .lookup + .commit_lookup .range((Bound::Included(&min_bytes_prefix), Bound::Unbounded)) .map(|(id, _pos)| id) .take_while(|&id| prefix.matches(id)) @@ -417,8 +417,8 @@ impl Index for DefaultMutableIndex { .shortest_unique_commit_id_prefix_len(commit_id) } - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - self.as_composite().resolve_prefix(prefix) + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + self.as_composite().resolve_commit_id_prefix(prefix) } fn has_id(&self, commit_id: &CommitId) -> bool { diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 84763f1b4c..8c13d1e00e 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -272,9 +272,9 @@ impl ReadonlyIndexSegment { let commit_graph_entry_size = CommitGraphEntry::size(commit_id_length, change_id_length); let graph_size = (num_local_commits as usize) * commit_graph_entry_size; let commit_lookup_entry_size = CommitLookupEntry::size(commit_id_length); - let lookup_size = (num_local_commits as usize) * commit_lookup_entry_size; + let commit_lookup_size = (num_local_commits as usize) * commit_lookup_entry_size; 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 + commit_lookup_size + parent_overflow_size; if data.len() != expected_size { return Err(ReadonlyIndexLoadError::invalid_data( name, @@ -320,7 +320,7 @@ impl ReadonlyIndexSegment { } } - fn lookup_entry(&self, lookup_pos: u32) -> CommitLookupEntry { + fn commit_lookup_entry(&self, lookup_pos: u32) -> CommitLookupEntry { assert!(lookup_pos < self.num_local_commits); let offset = (lookup_pos as usize) * self.commit_lookup_entry_size + (self.num_local_commits as usize) * self.commit_graph_entry_size; @@ -352,7 +352,7 @@ impl ReadonlyIndexSegment { if high == low { return Some(mid); } - let entry = self.lookup_entry(mid); + let entry = self.commit_lookup_entry(mid); if entry.commit_id_bytes() < prefix.as_bytes() { low = mid + 1; } else { @@ -381,7 +381,7 @@ impl IndexSegment for ReadonlyIndexSegment { fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option { let lookup_pos = self.commit_id_byte_prefix_to_lookup_pos(commit_id)?; - let entry = self.lookup_entry(lookup_pos); + let entry = self.commit_lookup_entry(lookup_pos); (&entry.commit_id() == commit_id).then(|| entry.pos()) } @@ -390,7 +390,7 @@ impl IndexSegment for ReadonlyIndexSegment { commit_id: &CommitId, ) -> (Option, Option) { if let Some(lookup_pos) = self.commit_id_byte_prefix_to_lookup_pos(commit_id) { - let entry_commit_id = self.lookup_entry(lookup_pos).commit_id(); + let entry_commit_id = self.commit_lookup_entry(lookup_pos).commit_id(); let (prev_lookup_pos, next_lookup_pos) = match entry_commit_id.cmp(commit_id) { Ordering::Less => { assert_eq!(lookup_pos + 1, self.num_local_commits); @@ -402,21 +402,21 @@ impl IndexSegment for ReadonlyIndexSegment { } Ordering::Greater => (lookup_pos.checked_sub(1), Some(lookup_pos)), }; - let prev_id = prev_lookup_pos.map(|p| self.lookup_entry(p).commit_id()); - let next_id = next_lookup_pos.map(|p| self.lookup_entry(p).commit_id()); + let prev_id = prev_lookup_pos.map(|p| self.commit_lookup_entry(p).commit_id()); + let next_id = next_lookup_pos.map(|p| self.commit_lookup_entry(p).commit_id()); (prev_id, next_id) } else { (None, None) } } - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes()); let lookup_pos = self .commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix) .unwrap_or(self.num_local_commits); let mut matches = (lookup_pos..self.num_local_commits) - .map(|pos| self.lookup_entry(pos).commit_id()) + .map(|pos| self.commit_lookup_entry(pos).commit_id()) .take_while(|id| prefix.matches(id)) .fuse(); match (matches.next(), matches.next()) { @@ -485,8 +485,8 @@ impl Index for DefaultReadonlyIndex { .shortest_unique_commit_id_prefix_len(commit_id) } - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - self.as_composite().resolve_prefix(prefix) + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { + self.as_composite().resolve_commit_id_prefix(prefix) } fn has_id(&self, commit_id: &CommitId) -> bool { diff --git a/lib/src/id_prefix.rs b/lib/src/id_prefix.rs index 938f7ad492..1e5d4366fb 100644 --- a/lib/src/id_prefix.rs +++ b/lib/src/id_prefix.rs @@ -126,7 +126,7 @@ impl IdPrefixContext { return PrefixResolution::SingleMatch(id); } } - repo.index().resolve_prefix(prefix) + repo.index().resolve_commit_id_prefix(prefix) } /// Returns the shortest length of a prefix of `commit_id` that diff --git a/lib/src/index.rs b/lib/src/index.rs index 159e705e2b..3570888073 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -58,7 +58,7 @@ pub trait IndexStore: Send + Sync + Debug { pub trait Index: Send + Sync { fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize; - fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; + fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; fn has_id(&self, commit_id: &CommitId) -> bool; diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 8c40bb668b..22a5bdb2ef 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -2046,7 +2046,9 @@ impl<'a> DefaultSymbolResolver<'a> { pub fn new(repo: &'a dyn Repo) -> Self { DefaultSymbolResolver { repo, - commit_id_resolver: Box::new(|repo, prefix| repo.index().resolve_prefix(prefix)), + commit_id_resolver: Box::new(|repo, prefix| { + repo.index().resolve_commit_id_prefix(prefix) + }), change_id_resolver: Box::new(|repo, prefix| repo.resolve_change_id_prefix(prefix)), } }