Skip to content

Commit

Permalink
index: rename resolve_prefix() to resolve_commit_id_prefix()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yuja committed Dec 25, 2023
1 parent 2d82025 commit 5b401a2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(super) trait IndexSegment: Send + Sync {
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>);

fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;

fn generation_number(&self, local_pos: LocalPosition) -> u32;

Expand Down Expand Up @@ -324,13 +324,13 @@ impl Index for CompositeIndex<'_> {
.unwrap_or(0)
}

fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
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)
}
})
Expand Down
18 changes: 9 additions & 9 deletions lib/src/default_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
26 changes: 13 additions & 13 deletions lib/src/default_index/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(super) struct MutableIndexSegment {
commit_id_length: usize,
change_id_length: usize,
graph: Vec<MutableGraphEntry>,
lookup: BTreeMap<CommitId, IndexPosition>,
commit_lookup: BTreeMap<CommitId, IndexPosition>,
}

impl MutableIndexSegment {
Expand All @@ -64,7 +64,7 @@ impl MutableIndexSegment {
commit_id_length,
change_id_length,
graph: vec![],
lookup: BTreeMap::new(),
commit_lookup: BTreeMap::new(),
}
}

Expand All @@ -78,7 +78,7 @@ impl MutableIndexSegment {
commit_id_length,
change_id_length,
graph: vec![],
lookup: BTreeMap::new(),
commit_lookup: BTreeMap::new(),
}
}

Expand Down Expand Up @@ -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),
);
Expand Down Expand Up @@ -183,7 +183,7 @@ impl MutableIndexSegment {
}

fn serialize_local_entries(&self, buf: &mut Vec<u8>) {
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());
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -316,30 +316,30 @@ impl IndexSegment for MutableIndexSegment {
}

fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
self.lookup.get(commit_id).cloned()
self.commit_lookup.get(commit_id).cloned()
}

fn resolve_neighbor_commit_ids(
&self,
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
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<CommitId> {
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
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))
Expand Down Expand Up @@ -417,8 +417,8 @@ impl Index for DefaultMutableIndex {
.shortest_unique_commit_id_prefix_len(commit_id)
}

fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
self.as_composite().resolve_prefix(prefix)
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
self.as_composite().resolve_commit_id_prefix(prefix)
}

fn has_id(&self, commit_id: &CommitId) -> bool {
Expand Down
24 changes: 12 additions & 12 deletions lib/src/default_index/readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -381,7 +381,7 @@ impl IndexSegment for ReadonlyIndexSegment {

fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
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())
}

Expand All @@ -390,7 +390,7 @@ impl IndexSegment for ReadonlyIndexSegment {
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
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);
Expand All @@ -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<CommitId> {
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
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()) {
Expand Down Expand Up @@ -485,8 +485,8 @@ impl Index for DefaultReadonlyIndex {
.shortest_unique_commit_id_prefix_len(commit_id)
}

fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
self.as_composite().resolve_prefix(prefix)
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
self.as_composite().resolve_commit_id_prefix(prefix)
}

fn has_id(&self, commit_id: &CommitId) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/id_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommitId>;
fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;

fn has_id(&self, commit_id: &CommitId) -> bool;

Expand Down
4 changes: 3 additions & 1 deletion lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}
Expand Down

0 comments on commit 5b401a2

Please sign in to comment.