Skip to main content

zu_common/
id.rs

1//! Packed graph identifiers.
2//!
3//! `NodeId` packs a table, a node group, and a row offset into one `u64`:
4//!
5//! ```text
6//! bits 63..50  table_id     (14 bits, max 16 384 tables)
7//! bits 49..28  node_group   (22 bits, max 4 194 304 groups per table)
8//! bits 27..11  row          (17 bits, GROUP_ROWS = 131 072 rows per group)
9//! bits 10..0   reserved     (11 bits, must be zero in v1)
10//! ```
11//!
12//! The layout is format-stable: it is written to disk by every engine.
13
14/// Rows per node group, fixed at 2^17.
15pub const GROUP_ROWS: u32 = 1 << 17;
16
17const TABLE_BITS: u32 = 14;
18const GROUP_BITS: u32 = 22;
19const ROW_BITS: u32 = 17;
20const RESERVED_BITS: u32 = 11;
21
22const ROW_SHIFT: u32 = RESERVED_BITS;
23const GROUP_SHIFT: u32 = ROW_SHIFT + ROW_BITS;
24const TABLE_SHIFT: u32 = GROUP_SHIFT + GROUP_BITS;
25
26/// Identifies a node or rel table in the catalog.
27pub type TableId = u16;
28
29/// Index of a node group within a table.
30pub type NodeGroupId = u32;
31
32/// Row offset within a node group, `0..GROUP_ROWS`.
33pub type NodeOffset = u32;
34
35/// Checkpoint or commit epoch, monotonically increasing.
36pub type Epoch = u64;
37
38/// Packed node identifier. See the module docs for the bit layout.
39#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
40pub struct NodeId(pub u64);
41
42impl NodeId {
43    /// Packs the three components. Debug-asserts that each fits its field.
44    #[inline]
45    pub fn new(table: TableId, group: NodeGroupId, row: NodeOffset) -> Self {
46        debug_assert!(u32::from(table) < (1 << TABLE_BITS));
47        debug_assert!(group < (1 << GROUP_BITS));
48        debug_assert!(row < GROUP_ROWS);
49        Self(
50            (u64::from(table) << TABLE_SHIFT)
51                | (u64::from(group) << GROUP_SHIFT)
52                | (u64::from(row) << ROW_SHIFT),
53        )
54    }
55
56    #[inline]
57    pub fn table(self) -> TableId {
58        (self.0 >> TABLE_SHIFT) as TableId
59    }
60
61    #[inline]
62    pub fn group(self) -> NodeGroupId {
63        ((self.0 >> GROUP_SHIFT) & ((1 << GROUP_BITS) - 1)) as NodeGroupId
64    }
65
66    #[inline]
67    pub fn row(self) -> NodeOffset {
68        ((self.0 >> ROW_SHIFT) & ((1 << ROW_BITS) - 1)) as NodeOffset
69    }
70
71    /// Row index within the table, `group * GROUP_ROWS + row`.
72    #[inline]
73    pub fn table_row(self) -> u64 {
74        u64::from(self.group()) * u64::from(GROUP_ROWS) + u64::from(self.row())
75    }
76}
77
78impl std::fmt::Debug for NodeId {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        write!(
81            f,
82            "NodeId({}:{}:{})",
83            self.table(),
84            self.group(),
85            self.row()
86        )
87    }
88}
89
90/// Identifies a relationship: the rel table plus its position in CSR order.
91#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
92pub struct RelId {
93    pub table: TableId,
94    pub group: NodeGroupId,
95    pub slot: u32,
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn pack_roundtrip() {
104        let id = NodeId::new(3, 7, 42);
105        assert_eq!(id.table(), 3);
106        assert_eq!(id.group(), 7);
107        assert_eq!(id.row(), 42);
108        assert_eq!(id.table_row(), 7 * u64::from(GROUP_ROWS) + 42);
109    }
110
111    #[test]
112    fn pack_extremes() {
113        let id = NodeId::new((1 << 14) - 1, (1 << 22) - 1, GROUP_ROWS - 1);
114        assert_eq!(id.table(), (1 << 14) - 1);
115        assert_eq!(id.group(), (1 << 22) - 1);
116        assert_eq!(id.row(), GROUP_ROWS - 1);
117        let zero = NodeId::new(0, 0, 0);
118        assert_eq!(zero.0, 0);
119    }
120
121    #[test]
122    fn reserved_bits_are_zero() {
123        let id = NodeId::new(123, 456, 789);
124        assert_eq!(id.0 & ((1 << 11) - 1), 0);
125    }
126
127    #[test]
128    fn ordering_follows_physical_layout() {
129        let a = NodeId::new(1, 0, 100);
130        let b = NodeId::new(1, 1, 0);
131        let c = NodeId::new(2, 0, 0);
132        assert!(a < b);
133        assert!(b < c);
134    }
135}