Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / tilecodec / src / main / rust / tests / overlay_lookup.rs
overlay_lookup.rs191 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 | |
| 8 | use std::sync::Arc; |
| 9 | |
| 10 | use tilecodec::mamaps::body::{ |
| 11 | Body, Feature, Layer, Part, DEFAULT_EXTENT, GEOM_LINE, NAME_NONE, WINDING_OUTER, |
| 12 | }; |
| 13 | use tilecodec::mamaps::dict; |
| 14 | use tilecodec::mamaps::read::{LeafCache, MamapsArchive, OverlayArchive, TileSource}; |
| 15 | use tilecodec::mamaps::write::{Options, StreamWriter}; |
| 16 | use tilecodec::pmtiles::{tile_id, tile_zxy}; |
| 17 | use tilecodec::proto::Result; |
| 18 | use tilecodec::stream::RangeReader; |
| 19 | |
| 20 | /// A [`RangeReader`] over bytes in memory. |
| 21 | struct Memory { |
| 22 | bytes: Vec<u8>, |
| 23 | } |
| 24 | |
| 25 | impl 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`. |
| 36 | fn 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. |
| 69 | fn 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 | |
| 79 | fn 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 | |
| 84 | fn 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. |
| 91 | fn 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] |
| 97 | fn 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 = |
| 105 | OverlayArchive::new(base, vec![seg], Vec::new(), Arc::new(LeafCache::new())); |
| 106 | |
| 107 | let (z, x, y) = zxy(ids[2]); |
| 108 | let hit = overlay.tile(z, x, y).expect("read").expect("segment hit"); |
| 109 | assert_eq!(hit.layers[0].coords, vec![(0, 0), (2, 2)], "the segment's bytes win"); |
| 110 | |
| 111 | let (z, x, y) = zxy(ids[0]); |
| 112 | let miss = overlay.tile(z, x, y).expect("read").expect("base fallthrough"); |
| 113 | assert_eq!(miss.layers[0].coords, vec![(0, 0), (1, 1)], "a miss reads the base"); |
| 114 | } |
| 115 | |
| 116 | /// A tombstoned tile answers absent even though the base holds its old body. |
| 117 | #[test] |
| 118 | fn a_tombstone_answers_absent() { |
| 119 | let ids = six(); |
| 120 | let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>()); |
| 121 | let base = open(base_bytes); |
| 122 | let overlay = |
| 123 | OverlayArchive::new(base, Vec::new(), vec![ids[3]], Arc::new(LeafCache::new())); |
| 124 | |
| 125 | let (z, x, y) = zxy(ids[3]); |
| 126 | assert!(overlay.tile(z, x, y).expect("read").is_none(), "tombstoned is absent"); |
| 127 | let (z, x, y) = zxy(ids[4]); |
| 128 | assert!(overlay.tile(z, x, y).expect("read").is_some(), "untombstoned still reads"); |
| 129 | } |
| 130 | |
| 131 | /// The batch path resolves per-slot: hit, tombstone, fallthrough, error-free. |
| 132 | #[test] |
| 133 | fn a_batch_resolves_each_slot_patch_first() { |
| 134 | let ids = six(); |
| 135 | let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>()); |
| 136 | let seg_bytes = archive(&[(ids[1], 7)]); |
| 137 | let base = open(base_bytes); |
| 138 | let seg = open(seg_bytes); |
| 139 | let overlay = |
| 140 | OverlayArchive::new(base, vec![seg], vec![ids[5]], Arc::new(LeafCache::new())); |
| 141 | |
| 142 | let coords: Vec<(u8, u32, u32)> = ids.iter().map(|id| zxy(*id)).collect(); |
| 143 | let got = overlay.tiles(&coords); |
| 144 | assert_eq!(got.len(), 6); |
| 145 | // Slot 1: segment bytes. Slot 5: tombstone absent. Rest: base bytes. |
| 146 | let b1 = got[1].as_ref().expect("slot 1 ok").as_ref().expect("slot 1 hit"); |
| 147 | assert_eq!(b1.layers[0].coords, vec![(0, 0), (7, 7)]); |
| 148 | assert!(got[5].as_ref().expect("slot 5 ok").is_none(), "slot 5 tombstoned"); |
| 149 | for n in [0, 2, 3, 4] { |
| 150 | let b = got[n].as_ref().expect("slot ok").as_ref().expect("base hit"); |
| 151 | assert_eq!(b.layers[0].coords, vec![(0, 0), (1, 1)], "slot {n} reads the base"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// Newest segment wins when two carry the same tile. |
| 156 | #[test] |
| 157 | fn the_newest_segment_wins() { |
| 158 | let ids = six(); |
| 159 | let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>()); |
| 160 | let older_bytes = archive(&[(ids[0], 10)]); |
| 161 | let newer_bytes = archive(&[(ids[0], 20)]); |
| 162 | let base = open(base_bytes); |
| 163 | let older = open(older_bytes); |
| 164 | let newer = open(newer_bytes); |
| 165 | let overlay = OverlayArchive::new( |
| 166 | base, |
| 167 | vec![newer, older], |
| 168 | Vec::new(), |
| 169 | Arc::new(LeafCache::new()), |
| 170 | ); |
| 171 | |
| 172 | let (z, x, y) = zxy(ids[0]); |
| 173 | let hit = overlay.tile(z, x, y).expect("read").expect("hit"); |
| 174 | assert_eq!(hit.layers[0].coords, vec![(0, 0), (20, 20)], "newest wins"); |
| 175 | } |
| 176 | |
| 177 | /// The overlay reports the base header (range, build id), never a segment's. |
| 178 | #[test] |
| 179 | fn the_header_is_the_base_header() { |
| 180 | let ids = six(); |
| 181 | let base_bytes = archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>()); |
| 182 | let seg_bytes = archive(&[(ids[0], 9)]); |
| 183 | let base_build = open(base_bytes).header.build_id; |
| 184 | let base = open(archive(&ids.iter().map(|id| (*id, 1)).collect::<Vec<_>>())); |
| 185 | let seg = open(seg_bytes); |
| 186 | let overlay = |
| 187 | OverlayArchive::new(base, vec![seg], Vec::new(), Arc::new(LeafCache::new())); |
| 188 | assert_eq!(overlay.header().build_id, base_build, "base identity wins"); |
| 189 | assert!(tilecodec::mamaps::read::base_matches(&overlay, base_build)); |
| 190 | assert!(!tilecodec::mamaps::read::base_matches(&overlay, base_build.wrapping_add(1))); |
| 191 | } |