jujutsu_lib/
operation.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
15use std::cmp::Ordering;
16use std::collections::HashSet;
17use std::fmt::{Debug, Error, Formatter};
18use std::hash::{Hash, Hasher};
19use std::sync::Arc;
20
21use crate::backend::CommitId;
22use crate::op_store;
23use crate::op_store::{OpStore, OperationId, ViewId};
24
25#[derive(Clone)]
26pub struct Operation {
27    op_store: Arc<dyn OpStore>,
28    id: OperationId,
29    data: op_store::Operation,
30}
31
32impl Debug for Operation {
33    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
34        f.debug_struct("Operation").field("id", &self.id).finish()
35    }
36}
37
38impl PartialEq for Operation {
39    fn eq(&self, other: &Self) -> bool {
40        self.id == other.id
41    }
42}
43
44impl Eq for Operation {}
45
46impl Ord for Operation {
47    fn cmp(&self, other: &Self) -> Ordering {
48        self.id.cmp(&other.id)
49    }
50}
51
52impl PartialOrd for Operation {
53    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
54        Some(self.id.cmp(&other.id))
55    }
56}
57
58impl Hash for Operation {
59    fn hash<H: Hasher>(&self, state: &mut H) {
60        self.id.hash(state)
61    }
62}
63
64impl Operation {
65    pub fn new(op_store: Arc<dyn OpStore>, id: OperationId, data: op_store::Operation) -> Self {
66        Operation { op_store, id, data }
67    }
68
69    pub fn op_store(&self) -> Arc<dyn OpStore> {
70        self.op_store.clone()
71    }
72
73    pub fn id(&self) -> &OperationId {
74        &self.id
75    }
76
77    pub fn parent_ids(&self) -> &Vec<OperationId> {
78        &self.data.parents
79    }
80
81    pub fn parents(&self) -> Vec<Operation> {
82        let mut parents = Vec::new();
83        for parent_id in &self.data.parents {
84            let data = self.op_store.read_operation(parent_id).unwrap();
85            parents.push(Operation::new(
86                self.op_store.clone(),
87                parent_id.clone(),
88                data,
89            ));
90        }
91        parents
92    }
93
94    pub fn view(&self) -> View {
95        let data = self.op_store.read_view(&self.data.view_id).unwrap();
96        View::new(self.op_store.clone(), self.data.view_id.clone(), data)
97    }
98
99    pub fn store_operation(&self) -> &op_store::Operation {
100        &self.data
101    }
102}
103
104#[derive(Clone)]
105pub struct View {
106    op_store: Arc<dyn OpStore>,
107    id: ViewId,
108    data: op_store::View,
109}
110
111impl Debug for View {
112    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
113        f.debug_struct("View").field("id", &self.id).finish()
114    }
115}
116
117impl PartialEq for View {
118    fn eq(&self, other: &Self) -> bool {
119        self.id == other.id
120    }
121}
122
123impl Eq for View {}
124
125impl Ord for View {
126    fn cmp(&self, other: &Self) -> Ordering {
127        self.id.cmp(&other.id)
128    }
129}
130
131impl PartialOrd for View {
132    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
133        Some(self.id.cmp(&other.id))
134    }
135}
136
137impl Hash for View {
138    fn hash<H: Hasher>(&self, state: &mut H) {
139        self.id.hash(state)
140    }
141}
142
143impl View {
144    pub fn new(op_store: Arc<dyn OpStore>, id: ViewId, data: op_store::View) -> Self {
145        View { op_store, id, data }
146    }
147
148    pub fn op_store(&self) -> Arc<dyn OpStore> {
149        self.op_store.clone()
150    }
151
152    pub fn id(&self) -> &ViewId {
153        &self.id
154    }
155
156    pub fn store_view(&self) -> &op_store::View {
157        &self.data
158    }
159
160    pub fn take_store_view(self) -> op_store::View {
161        self.data
162    }
163
164    pub fn heads(&self) -> &HashSet<CommitId> {
165        &self.data.head_ids
166    }
167}