Skip to main content

nodedb_array/types/
tile_id.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Tile identifier — `(hilbert_prefix, system_from_ms)`.
4//!
5//! The Hilbert prefix locates the tile in ND space; the system-time
6//! suffix carries the bitemporal system-time version. Until bitemporal
7//! tiles land, callers pass `system_from_ms = 0`.
8
9use serde::{Deserialize, Serialize};
10
11/// Composite tile key. Lexicographic ordering of `(hilbert_prefix,
12/// system_from_ms)` keeps newer tile versions adjacent to their
13/// spatial parents, so the Ceiling resolver can reverse-scan a single
14/// Hilbert range.
15#[derive(
16    Debug,
17    Clone,
18    Copy,
19    PartialEq,
20    Eq,
21    PartialOrd,
22    Ord,
23    Hash,
24    Serialize,
25    Deserialize,
26    zerompk::ToMessagePack,
27    zerompk::FromMessagePack,
28)]
29pub struct TileId {
30    pub hilbert_prefix: u64,
31    pub system_from_ms: i64,
32}
33
34impl TileId {
35    pub fn new(hilbert_prefix: u64, system_from_ms: i64) -> Self {
36        Self {
37            hilbert_prefix,
38            system_from_ms,
39        }
40    }
41
42    /// Tile id for a non-bitemporal write (pass `system_from_ms = 0`).
43    pub fn snapshot(hilbert_prefix: u64) -> Self {
44        Self::new(hilbert_prefix, 0)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn tile_id_orders_by_prefix_then_time() {
54        let a = TileId::new(10, 100);
55        let b = TileId::new(10, 200);
56        let c = TileId::new(11, 0);
57        assert!(a < b);
58        assert!(b < c);
59    }
60}