Skip to main content

kyu_common/
id.rs

1/// Internal row identifier within the engine.
2/// Matches the C++ `internalID_t` layout: (table_id, offset).
3/// Total size: 16 bytes.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
5#[repr(C)]
6pub struct InternalId {
7    pub table_id: u64,
8    pub offset: u64,
9}
10
11impl InternalId {
12    pub const INVALID: Self = Self {
13        table_id: u64::MAX,
14        offset: u64::MAX,
15    };
16
17    pub const fn new(table_id: u64, offset: u64) -> Self {
18        Self { table_id, offset }
19    }
20
21    pub const fn is_valid(&self) -> bool {
22        self.table_id != u64::MAX
23    }
24}
25
26impl std::fmt::Display for InternalId {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}:{}", self.table_id, self.offset)
29    }
30}
31
32/// Table identifier in the catalog.
33#[derive(
34    Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
35)]
36pub struct TableId(pub u64);
37
38/// Property identifier within a table.
39#[derive(
40    Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
41)]
42pub struct PropertyId(pub u32);
43
44/// Column identifier within a node group.
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
46pub struct ColumnId(pub u32);
47
48/// Transaction timestamp / sequence number.
49pub type TxnTs = u64;
50
51/// Write-ahead log sequence number.
52pub type Lsn = u64;
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use std::collections::HashSet;
58
59    #[test]
60    fn internal_id_invalid() {
61        assert!(!InternalId::INVALID.is_valid());
62    }
63
64    #[test]
65    fn internal_id_valid() {
66        let id = InternalId::new(0, 0);
67        assert!(id.is_valid());
68    }
69
70    #[test]
71    fn internal_id_display() {
72        let id = InternalId::new(3, 42);
73        assert_eq!(id.to_string(), "3:42");
74    }
75
76    #[test]
77    fn internal_id_ordering() {
78        let a = InternalId::new(1, 10);
79        let b = InternalId::new(1, 20);
80        let c = InternalId::new(2, 5);
81        assert!(a < b);
82        assert!(b < c);
83    }
84
85    #[test]
86    fn internal_id_hash() {
87        let mut set = HashSet::new();
88        set.insert(InternalId::new(1, 1));
89        set.insert(InternalId::new(1, 1));
90        assert_eq!(set.len(), 1);
91    }
92
93    #[test]
94    fn internal_id_size() {
95        assert_eq!(std::mem::size_of::<InternalId>(), 16);
96    }
97
98    #[test]
99    fn newtype_ids() {
100        let t1 = TableId(1);
101        let t2 = TableId(2);
102        assert_ne!(t1, t2);
103        assert!(t1 < t2);
104
105        let p = PropertyId(0);
106        assert_eq!(p.0, 0);
107
108        let c = ColumnId(5);
109        assert_eq!(c.0, 5);
110    }
111}