index: remove "segment_" prefix from IndexSegment methods

Since Readonly/MutableIndexSegment no longer implement Index trait, there's
no ambiguity between segment-local and index-global operations. Let's shorten
the method names.
This commit is contained in:
Yuya Nishihara 2023-12-23 16:24:36 +09:00
parent c9b9e2864e
commit 0f2f566188
5 changed files with 70 additions and 70 deletions

View file

@ -33,34 +33,34 @@ use crate::store::Store;
use crate::{backend, default_revset_engine};
pub(super) trait IndexSegment: Send + Sync {
fn segment_num_parent_commits(&self) -> u32;
fn num_parent_commits(&self) -> u32;
fn segment_num_commits(&self) -> u32;
fn num_local_commits(&self) -> u32;
fn segment_parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>>;
fn parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>>;
fn segment_name(&self) -> Option<String>;
fn name(&self) -> Option<String>;
fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition>;
fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition>;
/// Suppose the given `commit_id` exists, returns the previous and next
/// commit ids in lexicographical order.
fn segment_resolve_neighbor_commit_ids(
fn resolve_neighbor_commit_ids(
&self,
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>);
fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;
fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;
fn segment_generation_number(&self, local_pos: LocalPosition) -> u32;
fn generation_number(&self, local_pos: LocalPosition) -> u32;
fn segment_commit_id(&self, local_pos: LocalPosition) -> CommitId;
fn commit_id(&self, local_pos: LocalPosition) -> CommitId;
fn segment_change_id(&self, local_pos: LocalPosition) -> ChangeId;
fn change_id(&self, local_pos: LocalPosition) -> ChangeId;
fn segment_num_parents(&self, local_pos: LocalPosition) -> u32;
fn num_parents(&self, local_pos: LocalPosition) -> u32;
fn segment_parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec;
fn parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec;
}
/// Abstraction over owned and borrowed types that can be cheaply converted to
@ -95,8 +95,8 @@ impl<'a> CompositeIndex<'a> {
pub(super) fn ancestor_files_without_local(
&self,
) -> impl Iterator<Item = &'a Arc<ReadonlyIndexSegment>> {
let parent_file = self.0.segment_parent_file();
iter::successors(parent_file, |file| file.segment_parent_file())
let parent_file = self.0.parent_file();
iter::successors(parent_file, |file| file.parent_file())
}
/// Iterates self and its ancestor index segments.
@ -108,7 +108,7 @@ impl<'a> CompositeIndex<'a> {
}
pub fn num_commits(&self) -> u32 {
self.0.segment_num_parent_commits() + self.0.segment_num_commits()
self.0.num_parent_commits() + self.0.num_local_commits()
}
pub fn stats(&self) -> IndexStats {
@ -133,8 +133,8 @@ impl<'a> CompositeIndex<'a> {
let mut levels = self
.ancestor_index_segments()
.map(|segment| IndexLevelStats {
num_commits: segment.segment_num_commits(),
name: segment.segment_name(),
num_commits: segment.num_local_commits(),
name: segment.name(),
})
.collect_vec();
levels.reverse();
@ -152,7 +152,7 @@ impl<'a> CompositeIndex<'a> {
pub fn entry_by_pos(&self, pos: IndexPosition) -> IndexEntry<'a> {
self.ancestor_index_segments()
.find_map(|segment| {
u32::checked_sub(pos.0, segment.segment_num_parent_commits())
u32::checked_sub(pos.0, segment.num_parent_commits())
.map(|local_pos| IndexEntry::new(segment, pos, LocalPosition(local_pos)))
})
.unwrap()
@ -160,7 +160,7 @@ impl<'a> CompositeIndex<'a> {
pub fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
self.ancestor_index_segments()
.find_map(|segment| segment.segment_commit_id_to_pos(commit_id))
.find_map(|segment| segment.commit_id_to_pos(commit_id))
}
/// Suppose the given `commit_id` exists, returns the previous and next
@ -170,7 +170,7 @@ impl<'a> CompositeIndex<'a> {
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
self.ancestor_index_segments()
.map(|segment| segment.segment_resolve_neighbor_commit_ids(commit_id))
.map(|segment| segment.resolve_neighbor_commit_ids(commit_id))
.reduce(|(acc_prev_id, acc_next_id), (prev_id, next_id)| {
(
acc_prev_id.into_iter().chain(prev_id).max(),
@ -330,7 +330,7 @@ impl Index for CompositeIndex<'_> {
if acc_match == PrefixResolution::AmbiguousMatch {
acc_match // avoid checking the parent file(s)
} else {
let local_match = segment.segment_resolve_prefix(prefix);
let local_match = segment.resolve_prefix(prefix);
acc_match.plus(&local_match)
}
})

View file

@ -88,23 +88,23 @@ impl<'a> IndexEntry<'a> {
}
pub fn generation_number(&self) -> u32 {
self.source.segment_generation_number(self.local_pos)
self.source.generation_number(self.local_pos)
}
pub fn commit_id(&self) -> CommitId {
self.source.segment_commit_id(self.local_pos)
self.source.commit_id(self.local_pos)
}
pub fn change_id(&self) -> ChangeId {
self.source.segment_change_id(self.local_pos)
self.source.change_id(self.local_pos)
}
pub fn num_parents(&self) -> u32 {
self.source.segment_num_parents(self.local_pos)
self.source.num_parents(self.local_pos)
}
pub fn parent_positions(&self) -> SmallIndexPositionsVec {
self.source.segment_parent_positions(self.local_pos)
self.source.parent_positions(self.local_pos)
}
pub fn parents(&self) -> impl ExactSizeIterator<Item = IndexEntry<'a>> {

View file

@ -423,57 +423,57 @@ mod tests {
// Local lookup in readonly index, commit_id exists.
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&id_0),
initial_file.resolve_neighbor_commit_ids(&id_0),
(None, Some(id_1.clone())),
);
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&id_1),
initial_file.resolve_neighbor_commit_ids(&id_1),
(Some(id_0.clone()), Some(id_2.clone())),
);
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&id_2),
initial_file.resolve_neighbor_commit_ids(&id_2),
(Some(id_1.clone()), None),
);
// Local lookup in readonly index, commit_id does not exist.
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("000000")),
initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("000000")),
(None, Some(id_0.clone())),
);
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("000002")),
initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("000002")),
(Some(id_0.clone()), Some(id_1.clone())),
);
assert_eq!(
initial_file.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
(Some(id_2.clone()), None),
);
// Local lookup in mutable index, commit_id exists. id_5 < id_3 < id_4
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&id_5),
mutable_segment.resolve_neighbor_commit_ids(&id_5),
(None, Some(id_3.clone())),
);
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&id_3),
mutable_segment.resolve_neighbor_commit_ids(&id_3),
(Some(id_5.clone()), Some(id_4.clone())),
);
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&id_4),
mutable_segment.resolve_neighbor_commit_ids(&id_4),
(Some(id_3.clone()), None),
);
// Local lookup in mutable index, commit_id does not exist. id_5 < id_3 < id_4
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("033332")),
mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("033332")),
(None, Some(id_5.clone())),
);
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("033334")),
mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("033334")),
(Some(id_5.clone()), Some(id_3.clone())),
);
assert_eq!(
mutable_segment.segment_resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
(Some(id_4.clone()), None),
);

View file

@ -129,7 +129,7 @@ impl MutableIndexSegment {
pub(super) fn add_commits_from(&mut self, other_segment: &dyn IndexSegment) {
let other = CompositeIndex::new(other_segment);
for pos in other_segment.segment_num_parent_commits()..other.num_commits() {
for pos in other_segment.num_parent_commits()..other.num_commits() {
let entry = other.entry_by_pos(IndexPosition(pos));
let parent_ids = entry.parents().map(|entry| entry.commit_id()).collect_vec();
self.add_commit_data(entry.commit_id(), entry.change_id(), &parent_ids);
@ -147,7 +147,7 @@ impl MutableIndexSegment {
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.segment_parent_file().cloned();
maybe_other_ancestor = other_ancestor.parent_file().cloned();
continue;
}
let own_ancestor = maybe_own_ancestor.as_ref().unwrap();
@ -158,9 +158,9 @@ impl MutableIndexSegment {
< other_ancestor.as_composite().num_commits()
{
files_to_add.push(other_ancestor.clone());
maybe_other_ancestor = other_ancestor.segment_parent_file().cloned();
maybe_other_ancestor = other_ancestor.parent_file().cloned();
} else {
maybe_own_ancestor = own_ancestor.segment_parent_file().cloned();
maybe_own_ancestor = own_ancestor.parent_file().cloned();
}
}
@ -238,17 +238,17 @@ impl MutableIndexSegment {
/// 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) -> MutableIndexSegment {
let mut num_new_commits = self.segment_num_commits();
let mut num_new_commits = self.num_local_commits();
let mut files_to_squash = vec![];
let mut base_parent_file = None;
for parent_file in self.as_composite().ancestor_files_without_local() {
// 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() {
if 2 * num_new_commits < parent_file.num_local_commits() {
base_parent_file = Some(parent_file.clone());
break;
}
num_new_commits += parent_file.segment_num_commits();
num_new_commits += parent_file.num_local_commits();
files_to_squash.push(parent_file.clone());
}
@ -269,7 +269,7 @@ impl MutableIndexSegment {
}
pub(super) fn save_in(self, dir: &Path) -> io::Result<Arc<ReadonlyIndexSegment>> {
if self.segment_num_commits() == 0 && self.parent_file.is_some() {
if self.num_local_commits() == 0 && self.parent_file.is_some() {
return Ok(self.parent_file.unwrap());
}
@ -299,27 +299,27 @@ impl MutableIndexSegment {
}
impl IndexSegment for MutableIndexSegment {
fn segment_num_parent_commits(&self) -> u32 {
fn num_parent_commits(&self) -> u32 {
self.num_parent_commits
}
fn segment_num_commits(&self) -> u32 {
fn num_local_commits(&self) -> u32 {
self.graph.len().try_into().unwrap()
}
fn segment_parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>> {
fn parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>> {
self.parent_file.as_ref()
}
fn segment_name(&self) -> Option<String> {
fn name(&self) -> Option<String> {
None
}
fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
self.lookup.get(commit_id).cloned()
}
fn segment_resolve_neighbor_commit_ids(
fn resolve_neighbor_commit_ids(
&self,
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
@ -336,7 +336,7 @@ impl IndexSegment for MutableIndexSegment {
(prev_id, next_id)
}
fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes());
let mut matches = self
.lookup
@ -351,19 +351,19 @@ impl IndexSegment for MutableIndexSegment {
}
}
fn segment_generation_number(&self, local_pos: LocalPosition) -> u32 {
fn generation_number(&self, local_pos: LocalPosition) -> u32 {
self.graph[local_pos.0 as usize].generation_number
}
fn segment_commit_id(&self, local_pos: LocalPosition) -> CommitId {
fn commit_id(&self, local_pos: LocalPosition) -> CommitId {
self.graph[local_pos.0 as usize].commit_id.clone()
}
fn segment_change_id(&self, local_pos: LocalPosition) -> ChangeId {
fn change_id(&self, local_pos: LocalPosition) -> ChangeId {
self.graph[local_pos.0 as usize].change_id.clone()
}
fn segment_num_parents(&self, local_pos: LocalPosition) -> u32 {
fn num_parents(&self, local_pos: LocalPosition) -> u32 {
self.graph[local_pos.0 as usize]
.parent_positions
.len()
@ -371,7 +371,7 @@ impl IndexSegment for MutableIndexSegment {
.unwrap()
}
fn segment_parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec {
fn parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec {
self.graph[local_pos.0 as usize].parent_positions.clone()
}
}

View file

@ -363,29 +363,29 @@ impl ReadonlyIndexSegment {
}
impl IndexSegment for ReadonlyIndexSegment {
fn segment_num_parent_commits(&self) -> u32 {
fn num_parent_commits(&self) -> u32 {
self.num_parent_commits
}
fn segment_num_commits(&self) -> u32 {
fn num_local_commits(&self) -> u32 {
self.num_local_commits
}
fn segment_parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>> {
fn parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>> {
self.parent_file.as_ref()
}
fn segment_name(&self) -> Option<String> {
fn name(&self) -> Option<String> {
Some(self.name.clone())
}
fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
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);
(&entry.commit_id() == commit_id).then(|| entry.pos())
}
fn segment_resolve_neighbor_commit_ids(
fn resolve_neighbor_commit_ids(
&self,
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
@ -410,7 +410,7 @@ impl IndexSegment for ReadonlyIndexSegment {
}
}
fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
fn resolve_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)
@ -426,23 +426,23 @@ impl IndexSegment for ReadonlyIndexSegment {
}
}
fn segment_generation_number(&self, local_pos: LocalPosition) -> u32 {
fn generation_number(&self, local_pos: LocalPosition) -> u32 {
self.graph_entry(local_pos).generation_number()
}
fn segment_commit_id(&self, local_pos: LocalPosition) -> CommitId {
fn commit_id(&self, local_pos: LocalPosition) -> CommitId {
self.graph_entry(local_pos).commit_id()
}
fn segment_change_id(&self, local_pos: LocalPosition) -> ChangeId {
fn change_id(&self, local_pos: LocalPosition) -> ChangeId {
self.graph_entry(local_pos).change_id()
}
fn segment_num_parents(&self, local_pos: LocalPosition) -> u32 {
fn num_parents(&self, local_pos: LocalPosition) -> u32 {
self.graph_entry(local_pos).num_parents()
}
fn segment_parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec {
fn parent_positions(&self, local_pos: LocalPosition) -> SmallIndexPositionsVec {
let graph_entry = self.graph_entry(local_pos);
let mut parent_entries = SmallVec::with_capacity(graph_entry.num_parents() as usize);
if graph_entry.num_parents() >= 1 {