Skip to main content

umadb_core/
node.rs

1use crate::events_tree_nodes::{EventInternalNode, EventLeafNode, EventOverflowNode};
2use crate::free_lists_tree_nodes::{
3    FreeListInternalNode, FreeListLeafNode, FreeListTsnInternalNode, FreeListTsnLeafNode,
4};
5use crate::header_node::HeaderNode;
6use crate::tags_tree_nodes::{TagInternalNode, TagLeafNode, TagsInternalNode, TagsLeafNode};
7use crate::tracking_tree_nodes::{TrackingInternalNode, TrackingLeafNode};
8use umadb_dcb::{DcbError, DcbResult};
9
10// Constants for serialization
11const PAGE_TYPE_HEADER: u8 = b'1';
12const PAGE_TYPE_FREELIST_LEAF: u8 = b'2';
13const PAGE_TYPE_FREELIST_INTERNAL: u8 = b'3';
14const PAGE_TYPE_EVENT_LEAF: u8 = b'4';
15const PAGE_TYPE_EVENT_INTERNAL: u8 = b'5';
16const PAGE_TYPE_TAGS_LEAF: u8 = b'6';
17const PAGE_TYPE_TAGS_INTERNAL: u8 = b'7';
18const PAGE_TYPE_TAG_LEAF: u8 = b'8';
19const PAGE_TYPE_TAG_INTERNAL: u8 = b'9';
20const PAGE_TYPE_EVENT_OVERFLOW: u8 = b'a';
21const PAGE_TYPE_FREELIST_TSN_LEAF: u8 = b'b';
22const PAGE_TYPE_FREELIST_TSN_INTERNAL: u8 = b'c';
23const PAGE_TYPE_TRACKING_LEAF: u8 = b'd';
24const PAGE_TYPE_TRACKING_INTERNAL: u8 = b'e';
25
26// Enum to represent different node types
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum Node {
29    Header(HeaderNode),
30    FreeListLeaf(FreeListLeafNode),
31    FreeListInternal(FreeListInternalNode),
32    EventLeaf(EventLeafNode),
33    EventInternal(EventInternalNode),
34    EventOverflow(EventOverflowNode),
35    TagsLeaf(TagsLeafNode),
36    TagsInternal(TagsInternalNode),
37    TagLeaf(TagLeafNode),
38    TagInternal(TagInternalNode),
39    FreeListTsnLeaf(FreeListTsnLeafNode),
40    FreeListTsnInternal(FreeListTsnInternalNode),
41    TrackingLeaf(TrackingLeafNode),
42    TrackingInternal(TrackingInternalNode),
43}
44
45impl Node {
46    pub fn get_type_byte(&self) -> u8 {
47        match self {
48            Node::Header(_) => PAGE_TYPE_HEADER,
49            Node::FreeListLeaf(_) => PAGE_TYPE_FREELIST_LEAF,
50            Node::FreeListInternal(_) => PAGE_TYPE_FREELIST_INTERNAL,
51            Node::EventLeaf(_) => PAGE_TYPE_EVENT_LEAF,
52            Node::EventInternal(_) => PAGE_TYPE_EVENT_INTERNAL,
53            Node::EventOverflow(_) => PAGE_TYPE_EVENT_OVERFLOW,
54            Node::TagsLeaf(_) => PAGE_TYPE_TAGS_LEAF,
55            Node::TagsInternal(_) => PAGE_TYPE_TAGS_INTERNAL,
56            Node::TagLeaf(_) => PAGE_TYPE_TAG_LEAF,
57            Node::TagInternal(_) => PAGE_TYPE_TAG_INTERNAL,
58            Node::FreeListTsnLeaf(_) => PAGE_TYPE_FREELIST_TSN_LEAF,
59            Node::FreeListTsnInternal(_) => PAGE_TYPE_FREELIST_TSN_INTERNAL,
60            Node::TrackingLeaf(_) => PAGE_TYPE_TRACKING_LEAF,
61            Node::TrackingInternal(_) => PAGE_TYPE_TRACKING_INTERNAL,
62        }
63    }
64
65    pub fn type_name(&self) -> &'static str {
66        match self {
67            Node::Header(_) => "Header",
68            Node::FreeListLeaf(_) => "FreeListLeaf",
69            Node::FreeListInternal(_) => "FreeListInternal",
70            Node::EventLeaf(_) => "EventLeaf",
71            Node::EventInternal(_) => "EventInternal",
72            Node::EventOverflow(_) => "EventOverflow",
73            Node::TagsLeaf(_) => "TagsLeaf",
74            Node::TagsInternal(_) => "TagsInternal",
75            Node::TagLeaf(_) => "TagLeaf",
76            Node::TagInternal(_) => "TagInternal",
77            Node::FreeListTsnLeaf(_) => "FreeListTsnLeaf",
78            Node::FreeListTsnInternal(_) => "FreeListTsnInternal",
79            Node::TrackingLeaf(_) => "TrackingLeaf",
80            Node::TrackingInternal(_) => "TrackingInternal",
81        }
82    }
83
84    pub fn calc_serialized_size(&self) -> usize {
85        match self {
86            Node::Header(node) => node.calc_serialized_size(),
87            Node::FreeListLeaf(node) => node.calc_serialized_size(),
88            Node::FreeListInternal(node) => node.calc_serialized_size(),
89            Node::EventLeaf(node) => node.calc_serialized_size(),
90            Node::EventInternal(node) => node.calc_serialized_size(),
91            Node::EventOverflow(node) => node.calc_serialized_size(),
92            Node::TagsLeaf(node) => node.calc_serialized_size(),
93            Node::TagsInternal(node) => node.calc_serialized_size(),
94            Node::TagLeaf(node) => node.calc_serialized_size(),
95            Node::TagInternal(node) => node.calc_serialized_size(),
96            Node::FreeListTsnLeaf(node) => node.calc_serialized_size(),
97            Node::FreeListTsnInternal(node) => node.calc_serialized_size(),
98            Node::TrackingLeaf(node) => node.calc_serialized_size(),
99            Node::TrackingInternal(node) => node.calc_serialized_size(),
100        }
101    }
102
103    /// No-allocation serialization into a provided buffer slice.
104    /// Returns the number of bytes written.
105    /// Implemented for key node types; for others it falls back to allocate-and-copy.
106    pub fn serialize_into(&self, buf: &mut [u8]) -> DcbResult<usize> {
107        match self {
108            Node::Header(node) => node.serialize_into(buf),
109            Node::FreeListLeaf(node) => node.serialize_into(buf),
110            Node::FreeListInternal(node) => node.serialize_into(buf),
111            Node::EventLeaf(node) => node.serialize_into(buf),
112            Node::EventInternal(node) => node.serialize_into(buf),
113            Node::EventOverflow(node) => node.serialize_into(buf),
114            Node::TagsLeaf(node) => node.serialize_into(buf),
115            Node::TagsInternal(node) => node.serialize_into(buf),
116            Node::TagLeaf(node) => node.serialize_into(buf),
117            Node::TagInternal(node) => node.serialize_into(buf),
118            Node::FreeListTsnLeaf(node) => node.serialize_into(buf),
119            Node::FreeListTsnInternal(node) => node.serialize_into(buf),
120            Node::TrackingLeaf(node) => node.serialize_into(buf),
121            Node::TrackingInternal(node) => node.serialize_into(buf),
122        }
123    }
124
125    pub fn deserialize(node_type: u8, data: &[u8]) -> DcbResult<Self> {
126        match node_type {
127            PAGE_TYPE_HEADER => {
128                let node = HeaderNode::from_slice(data)?;
129                Ok(Node::Header(node))
130            }
131            PAGE_TYPE_FREELIST_LEAF => {
132                let node = FreeListLeafNode::from_slice(data)?;
133                Ok(Node::FreeListLeaf(node))
134            }
135            PAGE_TYPE_FREELIST_INTERNAL => {
136                let node = FreeListInternalNode::from_slice(data)?;
137                Ok(Node::FreeListInternal(node))
138            }
139            PAGE_TYPE_EVENT_LEAF => {
140                let node = EventLeafNode::from_slice(data)?;
141                Ok(Node::EventLeaf(node))
142            }
143            PAGE_TYPE_EVENT_INTERNAL => {
144                let node = EventInternalNode::from_slice(data)?;
145                Ok(Node::EventInternal(node))
146            }
147            PAGE_TYPE_EVENT_OVERFLOW => {
148                let node = EventOverflowNode::from_slice(data)?;
149                Ok(Node::EventOverflow(node))
150            }
151            PAGE_TYPE_TAGS_LEAF => {
152                let node = TagsLeafNode::from_slice(data)?;
153                Ok(Node::TagsLeaf(node))
154            }
155            PAGE_TYPE_TAGS_INTERNAL => {
156                let node = TagsInternalNode::from_slice(data)?;
157                Ok(Node::TagsInternal(node))
158            }
159            PAGE_TYPE_TAG_LEAF => {
160                let node = TagLeafNode::from_slice(data)?;
161                Ok(Node::TagLeaf(node))
162            }
163            PAGE_TYPE_TAG_INTERNAL => {
164                let node = TagInternalNode::from_slice(data)?;
165                Ok(Node::TagInternal(node))
166            }
167            PAGE_TYPE_FREELIST_TSN_LEAF => {
168                let node = FreeListTsnLeafNode::from_slice(data)?;
169                Ok(Node::FreeListTsnLeaf(node))
170            }
171            PAGE_TYPE_FREELIST_TSN_INTERNAL => {
172                let node = FreeListTsnInternalNode::from_slice(data)?;
173                Ok(Node::FreeListTsnInternal(node))
174            }
175            PAGE_TYPE_TRACKING_LEAF => {
176                let node = TrackingLeafNode::from_slice(data)?;
177                Ok(Node::TrackingLeaf(node))
178            }
179            PAGE_TYPE_TRACKING_INTERNAL => {
180                let node = TrackingInternalNode::from_slice(data)?;
181                Ok(Node::TrackingInternal(node))
182            }
183            _ => Err(DcbError::DatabaseCorrupted(format!(
184                "Invalid node type: {node_type}"
185            ))),
186        }
187    }
188}