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