reifydb_core/interface/catalog/
id.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::{
5	fmt,
6	fmt::{Display, Formatter},
7	ops::Deref,
8};
9
10use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
11
12#[repr(transparent)]
13#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
14pub struct ColumnId(pub u64);
15
16impl Deref for ColumnId {
17	type Target = u64;
18
19	fn deref(&self) -> &Self::Target {
20		&self.0
21	}
22}
23
24impl PartialEq<u64> for ColumnId {
25	fn eq(&self, other: &u64) -> bool {
26		self.0.eq(other)
27	}
28}
29
30impl From<ColumnId> for u64 {
31	fn from(value: ColumnId) -> Self {
32		value.0
33	}
34}
35
36impl Serialize for ColumnId {
37	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38	where
39		S: Serializer,
40	{
41		serializer.serialize_u64(self.0)
42	}
43}
44
45impl<'de> Deserialize<'de> for ColumnId {
46	fn deserialize<D>(deserializer: D) -> Result<ColumnId, D::Error>
47	where
48		D: Deserializer<'de>,
49	{
50		struct U64Visitor;
51
52		impl Visitor<'_> for U64Visitor {
53			type Value = ColumnId;
54
55			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
56				formatter.write_str("an unsigned 64-bit number")
57			}
58
59			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
60				Ok(ColumnId(value))
61			}
62		}
63
64		deserializer.deserialize_u64(U64Visitor)
65	}
66}
67
68#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
69pub enum IndexId {
70	Primary(PrimaryKeyId),
71	// Future: Secondary, Unique, etc.
72}
73
74impl IndexId {
75	pub fn as_u64(&self) -> u64 {
76		match self {
77			IndexId::Primary(id) => id.0,
78		}
79	}
80
81	pub fn primary(id: impl Into<PrimaryKeyId>) -> Self {
82		IndexId::Primary(id.into())
83	}
84
85	/// Creates a next index id for range operations (numerically next)
86	pub fn next(&self) -> IndexId {
87		match self {
88			IndexId::Primary(primary) => IndexId::Primary(PrimaryKeyId(primary.0 + 1)), /* Future: handle
89			                                                                             * other index
90			                                                                             * types */
91		}
92	}
93
94	pub fn prev(&self) -> IndexId {
95		match self {
96			IndexId::Primary(primary) => IndexId::Primary(PrimaryKeyId(primary.0.wrapping_sub(1))),
97		}
98	}
99}
100
101impl Deref for IndexId {
102	type Target = u64;
103
104	fn deref(&self) -> &Self::Target {
105		match self {
106			IndexId::Primary(id) => &id.0,
107		}
108	}
109}
110
111impl PartialEq<u64> for IndexId {
112	fn eq(&self, other: &u64) -> bool {
113		self.as_u64().eq(other)
114	}
115}
116
117impl From<IndexId> for u64 {
118	fn from(value: IndexId) -> Self {
119		value.as_u64()
120	}
121}
122
123impl Serialize for IndexId {
124	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
125	where
126		S: Serializer,
127	{
128		serializer.serialize_u64(self.as_u64())
129	}
130}
131
132impl<'de> Deserialize<'de> for IndexId {
133	fn deserialize<D>(deserializer: D) -> Result<IndexId, D::Error>
134	where
135		D: Deserializer<'de>,
136	{
137		struct U64Visitor;
138
139		impl Visitor<'_> for U64Visitor {
140			type Value = IndexId;
141
142			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
143				formatter.write_str("an unsigned 64-bit number")
144			}
145
146			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
147				// Deserialize as primary key ID for now
148				Ok(IndexId::Primary(PrimaryKeyId(value)))
149			}
150		}
151
152		deserializer.deserialize_u64(U64Visitor)
153	}
154}
155
156#[repr(transparent)]
157#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
158pub struct ColumnPolicyId(pub u64);
159
160impl Deref for ColumnPolicyId {
161	type Target = u64;
162
163	fn deref(&self) -> &Self::Target {
164		&self.0
165	}
166}
167
168impl PartialEq<u64> for ColumnPolicyId {
169	fn eq(&self, other: &u64) -> bool {
170		self.0.eq(other)
171	}
172}
173
174impl From<ColumnPolicyId> for u64 {
175	fn from(value: ColumnPolicyId) -> Self {
176		value.0
177	}
178}
179
180impl Serialize for ColumnPolicyId {
181	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
182	where
183		S: Serializer,
184	{
185		serializer.serialize_u64(self.0)
186	}
187}
188
189impl<'de> Deserialize<'de> for ColumnPolicyId {
190	fn deserialize<D>(deserializer: D) -> Result<ColumnPolicyId, D::Error>
191	where
192		D: Deserializer<'de>,
193	{
194		struct U64Visitor;
195
196		impl Visitor<'_> for U64Visitor {
197			type Value = ColumnPolicyId;
198
199			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
200				formatter.write_str("an unsigned 64-bit number")
201			}
202
203			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
204				Ok(ColumnPolicyId(value))
205			}
206		}
207
208		deserializer.deserialize_u64(U64Visitor)
209	}
210}
211
212#[repr(transparent)]
213#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
214pub struct NamespaceId(pub u64);
215
216impl Display for NamespaceId {
217	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
218		Display::fmt(&self.0, f)
219	}
220}
221
222impl Deref for NamespaceId {
223	type Target = u64;
224
225	fn deref(&self) -> &Self::Target {
226		&self.0
227	}
228}
229
230impl PartialEq<u64> for NamespaceId {
231	fn eq(&self, other: &u64) -> bool {
232		self.0.eq(other)
233	}
234}
235
236impl From<NamespaceId> for u64 {
237	fn from(value: NamespaceId) -> Self {
238		value.0
239	}
240}
241
242impl Serialize for NamespaceId {
243	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
244	where
245		S: Serializer,
246	{
247		serializer.serialize_u64(self.0)
248	}
249}
250
251impl<'de> Deserialize<'de> for NamespaceId {
252	fn deserialize<D>(deserializer: D) -> Result<NamespaceId, D::Error>
253	where
254		D: Deserializer<'de>,
255	{
256		struct U64Visitor;
257
258		impl Visitor<'_> for U64Visitor {
259			type Value = NamespaceId;
260
261			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
262				formatter.write_str("an unsigned 64-bit number")
263			}
264
265			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
266				Ok(NamespaceId(value))
267			}
268		}
269
270		deserializer.deserialize_u64(U64Visitor)
271	}
272}
273
274#[repr(transparent)]
275#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
276pub struct TableId(pub u64);
277
278impl Display for TableId {
279	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
280		Display::fmt(&self.0, f)
281	}
282}
283
284impl Deref for TableId {
285	type Target = u64;
286
287	fn deref(&self) -> &Self::Target {
288		&self.0
289	}
290}
291
292impl PartialEq<u64> for TableId {
293	fn eq(&self, other: &u64) -> bool {
294		self.0.eq(other)
295	}
296}
297
298impl From<TableId> for u64 {
299	fn from(value: TableId) -> Self {
300		value.0
301	}
302}
303
304impl From<i32> for TableId {
305	fn from(value: i32) -> Self {
306		Self(value as u64)
307	}
308}
309
310impl From<u64> for TableId {
311	fn from(value: u64) -> Self {
312		Self(value)
313	}
314}
315
316impl Serialize for TableId {
317	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
318	where
319		S: Serializer,
320	{
321		serializer.serialize_u64(self.0)
322	}
323}
324
325impl<'de> Deserialize<'de> for TableId {
326	fn deserialize<D>(deserializer: D) -> Result<TableId, D::Error>
327	where
328		D: Deserializer<'de>,
329	{
330		struct U64Visitor;
331
332		impl Visitor<'_> for U64Visitor {
333			type Value = TableId;
334
335			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
336				formatter.write_str("an unsigned 64-bit number")
337			}
338
339			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
340				Ok(TableId(value))
341			}
342		}
343
344		deserializer.deserialize_u64(U64Visitor)
345	}
346}
347
348#[repr(transparent)]
349#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
350pub struct ViewId(pub u64);
351
352impl Display for ViewId {
353	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
354		Display::fmt(&self.0, f)
355	}
356}
357
358impl Deref for ViewId {
359	type Target = u64;
360
361	fn deref(&self) -> &Self::Target {
362		&self.0
363	}
364}
365
366impl PartialEq<u64> for ViewId {
367	fn eq(&self, other: &u64) -> bool {
368		self.0.eq(other)
369	}
370}
371
372impl From<ViewId> for u64 {
373	fn from(value: ViewId) -> Self {
374		value.0
375	}
376}
377
378impl From<i32> for ViewId {
379	fn from(value: i32) -> Self {
380		Self(value as u64)
381	}
382}
383
384impl From<u64> for ViewId {
385	fn from(value: u64) -> Self {
386		Self(value)
387	}
388}
389
390impl Serialize for ViewId {
391	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
392	where
393		S: Serializer,
394	{
395		serializer.serialize_u64(self.0)
396	}
397}
398
399impl<'de> Deserialize<'de> for ViewId {
400	fn deserialize<D>(deserializer: D) -> Result<ViewId, D::Error>
401	where
402		D: Deserializer<'de>,
403	{
404		struct U64Visitor;
405
406		impl Visitor<'_> for U64Visitor {
407			type Value = ViewId;
408
409			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
410				formatter.write_str("an unsigned 64-bit number")
411			}
412
413			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
414				Ok(ViewId(value))
415			}
416		}
417
418		deserializer.deserialize_u64(U64Visitor)
419	}
420}
421
422#[repr(transparent)]
423#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
424pub struct PrimaryKeyId(pub u64);
425
426impl Display for PrimaryKeyId {
427	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
428		Display::fmt(&self.0, f)
429	}
430}
431
432impl Deref for PrimaryKeyId {
433	type Target = u64;
434
435	fn deref(&self) -> &Self::Target {
436		&self.0
437	}
438}
439
440impl PartialEq<u64> for PrimaryKeyId {
441	fn eq(&self, other: &u64) -> bool {
442		self.0.eq(other)
443	}
444}
445
446impl From<PrimaryKeyId> for u64 {
447	fn from(value: PrimaryKeyId) -> Self {
448		value.0
449	}
450}
451
452impl From<i32> for PrimaryKeyId {
453	fn from(value: i32) -> Self {
454		Self(value as u64)
455	}
456}
457
458impl From<u64> for PrimaryKeyId {
459	fn from(value: u64) -> Self {
460		Self(value)
461	}
462}
463
464impl Serialize for PrimaryKeyId {
465	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
466	where
467		S: Serializer,
468	{
469		serializer.serialize_u64(self.0)
470	}
471}
472
473impl<'de> Deserialize<'de> for PrimaryKeyId {
474	fn deserialize<D>(deserializer: D) -> Result<PrimaryKeyId, D::Error>
475	where
476		D: Deserializer<'de>,
477	{
478		struct U64Visitor;
479
480		impl Visitor<'_> for U64Visitor {
481			type Value = PrimaryKeyId;
482
483			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
484				formatter.write_str("an unsigned 64-bit number")
485			}
486
487			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
488				Ok(PrimaryKeyId(value))
489			}
490		}
491
492		deserializer.deserialize_u64(U64Visitor)
493	}
494}
495
496#[repr(transparent)]
497#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
498pub struct RingBufferId(pub u64);
499
500impl Display for RingBufferId {
501	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
502		Display::fmt(&self.0, f)
503	}
504}
505
506impl Deref for RingBufferId {
507	type Target = u64;
508
509	fn deref(&self) -> &Self::Target {
510		&self.0
511	}
512}
513
514impl PartialEq<u64> for RingBufferId {
515	fn eq(&self, other: &u64) -> bool {
516		self.0.eq(other)
517	}
518}
519
520impl From<RingBufferId> for u64 {
521	fn from(value: RingBufferId) -> Self {
522		value.0
523	}
524}
525
526impl From<i32> for RingBufferId {
527	fn from(value: i32) -> Self {
528		Self(value as u64)
529	}
530}
531
532impl From<u64> for RingBufferId {
533	fn from(value: u64) -> Self {
534		Self(value)
535	}
536}
537
538impl Serialize for RingBufferId {
539	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
540	where
541		S: Serializer,
542	{
543		serializer.serialize_u64(self.0)
544	}
545}
546
547impl<'de> Deserialize<'de> for RingBufferId {
548	fn deserialize<D>(deserializer: D) -> Result<RingBufferId, D::Error>
549	where
550		D: Deserializer<'de>,
551	{
552		struct U64Visitor;
553
554		impl Visitor<'_> for U64Visitor {
555			type Value = RingBufferId;
556
557			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
558				formatter.write_str("an unsigned 64-bit number")
559			}
560
561			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
562				Ok(RingBufferId(value))
563			}
564		}
565
566		deserializer.deserialize_u64(U64Visitor)
567	}
568}
569
570#[repr(transparent)]
571#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
572pub struct SequenceId(pub u64);
573
574impl Deref for SequenceId {
575	type Target = u64;
576
577	fn deref(&self) -> &Self::Target {
578		&self.0
579	}
580}
581
582impl PartialEq<u64> for SequenceId {
583	fn eq(&self, other: &u64) -> bool {
584		self.0.eq(other)
585	}
586}
587
588impl Serialize for SequenceId {
589	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
590	where
591		S: Serializer,
592	{
593		serializer.serialize_u64(self.0)
594	}
595}
596
597impl<'de> Deserialize<'de> for SequenceId {
598	fn deserialize<D>(deserializer: D) -> Result<SequenceId, D::Error>
599	where
600		D: Deserializer<'de>,
601	{
602		struct U64Visitor;
603
604		impl Visitor<'_> for U64Visitor {
605			type Value = SequenceId;
606
607			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
608				formatter.write_str("an unsigned 64-bit number")
609			}
610
611			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
612				Ok(SequenceId(value))
613			}
614		}
615
616		deserializer.deserialize_u64(U64Visitor)
617	}
618}