Skip to main content

nodedb_array/segment/format/
tile_entry.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Per-tile entry stored in the segment footer.
4//!
5//! Each entry locates a tile payload inside the segment file and
6//! carries its [`TileMBR`] for predicate pushdown. The R-tree built by
7//! [`super::super::mbr_index`] indexes these entries.
8
9use serde::{Deserialize, Serialize};
10
11use crate::tile::mbr::TileMBR;
12use crate::types::TileId;
13
14/// Discriminant for the on-disk tile payload variant.
15#[derive(
16    Debug,
17    Clone,
18    Copy,
19    PartialEq,
20    Eq,
21    Serialize,
22    Deserialize,
23    zerompk::ToMessagePack,
24    zerompk::FromMessagePack,
25)]
26#[repr(u8)]
27pub enum TileKind {
28    Sparse = 0,
29    Dense = 1,
30}
31
32/// One entry in the segment's per-tile table.
33///
34/// `offset` and `length` cover the **framed** block (length prefix +
35/// payload + CRC). Readers feed the slice through
36/// [`super::framing::BlockFraming::decode`] to validate and extract
37/// the inner zerompk-encoded payload.
38#[derive(
39    Debug,
40    Clone,
41    PartialEq,
42    Serialize,
43    Deserialize,
44    zerompk::ToMessagePack,
45    zerompk::FromMessagePack,
46)]
47pub struct TileEntry {
48    pub tile_id: TileId,
49    pub kind: TileKind,
50    pub offset: u64,
51    pub length: u32,
52    pub mbr: TileMBR,
53}
54
55impl TileEntry {
56    pub fn new(tile_id: TileId, kind: TileKind, offset: u64, length: u32, mbr: TileMBR) -> Self {
57        Self {
58            tile_id,
59            kind,
60            offset,
61            length,
62            mbr,
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn tile_entry_round_trip_msgpack() {
73        let mbr = TileMBR::new(0, 0);
74        let e = TileEntry::new(TileId::snapshot(42), TileKind::Sparse, 100, 256, mbr);
75        let bytes = zerompk::to_msgpack_vec(&e).unwrap();
76        let d: TileEntry = zerompk::from_msgpack(&bytes).unwrap();
77        assert_eq!(d, e);
78    }
79}