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