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 thiserror::Error;
22
23use crate::backend::ChangeId;
24use crate::backend::CommitId;
25use crate::commit::Commit;
26use crate::object_id::HexPrefix;
27use crate::object_id::PrefixResolution;
28use crate::operation::Operation;
29use crate::repo_path::RepoPathBuf;
30use crate::revset::ResolvedExpression;
31use crate::revset::Revset;
32use crate::revset::RevsetEvaluationError;
33use crate::store::Store;
34
35/// Returned by [`IndexStore`] in the event of an error.
36#[derive(Debug, Error)]
37pub enum IndexStoreError {
38    /// Error reading a [`ReadonlyIndex`] from the [`IndexStore`].
39    #[error("Failed to read index")]
40    Read(#[source] Box<dyn std::error::Error + Send + Sync>),
41    /// Error writing a [`MutableIndex`] to the [`IndexStore`].
42    #[error("Failed to write index")]
43    Write(#[source] Box<dyn std::error::Error + Send + Sync>),
44}
45
46/// Result of [`IndexStore`] operations.
47pub type IndexStoreResult<T> = Result<T, IndexStoreError>;
48
49/// Returned by [`Index`] backend in the event of an error.
50#[derive(Debug, Error)]
51pub enum IndexError {
52    /// Error returned if [`Index::all_heads_for_gc()`] is not supported by the
53    /// [`Index`] backend.
54    #[error("Cannot collect all heads by index of this type")]
55    AllHeadsForGcUnsupported,
56    /// Some other index error.
57    #[error(transparent)]
58    Other(Box<dyn std::error::Error + Send + Sync>),
59}
60
61/// Result of [`Index`] operations.
62pub type IndexResult<T> = Result<T, IndexError>;
63
64/// Defines the interface for types that provide persistent storage for an
65/// index.
66pub trait IndexStore: Any + Send + Sync + Debug {
67    /// Returns a name representing the type of index that the `IndexStore` is
68    /// compatible with. For example, the `IndexStore` for the default index
69    /// returns "default".
70    fn name(&self) -> &str;
71
72    /// Returns the index at the specified operation.
73    fn get_index_at_op(
74        &self,
75        op: &Operation,
76        store: &Arc<Store>,
77    ) -> IndexStoreResult<Box<dyn ReadonlyIndex>>;
78
79    /// Writes `index` to the index store and returns a read-only version of the
80    /// index.
81    fn write_index(
82        &self,
83        index: Box<dyn MutableIndex>,
84        op: &Operation,
85    ) -> IndexStoreResult<Box<dyn ReadonlyIndex>>;
86}
87
88impl dyn IndexStore {
89    /// Returns reference of the implementation type.
90    pub fn downcast_ref<T: IndexStore>(&self) -> Option<&T> {
91        (self as &dyn Any).downcast_ref()
92    }
93}
94
95/// Defines the interface for types that provide an index of the commits in a
96/// repository by [`CommitId`].
97pub trait Index: Send + Sync {
98    /// Returns the minimum prefix length to disambiguate `commit_id` from other
99    /// commits in the index. The length returned is the number of hexadecimal
100    /// digits in the minimum prefix.
101    ///
102    /// If the given `commit_id` doesn't exist, returns the minimum prefix
103    /// length which matches none of the commits in the index.
104    fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> IndexResult<usize>;
105
106    /// Searches the index for commit IDs matching `prefix`. Returns a
107    /// [`PrefixResolution`] with a [`CommitId`] if the prefix matches a single
108    /// commit.
109    fn resolve_commit_id_prefix(
110        &self,
111        prefix: &HexPrefix,
112    ) -> IndexResult<PrefixResolution<CommitId>>;
113
114    /// Returns true if `commit_id` is present in the index.
115    fn has_id(&self, commit_id: &CommitId) -> IndexResult<bool>;
116
117    /// Returns true if `ancestor_id` commit is an ancestor of the
118    /// `descendant_id` commit, or if `ancestor_id` equals `descendant_id`.
119    fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> IndexResult<bool>;
120
121    /// Returns the best common ancestor or ancestors of the commits in `set1`
122    /// and `set2`. A "best common ancestor" has no descendants that are also
123    /// common ancestors.
124    fn common_ancestors(&self, set1: &[CommitId], set2: &[CommitId]) -> IndexResult<Vec<CommitId>>;
125
126    /// Heads among all indexed commits at the associated operation.
127    ///
128    /// Suppose the index contains all the historical heads and their ancestors
129    /// reachable from the associated operation, this function returns the heads
130    /// that should be preserved on garbage collection.
131    ///
132    /// The iteration order is unspecified.
133    fn all_heads_for_gc(&self) -> IndexResult<Box<dyn Iterator<Item = CommitId> + '_>>;
134
135    /// Returns the subset of commit IDs in `candidates` which are not ancestors
136    /// of other commits in `candidates`. If a commit id is duplicated in the
137    /// `candidates` list it will appear at most once in the output.
138    fn heads(&self, candidates: &mut dyn Iterator<Item = &CommitId>) -> IndexResult<Vec<CommitId>>;
139
140    /// Returns iterator over paths changed at the specified commit. The paths
141    /// are sorted. Returns `None` if the commit wasn't indexed.
142    fn changed_paths_in_commit(
143        &self,
144        commit_id: &CommitId,
145    ) -> IndexResult<Option<Box<dyn Iterator<Item = RepoPathBuf> + '_>>>;
146
147    /// Resolves the revset `expression` against the index and corresponding
148    /// `store`.
149    fn evaluate_revset(
150        &self,
151        expression: &ResolvedExpression,
152        store: &Arc<Store>,
153    ) -> Result<Box<dyn Revset + '_>, RevsetEvaluationError>;
154}
155
156#[expect(missing_docs)]
157pub trait ReadonlyIndex: Any + Send + Sync {
158    fn as_index(&self) -> &dyn Index;
159
160    fn change_id_index(&self, heads: &mut dyn Iterator<Item = &CommitId>)
161    -> Box<dyn ChangeIdIndex>;
162
163    fn start_modification(&self) -> Box<dyn MutableIndex>;
164}
165
166impl dyn ReadonlyIndex {
167    /// Returns reference of the implementation type.
168    pub fn downcast_ref<T: ReadonlyIndex>(&self) -> Option<&T> {
169        (self as &dyn Any).downcast_ref()
170    }
171}
172
173#[expect(missing_docs)]
174pub trait MutableIndex: Any {
175    fn as_index(&self) -> &dyn Index;
176
177    fn change_id_index(
178        &self,
179        heads: &mut dyn Iterator<Item = &CommitId>,
180    ) -> Box<dyn ChangeIdIndex + '_>;
181
182    fn add_commit(&mut self, commit: &Commit) -> IndexResult<()>;
183
184    fn merge_in(&mut self, other: &dyn ReadonlyIndex) -> IndexResult<()>;
185}
186
187impl dyn MutableIndex {
188    /// Downcasts to the implementation type.
189    pub fn downcast<T: MutableIndex>(self: Box<Self>) -> Option<Box<T>> {
190        (self as Box<dyn Any>).downcast().ok()
191    }
192
193    /// Returns reference of the implementation type.
194    pub fn downcast_ref<T: MutableIndex>(&self) -> Option<&T> {
195        (self as &dyn Any).downcast_ref()
196    }
197}
198
199/// Defines the interface for types that provide an index of the commits in a
200/// repository by [`ChangeId`].
201pub trait ChangeIdIndex: Send + Sync {
202    /// Resolve an unambiguous change ID prefix to the commit IDs in the index.
203    ///
204    /// The order of the returned commit IDs is unspecified.
205    fn resolve_prefix(&self, prefix: &HexPrefix) -> IndexResult<PrefixResolution<Vec<CommitId>>>;
206
207    /// This function returns the shortest length of a prefix of `key` that
208    /// disambiguates it from every other key in the index.
209    ///
210    /// The length returned is a number of hexadecimal digits.
211    ///
212    /// This has some properties that we do not currently make much use of:
213    ///
214    /// - The algorithm works even if `key` itself is not in the index.
215    ///
216    /// - In the special case when there are keys in the trie for which our
217    ///   `key` is an exact prefix, returns `key.len() + 1`. Conceptually, in
218    ///   order to disambiguate, you need every letter of the key *and* the
219    ///   additional fact that it's the entire key). This case is extremely
220    ///   unlikely for hashes with 12+ hexadecimal characters.
221    fn shortest_unique_prefix_len(&self, change_id: &ChangeId) -> IndexResult<usize>;
222}