1use std::{
5 fmt,
6 fmt::{Display, Formatter},
7 num::ParseIntError,
8 str::FromStr,
9};
10
11use reifydb_value::value::duration::Duration;
12use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
13
14#[repr(transparent)]
15#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
16pub struct CommitVersion(pub u64);
17
18impl FromStr for CommitVersion {
19 type Err = ParseIntError;
20
21 fn from_str(s: &str) -> Result<Self, Self::Err> {
22 Ok(CommitVersion(u64::from_str(s)?))
23 }
24}
25
26impl Display for CommitVersion {
27 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28 Display::fmt(&self.0, f)
29 }
30}
31
32impl PartialEq<i32> for CommitVersion {
33 fn eq(&self, other: &i32) -> bool {
34 self.0 == *other as u64
35 }
36}
37
38impl PartialEq<CommitVersion> for i32 {
39 fn eq(&self, other: &CommitVersion) -> bool {
40 *self as u64 == other.0
41 }
42}
43
44impl PartialEq<u64> for CommitVersion {
45 fn eq(&self, other: &u64) -> bool {
46 self.0.eq(other)
47 }
48}
49
50impl From<CommitVersion> for u64 {
51 fn from(value: CommitVersion) -> Self {
52 value.0
53 }
54}
55
56impl From<i32> for CommitVersion {
57 fn from(value: i32) -> Self {
58 Self(value as u64)
59 }
60}
61
62impl From<u64> for CommitVersion {
63 fn from(value: u64) -> Self {
64 Self(value)
65 }
66}
67
68impl Serialize for CommitVersion {
69 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
70 where
71 S: Serializer,
72 {
73 serializer.serialize_u64(self.0)
74 }
75}
76
77impl<'de> Deserialize<'de> for CommitVersion {
78 fn deserialize<D>(deserializer: D) -> Result<CommitVersion, D::Error>
79 where
80 D: Deserializer<'de>,
81 {
82 struct U64Visitor;
83
84 impl Visitor<'_> for U64Visitor {
85 type Value = CommitVersion;
86
87 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
88 formatter.write_str("an unsigned 64-bit number")
89 }
90
91 fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
92 Ok(CommitVersion(value))
93 }
94 }
95
96 deserializer.deserialize_u64(U64Visitor)
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
101pub enum JoinType {
102 Inner,
103 #[default]
104 Left,
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
108pub enum IndexType {
109 #[default]
110 Index,
111 Unique,
112 Primary,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub enum WindowSize {
117 Duration(Duration),
118 Count(u64),
119}
120
121impl WindowSize {
122 pub fn is_count(&self) -> bool {
123 matches!(self, WindowSize::Count(_))
124 }
125
126 pub fn as_duration(&self) -> Option<Duration> {
127 match self {
128 WindowSize::Duration(d) => Some(*d),
129 _ => None,
130 }
131 }
132
133 pub fn as_count(&self) -> Option<u64> {
134 match self {
135 WindowSize::Count(c) => Some(*c),
136 _ => None,
137 }
138 }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
142pub enum TimeDomain {
143 Event,
144 Processing,
145}
146
147#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
148pub enum WindowKind {
149 Tumbling {
150 size: WindowSize,
151 time: TimeDomain,
152 },
153
154 Sliding {
155 size: WindowSize,
156 slide: WindowSize,
157 time: TimeDomain,
158 },
159
160 Rolling {
161 size: WindowSize,
162 #[serde(default)]
163 lag: Option<Duration>,
164 time: TimeDomain,
165 },
166
167 Session {
168 gap: Duration,
169 time: TimeDomain,
170 },
171}
172
173impl WindowKind {
174 pub fn size(&self) -> Option<&WindowSize> {
175 match self {
176 WindowKind::Tumbling {
177 size,
178 ..
179 } => Some(size),
180 WindowKind::Sliding {
181 size,
182 ..
183 } => Some(size),
184 WindowKind::Rolling {
185 size,
186 ..
187 } => Some(size),
188 WindowKind::Session {
189 ..
190 } => None,
191 }
192 }
193
194 pub fn time(&self) -> TimeDomain {
195 match self {
196 WindowKind::Tumbling {
197 time,
198 ..
199 }
200 | WindowKind::Sliding {
201 time,
202 ..
203 }
204 | WindowKind::Rolling {
205 time,
206 ..
207 }
208 | WindowKind::Session {
209 time,
210 ..
211 } => *time,
212 }
213 }
214}