Skip to main content

jj_lib/
op_store.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#![expect(missing_docs)]
16
17use std::any::Any;
18use std::collections::BTreeMap;
19use std::collections::HashSet;
20use std::fmt::Debug;
21use std::iter;
22use std::sync::LazyLock;
23use std::time::SystemTime;
24
25use async_trait::async_trait;
26use itertools::Itertools as _;
27use thiserror::Error;
28
29use crate::backend::CommitId;
30use crate::backend::MillisSinceEpoch;
31use crate::backend::Timestamp;
32use crate::content_hash::ContentHash;
33use crate::merge::Merge;
34use crate::object_id::HexPrefix;
35use crate::object_id::ObjectId as _;
36use crate::object_id::PrefixResolution;
37use crate::object_id::id_type;
38use crate::ref_name::GitRefNameBuf;
39use crate::ref_name::RefName;
40use crate::ref_name::RefNameBuf;
41use crate::ref_name::RemoteName;
42use crate::ref_name::RemoteNameBuf;
43use crate::ref_name::RemoteRefSymbol;
44use crate::ref_name::WorkspaceNameBuf;
45
46id_type!(pub ViewId { hex() });
47id_type!(pub OperationId { hex() });
48
49#[derive(ContentHash, PartialEq, Eq, Hash, Clone, Debug, serde::Serialize)]
50#[serde(transparent)]
51pub struct RefTarget {
52    merge: Merge<Option<CommitId>>,
53}
54
55impl Default for RefTarget {
56    fn default() -> Self {
57        Self::absent()
58    }
59}
60
61impl RefTarget {
62    /// Creates non-conflicting target pointing to no commit.
63    pub fn absent() -> Self {
64        Self::from_merge(Merge::absent())
65    }
66
67    /// Returns non-conflicting target pointing to no commit.
68    ///
69    /// This will typically be used in place of `None` returned by map lookup.
70    pub fn absent_ref() -> &'static Self {
71        static TARGET: LazyLock<RefTarget> = LazyLock::new(RefTarget::absent);
72        &TARGET
73    }
74
75    /// Creates non-conflicting target that optionally points to a commit.
76    pub fn resolved(maybe_id: Option<CommitId>) -> Self {
77        Self::from_merge(Merge::resolved(maybe_id))
78    }
79
80    /// Creates non-conflicting target pointing to a commit.
81    pub fn normal(id: CommitId) -> Self {
82        Self::from_merge(Merge::normal(id))
83    }
84
85    /// Creates target from removed/added ids.
86    pub fn from_legacy_form(
87        removed_ids: impl IntoIterator<Item = CommitId>,
88        added_ids: impl IntoIterator<Item = CommitId>,
89    ) -> Self {
90        Self::from_merge(Merge::from_legacy_form(removed_ids, added_ids))
91    }
92
93    pub fn from_merge(merge: Merge<Option<CommitId>>) -> Self {
94        Self { merge }
95    }
96
97    /// Returns the underlying value if this target is non-conflicting.
98    pub fn as_resolved(&self) -> Option<&Option<CommitId>> {
99        self.merge.as_resolved()
100    }
101
102    /// Returns id if this target is non-conflicting and points to a commit.
103    pub fn as_normal(&self) -> Option<&CommitId> {
104        self.merge.as_normal()
105    }
106
107    /// Returns true if this target points to no commit.
108    pub fn is_absent(&self) -> bool {
109        self.merge.is_absent()
110    }
111
112    /// Returns true if this target points to any commit. Conflicting target is
113    /// always "present" as it should have at least one commit id.
114    pub fn is_present(&self) -> bool {
115        self.merge.is_present()
116    }
117
118    /// Whether this target has conflicts.
119    pub fn has_conflict(&self) -> bool {
120        !self.merge.is_resolved()
121    }
122
123    pub fn removed_ids(&self) -> impl Iterator<Item = &CommitId> {
124        self.merge.removes().flatten()
125    }
126
127    pub fn added_ids(&self) -> impl Iterator<Item = &CommitId> {
128        self.merge.adds().flatten()
129    }
130
131    pub fn as_merge(&self) -> &Merge<Option<CommitId>> {
132        &self.merge
133    }
134}
135
136/// Remote bookmark or tag.
137#[derive(ContentHash, Clone, Debug, Eq, Hash, PartialEq)]
138pub struct RemoteRef {
139    pub target: RefTarget,
140    pub state: RemoteRefState,
141}
142
143impl RemoteRef {
144    /// Creates remote ref pointing to no commit.
145    pub fn absent() -> Self {
146        Self {
147            target: RefTarget::absent(),
148            state: RemoteRefState::New,
149        }
150    }
151
152    /// Returns remote ref pointing to no commit.
153    ///
154    /// This will typically be used in place of `None` returned by map lookup.
155    pub fn absent_ref() -> &'static Self {
156        static TARGET: LazyLock<RemoteRef> = LazyLock::new(RemoteRef::absent);
157        &TARGET
158    }
159
160    /// Returns true if the target points to no commit.
161    pub fn is_absent(&self) -> bool {
162        self.target.is_absent()
163    }
164
165    /// Returns true if the target points to any commit.
166    pub fn is_present(&self) -> bool {
167        self.target.is_present()
168    }
169
170    /// Returns true if the ref is supposed to be merged in to the local ref.
171    pub fn is_tracked(&self) -> bool {
172        self.state == RemoteRefState::Tracked
173    }
174
175    /// Target that should have been merged in to the local ref.
176    ///
177    /// Use this as the base or known target when merging new remote ref in to
178    /// local or pushing local ref to remote.
179    pub fn tracked_target(&self) -> &RefTarget {
180        if self.is_tracked() {
181            &self.target
182        } else {
183            RefTarget::absent_ref()
184        }
185    }
186}
187
188/// Whether the ref is tracked or not.
189#[derive(ContentHash, Clone, Copy, Debug, Eq, Hash, PartialEq)]
190pub enum RemoteRefState {
191    /// Remote ref is not merged in to the local ref.
192    New,
193    /// Remote ref has been merged in to the local ref. Incoming ref will be
194    /// merged, too.
195    Tracked,
196}
197
198/// Helper to strip redundant `Option<T>` from `RefTarget` lookup result.
199pub trait RefTargetOptionExt {
200    type Value;
201
202    fn flatten(self) -> Self::Value;
203}
204
205impl RefTargetOptionExt for Option<RefTarget> {
206    type Value = RefTarget;
207
208    fn flatten(self) -> Self::Value {
209        self.unwrap_or_else(RefTarget::absent)
210    }
211}
212
213impl<'a> RefTargetOptionExt for Option<&'a RefTarget> {
214    type Value = &'a RefTarget;
215
216    fn flatten(self) -> Self::Value {
217        self.unwrap_or_else(|| RefTarget::absent_ref())
218    }
219}
220
221impl RefTargetOptionExt for Option<RemoteRef> {
222    type Value = RemoteRef;
223
224    fn flatten(self) -> Self::Value {
225        self.unwrap_or_else(RemoteRef::absent)
226    }
227}
228
229impl<'a> RefTargetOptionExt for Option<&'a RemoteRef> {
230    type Value = &'a RemoteRef;
231
232    fn flatten(self) -> Self::Value {
233        self.unwrap_or_else(|| RemoteRef::absent_ref())
234    }
235}
236
237/// Local and remote refs of the same name.
238#[derive(PartialEq, Eq, Clone, Debug)]
239pub struct LocalRemoteRefTarget<'a> {
240    /// The commit the ref points to locally.
241    pub local_target: &'a RefTarget,
242    /// `(remote_name, remote_ref)` pairs in lexicographical order.
243    pub remote_refs: Vec<(&'a RemoteName, &'a RemoteRef)>,
244}
245
246/// Represents the way the repo looks at a given time, just like how a Tree
247/// object represents how the file system looks at a given time.
248#[derive(ContentHash, PartialEq, Eq, Clone, Debug)]
249pub struct View {
250    /// All head commits. There should be at least one head commit.
251    pub head_ids: HashSet<CommitId>,
252    pub local_bookmarks: BTreeMap<RefNameBuf, RefTarget>,
253    pub local_tags: BTreeMap<RefNameBuf, RefTarget>,
254    pub remote_views: BTreeMap<RemoteNameBuf, RemoteView>,
255    pub git_refs: BTreeMap<GitRefNameBuf, RefTarget>,
256    /// The commit the Git HEAD points to.
257    // TODO: Support multiple Git worktrees?
258    // TODO: Do we want to store the current bookmark name too?
259    pub git_head: RefTarget,
260    // The commit that *should be* checked out in the workspace. Note that the working copy
261    // (.jj/working_copy/) has the source of truth about which commit *is* checked out (to be
262    // precise: the commit to which we most recently completed an update to).
263    pub wc_commit_ids: BTreeMap<WorkspaceNameBuf, CommitId>,
264}
265
266impl View {
267    /// Creates new (mostly empty) view containing the given commit as the head.
268    pub fn make_root(root_commit_id: CommitId) -> Self {
269        Self {
270            head_ids: HashSet::from([root_commit_id]),
271            local_bookmarks: BTreeMap::new(),
272            local_tags: BTreeMap::new(),
273            remote_views: BTreeMap::new(),
274            git_refs: BTreeMap::new(),
275            git_head: RefTarget::absent(),
276            wc_commit_ids: BTreeMap::new(),
277        }
278    }
279}
280
281/// Represents the state of the remote repo.
282#[derive(ContentHash, Clone, Debug, Default, Eq, PartialEq)]
283pub struct RemoteView {
284    // TODO: Do we need to support tombstones for remote bookmarks? For example, if the bookmark
285    // has been deleted locally and you pull from a remote, maybe it should make a difference
286    // whether the bookmark is known to have existed on the remote. We may not want to resurrect
287    // the bookmark if the bookmark's state on the remote was just not known.
288    pub bookmarks: BTreeMap<RefNameBuf, RemoteRef>,
289    pub tags: BTreeMap<RefNameBuf, RemoteRef>,
290}
291
292/// Iterates pair of local and remote refs by name.
293pub(crate) fn merge_join_ref_views<'a>(
294    local_refs: &'a BTreeMap<RefNameBuf, RefTarget>,
295    remote_views: &'a BTreeMap<RemoteNameBuf, RemoteView>,
296    get_remote_refs: impl FnMut(&RemoteView) -> &BTreeMap<RefNameBuf, RemoteRef>,
297) -> impl Iterator<Item = (&'a RefName, LocalRemoteRefTarget<'a>)> {
298    let mut local_refs_iter = local_refs
299        .iter()
300        .map(|(name, target)| (&**name, target))
301        .peekable();
302    let mut remote_refs_iter = flatten_remote_refs(remote_views, get_remote_refs).peekable();
303
304    iter::from_fn(move || {
305        // Pick earlier bookmark name
306        let (name, local_target) = if let Some((symbol, _)) = remote_refs_iter.peek() {
307            local_refs_iter
308                .next_if(|&(local_name, _)| local_name <= symbol.name)
309                .unwrap_or((symbol.name, RefTarget::absent_ref()))
310        } else {
311            local_refs_iter.next()?
312        };
313        let remote_refs = remote_refs_iter
314            .peeking_take_while(|(symbol, _)| symbol.name == name)
315            .map(|(symbol, remote_ref)| (symbol.remote, remote_ref))
316            .collect();
317        let local_remote_target = LocalRemoteRefTarget {
318            local_target,
319            remote_refs,
320        };
321        Some((name, local_remote_target))
322    })
323}
324
325/// Iterates `(symbol, remote_ref)`s in lexicographical order.
326pub(crate) fn flatten_remote_refs(
327    remote_views: &BTreeMap<RemoteNameBuf, RemoteView>,
328    mut get_remote_refs: impl FnMut(&RemoteView) -> &BTreeMap<RefNameBuf, RemoteRef>,
329) -> impl Iterator<Item = (RemoteRefSymbol<'_>, &RemoteRef)> {
330    remote_views
331        .iter()
332        .map(|(remote, remote_view)| {
333            get_remote_refs(remote_view)
334                .iter()
335                .map(move |(name, remote_ref)| (name.to_remote_symbol(remote), remote_ref))
336        })
337        .kmerge_by(|(symbol1, _), (symbol2, _)| symbol1 < symbol2)
338}
339
340#[derive(Clone, ContentHash, Debug, Eq, PartialEq, serde::Serialize)]
341pub struct TimestampRange {
342    // Could be aliased to Range<Timestamp> if needed.
343    pub start: Timestamp,
344    pub end: Timestamp,
345}
346
347/// Represents an operation (transaction) on the repo view, just like how a
348/// Commit object represents an operation on the tree.
349///
350/// Operations and views are not meant to be exchanged between repos or users;
351/// they represent local state and history.
352///
353/// The operation history will almost always be linear. It will only have
354/// forks when parallel operations occurred. The parent is determined when
355/// the transaction starts. When the transaction commits, a lock will be
356/// taken and it will be checked that the current head of the operation
357/// graph is unchanged. If the current head has changed, there has been
358/// concurrent operation.
359#[derive(ContentHash, PartialEq, Eq, Clone, Debug, serde::Serialize)]
360pub struct Operation {
361    #[serde(skip)] // TODO: should be exposed?
362    pub view_id: ViewId,
363    pub parents: Vec<OperationId>,
364    #[serde(flatten)]
365    pub metadata: OperationMetadata,
366    /// Mapping from new commit to its predecessors, or `None` if predecessors
367    /// weren't recorded when the operation was written.
368    ///
369    /// * `commit_id: []` if the commit was newly created.
370    /// * `commit_id: [predecessor_id, ..]` if the commit was rewritten.
371    ///
372    /// This mapping preserves all transitive predecessors if a commit was
373    /// rewritten multiple times within the same transaction. For example, if
374    /// `X` was rewritten as `Y`, then rebased as `Z`, these modifications are
375    /// recorded as `{Y: [X], Z: [Y]}`.
376    ///
377    /// Existing commits (including commits imported from Git) aren't tracked
378    /// even if they became visible at this operation.
379    // BTreeMap for ease of deterministic serialization. If the deserialization
380    // cost matters, maybe this can be changed to sorted Vec.
381    #[serde(skip)] // TODO: should be exposed?
382    pub commit_predecessors: Option<BTreeMap<CommitId, Vec<CommitId>>>,
383}
384
385impl Operation {
386    pub fn make_root(root_view_id: ViewId) -> Self {
387        let timestamp = Timestamp {
388            timestamp: MillisSinceEpoch(0),
389            tz_offset: 0,
390        };
391        let metadata = OperationMetadata {
392            time: TimestampRange {
393                start: timestamp,
394                end: timestamp,
395            },
396            description: "".to_string(),
397            hostname: "".to_string(),
398            username: "".to_string(),
399            is_snapshot: false,
400            workspace_name: None,
401            attributes: BTreeMap::new(),
402        };
403        Self {
404            view_id: root_view_id,
405            parents: vec![],
406            metadata,
407            // The root operation is guaranteed to have no new commits. The root
408            // commit could be considered born at the root operation, but there
409            // may be other commits created within the abandoned operations.
410            // They don't have any predecessors records as well.
411            commit_predecessors: Some(BTreeMap::new()),
412        }
413    }
414}
415
416#[derive(ContentHash, PartialEq, Eq, Clone, Debug, serde::Serialize)]
417pub struct OperationMetadata {
418    pub time: TimestampRange,
419    // Whatever is useful to the user, such as exact command line call
420    pub description: String,
421    pub hostname: String,
422    pub username: String,
423    /// Whether this operation represents a pure snapshotting of the working
424    /// copy.
425    pub is_snapshot: bool,
426    /// The workspace this operation was performed in, if any
427    pub workspace_name: Option<WorkspaceNameBuf>,
428    pub attributes: BTreeMap<String, String>,
429}
430
431/// Data to be loaded into the root operation/view.
432#[derive(Clone, Debug)]
433pub struct RootOperationData {
434    /// The root commit ID, which should exist in the root view.
435    pub root_commit_id: CommitId,
436}
437
438#[derive(Debug, Error)]
439pub enum OpStoreError {
440    #[error("Object {hash} of type {object_type} not found")]
441    ObjectNotFound {
442        object_type: String,
443        hash: String,
444        source: Box<dyn std::error::Error + Send + Sync>,
445    },
446    #[error("Error when reading object {hash} of type {object_type}")]
447    ReadObject {
448        object_type: String,
449        hash: String,
450        source: Box<dyn std::error::Error + Send + Sync>,
451    },
452    #[error("Could not write object of type {object_type}")]
453    WriteObject {
454        object_type: &'static str,
455        source: Box<dyn std::error::Error + Send + Sync>,
456    },
457    #[error(transparent)]
458    Other(Box<dyn std::error::Error + Send + Sync>),
459}
460
461pub type OpStoreResult<T> = Result<T, OpStoreError>;
462
463#[async_trait]
464pub trait OpStore: Any + Send + Sync + Debug {
465    fn name(&self) -> &str;
466
467    fn root_operation_id(&self) -> &OperationId;
468
469    async fn read_view(&self, id: &ViewId) -> OpStoreResult<View>;
470
471    async fn write_view(&self, contents: &View) -> OpStoreResult<ViewId>;
472
473    async fn read_operation(&self, id: &OperationId) -> OpStoreResult<Operation>;
474
475    async fn write_operation(&self, contents: &Operation) -> OpStoreResult<OperationId>;
476
477    /// Resolves an unambiguous operation ID prefix.
478    async fn resolve_operation_id_prefix(
479        &self,
480        prefix: &HexPrefix,
481    ) -> OpStoreResult<PrefixResolution<OperationId>>;
482
483    /// Prunes unreachable operations and views.
484    ///
485    /// All operations and views reachable from the `head_ids` won't be
486    /// removed. In addition to that, objects created after `keep_newer` will be
487    /// preserved. This mitigates a risk of deleting new heads created
488    /// concurrently by another process.
489    // TODO: return stats?
490    async fn gc(&self, head_ids: &[OperationId], keep_newer: SystemTime) -> OpStoreResult<()>;
491}
492
493impl dyn OpStore {
494    /// Returns reference of the implementation type.
495    pub fn downcast_ref<T: OpStore>(&self) -> Option<&T> {
496        (self as &dyn Any).downcast_ref()
497    }
498}
499
500#[cfg(test)]
501mod tests {
502    use maplit::btreemap;
503
504    use super::*;
505
506    #[test]
507    fn test_merge_join_bookmark_views() {
508        let remote_ref = |target: &RefTarget| RemoteRef {
509            target: target.clone(),
510            state: RemoteRefState::Tracked, // doesn't matter
511        };
512        let local_bookmark1_target = RefTarget::normal(CommitId::from_hex("111111"));
513        let local_bookmark2_target = RefTarget::normal(CommitId::from_hex("222222"));
514        let git_bookmark1_remote_ref = remote_ref(&RefTarget::normal(CommitId::from_hex("333333")));
515        let git_bookmark2_remote_ref = remote_ref(&RefTarget::normal(CommitId::from_hex("444444")));
516        let remote1_bookmark1_remote_ref =
517            remote_ref(&RefTarget::normal(CommitId::from_hex("555555")));
518        let remote2_bookmark2_remote_ref =
519            remote_ref(&RefTarget::normal(CommitId::from_hex("666666")));
520
521        let local_bookmarks = btreemap! {
522            "bookmark1".into() => local_bookmark1_target.clone(),
523            "bookmark2".into() => local_bookmark2_target.clone(),
524        };
525        let remote_views = btreemap! {
526            "git".into() => RemoteView {
527                bookmarks: btreemap! {
528                    "bookmark1".into() => git_bookmark1_remote_ref.clone(),
529                    "bookmark2".into() => git_bookmark2_remote_ref.clone(),
530                },
531                tags: btreemap! {},
532            },
533            "remote1".into() => RemoteView {
534                bookmarks: btreemap! {
535                    "bookmark1".into() => remote1_bookmark1_remote_ref.clone(),
536                },
537                tags: btreemap! {},
538            },
539            "remote2".into() => RemoteView {
540                bookmarks: btreemap! {
541                    "bookmark2".into() => remote2_bookmark2_remote_ref.clone(),
542                },
543                tags: btreemap! {},
544            },
545        };
546        assert_eq!(
547            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
548                .collect_vec(),
549            vec![
550                (
551                    "bookmark1".as_ref(),
552                    LocalRemoteRefTarget {
553                        local_target: &local_bookmark1_target,
554                        remote_refs: vec![
555                            ("git".as_ref(), &git_bookmark1_remote_ref),
556                            ("remote1".as_ref(), &remote1_bookmark1_remote_ref),
557                        ],
558                    },
559                ),
560                (
561                    "bookmark2".as_ref(),
562                    LocalRemoteRefTarget {
563                        local_target: &local_bookmark2_target.clone(),
564                        remote_refs: vec![
565                            ("git".as_ref(), &git_bookmark2_remote_ref),
566                            ("remote2".as_ref(), &remote2_bookmark2_remote_ref),
567                        ],
568                    },
569                ),
570            ],
571        );
572
573        // Local only
574        let local_bookmarks = btreemap! {
575            "bookmark1".into() => local_bookmark1_target.clone(),
576        };
577        let remote_views = btreemap! {};
578        assert_eq!(
579            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
580                .collect_vec(),
581            vec![(
582                "bookmark1".as_ref(),
583                LocalRemoteRefTarget {
584                    local_target: &local_bookmark1_target,
585                    remote_refs: vec![]
586                },
587            )],
588        );
589
590        // Remote only
591        let local_bookmarks = btreemap! {};
592        let remote_views = btreemap! {
593            "remote1".into() => RemoteView {
594                bookmarks: btreemap! {
595                    "bookmark1".into() => remote1_bookmark1_remote_ref.clone(),
596                },
597                tags: btreemap! {},
598            },
599        };
600        assert_eq!(
601            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
602                .collect_vec(),
603            vec![(
604                "bookmark1".as_ref(),
605                LocalRemoteRefTarget {
606                    local_target: RefTarget::absent_ref(),
607                    remote_refs: vec![("remote1".as_ref(), &remote1_bookmark1_remote_ref)],
608                },
609            )],
610        );
611    }
612}