ok/jj
1
0
Fork 0
forked from mirrors/jj

index: add 'static version of evaluate_revset() to ReadonlyIndex

We'll probably need a better abstraction, but a separate method is good
enough to remove unsafe code from ReadonlyRepo.

I'm not sure if this is feasible for the other backends, but I guess there
would be less lifetimed variables than DefaultReadonlyIndex.
This commit is contained in:
Yuya Nishihara 2023-12-08 00:22:48 +09:00
parent 2ba50c76c7
commit d9e8297059
3 changed files with 23 additions and 5 deletions

View file

@ -30,6 +30,7 @@ use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec};
use super::mutable::DefaultMutableIndex; use super::mutable::DefaultMutableIndex;
use super::store::IndexLoadError; use super::store::IndexLoadError;
use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::default_revset_engine;
use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex};
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store; use crate::store::Store;
@ -373,7 +374,7 @@ impl IndexSegment for ReadonlyIndexSegment {
} }
/// Commit index backend which stores data on local disk. /// Commit index backend which stores data on local disk.
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct DefaultReadonlyIndex(Arc<ReadonlyIndexSegment>); pub struct DefaultReadonlyIndex(Arc<ReadonlyIndexSegment>);
impl DefaultReadonlyIndex { impl DefaultReadonlyIndex {
@ -440,6 +441,15 @@ impl ReadonlyIndex for DefaultReadonlyIndex {
self self
} }
fn evaluate_revset_static(
&self,
expression: &ResolvedExpression,
store: &Arc<Store>,
) -> Result<Box<dyn Revset<'static>>, RevsetEvaluationError> {
let revset_impl = default_revset_engine::evaluate(expression, store, self.clone())?;
Ok(Box::new(revset_impl))
}
fn start_modification(&self) -> Box<dyn MutableIndex> { fn start_modification(&self) -> Box<dyn MutableIndex> {
Box::new(DefaultMutableIndex::incremental(self.0.clone())) Box::new(DefaultMutableIndex::incremental(self.0.clone()))
} }

View file

@ -75,6 +75,14 @@ pub trait ReadonlyIndex: Send + Sync {
fn as_index(&self) -> &dyn Index; fn as_index(&self) -> &dyn Index;
// TODO: might be better to split Index::evaluate_revset() to
// Readonly/MutableIndex::evaluate_static().
fn evaluate_revset_static(
&self,
expression: &ResolvedExpression,
store: &Arc<Store>,
) -> Result<Box<dyn Revset<'static>>, RevsetEvaluationError>;
fn start_modification(&self) -> Box<dyn MutableIndex>; fn start_modification(&self) -> Box<dyn MutableIndex>;
} }

View file

@ -22,10 +22,10 @@ use jj_lib::revset_graph::RevsetGraphEdge;
use test_case::test_case; use test_case::test_case;
use testutils::{CommitGraphBuilder, TestRepo}; use testutils::{CommitGraphBuilder, TestRepo};
fn revset_for_commits<'index>( fn revset_for_commits(
repo: &'index ReadonlyRepo, repo: &ReadonlyRepo,
commits: &[&Commit], commits: &[&Commit],
) -> RevsetImpl<&'index DefaultReadonlyIndex> { ) -> RevsetImpl<DefaultReadonlyIndex> {
let index = repo let index = repo
.readonly_index() .readonly_index()
.as_any() .as_any()
@ -33,7 +33,7 @@ fn revset_for_commits<'index>(
.unwrap(); .unwrap();
let expression = let expression =
ResolvedExpression::Commits(commits.iter().map(|commit| commit.id().clone()).collect()); ResolvedExpression::Commits(commits.iter().map(|commit| commit.id().clone()).collect());
evaluate(&expression, repo.store(), index).unwrap() evaluate(&expression, repo.store(), index.clone()).unwrap()
} }
fn direct(commit: &Commit) -> RevsetGraphEdge { fn direct(commit: &Commit) -> RevsetGraphEdge {