forked from mirrors/jj
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.
This commit is contained in:
parent
0f2f566188
commit
dde42b9c05
8 changed files with 43 additions and 41 deletions
|
@ -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()),
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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<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());
|
||||
|
@ -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<IndexPosition> {
|
||||
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<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))
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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<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())
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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()) {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue