index: move lifetimed change_id_index() to MutableIndex, rename 'static version

change_id_index() is only used by Readonly/MutableRepo, so we don't need an
abstraction at Index. evaluate_revset() is somewhat similar, but the callers
rely on &dyn Repo.
This commit is contained in:
Yuya Nishihara 2024-01-08 10:48:21 +09:00
parent dc68f1eeb2
commit e5286aed08
6 changed files with 21 additions and 52 deletions

View file

@ -296,13 +296,6 @@ impl<'a> CompositeIndex<'a> {
candidate_positions
}
pub(super) fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + 'a> {
Box::new(ChangeIdIndexImpl::new(*self, heads))
}
pub(super) fn evaluate_revset(
&self,
expression: &ResolvedExpression,
@ -389,13 +382,6 @@ impl Index for CompositeIndex<'_> {
ids
}
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
CompositeIndex::change_id_index(self, heads)
}
fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,

View file

@ -29,7 +29,7 @@ use itertools::Itertools;
use smallvec::SmallVec;
use tempfile::NamedTempFile;
use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment};
use super::composite::{AsCompositeIndex, ChangeIdIndexImpl, CompositeIndex, IndexSegment};
use super::entry::{IndexPosition, LocalPosition, SmallIndexPositionsVec};
use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment};
use crate::backend::{ChangeId, CommitId};
@ -442,13 +442,6 @@ impl Index for DefaultMutableIndex {
self.as_composite().topo_order(input)
}
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
self.as_composite().change_id_index(heads)
}
fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
@ -471,6 +464,13 @@ impl MutableIndex for DefaultMutableIndex {
self
}
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
Box::new(ChangeIdIndexImpl::new(self, heads))
}
fn add_commit(&mut self, commit: &Commit) {
self.0.add_commit(commit);
}

View file

@ -509,14 +509,6 @@ impl Index for DefaultReadonlyIndex {
self.as_composite().topo_order(input)
}
// TODO: Create a persistent lookup from change id to commit ids.
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
self.as_composite().change_id_index(heads)
}
fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
@ -535,7 +527,8 @@ impl ReadonlyIndex for DefaultReadonlyIndex {
self
}
fn change_id_index_static(
// TODO: Create a persistent lookup from change id to commit ids.
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex> {

View file

@ -72,11 +72,6 @@ pub trait Index: Send + Sync {
/// Parents before children
fn topo_order(&self, input: &mut dyn Iterator<Item = &CommitId>) -> Vec<CommitId>;
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_>;
fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
@ -89,12 +84,8 @@ pub trait ReadonlyIndex: Send + Sync {
fn as_index(&self) -> &dyn Index;
// TODO: might be better to split Index::change_id_index() to
// Readonly/MutableIndex::change_id_index_static().
fn change_id_index_static(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex>;
fn change_id_index(&self, heads: &mut dyn Iterator<Item = &CommitId>)
-> Box<dyn ChangeIdIndex>;
fn start_modification(&self) -> Box<dyn MutableIndex>;
}
@ -106,6 +97,11 @@ pub trait MutableIndex {
fn as_index(&self) -> &dyn Index;
fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_>;
fn add_commit(&mut self, commit: &Commit);
fn merge_in(&mut self, other: &dyn ReadonlyIndex);

View file

@ -265,10 +265,8 @@ impl ReadonlyRepo {
fn change_id_index(&self) -> &dyn ChangeIdIndex {
self.change_id_index
.get_or_init(|| {
// TODO: maybe add abstraction over 'static/'index revset
// evaluation, and use it.
self.readonly_index()
.change_id_index_static(&mut self.view().heads().iter())
.change_id_index(&mut self.view().heads().iter())
})
.as_ref()
}
@ -1401,16 +1399,12 @@ impl Repo for MutableRepo {
}
fn resolve_change_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<Vec<CommitId>> {
let change_id_index = self
.index()
.change_id_index(&mut self.view().heads().iter());
let change_id_index = self.index.change_id_index(&mut self.view().heads().iter());
change_id_index.resolve_prefix(prefix)
}
fn shortest_unique_change_id_prefix_len(&self, target_id: &ChangeId) -> usize {
let change_id_index = self
.index()
.change_id_index(&mut self.view().heads().iter());
let change_id_index = self.index.change_id_index(&mut self.view().heads().iter());
change_id_index.shortest_unique_prefix_len(target_id)
}
}

View file

@ -674,7 +674,7 @@ fn test_change_id_index() {
let index_for_heads = |commits: &[&Commit]| {
tx.repo()
.index()
.mutable_index()
.change_id_index(&mut commits.iter().map(|commit| commit.id()))
};
let change_id_index = index_for_heads(&[&commit_1, &commit_2, &commit_3, &commit_4, &commit_5]);