Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index: IndexSegment API cleanup #2743

Merged
merged 5 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
martinvonz marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -152,15 +152,15 @@ 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()
}

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
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
}
})
Expand Down
10 changes: 5 additions & 5 deletions lib/src/default_index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down
24 changes: 12 additions & 12 deletions lib/src/default_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);

Expand Down
40 changes: 20 additions & 20 deletions lib/src/default_index/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -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>) {
Expand All @@ -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
Expand All @@ -351,27 +351,27 @@ 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()
.try_into()
.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()
}
}
Expand Down
Loading