Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / tilecodec / src / main / rust / tests / overlay_lookup.rs

overlay_lookup.rs183 lines · 7.0 KiBRaw
1//! Phase 3 overlay lookup: integration gate for `mamaps::read::overlay`.
2//!
3//! Builds base + segment archives through the public writer API only, then
4//! asserts patch-first semantics through the public [`TileSource`] seam:
5//! segment hits win, tombstones answer absent, misses fall to the base, and
6//! the base file's bytes are never consulted for a tombstoned tile.
7
8use std::sync::Arc;
9
10use tilecodec::mamaps::body::{
11 Body, Feature, Layer, Part, DEFAULT_EXTENT, GEOM_LINE, NAME_NONE, WINDING_OUTER,
12};
13use tilecodec::mamaps::dict;
14use tilecodec::mamaps::read::{LeafCache, MamapsArchive, OverlayArchive, TileSource};
15use tilecodec::mamaps::write::{Options, StreamWriter};
16use tilecodec::pmtiles::{tile_id, tile_zxy};
17use tilecodec::proto::Result;
18use tilecodec::stream::RangeReader;
19
20/// A [`RangeReader`] over bytes in memory.
21struct Memory {
22 bytes: Vec<u8>,
23}
24
25impl RangeReader for Memory {
26 fn read(&self, offset: u64, length: u32) -> Result<Vec<u8>> {
27 if offset >= self.bytes.len() as u64 {
28 return Ok(Vec::new());
29 }
30 let end = (offset + length as u64).min(self.bytes.len() as u64);
31 Ok(self.bytes[offset as usize..end as usize].to_vec())
32 }
33}
34
35/// A one-line body whose contents depend on `seed`.
36fn body_for(seed: i16) -> Body {
37 let mut roads = Layer::new(dict::LAYER_ROADS);
38 roads.features.push(Feature {
39 kind: 45,
40 kind_detail: dict::NONE,
41 geom_type: GEOM_LINE,
42 flags: 0,
43 name_idx: NAME_NONE,
44 parts_offset: 0,
45 part_count: 1,
46 transit_color: 0,
47 transit_ordinal: 0,
48 transit_lanes: 0,
49 transit_taper: 0,
50 lane_count: 0,
51 });
52 roads.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });
53 roads.coords = vec![(0, 0), (seed, seed)];
54 Body {
55 extent: DEFAULT_EXTENT,
56 layers: vec![roads],
57 names: Vec::new(),
58 ids: Vec::new(),
59 turn_lanes: Vec::new(),
60 buildings: Vec::new(),
61 heightmap: None,
62 carriageways: Vec::new(),
63 convention: None,
64 region_links: Vec::new(),
65 }
66}
67
68/// An archive of `(tile_id, seed)` tiles, ascending as the writer requires.
69fn archive(rows: &[(u64, i16)]) -> Vec<u8> {
70 let mut sorted = rows.to_vec();
71 sorted.sort_by_key(|(id, _)| *id);
72 let mut writer = StreamWriter::new(Options::default()).expect("options");
73 for (id, seed) in sorted {
74 writer.append(id, &body_for(seed)).expect("append");
75 }
76 writer.finish().expect("finish")
77}
78
79fn open(bytes: Vec<u8>) -> MamapsArchive<Memory> {
80 // Its own leaf cache: offsets collide across archives sharing one.
81 MamapsArchive::open_with_cache(Memory { bytes }, Arc::new(LeafCache::new())).expect("open")
82}
83
84fn zxy(id: u64) -> (u8, u32, u32) {
85 let (z, x, y) = tile_zxy(id);
86 (z, x as u32, y as u32)
87}
88
89/// Six tiles at z4: ids change per seed, so base and segment hold different
90/// bytes at the same tile exactly where asked.
91fn six() -> Vec<u64> {
92 (0..6u64).map(|n| tile_id(4, n, 0)).collect()
93}
94
95/// A segment hit wins over the base, and an unmentioned tile falls through.
96#[test]
97fn a_segment_hit_wins_and_a_miss_falls_through() {
98 let ids = six();
99 let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>());
100 // Segment carries only tile 2 with new bytes (seed 2).
101 let seg_bytes = archive(&[(ids[2], 2)]);
102 let base = open(base_bytes);
103 let seg = open(seg_bytes);
104 let overlay = OverlayArchive::new(base, vec![seg], Vec::new(), Arc::new(LeafCache::new()));
105
106 let (z, x, y) = zxy(ids[2]);
107 let hit = overlay.tile(z, x, y).expect("read").expect("segment hit");
108 assert_eq!(hit.layers[0].coords, vec![(0, 0), (2, 2)], "the segment's bytes win");
109
110 let (z, x, y) = zxy(ids[0]);
111 let miss = overlay.tile(z, x, y).expect("read").expect("base fallthrough");
112 assert_eq!(miss.layers[0].coords, vec![(0, 0), (1, 1)], "a miss reads the base");
113}
114
115/// A tombstoned tile answers absent even though the base holds its old body.
116#[test]
117fn a_tombstone_answers_absent() {
118 let ids = six();
119 let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>());
120 let base = open(base_bytes);
121 let overlay = OverlayArchive::new(base, Vec::new(), vec![ids[3]], Arc::new(LeafCache::new()));
122
123 let (z, x, y) = zxy(ids[3]);
124 assert!(overlay.tile(z, x, y).expect("read").is_none(), "tombstoned is absent");
125 let (z, x, y) = zxy(ids[4]);
126 assert!(overlay.tile(z, x, y).expect("read").is_some(), "untombstoned still reads");
127}
128
129/// The batch path resolves per-slot: hit, tombstone, fallthrough, error-free.
130#[test]
131fn a_batch_resolves_each_slot_patch_first() {
132 let ids = six();
133 let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>());
134 let seg_bytes = archive(&[(ids[1], 7)]);
135 let base = open(base_bytes);
136 let seg = open(seg_bytes);
137 let overlay = OverlayArchive::new(base, vec![seg], vec![ids[5]], Arc::new(LeafCache::new()));
138
139 let coords: Vec<(u8, u32, u32)> = ids.iter().map(|id| zxy(*id)).collect();
140 let got = overlay.tiles(&coords);
141 assert_eq!(got.len(), 6);
142 // Slot 1: segment bytes. Slot 5: tombstone absent. Rest: base bytes.
143 let b1 = got[1].as_ref().expect("slot 1 ok").as_ref().expect("slot 1 hit");
144 assert_eq!(b1.layers[0].coords, vec![(0, 0), (7, 7)]);
145 assert!(got[5].as_ref().expect("slot 5 ok").is_none(), "slot 5 tombstoned");
146 for n in [0, 2, 3, 4] {
147 let b = got[n].as_ref().expect("slot ok").as_ref().expect("base hit");
148 assert_eq!(b.layers[0].coords, vec![(0, 0), (1, 1)], "slot {n} reads the base");
149 }
150}
151
152/// Newest segment wins when two carry the same tile.
153#[test]
154fn the_newest_segment_wins() {
155 let ids = six();
156 let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>());
157 let older_bytes = archive(&[(ids[0], 10)]);
158 let newer_bytes = archive(&[(ids[0], 20)]);
159 let base = open(base_bytes);
160 let older = open(older_bytes);
161 let newer = open(newer_bytes);
162 let overlay =
163 OverlayArchive::new(base, vec![newer, older], Vec::new(), Arc::new(LeafCache::new()));
164
165 let (z, x, y) = zxy(ids[0]);
166 let hit = overlay.tile(z, x, y).expect("read").expect("hit");
167 assert_eq!(hit.layers[0].coords, vec![(0, 0), (20, 20)], "newest wins");
168}
169
170/// The overlay reports the base header (range, build id), never a segment's.
171#[test]
172fn the_header_is_the_base_header() {
173 let ids = six();
174 let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>());
175 let seg_bytes = archive(&[(ids[0], 9)]);
176 let base_build = open(base_bytes).header.build_id;
177 let base = open(archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>()));
178 let seg = open(seg_bytes);
179 let overlay = OverlayArchive::new(base, vec![seg], Vec::new(), Arc::new(LeafCache::new()));
180 assert_eq!(overlay.header().build_id, base_build, "base identity wins");
181 assert!(tilecodec::mamaps::read::base_matches(&overlay, base_build));
182 assert!(!tilecodec::mamaps::read::base_matches(&overlay, base_build.wrapping_add(1)));
183}