Skip to content

Commit

Permalink
index: add CompositeIndex constructor instead of pub(super)-ing field
Browse files Browse the repository at this point in the history
This wouldn't matter, but seemed slightly better.
  • Loading branch information
yuja committed Dec 11, 2023
1 parent 679518f commit caa1b99
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 5 additions & 1 deletion lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ use crate::store::Store;
use crate::{backend, default_revset_engine};

#[derive(Clone, Copy)]
pub struct CompositeIndex<'a>(pub(super) &'a dyn IndexSegment);
pub struct CompositeIndex<'a>(&'a dyn IndexSegment);

impl<'a> CompositeIndex<'a> {
pub(super) fn new(segment: &'a dyn IndexSegment) -> Self {
CompositeIndex(segment)
}

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())
Expand Down
21 changes: 11 additions & 10 deletions lib/src/default_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl MutableIndexSegment {
}

fn as_composite(&self) -> CompositeIndex {
CompositeIndex(self)
CompositeIndex::new(self)
}

fn add_commit(&mut self, commit: &Commit) {
Expand All @@ -265,7 +265,8 @@ impl MutableIndexSegment {
parent_positions: SmallVec::new(),
};
for parent_id in parent_ids {
let parent_entry = CompositeIndex(self)
let parent_entry = self
.as_composite()
.entry_by_id(parent_id)
.expect("parent commit is not indexed");
entry.generation_number = max(
Expand All @@ -282,7 +283,7 @@ impl MutableIndexSegment {
}

fn add_commits_from(&mut self, other_segment: &dyn IndexSegment) {
let other = CompositeIndex(other_segment);
let other = CompositeIndex::new(other_segment);
for pos in other_segment.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();
Expand Down Expand Up @@ -1351,7 +1352,7 @@ impl<'a> IndexEntry<'a> {
}

pub fn parents(&self) -> impl ExactSizeIterator<Item = IndexEntry<'a>> {
let composite = CompositeIndex(self.source);
let composite = CompositeIndex::new(self.source);
self.parent_positions()
.into_iter()
.map(move |pos| composite.entry_by_pos(pos))
Expand Down Expand Up @@ -1420,7 +1421,7 @@ impl ReadonlyIndexSegment {
}

fn as_composite(&self) -> CompositeIndex {
CompositeIndex(self)
CompositeIndex::new(self)
}

fn name(&self) -> &str {
Expand Down Expand Up @@ -1549,7 +1550,7 @@ mod tests {
} else {
Box::new(mutable_segment)
};
let index = CompositeIndex(index_segment.as_ref());
let index = CompositeIndex::new(index_segment.as_ref());

// Stats are as expected
let stats = index.stats();
Expand Down Expand Up @@ -1580,7 +1581,7 @@ mod tests {
} else {
Box::new(mutable_segment)
};
let index = CompositeIndex(index_segment.as_ref());
let index = CompositeIndex::new(index_segment.as_ref());

// Stats are as expected
let stats = index.stats();
Expand Down Expand Up @@ -1665,7 +1666,7 @@ mod tests {
} else {
Box::new(mutable_segment)
};
let index = CompositeIndex(index_segment.as_ref());
let index = CompositeIndex::new(index_segment.as_ref());

// Stats are as expected
let stats = index.stats();
Expand Down Expand Up @@ -1768,7 +1769,7 @@ mod tests {
} else {
Box::new(mutable_segment)
};
let index = CompositeIndex(index_segment.as_ref());
let index = CompositeIndex::new(index_segment.as_ref());

// Stats are as expected
let stats = index.stats();
Expand Down Expand Up @@ -1948,7 +1949,7 @@ mod tests {
);

// Global lookup, commit_id exists. id_0 < id_1 < id_5 < id_3 < id_2 < id_4
let composite_index = CompositeIndex(&mutable_segment);
let composite_index = CompositeIndex::new(&mutable_segment);
assert_eq!(
composite_index.resolve_neighbor_commit_ids(&id_0),
(None, Some(id_1.clone())),
Expand Down

0 comments on commit caa1b99

Please sign in to comment.