From e5286aed08390784b47905723a1860f4ecc0ba62 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 8 Jan 2024 10:48:21 +0900 Subject: [PATCH] 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. --- lib/src/default_index/composite.rs | 14 -------------- lib/src/default_index/mutable.rs | 16 ++++++++-------- lib/src/default_index/readonly.rs | 11 ++--------- lib/src/index.rs | 18 +++++++----------- lib/src/repo.rs | 12 +++--------- lib/tests/test_index.rs | 2 +- 6 files changed, 21 insertions(+), 52 deletions(-) diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index 3d298ebea..117aeeda2 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -296,13 +296,6 @@ impl<'a> CompositeIndex<'a> { candidate_positions } - pub(super) fn change_id_index( - &self, - heads: &mut dyn Iterator, - ) -> Box { - 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, - ) -> Box { - CompositeIndex::change_id_index(self, heads) - } - fn evaluate_revset<'index>( &'index self, expression: &ResolvedExpression, diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index 55314929b..319f36f73 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -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, - ) -> Box { - 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, + ) -> Box { + Box::new(ChangeIdIndexImpl::new(self, heads)) + } + fn add_commit(&mut self, commit: &Commit) { self.0.add_commit(commit); } diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index dc0be20a4..79b2f13a3 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -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, - ) -> Box { - 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, ) -> Box { diff --git a/lib/src/index.rs b/lib/src/index.rs index 58e9c8f9d..8e37a6c7b 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -72,11 +72,6 @@ pub trait Index: Send + Sync { /// Parents before children fn topo_order(&self, input: &mut dyn Iterator) -> Vec; - fn change_id_index( - &self, - heads: &mut dyn Iterator, - ) -> Box; - 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, - ) -> Box; + fn change_id_index(&self, heads: &mut dyn Iterator) + -> Box; fn start_modification(&self) -> Box; } @@ -106,6 +97,11 @@ pub trait MutableIndex { fn as_index(&self) -> &dyn Index; + fn change_id_index( + &self, + heads: &mut dyn Iterator, + ) -> Box; + fn add_commit(&mut self, commit: &Commit); fn merge_in(&mut self, other: &dyn ReadonlyIndex); diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 39ed1485c..81bb36fe9 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -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> { - 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) } } diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index 60c12bcda..4094c3a9e 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -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]);