Skip to main content

reifydb_core/interface/catalog/
id.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
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 TableId {
305	/// Get the inner u64 value.
306	#[inline]
307	pub fn to_u64(self) -> u64 {
308		self.0
309	}
310}
311
312impl From<i32> for TableId {
313	fn from(value: i32) -> Self {
314		Self(value as u64)
315	}
316}
317
318impl From<u64> for TableId {
319	fn from(value: u64) -> Self {
320		Self(value)
321	}
322}
323
324impl Serialize for TableId {
325	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
326	where
327		S: Serializer,
328	{
329		serializer.serialize_u64(self.0)
330	}
331}
332
333impl<'de> Deserialize<'de> for TableId {
334	fn deserialize<D>(deserializer: D) -> Result<TableId, D::Error>
335	where
336		D: Deserializer<'de>,
337	{
338		struct U64Visitor;
339
340		impl Visitor<'_> for U64Visitor {
341			type Value = TableId;
342
343			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
344				formatter.write_str("an unsigned 64-bit number")
345			}
346
347			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
348				Ok(TableId(value))
349			}
350		}
351
352		deserializer.deserialize_u64(U64Visitor)
353	}
354}
355
356#[repr(transparent)]
357#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
358pub struct ViewId(pub u64);
359
360impl Display for ViewId {
361	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
362		Display::fmt(&self.0, f)
363	}
364}
365
366impl Deref for ViewId {
367	type Target = u64;
368
369	fn deref(&self) -> &Self::Target {
370		&self.0
371	}
372}
373
374impl PartialEq<u64> for ViewId {
375	fn eq(&self, other: &u64) -> bool {
376		self.0.eq(other)
377	}
378}
379
380impl From<ViewId> for u64 {
381	fn from(value: ViewId) -> Self {
382		value.0
383	}
384}
385
386impl ViewId {
387	/// Get the inner u64 value.
388	#[inline]
389	pub fn to_u64(self) -> u64 {
390		self.0
391	}
392}
393
394impl From<i32> for ViewId {
395	fn from(value: i32) -> Self {
396		Self(value as u64)
397	}
398}
399
400impl From<u64> for ViewId {
401	fn from(value: u64) -> Self {
402		Self(value)
403	}
404}
405
406impl Serialize for ViewId {
407	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
408	where
409		S: Serializer,
410	{
411		serializer.serialize_u64(self.0)
412	}
413}
414
415impl<'de> Deserialize<'de> for ViewId {
416	fn deserialize<D>(deserializer: D) -> Result<ViewId, D::Error>
417	where
418		D: Deserializer<'de>,
419	{
420		struct U64Visitor;
421
422		impl Visitor<'_> for U64Visitor {
423			type Value = ViewId;
424
425			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
426				formatter.write_str("an unsigned 64-bit number")
427			}
428
429			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
430				Ok(ViewId(value))
431			}
432		}
433
434		deserializer.deserialize_u64(U64Visitor)
435	}
436}
437
438#[repr(transparent)]
439#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
440pub struct PrimaryKeyId(pub u64);
441
442impl Display for PrimaryKeyId {
443	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
444		Display::fmt(&self.0, f)
445	}
446}
447
448impl Deref for PrimaryKeyId {
449	type Target = u64;
450
451	fn deref(&self) -> &Self::Target {
452		&self.0
453	}
454}
455
456impl PartialEq<u64> for PrimaryKeyId {
457	fn eq(&self, other: &u64) -> bool {
458		self.0.eq(other)
459	}
460}
461
462impl From<PrimaryKeyId> for u64 {
463	fn from(value: PrimaryKeyId) -> Self {
464		value.0
465	}
466}
467
468impl From<i32> for PrimaryKeyId {
469	fn from(value: i32) -> Self {
470		Self(value as u64)
471	}
472}
473
474impl From<u64> for PrimaryKeyId {
475	fn from(value: u64) -> Self {
476		Self(value)
477	}
478}
479
480impl Serialize for PrimaryKeyId {
481	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
482	where
483		S: Serializer,
484	{
485		serializer.serialize_u64(self.0)
486	}
487}
488
489impl<'de> Deserialize<'de> for PrimaryKeyId {
490	fn deserialize<D>(deserializer: D) -> Result<PrimaryKeyId, D::Error>
491	where
492		D: Deserializer<'de>,
493	{
494		struct U64Visitor;
495
496		impl Visitor<'_> for U64Visitor {
497			type Value = PrimaryKeyId;
498
499			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
500				formatter.write_str("an unsigned 64-bit number")
501			}
502
503			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
504				Ok(PrimaryKeyId(value))
505			}
506		}
507
508		deserializer.deserialize_u64(U64Visitor)
509	}
510}
511
512#[repr(transparent)]
513#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
514pub struct RingBufferId(pub u64);
515
516impl Display for RingBufferId {
517	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
518		Display::fmt(&self.0, f)
519	}
520}
521
522impl Deref for RingBufferId {
523	type Target = u64;
524
525	fn deref(&self) -> &Self::Target {
526		&self.0
527	}
528}
529
530impl PartialEq<u64> for RingBufferId {
531	fn eq(&self, other: &u64) -> bool {
532		self.0.eq(other)
533	}
534}
535
536impl From<RingBufferId> for u64 {
537	fn from(value: RingBufferId) -> Self {
538		value.0
539	}
540}
541
542impl RingBufferId {
543	/// Get the inner u64 value.
544	#[inline]
545	pub fn to_u64(self) -> u64 {
546		self.0
547	}
548}
549
550impl From<i32> for RingBufferId {
551	fn from(value: i32) -> Self {
552		Self(value as u64)
553	}
554}
555
556impl From<u64> for RingBufferId {
557	fn from(value: u64) -> Self {
558		Self(value)
559	}
560}
561
562impl Serialize for RingBufferId {
563	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
564	where
565		S: Serializer,
566	{
567		serializer.serialize_u64(self.0)
568	}
569}
570
571impl<'de> Deserialize<'de> for RingBufferId {
572	fn deserialize<D>(deserializer: D) -> Result<RingBufferId, D::Error>
573	where
574		D: Deserializer<'de>,
575	{
576		struct U64Visitor;
577
578		impl Visitor<'_> for U64Visitor {
579			type Value = RingBufferId;
580
581			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
582				formatter.write_str("an unsigned 64-bit number")
583			}
584
585			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
586				Ok(RingBufferId(value))
587			}
588		}
589
590		deserializer.deserialize_u64(U64Visitor)
591	}
592}
593
594/// A unique identifier for a subscription.
595/// Uses u64 for efficient storage and to unify with FlowId (FlowId == SubscriptionId for subscription flows).
596#[repr(transparent)]
597#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
598pub struct SubscriptionId(pub u64);
599
600impl Display for SubscriptionId {
601	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
602		Display::fmt(&self.0, f)
603	}
604}
605
606impl Deref for SubscriptionId {
607	type Target = u64;
608
609	fn deref(&self) -> &Self::Target {
610		&self.0
611	}
612}
613
614impl PartialEq<u64> for SubscriptionId {
615	fn eq(&self, other: &u64) -> bool {
616		self.0.eq(other)
617	}
618}
619
620impl From<SubscriptionId> for u64 {
621	fn from(value: SubscriptionId) -> Self {
622		value.0
623	}
624}
625
626impl From<u64> for SubscriptionId {
627	fn from(value: u64) -> Self {
628		Self(value)
629	}
630}
631
632impl Serialize for SubscriptionId {
633	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
634	where
635		S: Serializer,
636	{
637		serializer.serialize_u64(self.0)
638	}
639}
640
641impl<'de> Deserialize<'de> for SubscriptionId {
642	fn deserialize<D>(deserializer: D) -> Result<SubscriptionId, D::Error>
643	where
644		D: Deserializer<'de>,
645	{
646		struct U64Visitor;
647
648		impl Visitor<'_> for U64Visitor {
649			type Value = SubscriptionId;
650
651			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
652				formatter.write_str("an unsigned 64-bit number")
653			}
654
655			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
656				Ok(SubscriptionId(value))
657			}
658		}
659
660		deserializer.deserialize_u64(U64Visitor)
661	}
662}
663
664#[repr(transparent)]
665#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
666pub struct SequenceId(pub u64);
667
668impl Deref for SequenceId {
669	type Target = u64;
670
671	fn deref(&self) -> &Self::Target {
672		&self.0
673	}
674}
675
676impl PartialEq<u64> for SequenceId {
677	fn eq(&self, other: &u64) -> bool {
678		self.0.eq(other)
679	}
680}
681
682impl Serialize for SequenceId {
683	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
684	where
685		S: Serializer,
686	{
687		serializer.serialize_u64(self.0)
688	}
689}
690
691impl<'de> Deserialize<'de> for SequenceId {
692	fn deserialize<D>(deserializer: D) -> Result<SequenceId, D::Error>
693	where
694		D: Deserializer<'de>,
695	{
696		struct U64Visitor;
697
698		impl Visitor<'_> for U64Visitor {
699			type Value = SequenceId;
700
701			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
702				formatter.write_str("an unsigned 64-bit number")
703			}
704
705			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
706				Ok(SequenceId(value))
707			}
708		}
709
710		deserializer.deserialize_u64(U64Visitor)
711	}
712}
713
714#[repr(transparent)]
715#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
716pub struct SubscriptionColumnId(pub u64);
717
718impl Display for SubscriptionColumnId {
719	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
720		Display::fmt(&self.0, f)
721	}
722}
723
724impl Deref for SubscriptionColumnId {
725	type Target = u64;
726
727	fn deref(&self) -> &Self::Target {
728		&self.0
729	}
730}
731
732impl PartialEq<u64> for SubscriptionColumnId {
733	fn eq(&self, other: &u64) -> bool {
734		self.0.eq(other)
735	}
736}
737
738impl From<SubscriptionColumnId> for u64 {
739	fn from(value: SubscriptionColumnId) -> Self {
740		value.0
741	}
742}
743
744impl From<i32> for SubscriptionColumnId {
745	fn from(value: i32) -> Self {
746		Self(value as u64)
747	}
748}
749
750impl From<u64> for SubscriptionColumnId {
751	fn from(value: u64) -> Self {
752		Self(value)
753	}
754}
755
756impl Serialize for SubscriptionColumnId {
757	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
758	where
759		S: Serializer,
760	{
761		serializer.serialize_u64(self.0)
762	}
763}
764
765impl<'de> Deserialize<'de> for SubscriptionColumnId {
766	fn deserialize<D>(deserializer: D) -> Result<SubscriptionColumnId, D::Error>
767	where
768		D: Deserializer<'de>,
769	{
770		struct U64Visitor;
771
772		impl Visitor<'_> for U64Visitor {
773			type Value = SubscriptionColumnId;
774
775			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
776				formatter.write_str("an unsigned 64-bit number")
777			}
778
779			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
780				Ok(SubscriptionColumnId(value))
781			}
782		}
783
784		deserializer.deserialize_u64(U64Visitor)
785	}
786}