Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / nodeloc_s1.rs
nodeloc_s1.rs131 lines · 5.8 KiBRaw
| 1 | impl NodeLocations { |
| 2 | /// `ids` need not be sorted or unique; both are done here. |
| 3 | /// |
| 4 | /// Fallible because the coordinate array is a mapped file, and a temporary directory that is full |
| 5 | /// or read-only is a real thing to report rather than to panic on. |
| 6 | pub fn new(mut ids: Vec<i64>) -> Result<NodeLocations> { |
| 7 | ids.sort_unstable(); |
| 8 | ids.dedup(); |
| 9 | let index = NodeIds::build(&ids); |
| 10 | // The plain vector goes here rather than being kept alongside: it is 8 bytes per id against |
| 11 | // the index's ~1.2, and holding both would give back the saving. Freed before the coordinate |
| 12 | // array is mapped, so the two never peak together. |
| 13 | drop(ids); |
| 14 | let locs = Locs::new(index.len())?; |
| 15 | Ok(NodeLocations { ids: index, locs }) |
| 16 | } |
| 17 | |
| 18 | /// The same table from an **ascending, unique** id stream of known length. |
| 19 | /// |
| 20 | /// [`Self::new`] takes a vector because most callers have one. A continental or planet extract |
| 21 | /// does not want one: [`crate::extract`]-shaped callers hold the needed ids as a bitset over the |
| 22 | /// node id space, which is a fixed ~1.7 GB for any extract, and turning that back into 8 bytes an |
| 23 | /// id to hand it over would be the allocation the bitset exists to avoid. |
| 24 | /// |
| 25 | /// **Sorted and unique is a precondition, not a request.** It is what makes every gap positive, |
| 26 | /// and an out-of-order id would encode as a ten-byte varint and quietly corrupt the index. A |
| 27 | /// bitset walked low to high satisfies it by construction, which is the only reason this is safe |
| 28 | /// to expose. |
| 29 | pub fn from_sorted(ids: impl Iterator<Item = i64>, len: usize) -> Result<NodeLocations> { |
| 30 | let index = NodeIds::build_sorted(ids, len); |
| 31 | let locs = Locs::new(index.len())?; |
| 32 | Ok(NodeLocations { ids: index, locs }) |
| 33 | } |
| 34 | |
| 35 | /// The same table from an already-populated bitset over the id space, taking ownership of its |
| 36 | /// words: bit `i` set iff id `i` is needed, `len` the number of set bits. |
| 37 | /// |
| 38 | /// [`Self::from_sorted`] walks the caller's bitset into ids and re-sets them here, holding two |
| 39 | /// words arrays at once. This moves the words across and builds the rank directory in place, so |
| 40 | /// the peak is one bitset rather than two. The only production caller is the planet ref |
| 41 | /// collector; the vector path ([`Self::new`]) is untouched. |
| 42 | pub fn from_bitset(words: Vec<u64>, len: usize) -> Result<NodeLocations> { |
| 43 | let index = NodeIds::from_words(words, len); |
| 44 | let locs = Locs::new(index.len())?; |
| 45 | Ok(NodeLocations { ids: index, locs }) |
| 46 | } |
| 47 | |
| 48 | pub fn len(&self) -> usize { |
| 49 | self.ids.len() |
| 50 | } |
| 51 | |
| 52 | pub fn is_empty(&self) -> bool { |
| 53 | self.ids.len() == 0 |
| 54 | } |
| 55 | |
| 56 | fn index_of(&self, id: i64) -> Option<u64> { |
| 57 | self.ids.find(id) |
| 58 | } |
| 59 | |
| 60 | /// Whether `id` is one of the ids this table was built for. |
| 61 | /// |
| 62 | /// Distinct from [`Self::get`] returning `None`, which also means "in the set but the node pass |
| 63 | /// never found it" -- the legitimate case at an extract's cut edges. |
| 64 | pub fn contains(&self, id: i64) -> bool { |
| 65 | self.index_of(id).is_some() |
| 66 | } |
| 67 | |
| 68 | pub fn get(&self, id: i64) -> Option<(i32, i32)> { |
| 69 | self.locs.get(self.index_of(id)? as usize) |
| 70 | } |
| 71 | |
| 72 | /// Every ref's table index into `out` (cleared first), `false` as soon as one ref has no |
| 73 | /// coordinate -- exactly the refs [`Self::line`] would drop. Reads only the in-memory id index |
| 74 | /// and resolved bits, never the coordinate array: a caller that needs only some of a way's |
| 75 | /// coordinates (its ends, or a prefix) can then read just those with [`Self::coord_at`]. |
| 76 | pub fn indices_if_complete(&self, refs: &[i64], out: &mut Vec<usize>) -> bool { |
| 77 | out.clear(); |
| 78 | for &id in refs { |
| 79 | match self.index_of(id) { |
| 80 | Some(i) if self.locs.is_resolved(i as usize) => out.push(i as usize), |
| 81 | _ => return false, |
| 82 | } |
| 83 | } |
| 84 | true |
| 85 | } |
| 86 | |
| 87 | /// The coordinate at a table index [`Self::indices_if_complete`] returned, in the same |
| 88 | /// `(lon, lat)` degrees [`Self::line`] produces. |
| 89 | pub fn coord_at(&self, index: usize) -> Coord { |
| 90 | let (lat_e7, lon_e7) = self.locs.get(index).expect("a resolved index"); |
| 91 | (lon_e7 as f64 * 1e-7, lat_e7 as f64 * 1e-7) |
| 92 | } |
| 93 | |
| 94 | /// A way's geometry in lon/lat order, with unresolved refs skipped. |
| 95 | /// |
| 96 | /// A ref can be unresolved for a legitimate reason: an extract cut through the |
| 97 | /// way, so some of its nodes are simply not in the file. Skipping the gap keeps |
| 98 | /// the rest of the line rather than discarding the whole way, which is what |
| 99 | /// `osmium extract`'s `complete_ways` produces for the same input. |
| 100 | pub fn line(&self, refs: &[i64]) -> Vec<Coord> { |
| 101 | refs.iter() |
| 102 | .filter_map(|id| self.get(*id)) |
| 103 | .map(|(lat_e7, lon_e7)| (lon_e7 as f64 * 1e-7, lat_e7 as f64 * 1e-7)) |
| 104 | .collect() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | include!("nodeloc_part1.rs"); |
| 109 | include!("nodeloc_part2.rs"); |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod scratch_tests { |
| 113 | use super::*; |
| 114 | |
| 115 | /// The coordinate table's scratch file is unlinked as soon as it is mapped: nothing is left |
| 116 | /// in the scratch dir even while the table is live, and the table still works. |
| 117 | #[test] |
| 118 | fn the_coordinate_scratch_is_unlinked_while_mapped() { |
| 119 | let dir = std::env::temp_dir().join(format!("osm_nodeloc_unlink_{}", std::process::id())); |
| 120 | std::fs::create_dir_all(&dir).unwrap(); |
| 121 | let mut locs = Locs::new_in(&dir, 1000).unwrap(); |
| 122 | locs.set(7, 123, -456); |
| 123 | let left: Vec<_> = std::fs::read_dir(&dir).unwrap().collect(); |
| 124 | #[cfg(unix)] |
| 125 | assert!(left.is_empty(), "scratch left in {}: {left:?}", dir.display()); |
| 126 | assert_eq!(locs.get(7), Some((123, -456))); |
| 127 | drop(locs); |
| 128 | assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0); |
| 129 | std::fs::remove_dir(&dir).unwrap(); |
| 130 | } |
| 131 | } |