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]>;
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub enum ChangeOrigin {
21 Shape(ShapeId),
22 Flow(FlowNodeId),
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub enum Diff {
27 Insert {
28 post: Arc<Columns>,
29 },
30 Update {
31 pre: Arc<Columns>,
32 post: Arc<Columns>,
33 },
34 Remove {
35 pre: Arc<Columns>,
36 },
37}
38
39impl Diff {
40 pub fn insert(post: Columns) -> Self {
41 Self::Insert {
42 post: Arc::new(post),
43 }
44 }
45
46 pub fn update(pre: Columns, post: Columns) -> Self {
47 Self::Update {
48 pre: Arc::new(pre),
49 post: Arc::new(post),
50 }
51 }
52
53 pub fn remove(pre: Columns) -> Self {
54 Self::Remove {
55 pre: Arc::new(pre),
56 }
57 }
58
59 pub fn insert_arc(post: Arc<Columns>) -> Self {
60 Self::Insert {
61 post,
62 }
63 }
64
65 pub fn update_arc(pre: Arc<Columns>, post: Arc<Columns>) -> Self {
66 Self::Update {
67 pre,
68 post,
69 }
70 }
71
72 pub fn remove_arc(pre: Arc<Columns>) -> Self {
73 Self::Remove {
74 pre,
75 }
76 }
77
78 pub fn pre(&self) -> Option<&Columns> {
79 match self {
80 Diff::Insert {
81 ..
82 } => None,
83 Diff::Update {
84 pre,
85 ..
86 } => Some(pre),
87 Diff::Remove {
88 pre,
89 } => Some(pre),
90 }
91 }
92
93 pub fn post(&self) -> Option<&Columns> {
94 match self {
95 Diff::Insert {
96 post,
97 } => Some(post),
98 Diff::Update {
99 post,
100 ..
101 } => Some(post),
102 Diff::Remove {
103 ..
104 } => None,
105 }
106 }
107
108 pub fn kind(&self) -> DiffType {
109 match self {
110 Diff::Insert {
111 ..
112 } => DiffType::Insert,
113 Diff::Update {
114 ..
115 } => DiffType::Update,
116 Diff::Remove {
117 ..
118 } => DiffType::Remove,
119 }
120 }
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct Change {
125 pub origin: ChangeOrigin,
126
127 pub diffs: Diffs,
128
129 pub version: CommitVersion,
130
131 pub changed_at: DateTime,
132}
133
134impl Change {
135 pub fn from_shape(
136 shape: ShapeId,
137 version: CommitVersion,
138 diffs: impl Into<Diffs>,
139 changed_at: DateTime,
140 ) -> Self {
141 Self {
142 origin: ChangeOrigin::Shape(shape),
143 diffs: diffs.into(),
144 version,
145 changed_at,
146 }
147 }
148
149 pub fn from_flow(
150 from: FlowNodeId,
151 version: CommitVersion,
152 diffs: impl Into<Diffs>,
153 changed_at: DateTime,
154 ) -> Self {
155 Self {
156 origin: ChangeOrigin::Flow(from),
157 diffs: diffs.into(),
158 version,
159 changed_at,
160 }
161 }
162}