reifydb_core/interface/catalog/
schema.rs1use std::fmt;
5
6use reifydb_type::{Result, value::dictionary::DictionaryId};
7use serde::{Deserialize, Serialize};
8
9use crate::{
10 interface::catalog::{
11 id::{RingBufferId, SeriesId, TableId, ViewId},
12 table::Table,
13 view::View,
14 vtable::{VTable, VTableId},
15 },
16 return_internal_error,
17};
18
19#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize)]
21pub enum SchemaId {
22 Table(TableId),
23 View(ViewId),
24 TableVirtual(VTableId),
25 RingBuffer(RingBufferId),
26 Dictionary(DictionaryId),
27 Series(SeriesId),
28}
29
30impl fmt::Display for SchemaId {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 SchemaId::Table(id) => write!(f, "{}", id.0),
34 SchemaId::View(id) => write!(f, "{}", id.0),
35 SchemaId::TableVirtual(id) => write!(f, "{}", id.0),
36 SchemaId::RingBuffer(id) => write!(f, "{}", id.0),
37 SchemaId::Dictionary(id) => write!(f, "{}", id.0),
38 SchemaId::Series(id) => write!(f, "{}", id.0),
39 }
40 }
41}
42
43impl SchemaId {
44 pub fn table(id: impl Into<TableId>) -> Self {
45 Self::Table(id.into())
46 }
47
48 pub fn view(id: impl Into<ViewId>) -> Self {
49 Self::View(id.into())
50 }
51
52 pub fn vtable(id: impl Into<VTableId>) -> Self {
53 Self::TableVirtual(id.into())
54 }
55
56 pub fn ringbuffer(id: impl Into<RingBufferId>) -> Self {
57 Self::RingBuffer(id.into())
58 }
59
60 pub fn dictionary(id: impl Into<DictionaryId>) -> Self {
61 Self::Dictionary(id.into())
62 }
63
64 pub fn series(id: impl Into<SeriesId>) -> Self {
65 Self::Series(id.into())
66 }
67
68 #[inline]
70 pub fn to_u64(self) -> u64 {
71 match self {
72 SchemaId::Table(id) => id.to_u64(),
73 SchemaId::View(id) => id.to_u64(),
74 SchemaId::TableVirtual(id) => id.to_u64(),
75 SchemaId::RingBuffer(id) => id.to_u64(),
76 SchemaId::Dictionary(id) => id.to_u64(),
77 SchemaId::Series(id) => id.to_u64(),
78 }
79 }
80}
81
82impl From<TableId> for SchemaId {
83 fn from(id: TableId) -> Self {
84 SchemaId::Table(id)
85 }
86}
87
88impl From<ViewId> for SchemaId {
89 fn from(id: ViewId) -> Self {
90 SchemaId::View(id)
91 }
92}
93
94impl From<VTableId> for SchemaId {
95 fn from(id: VTableId) -> Self {
96 SchemaId::TableVirtual(id)
97 }
98}
99
100impl From<RingBufferId> for SchemaId {
101 fn from(id: RingBufferId) -> Self {
102 SchemaId::RingBuffer(id)
103 }
104}
105
106impl From<DictionaryId> for SchemaId {
107 fn from(id: DictionaryId) -> Self {
108 SchemaId::Dictionary(id)
109 }
110}
111
112impl From<SeriesId> for SchemaId {
113 fn from(id: SeriesId) -> Self {
114 SchemaId::Series(id)
115 }
116}
117
118impl PartialEq<u64> for SchemaId {
119 fn eq(&self, other: &u64) -> bool {
120 match self {
121 SchemaId::Table(id) => id.0.eq(other),
122 SchemaId::View(id) => id.0.eq(other),
123 SchemaId::TableVirtual(id) => id.0.eq(other),
124 SchemaId::RingBuffer(id) => id.0.eq(other),
125 SchemaId::Dictionary(id) => id.0.eq(other),
126 SchemaId::Series(id) => id.0.eq(other),
127 }
128 }
129}
130
131impl PartialEq<TableId> for SchemaId {
132 fn eq(&self, other: &TableId) -> bool {
133 match self {
134 SchemaId::Table(id) => id.0 == other.0,
135 _ => false,
136 }
137 }
138}
139
140impl PartialEq<ViewId> for SchemaId {
141 fn eq(&self, other: &ViewId) -> bool {
142 match self {
143 SchemaId::View(id) => id.0 == other.0,
144 _ => false,
145 }
146 }
147}
148
149impl PartialEq<VTableId> for SchemaId {
150 fn eq(&self, other: &VTableId) -> bool {
151 match self {
152 SchemaId::TableVirtual(id) => id.0 == other.0,
153 _ => false,
154 }
155 }
156}
157
158impl PartialEq<RingBufferId> for SchemaId {
159 fn eq(&self, other: &RingBufferId) -> bool {
160 match self {
161 SchemaId::RingBuffer(id) => id.0 == other.0,
162 _ => false,
163 }
164 }
165}
166
167impl PartialEq<DictionaryId> for SchemaId {
168 fn eq(&self, other: &DictionaryId) -> bool {
169 match self {
170 SchemaId::Dictionary(id) => id.0 == other.0,
171 _ => false,
172 }
173 }
174}
175
176impl PartialEq<SeriesId> for SchemaId {
177 fn eq(&self, other: &SeriesId) -> bool {
178 match self {
179 SchemaId::Series(id) => id.0 == other.0,
180 _ => false,
181 }
182 }
183}
184
185impl From<SchemaId> for u64 {
186 fn from(object: SchemaId) -> u64 {
187 object.as_u64()
188 }
189}
190
191impl SchemaId {
192 pub fn to_type_u8(&self) -> u8 {
194 match self {
195 SchemaId::Table(_) => 1,
196 SchemaId::View(_) => 2,
197 SchemaId::TableVirtual(_) => 3,
198 SchemaId::RingBuffer(_) => 4,
199 SchemaId::Dictionary(_) => 5,
200 SchemaId::Series(_) => 6,
201 }
202 }
203
204 pub fn as_u64(&self) -> u64 {
206 match self {
207 SchemaId::Table(id) => id.0,
208 SchemaId::View(id) => id.0,
209 SchemaId::TableVirtual(id) => id.0,
210 SchemaId::RingBuffer(id) => id.0,
211 SchemaId::Dictionary(id) => id.0,
212 SchemaId::Series(id) => id.0,
213 }
214 }
215
216 pub fn next(&self) -> SchemaId {
218 match self {
219 SchemaId::Table(table) => SchemaId::table(table.0 + 1),
220 SchemaId::View(view) => SchemaId::view(view.0 + 1),
221 SchemaId::TableVirtual(vtable) => SchemaId::vtable(vtable.0 + 1),
222 SchemaId::RingBuffer(ringbuffer) => SchemaId::ringbuffer(ringbuffer.0 + 1),
223 SchemaId::Dictionary(dictionary) => SchemaId::dictionary(dictionary.0 + 1),
224 SchemaId::Series(series) => SchemaId::series(series.0 + 1),
225 }
226 }
227
228 pub fn prev(&self) -> SchemaId {
233 match self {
234 SchemaId::Table(table) => SchemaId::table(table.0.wrapping_sub(1)),
235 SchemaId::View(view) => SchemaId::view(view.0.wrapping_sub(1)),
236 SchemaId::TableVirtual(vtable) => SchemaId::vtable(vtable.0.wrapping_sub(1)),
237 SchemaId::RingBuffer(ringbuffer) => SchemaId::ringbuffer(ringbuffer.0.wrapping_sub(1)),
238 SchemaId::Dictionary(dictionary) => SchemaId::dictionary(dictionary.0.wrapping_sub(1)),
239 SchemaId::Series(series) => SchemaId::series(series.0.wrapping_sub(1)),
240 }
241 }
242
243 pub fn to_table_id(self) -> Result<TableId> {
244 if let SchemaId::Table(table) = self {
245 Ok(table)
246 } else {
247 return_internal_error!(
248 "Data inconsistency: Expected SchemaId::Table but found {:?}. \
249 This indicates a critical catalog inconsistency where a non-table object ID \
250 was used in a context that requires a table ID.",
251 self
252 )
253 }
254 }
255
256 pub fn to_view_id(self) -> Result<ViewId> {
257 if let SchemaId::View(view) = self {
258 Ok(view)
259 } else {
260 return_internal_error!(
261 "Data inconsistency: Expected SchemaId::View but found {:?}. \
262 This indicates a critical catalog inconsistency where a non-view object ID \
263 was used in a context that requires a view ID.",
264 self
265 )
266 }
267 }
268
269 pub fn to_vtable_id(self) -> Result<VTableId> {
270 if let SchemaId::TableVirtual(vtable) = self {
271 Ok(vtable)
272 } else {
273 return_internal_error!(
274 "Data inconsistency: Expected SchemaId::TableVirtual but found {:?}. \
275 This indicates a critical catalog inconsistency where a non-virtual-table object ID \
276 was used in a context that requires a virtual table ID.",
277 self
278 )
279 }
280 }
281
282 pub fn to_ringbuffer_id(self) -> Result<RingBufferId> {
283 if let SchemaId::RingBuffer(ringbuffer) = self {
284 Ok(ringbuffer)
285 } else {
286 return_internal_error!(
287 "Data inconsistency: Expected SchemaId::RingBuffer but found {:?}. \
288 This indicates a critical catalog inconsistency where a non-ring-buffer object ID \
289 was used in a context that requires a ring buffer ID.",
290 self
291 )
292 }
293 }
294
295 pub fn to_dictionary_id(self) -> Result<DictionaryId> {
296 if let SchemaId::Dictionary(dictionary) = self {
297 Ok(dictionary)
298 } else {
299 return_internal_error!(
300 "Data inconsistency: Expected SchemaId::Dictionary but found {:?}. \
301 This indicates a critical catalog inconsistency where a non-dictionary object ID \
302 was used in a context that requires a dictionary ID.",
303 self
304 )
305 }
306 }
307
308 pub fn to_series_id(self) -> Result<SeriesId> {
309 if let SchemaId::Series(series) = self {
310 Ok(series)
311 } else {
312 return_internal_error!(
313 "Data inconsistency: Expected SchemaId::Series but found {:?}. \
314 This indicates a critical catalog inconsistency where a non-series object ID \
315 was used in a context that requires a series ID.",
316 self
317 )
318 }
319 }
320}
321
322#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
323pub enum Schema {
324 Table(Table),
325 View(View),
326 TableVirtual(VTable),
327}
328
329impl Schema {
330 pub fn id(&self) -> SchemaId {
331 match self {
332 Schema::Table(table) => table.id.into(),
333 Schema::View(view) => view.id().into(),
334 Schema::TableVirtual(vtable) => vtable.id.into(),
335 }
336 }
337
338 pub fn schema_type(&self) -> SchemaId {
339 match self {
340 Schema::Table(table) => SchemaId::Table(table.id),
341 Schema::View(view) => SchemaId::View(view.id()),
342 Schema::TableVirtual(vtable) => SchemaId::TableVirtual(vtable.id),
343 }
344 }
345}