reifydb_core/interface/
change.rs1use std::sync::Arc;
5
6use reifydb_abi::flow::diff::DiffType;
7use reifydb_type::value::datetime::DateTime;
8use serde::{Deserialize, Serialize};
9use smallvec::SmallVec;
10
11use crate::{
12 common::CommitVersion,
13 interface::catalog::{flow::FlowNodeId, shape::ShapeId},
14 value::column::columns::Columns,
15};
16
17pub type Diffs = SmallVec<[Diff; 4]>;
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub enum ChangeOrigin {
25 Shape(ShapeId),
26 Flow(FlowNodeId),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
37pub enum Diff {
38 Insert {
39 post: Arc<Columns>,
40 },
41 Update {
42 pre: Arc<Columns>,
43 post: Arc<Columns>,
44 },
45 Remove {
46 pre: Arc<Columns>,
47 },
48}
49
50impl Diff {
51 pub fn insert(post: Columns) -> Self {
53 Self::Insert {
54 post: Arc::new(post),
55 }
56 }
57
58 pub fn update(pre: Columns, post: Columns) -> Self {
60 Self::Update {
61 pre: Arc::new(pre),
62 post: Arc::new(post),
63 }
64 }
65
66 pub fn remove(pre: Columns) -> Self {
68 Self::Remove {
69 pre: Arc::new(pre),
70 }
71 }
72
73 pub fn insert_arc(post: Arc<Columns>) -> Self {
76 Self::Insert {
77 post,
78 }
79 }
80
81 pub fn update_arc(pre: Arc<Columns>, post: Arc<Columns>) -> Self {
83 Self::Update {
84 pre,
85 post,
86 }
87 }
88
89 pub fn remove_arc(pre: Arc<Columns>) -> Self {
91 Self::Remove {
92 pre,
93 }
94 }
95
96 pub fn pre(&self) -> Option<&Columns> {
98 match self {
99 Diff::Insert {
100 ..
101 } => None,
102 Diff::Update {
103 pre,
104 ..
105 } => Some(pre),
106 Diff::Remove {
107 pre,
108 } => Some(pre),
109 }
110 }
111
112 pub fn post(&self) -> Option<&Columns> {
114 match self {
115 Diff::Insert {
116 post,
117 } => Some(post),
118 Diff::Update {
119 post,
120 ..
121 } => Some(post),
122 Diff::Remove {
123 ..
124 } => None,
125 }
126 }
127
128 pub fn kind(&self) -> DiffType {
130 match self {
131 Diff::Insert {
132 ..
133 } => DiffType::Insert,
134 Diff::Update {
135 ..
136 } => DiffType::Update,
137 Diff::Remove {
138 ..
139 } => DiffType::Remove,
140 }
141 }
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct Change {
147 pub origin: ChangeOrigin,
149 pub diffs: Diffs,
151 pub version: CommitVersion,
153 pub changed_at: DateTime,
155}
156
157impl Change {
158 pub fn from_shape(
160 shape: ShapeId,
161 version: CommitVersion,
162 diffs: impl Into<Diffs>,
163 changed_at: DateTime,
164 ) -> Self {
165 Self {
166 origin: ChangeOrigin::Shape(shape),
167 diffs: diffs.into(),
168 version,
169 changed_at,
170 }
171 }
172
173 pub fn from_flow(
175 from: FlowNodeId,
176 version: CommitVersion,
177 diffs: impl Into<Diffs>,
178 changed_at: DateTime,
179 ) -> Self {
180 Self {
181 origin: ChangeOrigin::Flow(from),
182 diffs: diffs.into(),
183 version,
184 changed_at,
185 }
186 }
187}