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

index: extract as_composite() to trait method

The revset engine will accept abstract AsCompositeIndex type, and the
evaluated revset can be 'static if the index is behind Arc<T>.
This commit is contained in:
Yuya Nishihara 2023-12-07 23:26:03 +09:00
parent 8fdf9db6e0
commit 72d9cd019b
8 changed files with 42 additions and 12 deletions

View file

@ -18,7 +18,7 @@ use std::io::Write as _;
use clap::Subcommand;
use jj_lib::backend::ObjectId;
use jj_lib::default_index::{DefaultIndexStore, DefaultReadonlyIndex};
use jj_lib::default_index::{AsCompositeIndex as _, DefaultIndexStore, DefaultReadonlyIndex};
use jj_lib::local_working_copy::LocalWorkingCopy;
use jj_lib::revset;
use jj_lib::working_copy::WorkingCopy;

View file

@ -63,6 +63,26 @@ pub(super) trait IndexSegment: Send + Sync {
fn segment_entry_by_pos(&self, pos: IndexPosition, local_pos: u32) -> IndexEntry;
}
/// Abstraction over owned and borrowed types that can be cheaply converted to
/// a `CompositeIndex` reference.
pub trait AsCompositeIndex {
/// Returns reference wrapper that provides global access to this index.
fn as_composite(&self) -> CompositeIndex<'_>;
}
impl<T: AsCompositeIndex + ?Sized> AsCompositeIndex for &T {
fn as_composite(&self) -> CompositeIndex<'_> {
<T as AsCompositeIndex>::as_composite(self)
}
}
impl<T: AsCompositeIndex + ?Sized> AsCompositeIndex for &mut T {
fn as_composite(&self) -> CompositeIndex<'_> {
<T as AsCompositeIndex>::as_composite(self)
}
}
/// Reference wrapper that provides global access to nested index segments.
#[derive(Clone, Copy)]
pub struct CompositeIndex<'a>(&'a dyn IndexSegment);
@ -288,6 +308,12 @@ impl<'a> CompositeIndex<'a> {
}
}
impl AsCompositeIndex for CompositeIndex<'_> {
fn as_composite(&self) -> CompositeIndex<'_> {
*self
}
}
impl Index for CompositeIndex<'_> {
/// Suppose the given `commit_id` exists, returns the minimum prefix length
/// to disambiguate it. The length to be returned is a number of hexadecimal

View file

@ -21,7 +21,7 @@ mod readonly;
mod rev_walk;
mod store;
pub use self::composite::{CompositeIndex, IndexLevelStats, IndexStats};
pub use self::composite::{AsCompositeIndex, CompositeIndex, IndexLevelStats, IndexStats};
pub use self::entry::{IndexEntry, IndexPosition};
pub use self::mutable::DefaultMutableIndex;
pub use self::readonly::DefaultReadonlyIndex;

View file

@ -30,7 +30,7 @@ use itertools::Itertools;
use smallvec::SmallVec;
use tempfile::NamedTempFile;
use super::composite::{CompositeIndex, IndexSegment};
use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment};
use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec};
use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment};
use super::store::IndexLoadError;
@ -403,10 +403,6 @@ impl DefaultMutableIndex {
DefaultMutableIndex(mutable_segment)
}
pub fn as_composite(&self) -> CompositeIndex {
self.0.as_composite()
}
#[cfg(test)]
pub(crate) fn add_commit_data(
&mut self,
@ -422,6 +418,12 @@ impl DefaultMutableIndex {
}
}
impl AsCompositeIndex for DefaultMutableIndex {
fn as_composite(&self) -> CompositeIndex<'_> {
self.0.as_composite()
}
}
impl Index for DefaultMutableIndex {
fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize {
self.as_composite()

View file

@ -25,7 +25,7 @@ use std::sync::Arc;
use byteorder::{LittleEndian, ReadBytesExt};
use smallvec::SmallVec;
use super::composite::{CompositeIndex, IndexSegment};
use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment};
use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec};
use super::mutable::DefaultMutableIndex;
use super::store::IndexLoadError;
@ -384,8 +384,10 @@ impl DefaultReadonlyIndex {
pub(super) fn as_segment(&self) -> &Arc<ReadonlyIndexSegment> {
&self.0
}
}
pub fn as_composite(&self) -> CompositeIndex {
impl AsCompositeIndex for DefaultReadonlyIndex {
fn as_composite(&self) -> CompositeIndex<'_> {
self.0.as_composite()
}
}

View file

@ -978,7 +978,7 @@ fn has_diff_from_parent(
mod tests {
use super::*;
use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::default_index::DefaultMutableIndex;
use crate::default_index::{AsCompositeIndex as _, DefaultMutableIndex};
/// Generator of unique 16-byte ChangeId excluding root id
fn change_id_generator() -> impl FnMut() -> ChangeId {

View file

@ -14,7 +14,7 @@
use itertools::Itertools;
use jj_lib::commit::Commit;
use jj_lib::default_index::DefaultReadonlyIndex;
use jj_lib::default_index::{AsCompositeIndex as _, DefaultReadonlyIndex};
use jj_lib::default_revset_engine::{evaluate, RevsetImpl};
use jj_lib::repo::{ReadonlyRepo, Repo as _};
use jj_lib::revset::ResolvedExpression;

View file

@ -18,7 +18,7 @@ use jj_lib::backend::CommitId;
use jj_lib::commit::Commit;
use jj_lib::commit_builder::CommitBuilder;
use jj_lib::default_index::{
CompositeIndex, DefaultMutableIndex, DefaultReadonlyIndex, IndexPosition,
AsCompositeIndex as _, CompositeIndex, DefaultMutableIndex, DefaultReadonlyIndex, IndexPosition,
};
use jj_lib::index::Index as _;
use jj_lib::repo::{MutableRepo, ReadonlyRepo, Repo};