Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / store_part6.rs
store_part6.rs389 lines · 13.9 KiBRaw
| 1 | // --- pool-built records ------------------------------------------------------------------------ |
| 2 | // |
| 3 | // The materialise drains used to build every spill record on the one serial thread: the props |
| 4 | // list (a `String` per key), `encode_props`, the record header, and -- per 64 features -- a DEFLATE |
| 5 | // frame. On California that was 19.0 s of a 26.6 s `materialise-ways`; on the planet, ~1.1 B ways, |
| 6 | // it was the bulk of a 30-40 minute single-core phase. Everything up to the record bytes is a pure |
| 7 | // function of one feature, so it now runs on the pool ([`PreRecord`]) and the serial thread only |
| 8 | // appends ([`Sink::push_pre`]); the frames compress in batches on the pool inside the writer. |
| 9 | // |
| 10 | // The props builders below are the bodies `push_named` / `push_road` / `push_building` always had, |
| 11 | // moved out so both paths run the very same code. Same props, same `encode_props`, same header: |
| 12 | // the same bytes. |
| 13 | |
| 14 | /// `push_named`'s props: the packed class, then the name and the tagged id when present. |
| 15 | fn fill_named( |
| 16 | props: &mut Vec<(String, Value)>, |
| 17 | class: &Class, |
| 18 | name: Option<&str>, |
| 19 | id: u64, |
| 20 | ) -> Result<()> { |
| 21 | props[0].1 = Value::Uint(pack(class)?); |
| 22 | props.truncate(1); |
| 23 | if let Some(name) = name.filter(|n| !n.is_empty()) { |
| 24 | props.push((NAME_KEY.to_string(), Value::String(name.to_string()))); |
| 25 | } |
| 26 | if id != tilecodec::mamaps::body::ID_NONE { |
| 27 | if !crate::extract::tracks_ids(class) { |
| 28 | return err(format!( |
| 29 | "layer {} carries a feature id, which only places, poi and a boundaries region shape may", |
| 30 | class.layer, |
| 31 | )); |
| 32 | } |
| 33 | props.push((ID_KEY.to_string(), Value::Uint(id))); |
| 34 | } |
| 35 | Ok(()) |
| 36 | } |
| 37 | |
| 38 | /// `push_road`'s props, including its refusal of a road with nothing to carry. |
| 39 | fn fill_road( |
| 40 | props: &mut Vec<(String, Value)>, |
| 41 | class: &Class, |
| 42 | name: Option<&str>, |
| 43 | lane_count: u8, |
| 44 | turn_fwd: &[u16], |
| 45 | turn_bwd: &[u16], |
| 46 | carriageway: Carriageway, |
| 47 | ) -> Result<()> { |
| 48 | if name.is_none() |
| 49 | && lane_count == 0 |
| 50 | && turn_fwd.is_empty() |
| 51 | && turn_bwd.is_empty() |
| 52 | && carriageway.is_empty() |
| 53 | { |
| 54 | return err("a road pushed with no lane data".to_string()); |
| 55 | } |
| 56 | props[0].1 = Value::Uint(pack(class)?); |
| 57 | props.truncate(1); |
| 58 | if let Some(name) = name.filter(|n| !n.is_empty()) { |
| 59 | props.push((NAME_KEY.to_string(), Value::String(name.to_string()))); |
| 60 | } |
| 61 | if lane_count > 0 { |
| 62 | props.push((LANE_COUNT_KEY.to_string(), Value::Uint(lane_count as u64))); |
| 63 | } |
| 64 | if !turn_fwd.is_empty() { |
| 65 | props.push((TURN_FWD_KEY.to_string(), Value::String(osm_ingest::roads::pack_lanes(turn_fwd)))); |
| 66 | } |
| 67 | if !turn_bwd.is_empty() { |
| 68 | props.push((TURN_BWD_KEY.to_string(), Value::String(osm_ingest::roads::pack_lanes(turn_bwd)))); |
| 69 | } |
| 70 | if !carriageway.is_empty() { |
| 71 | props.push((CARRIAGEWAY_KEY.to_string(), Value::Uint(pack_carriageway(carriageway)))); |
| 72 | } |
| 73 | Ok(()) |
| 74 | } |
| 75 | |
| 76 | /// `push_building`'s props: class, optional name, then the three packed S3DB words. |
| 77 | fn fill_building( |
| 78 | props: &mut Vec<(String, Value)>, |
| 79 | class: &Class, |
| 80 | name: Option<&str>, |
| 81 | attrs: BuildingAttrs, |
| 82 | ) -> Result<()> { |
| 83 | props[0].1 = Value::Uint(pack(class)?); |
| 84 | props.truncate(1); |
| 85 | if let Some(name) = name.filter(|n| !n.is_empty()) { |
| 86 | props.push((NAME_KEY.to_string(), Value::String(name.to_string()))); |
| 87 | } |
| 88 | let (a, b, c) = pack_building(&attrs); |
| 89 | props.push((BLD_A_KEY.to_string(), Value::Uint(a))); |
| 90 | props.push((BLD_B_KEY.to_string(), Value::Uint(b))); |
| 91 | props.push((BLD_C_KEY.to_string(), Value::Uint(c))); |
| 92 | Ok(()) |
| 93 | } |
| 94 | |
| 95 | /// One feature's spill record, built off the serial thread. |
| 96 | /// |
| 97 | /// Carries what [`Sink::write_record`] needs beyond the bytes -- the geometry kind and bounds for |
| 98 | /// the writer's summary, the e7 box for the sink's bbox, the class's `min_zoom` for the chunk |
| 99 | /// index -- so [`Sink::push_pre`] repeats `write_record`'s bookkeeping exactly. |
| 100 | pub struct PreRecord { |
| 101 | rec: Vec<u8>, |
| 102 | kind: GeomKind, |
| 103 | bounds: Option<Rect>, |
| 104 | ebbx: Option<(i32, i32, i32, i32)>, |
| 105 | min_zoom: u8, |
| 106 | } |
| 107 | |
| 108 | impl PreRecord { |
| 109 | /// What [`Sink::push_named`] would stage. |
| 110 | pub fn named( |
| 111 | class: &Class, |
| 112 | geometry: &EncodedGeom, |
| 113 | name: Option<&str>, |
| 114 | id: u64, |
| 115 | ) -> Result<PreRecord> { |
| 116 | let mut props = vec![(CLASS_KEY.to_string(), Value::Uint(0))]; |
| 117 | fill_named(&mut props, class, name, id)?; |
| 118 | Self::build(class, geometry, &props) |
| 119 | } |
| 120 | |
| 121 | /// What [`Sink::push_road`] would stage. |
| 122 | #[allow(clippy::too_many_arguments)] |
| 123 | pub fn road( |
| 124 | class: &Class, |
| 125 | geometry: &EncodedGeom, |
| 126 | name: Option<&str>, |
| 127 | lane_count: u8, |
| 128 | turn_fwd: &[u16], |
| 129 | turn_bwd: &[u16], |
| 130 | carriageway: Carriageway, |
| 131 | ) -> Result<PreRecord> { |
| 132 | let mut props = vec![(CLASS_KEY.to_string(), Value::Uint(0))]; |
| 133 | fill_road(&mut props, class, name, lane_count, turn_fwd, turn_bwd, carriageway)?; |
| 134 | Self::build(class, geometry, &props) |
| 135 | } |
| 136 | |
| 137 | /// What [`Sink::push_building`] would stage. |
| 138 | pub fn building( |
| 139 | class: &Class, |
| 140 | geometry: &EncodedGeom, |
| 141 | name: Option<&str>, |
| 142 | attrs: BuildingAttrs, |
| 143 | ) -> Result<PreRecord> { |
| 144 | let mut props = vec![(CLASS_KEY.to_string(), Value::Uint(0))]; |
| 145 | fill_building(&mut props, class, name, attrs)?; |
| 146 | Self::build(class, geometry, &props) |
| 147 | } |
| 148 | |
| 149 | fn build(class: &Class, geometry: &EncodedGeom, props: &[(String, Value)]) -> Result<PreRecord> { |
| 150 | let mut rec = Vec::with_capacity(64 + geometry.bytes.len()); |
| 151 | tile_build::spill::encode_normalized_record(geometry.kind, &geometry.bytes, props, &mut rec) |
| 152 | .map_err(|e| osm_ingest::proto::Error(e.to_string()))?; |
| 153 | Ok(PreRecord { |
| 154 | rec, |
| 155 | kind: geometry.kind, |
| 156 | bounds: geometry.bounds, |
| 157 | ebbx: geometry.ebbx, |
| 158 | min_zoom: class.min_zoom, |
| 159 | }) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /// Many pool-built records in one buffer, for [`Sink::push_run`]: what the materialise drains |
| 164 | /// used to push one `PreRecord` at a time. Built on the pool (one per ways block, per junction |
| 165 | /// group), so the serial thread does one call per run instead of per-record bookkeeping -- on the |
| 166 | /// planet ~1.1 B records, which was the floor under `materialise-ways` (195 s, all of it drain). |
| 167 | #[derive(Default)] |
| 168 | pub struct RecordRun { |
| 169 | bytes: Vec<u8>, |
| 170 | ends: Vec<usize>, |
| 171 | min_zoom: Vec<u8>, |
| 172 | first_kind: Option<GeomKind>, |
| 173 | bounds: Option<Rect>, |
| 174 | ebbx: Option<(i32, i32, i32, i32)>, |
| 175 | } |
| 176 | |
| 177 | impl RecordRun { |
| 178 | /// Append one record, folding its facts exactly as `Sink::push_pre` would one at a time. |
| 179 | pub fn add(&mut self, record: PreRecord) { |
| 180 | if self.bytes.is_empty() && self.ends.is_empty() { |
| 181 | self.bytes = record.rec; |
| 182 | } else { |
| 183 | self.bytes.extend_from_slice(&record.rec); |
| 184 | } |
| 185 | self.ends.push(self.bytes.len()); |
| 186 | self.min_zoom.push(record.min_zoom); |
| 187 | if self.first_kind.is_none() { |
| 188 | self.first_kind = Some(record.kind); |
| 189 | } |
| 190 | self.bounds = match (self.bounds, record.bounds) { |
| 191 | (None, b) => b, |
| 192 | (a, None) => a, |
| 193 | (Some(a), Some(b)) => Some(Rect { |
| 194 | min_x: a.min_x.min(b.min_x), |
| 195 | min_y: a.min_y.min(b.min_y), |
| 196 | max_x: a.max_x.max(b.max_x), |
| 197 | max_y: a.max_y.max(b.max_y), |
| 198 | }), |
| 199 | }; |
| 200 | if let Some((x0, y0, x1, y1)) = record.ebbx { |
| 201 | self.ebbx = Some(match self.ebbx { |
| 202 | None => (x0, y0, x1, y1), |
| 203 | Some(e) => (e.0.min(x0), e.1.min(y0), e.2.max(x1), e.3.max(y1)), |
| 204 | }); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | pub fn len(&self) -> usize { |
| 209 | self.ends.len() |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | impl Sink { |
| 214 | /// Append a whole [`RecordRun`]: the same writer bytes, chunk minimums, count and bounding box |
| 215 | /// as `push_pre` on each of its records in order. The chunk minimums close on the same |
| 216 | /// 64-record boundaries; the box and the summary folds are min/max, so folding a run first |
| 217 | /// changes nothing. |
| 218 | pub fn push_run(&mut self, run: RecordRun) -> Result<()> { |
| 219 | let RecordRun { bytes, ends, min_zoom, first_kind, bounds, ebbx } = run; |
| 220 | if ends.is_empty() { |
| 221 | return Ok(()); |
| 222 | } |
| 223 | self.writer |
| 224 | .push_run(bytes, &ends, first_kind, bounds) |
| 225 | .map_err(|e| osm_ingest::proto::Error(e.to_string()))?; |
| 226 | let per = NORM_CHUNK_FEATURES as usize; |
| 227 | let mut rest = &min_zoom[..]; |
| 228 | while !rest.is_empty() { |
| 229 | let room = per - (self.count % NORM_CHUNK_FEATURES) as usize; |
| 230 | let take = room.min(rest.len()); |
| 231 | let m = rest[..take].iter().copied().min().expect("non-empty"); |
| 232 | self.filling = self.filling.min(m); |
| 233 | self.count += take as u64; |
| 234 | if self.count % NORM_CHUNK_FEATURES == 0 { |
| 235 | self.chunk_mins.push(self.filling); |
| 236 | self.filling = u8::MAX; |
| 237 | } |
| 238 | rest = &rest[take..]; |
| 239 | } |
| 240 | self.grow_bounds(ebbx); |
| 241 | Ok(()) |
| 242 | } |
| 243 | |
| 244 | /// Append a pool-built record: [`write_record`](Self::write_record) with the encoding already |
| 245 | /// done. Same writer call, same chunk-min, count and bbox bookkeeping, in the same order. |
| 246 | pub fn push_pre(&mut self, record: &PreRecord) -> Result<()> { |
| 247 | self.writer |
| 248 | .push_record(record.kind, &record.rec, record.bounds) |
| 249 | .map_err(|e| osm_ingest::proto::Error(e.to_string()))?; |
| 250 | self.filling = self.filling.min(record.min_zoom); |
| 251 | self.count += 1; |
| 252 | if self.count % NORM_CHUNK_FEATURES == 0 { |
| 253 | self.chunk_mins.push(self.filling); |
| 254 | self.filling = u8::MAX; |
| 255 | } |
| 256 | self.grow_bounds(record.ebbx); |
| 257 | Ok(()) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // --- the ways spill, in blocks --------------------------------------------------------------------- |
| 262 | // |
| 263 | // The ways spill is one varint stream whose ids are delta-coded from record to record, so it could |
| 264 | // only ever be read by one cursor -- and it is read twice (lane inheritance, materialise), 1.1 B |
| 265 | // records on the planet, with every way decoded on one thread: 6 min and 30-40 min phases running |
| 266 | // at 4-6 of 192 cores. And it was *written* by pass 1's serial drain, record by record, which is |
| 267 | // what held pass 1 to ~4 cores for 4m40s. |
| 268 | // |
| 269 | // The stream is unchanged, byte for byte. What is new is an index over it: every |
| 270 | // [`WAY_BLOCK_WAYS`]-ish run of records is a [`WayBlockLoc`] -- its byte range plus the id the |
| 271 | // first record's delta is taken from -- so any run decodes on its own. Pass 1's workers encode |
| 272 | // their chunk's ways into [`WayBlock`]s (the record bytes with the first id left open), the drain |
| 273 | // only patches that one varint and appends, and the two readers decode blocks across the pool |
| 274 | // ([`WayBlocks`]) and consume them in block order, which is spill order. |
| 275 | |
| 276 | /// Ways per index entry, at most. Small enough that a planet pass decodes ~280 k blocks across the |
| 277 | /// pool with even load; large enough that the index is a few MB. |
| 278 | pub const WAY_BLOCK_WAYS: u64 = 4096; |
| 279 | |
| 280 | /// One run of the ways spill that decodes on its own. |
| 281 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 282 | pub struct WayBlockLoc { |
| 283 | /// Byte offset of the run's first record in the spill. |
| 284 | offset: u64, |
| 285 | /// Bytes in the run. |
| 286 | len: u64, |
| 287 | /// The id the first record's delta is taken from: the previous record's id, or zero. |
| 288 | start_last_id: i64, |
| 289 | /// Records in the run. |
| 290 | ways: u64, |
| 291 | } |
| 292 | |
| 293 | impl WayBlockLoc { |
| 294 | pub fn ways(&self) -> u64 { |
| 295 | self.ways |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// A run of way records encoded off the serial thread, for [`WaySink::push_block`]. |
| 300 | /// |
| 301 | /// Exactly the bytes [`WaySink::push`] would write for the same ways, except that the first |
| 302 | /// record's id delta is left open (it is relative to whatever the sink saw last, which a worker |
| 303 | /// cannot know): the block stores that varint as the absolute id and remembers its length, and the |
| 304 | /// sink re-encodes just that varint. |
| 305 | pub struct WayBlock { |
| 306 | bytes: Vec<u8>, |
| 307 | /// Length of the first record's id varint at the head of `bytes`. |
| 308 | head_len: usize, |
| 309 | first_id: i64, |
| 310 | last_id: i64, |
| 311 | count: u64, |
| 312 | refs: u64, |
| 313 | max_ref: i64, |
| 314 | } |
| 315 | |
| 316 | impl Default for WayBlock { |
| 317 | fn default() -> Self { |
| 318 | Self::new() |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | impl WayBlock { |
| 323 | pub fn new() -> WayBlock { |
| 324 | WayBlock { bytes: Vec::new(), head_len: 0, first_id: 0, last_id: 0, count: 0, refs: 0, max_ref: 0 } |
| 325 | } |
| 326 | |
| 327 | /// Ways encoded so far. |
| 328 | pub fn len(&self) -> u64 { |
| 329 | self.count |
| 330 | } |
| 331 | |
| 332 | #[allow(dead_code)] |
| 333 | pub fn is_empty(&self) -> bool { |
| 334 | self.count == 0 |
| 335 | } |
| 336 | |
| 337 | /// Encode one way. Same arguments, same ordering rule and same record bytes as |
| 338 | /// [`WaySink::push`]. |
| 339 | #[allow(clippy::too_many_arguments)] |
| 340 | pub fn push( |
| 341 | &mut self, |
| 342 | id: i64, |
| 343 | class: &Class, |
| 344 | refs: &[i64], |
| 345 | name: Option<&str>, |
| 346 | lane_count: u8, |
| 347 | turn_fwd: &[u16], |
| 348 | turn_bwd: &[u16], |
| 349 | carriageway: Carriageway, |
| 350 | building: Option<BuildingAttrs>, |
| 351 | ) -> Result<()> { |
| 352 | if self.count > 0 && id <= self.last_id { |
| 353 | return err(unsorted_ways(id, self.last_id)); |
| 354 | } |
| 355 | if self.count == 0 { |
| 356 | put_svarint(&mut self.bytes, id); |
| 357 | self.head_len = self.bytes.len(); |
| 358 | self.first_id = id; |
| 359 | } else { |
| 360 | put_svarint(&mut self.bytes, id.wrapping_sub(self.last_id)); |
| 361 | } |
| 362 | let max_ref = encode_way_tail( |
| 363 | &mut self.bytes, |
| 364 | class, |
| 365 | refs, |
| 366 | name, |
| 367 | lane_count, |
| 368 | turn_fwd, |
| 369 | turn_bwd, |
| 370 | carriageway, |
| 371 | building, |
| 372 | )?; |
| 373 | self.max_ref = self.max_ref.max(max_ref); |
| 374 | self.last_id = id; |
| 375 | self.count += 1; |
| 376 | self.refs += refs.len() as u64; |
| 377 | Ok(()) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /// The error a non-advancing way id gets, from the sink and from a block alike. |
| 382 | fn unsorted_ways(id: i64, last_id: i64) -> String { |
| 383 | format!( |
| 384 | "way {id} arrived after way {last_id}, so this PBF is not sorted by way id and the \ |
| 385 | sequential ways spill cannot be used for it", |
| 386 | ) |
| 387 | } |
| 388 | |
| 389 | include!("store_part6_s1.rs"); |