1#![allow(missing_docs)]
16
17use std::cmp::Ordering;
18use std::fmt::Debug;
19use std::fmt::Error;
20use std::fmt::Formatter;
21use std::hash::Hash;
22use std::hash::Hasher;
23use std::sync::Arc;
24
25use crate::backend::CommitId;
26use crate::op_store;
27use crate::op_store::OpStore;
28use crate::op_store::OpStoreResult;
29use crate::op_store::OperationId;
30use crate::op_store::OperationMetadata;
31use crate::op_store::ViewId;
32use crate::view::View;
33
34#[derive(Clone)]
37pub struct Operation {
38 op_store: Arc<dyn OpStore>,
39 id: OperationId,
40 data: Arc<op_store::Operation>, }
42
43impl Debug for Operation {
44 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
45 f.debug_struct("Operation").field("id", &self.id).finish()
46 }
47}
48
49impl PartialEq for Operation {
50 fn eq(&self, other: &Self) -> bool {
51 self.id == other.id
52 }
53}
54
55impl Eq for Operation {}
56
57impl Ord for Operation {
58 fn cmp(&self, other: &Self) -> Ordering {
59 self.id.cmp(&other.id)
60 }
61}
62
63impl PartialOrd for Operation {
64 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
65 Some(self.cmp(other))
66 }
67}
68
69impl Hash for Operation {
70 fn hash<H: Hasher>(&self, state: &mut H) {
71 self.id.hash(state);
72 }
73}
74
75impl Operation {
76 pub fn new(
77 op_store: Arc<dyn OpStore>,
78 id: OperationId,
79 data: impl Into<Arc<op_store::Operation>>,
80 ) -> Self {
81 Operation {
82 op_store,
83 id,
84 data: data.into(),
85 }
86 }
87
88 pub fn op_store(&self) -> Arc<dyn OpStore> {
89 self.op_store.clone()
90 }
91
92 pub fn id(&self) -> &OperationId {
93 &self.id
94 }
95
96 pub fn view_id(&self) -> &ViewId {
97 &self.data.view_id
98 }
99
100 pub fn parent_ids(&self) -> &[OperationId] {
101 &self.data.parents
102 }
103
104 pub fn parents(&self) -> impl ExactSizeIterator<Item = OpStoreResult<Operation>> + use<'_> {
105 let op_store = &self.op_store;
106 self.data.parents.iter().map(|parent_id| {
107 let data = op_store.read_operation(parent_id)?;
108 Ok(Operation::new(op_store.clone(), parent_id.clone(), data))
109 })
110 }
111
112 pub fn view(&self) -> OpStoreResult<View> {
113 let data = self.op_store.read_view(&self.data.view_id)?;
114 Ok(View::new(data))
115 }
116
117 pub fn metadata(&self) -> &OperationMetadata {
118 &self.data.metadata
119 }
120
121 pub fn stores_commit_predecessors(&self) -> bool {
125 self.data.commit_predecessors.is_some()
126 }
127
128 pub fn predecessors_for_commit(&self, commit_id: &CommitId) -> Option<&[CommitId]> {
130 let map = self.data.commit_predecessors.as_ref()?;
131 Some(map.get(commit_id)?)
132 }
133
134 pub fn store_operation(&self) -> &op_store::Operation {
135 &self.data
136 }
137}