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