From 5121e1f4e95f21b89e38885dc8644300d7721ded Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 11 Dec 2023 21:30:16 +0900 Subject: [PATCH] index: move IndexSegment trait to "composite" module Perhaps, this is the most controversial part. It could be moved to new "segment" module (or something like "common"), but I think IndexSegment can be considered a trait that enables the CompositeIndex abstraction. --- lib/src/default_index/composite.rs | 38 ++++++++++++++++++++++++-- lib/src/default_index/entry.rs | 3 +- lib/src/default_index/mod.rs | 44 +++--------------------------- lib/src/default_index/mutable.rs | 3 +- lib/src/default_index/readonly.rs | 3 +- 5 files changed, 42 insertions(+), 49 deletions(-) diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index cfa61bf54..97551fee0 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -21,16 +21,48 @@ use std::sync::Arc; use itertools::Itertools; -use super::entry::{IndexEntry, IndexPosition, IndexPositionByGeneration}; +use super::entry::{IndexEntry, IndexPosition, IndexPositionByGeneration, SmallIndexPositionsVec}; use super::readonly::ReadonlyIndexSegment; use super::rev_walk::RevWalk; -use super::IndexSegment; -use crate::backend::{CommitId, ObjectId}; +use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::index::{HexPrefix, Index, PrefixResolution}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::store::Store; use crate::{backend, default_revset_engine}; +pub(super) trait IndexSegment: Send + Sync { + fn segment_num_parent_commits(&self) -> u32; + + fn segment_num_commits(&self) -> u32; + + fn segment_parent_file(&self) -> Option<&Arc>; + + fn segment_name(&self) -> Option; + + fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option; + + /// Suppose the given `commit_id` exists, returns the positions of the + /// previous and next commit ids in lexicographical order. + fn segment_commit_id_to_neighbor_positions( + &self, + commit_id: &CommitId, + ) -> (Option, Option); + + fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; + + fn segment_generation_number(&self, local_pos: u32) -> u32; + + fn segment_commit_id(&self, local_pos: u32) -> CommitId; + + fn segment_change_id(&self, local_pos: u32) -> ChangeId; + + fn segment_num_parents(&self, local_pos: u32) -> u32; + + fn segment_parent_positions(&self, local_pos: u32) -> SmallIndexPositionsVec; + + fn segment_entry_by_pos(&self, pos: IndexPosition, local_pos: u32) -> IndexEntry; +} + #[derive(Clone, Copy)] pub struct CompositeIndex<'a>(&'a dyn IndexSegment); diff --git a/lib/src/default_index/entry.rs b/lib/src/default_index/entry.rs index 25ca9dd9e..cfe4c57ed 100644 --- a/lib/src/default_index/entry.rs +++ b/lib/src/default_index/entry.rs @@ -20,8 +20,7 @@ use std::hash::{Hash, Hasher}; use smallvec::SmallVec; -use super::composite::CompositeIndex; -use super::IndexSegment; +use super::composite::{CompositeIndex, IndexSegment}; use crate::backend::{ChangeId, CommitId, ObjectId}; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index 29f77162e..ea3a0f41a 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -21,66 +21,30 @@ mod readonly; mod rev_walk; mod store; -use std::sync::Arc; - pub use self::composite::{CompositeIndex, IndexLevelStats, IndexStats}; -use self::entry::SmallIndexPositionsVec; pub use self::entry::{IndexEntry, IndexEntryByPosition, IndexPosition}; pub use self::mutable::DefaultMutableIndex; pub use self::readonly::DefaultReadonlyIndex; -use self::readonly::ReadonlyIndexSegment; pub use self::rev_walk::{ RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange, }; pub use self::store::{DefaultIndexStore, DefaultIndexStoreError, IndexLoadError}; -use crate::backend::{ChangeId, CommitId}; -use crate::index::{HexPrefix, PrefixResolution}; - -trait IndexSegment: Send + Sync { - fn segment_num_parent_commits(&self) -> u32; - - fn segment_num_commits(&self) -> u32; - - fn segment_parent_file(&self) -> Option<&Arc>; - - fn segment_name(&self) -> Option; - - fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option; - - /// Suppose the given `commit_id` exists, returns the positions of the - /// previous and next commit ids in lexicographical order. - fn segment_commit_id_to_neighbor_positions( - &self, - commit_id: &CommitId, - ) -> (Option, Option); - - fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution; - - fn segment_generation_number(&self, local_pos: u32) -> u32; - - fn segment_commit_id(&self, local_pos: u32) -> CommitId; - - fn segment_change_id(&self, local_pos: u32) -> ChangeId; - - fn segment_num_parents(&self, local_pos: u32) -> u32; - - fn segment_parent_positions(&self, local_pos: u32) -> SmallIndexPositionsVec; - - fn segment_entry_by_pos(&self, pos: IndexPosition, local_pos: u32) -> IndexEntry; -} #[cfg(test)] mod tests { use std::ops::Range; + use std::sync::Arc; use itertools::Itertools; use smallvec::smallvec_inline; use test_case::test_case; + use super::composite::IndexSegment; + use super::entry::SmallIndexPositionsVec; use super::mutable::MutableIndexSegment; use super::*; use crate::backend::{ChangeId, CommitId, ObjectId}; - use crate::index::Index; + use crate::index::{HexPrefix, Index, PrefixResolution}; /// Generator of unique 16-byte ChangeId excluding root id fn change_id_generator() -> impl FnMut() -> ChangeId { diff --git a/lib/src/default_index/mutable.rs b/lib/src/default_index/mutable.rs index d0b87410d..11c12c636 100644 --- a/lib/src/default_index/mutable.rs +++ b/lib/src/default_index/mutable.rs @@ -30,11 +30,10 @@ use itertools::Itertools; use smallvec::SmallVec; use tempfile::NamedTempFile; -use super::composite::CompositeIndex; +use super::composite::{CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment}; use super::store::IndexLoadError; -use super::IndexSegment; use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::commit::Commit; use crate::file_util::persist_content_addressed_temp_file; diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index f21f9d194..1c01fbfb3 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -25,11 +25,10 @@ use std::sync::Arc; use byteorder::{LittleEndian, ReadBytesExt}; use smallvec::SmallVec; -use super::composite::CompositeIndex; +use super::composite::{CompositeIndex, IndexSegment}; use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::mutable::DefaultMutableIndex; use super::store::IndexLoadError; -use super::IndexSegment; use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};