Skip to main content

jj_lib/
index.rs

1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Interfaces for indexes of the commits in a repository.
16
17use std::any::Any;
18use std::fmt::Debug;
19use std::sync::Arc;
20
21use async_trait::async_trait;
22use itertools::Itertools as _;
23use thiserror::Error;
24
25use crate::backend::ChangeId;
26use crate::backend::CommitId;
27use crate::commit::Commit;
28use crate::object_id::HexPrefix;
29use crate::object_id::PrefixResolution;
30use crate::operation::Operation;
31use crate::repo_path::RepoPathBuf;
32use crate::revset::ResolvedExpression;
33use crate::revset::Revset;
34use crate::revset::RevsetEvaluationError;
35use crate::store::Store;
36
37/// Returned by [`IndexStore`] in the event of an error.
38#[derive(Debug, Error)]
39pub enum IndexStoreError {
40    /// Error reading a [`ReadonlyIndex`] from the [`IndexStore`].
41    #[error("Failed to read index")]
42    Read(#[source] Box<dyn std::error::Error + Send + Sync>),
43    /// Error writing a [`MutableIndex`] to the [`IndexStore`].
44    #[error("Failed to write index")]
45    Write(#[source] Box<dyn std::error::Error + Send + Sync>),
46}
47
48/// Result of [`IndexStore`] operations.
49pub type IndexStoreResult<T> = Result<T, IndexStoreError>;
50
51/// Returned by [`Index`] backend in the event of an error.
52#[derive(Debug, Error)]
53pub enum IndexError {
54    /// Error returned if [`Index::all_heads_for_gc()`] is not supported by the
55    /// [`Index`] backend.
56    #[error("Cannot collect all heads by index of this type")]
57    AllHeadsForGcUnsupported,
58    /// Some other index error.
59    #[error(transparent)]
60    Other(Box<dyn std::error::Error + Send + Sync>),
61}
62
63/// Result of [`Index`] operations.
64pub type IndexResult<T> = Result<T, IndexError>;
65
66/// Defines the interface for types that provide persistent storage for an
67/// index.
68#[async_trait]
69pub trait IndexStore: Any + Send + Sync + Debug {
70    /// Returns a name representing the type of index that the `IndexStore` is
71    /// compatible with. For example, the `IndexStore` for the default index
72    /// returns "default".
73    fn name(&self) -> &str;
74
75    /// Returns the index at the specified operation.
76    async fn get_index_at_op(
77        &self,
78        op: &Operation,
79        store: &Arc<Store>,
80    ) -> IndexStoreResult<Box<dyn ReadonlyIndex>>;
81
82    /// Writes `index` to the index store and returns a read-only version of the
83    /// index.
84    fn write_index(
85        &self,
86        index: Box<dyn MutableIndex>,
87        op: &Operation,
88    ) -> IndexStoreResult<Box<dyn ReadonlyIndex>>;
89}
90
91impl dyn IndexStore {
92    /// Returns reference of the implementation type.
93    pub fn downcast_ref<T: IndexStore>(&self) -> Option<&T> {
94        (self as &dyn Any).downcast_ref()
95    }
96}
97
98/// Defines the interface for types that provide an index of the commits in a
99/// repository by [`CommitId`].
100pub trait Index: Send + Sync {
101    /// Returns the minimum prefix length to disambiguate `commit_id` from other
102    /// commits in the index. The length returned is the number of hexadecimal
103    /// digits in the minimum prefix.
104    ///
105    /// If the given `commit_id` doesn't exist, returns the minimum prefix
106    /// length which matches none of the commits in the index.
107    fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> IndexResult<usize>;
108
109    /// Searches the index for commit IDs matching `prefix`. Returns a
110    /// [`PrefixResolution`] with a [`CommitId`] if the prefix matches a single
111    /// commit.
112    fn resolve_commit_id_prefix(
113        &self,
114        prefix: &HexPrefix,
115    ) -> IndexResult<PrefixResolution<CommitId>>;
116
117    /// Returns true if `commit_id` is present in the index.
118    fn has_id(&self, commit_id: &CommitId) -> IndexResult<bool>;
119
120    /// Returns true if `ancestor_id` commit is an ancestor of the
121    /// `descendant_id` commit, or if `ancestor_id` equals `descendant_id`.
122    fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> IndexResult<bool>;
123
124    /// Returns the best common ancestor or ancestors of the commits in `set1`
125    /// and `set2`. A "best common ancestor" has no descendants that are also
126    /// common ancestors.
127    fn common_ancestors(&self, set1: &[CommitId], set2: &[CommitId]) -> IndexResult<Vec<CommitId>>;
128
129    /// Heads among all indexed commits at the associated operation.
130    ///
131    /// Suppose the index contains all the historical heads and their ancestors
132    /// reachable from the associated operation, this function returns the heads
133    /// that should be preserved on garbage collection.
134    ///
135    /// The iteration order is unspecified.
136    fn all_heads_for_gc(&self) -> IndexResult<Box<dyn Iterator<Item = CommitId> + '_>>;
137
138    /// Returns the subset of commit IDs in `candidates` which are not ancestors
139    /// of other commits in `candidates`. If a commit id is duplicated in the
140    /// `candidates` list it will appear at most once in the output.
141    fn heads(&self, candidates: &mut dyn Iterator<Item = &CommitId>) -> IndexResult<Vec<CommitId>>;
142
143    /// Returns iterator over paths changed at the specified commit. The paths
144    /// are sorted. Returns `None` if the commit wasn't indexed.
145    fn changed_paths_in_commit(
146        &self,
147        commit_id: &CommitId,
148    ) -> IndexResult<Option<Box<dyn Iterator<Item = RepoPathBuf> + '_>>>;
149
150    /// Resolves the revset `expression` against the index and corresponding
151    /// `store`.
152    fn evaluate_revset(
153        &self,
154        expression: &ResolvedExpression,
155        store: &Arc<Store>,
156    ) -> Result<Box<dyn Revset + '_>, RevsetEvaluationError>;
157}
158
159#[expect(missing_docs)]
160pub trait ReadonlyIndex: Any + Send + Sync {
161    fn as_index(&self) -> &dyn Index;
162
163    fn change_id_index(&self, heads: &mut dyn Iterator<Item = &CommitId>)
164    -> Box<dyn ChangeIdIndex>;
165
166    fn start_modification(&self) -> Box<dyn MutableIndex>;
167}
168
169impl dyn ReadonlyIndex {
170    /// Returns reference of the implementation type.
171    pub fn downcast_ref<T: ReadonlyIndex>(&self) -> Option<&T> {
172        (self as &dyn Any).downcast_ref()
173    }
174}
175
176#[expect(missing_docs)]
177#[async_trait]
178pub trait MutableIndex: Any {
179    fn as_index(&self) -> &dyn Index;
180
181    fn change_id_index(
182        &self,
183        heads: &mut dyn Iterator<Item = &CommitId>,
184    ) -> Box<dyn ChangeIdIndex + '_>;
185
186    async fn add_commit(&mut self, commit: &Commit) -> IndexResult<()>;
187
188    fn merge_in(&mut self, other: &dyn ReadonlyIndex) -> IndexResult<()>;
189}
190
191impl dyn MutableIndex {
192    /// Downcasts to the implementation type.
193    pub fn downcast<T: MutableIndex>(self: Box<Self>) -> Option<Box<T>> {
194        (self as Box<dyn Any>).downcast().ok()
195    }
196
197    /// Returns reference of the implementation type.
198    pub fn downcast_ref<T: MutableIndex>(&self) -> Option<&T> {
199        (self as &dyn Any).downcast_ref()
200    }
201}
202
203/// The state of a commit with a given change ID.
204#[derive(Copy, Clone, Eq, PartialEq, Debug)]
205pub enum ResolvedChangeState {
206    /// The commit is visible (reachable from the visible heads).
207    Visible,
208    /// The commit is hidden (not reachable from the visible heads).
209    Hidden,
210}
211
212/// Represents the possible target commits of a resolved change ID. If the
213/// change is divergent, there may be multiple visible commits. Hidden commits
214/// can also be returned to allow showing a change offset number in the evolog.
215#[derive(Clone, Eq, PartialEq, Debug)]
216pub struct ResolvedChangeTargets {
217    /// All indexed commits with this change ID. The sort order of the commits
218    /// is determined by the index implementation, but it is preferred that more
219    /// recent commits should be sorted before later commits when possible. All
220    /// visible commits must be included, but some hidden commits may be omitted
221    /// if it would be inefficient for the index to support them.
222    pub targets: Vec<(CommitId, ResolvedChangeState)>,
223}
224
225impl ResolvedChangeTargets {
226    /// Returns an iterator over all visible commits for this change ID, as well
227    /// as their offsets.
228    pub fn visible_with_offsets(&self) -> impl Iterator<Item = (usize, &CommitId)> {
229        self.targets
230            .iter()
231            .enumerate()
232            .filter_map(|(i, (target, state))| {
233                (*state == ResolvedChangeState::Visible).then_some((i, target))
234            })
235    }
236
237    /// Returns true if the commit ID is one of the visible targets of this
238    /// change ID.
239    pub fn has_visible(&self, commit: &CommitId) -> bool {
240        self.visible_with_offsets()
241            .any(|(_, target)| target == commit)
242    }
243
244    /// Returns true if there are multiple visible targets for this change ID.
245    pub fn is_divergent(&self) -> bool {
246        self.visible_with_offsets().take(2).count() > 1
247    }
248
249    /// Returns the commit ID at a given offset. The change offset of a commit
250    /// can be found using [`ResolvedChangeTargets::find_offset`].
251    pub fn at_offset(&self, offset: usize) -> Option<&CommitId> {
252        self.targets.get(offset).map(|(target, _state)| target)
253    }
254
255    /// Finds the change offset corresponding to a commit. Newer commits should
256    /// generally have a lower offset than older commits, but this is not
257    /// guaranteed. Hidden commits may not have an offset at all.
258    pub fn find_offset(&self, commit_id: &CommitId) -> Option<usize> {
259        self.targets
260            .iter()
261            .position(|(target, _state)| target == commit_id)
262    }
263
264    /// Extracts the visible commits for this change ID. Returns `None` if there
265    /// are no visible commits with this change ID.
266    pub fn into_visible(self) -> Option<Vec<CommitId>> {
267        let visible = self
268            .targets
269            .into_iter()
270            .filter_map(|(target, state)| (state == ResolvedChangeState::Visible).then_some(target))
271            .collect_vec();
272        (!visible.is_empty()).then_some(visible)
273    }
274}
275
276/// Defines the interface for types that provide an index of the commits in a
277/// repository by [`ChangeId`].
278pub trait ChangeIdIndex: Send + Sync {
279    /// Resolve an unambiguous change ID prefix to the commit IDs in the index.
280    fn resolve_prefix(
281        &self,
282        prefix: &HexPrefix,
283    ) -> IndexResult<PrefixResolution<ResolvedChangeTargets>>;
284
285    /// This function returns the shortest length of a prefix of `key` that
286    /// disambiguates it from every other key in the index.
287    ///
288    /// The length returned is a number of hexadecimal digits.
289    ///
290    /// This has some properties that we do not currently make much use of:
291    ///
292    /// - The algorithm works even if `key` itself is not in the index.
293    ///
294    /// - In the special case when there are keys in the trie for which our
295    ///   `key` is an exact prefix, returns `key.len() + 1`. Conceptually, in
296    ///   order to disambiguate, you need every letter of the key *and* the
297    ///   additional fact that it's the entire key). This case is extremely
298    ///   unlikely for hashes with 12+ hexadecimal characters.
299    fn shortest_unique_prefix_len(&self, change_id: &ChangeId) -> IndexResult<usize>;
300}