impl ChunkReader<'_> {
/// Skip one entry without decoding it, returning its key and merge cost.
///
/// Byte-for-byte the same cursor movement as [`next`](Self::next) —
/// header, fixed arenas, name lengths, turn counts, building and
/// carriageway sections — minus every allocation, UTF-8 check and struct
/// build. The range planner uses it to walk chunks cheaply (cost histogram
/// plus boundary offsets); the merge workers then decode for real. A corrupt
/// entry errors here exactly as `next` would surface it, so planning fails
/// fast rather than handing workers a desynced stream.
///
/// Never touches the spill's read counters: planning reads are not
/// decoded entries, and `check_books` must see each entry counted once —
/// by the worker that decodes it.
pub fn skip_entry(&mut self) -> Result> {
if self.left == 0 {
return Ok(None);
}
self.fill(ENTRY_HEADER_BYTES)?;
let head: &[u8; ENTRY_HEADER_BYTES] = self.buf[self.used..self.used + ENTRY_HEADER_BYTES]
.try_into()
.expect("a header's worth of bytes");
let header = entry_header(head)?;
let fixed =
payload_bytes(header.features, header.parts, header.coords, header.ids) as usize;
self.fill(ENTRY_HEADER_BYTES + fixed)?;
self.used += ENTRY_HEADER_BYTES + fixed;
for _ in 0..header.names {
self.fill(4)?;
let len = u32::from_le_bytes(
self.buf[self.used..self.used + 4].try_into().expect("4 bytes"),
) as usize;
if len > 1 << 20 {
return err("a tile chunk entry names a megabyte-plus string".to_string());
}
self.fill(4 + len)?;
self.used += 4 + len;
}
if header.has_turns {
for _ in 0..header.features {
self.fill(2)?;
let (fwd, bwd) =
(self.buf[self.used] as usize, self.buf[self.used + 1] as usize);
let bytes = 2 + 2 * (fwd + bwd);
self.fill(bytes)?;
self.used += bytes;
}
}
if header.has_buildings {
let bytes = BUILDING_BYTES * header.features;
self.fill(bytes)?;
self.used += bytes;
}
if header.has_carriageways {
let bytes = CARRIAGEWAY_RECORD_LEN * header.features;
self.fill(bytes)?;
self.used += bytes;
}
self.left -= 1;
// Same cost unit the merge batches by (`tile_cost`): features plus
// coordinates, the proxy the encode pass balances tasks by.
let cost = header.features as u64 + header.coords as u64;
Ok(Some(((header.tile, header.layer_id), cost)))
}
/// File offset of the next unread byte: what a range worker seeks to.
///
/// `at` is the first byte not yet buffered, so the unread position is
/// behind it by exactly the buffered-but-unconsumed span.
pub fn position(&self) -> u64 {
self.at - (self.buf.len() - self.used) as u64
}
/// The next entry's tile id without consuming it.
///
/// A range worker seeks to a bucket edge (coarser than its tile bound),
/// then advances past entries below the bound with [`skip_entry`](Self::skip_entry)
/// — counted down from `left` like any consumed entry, uncounted in the
/// spill books like any skip. `None` past the last entry (no trailing
/// check here; [`next`](Self::next) owns that).
pub fn peek_tile(&mut self) -> Result > {
if self.left == 0 {
return Ok(None);
}
self.fill(ENTRY_HEADER_BYTES)?;
let head: &[u8; ENTRY_HEADER_BYTES] = self.buf[self.used..self.used + ENTRY_HEADER_BYTES]
.try_into()
.expect("a header's worth of bytes");
Ok(Some(entry_header(head)?.tile))
}
/// Reposition a reader at a planned offset with a planned entry count.
///
/// The buffer is discarded (its bytes belong to another range now and
/// will be re-read through the window); `end` is unchanged. `exact_end`
/// says whether this segment runs to the chunk's last byte: only then
/// does [`next`](Self::next) enforce the no-trailing-bytes check, since
/// any other segment legitimately ends mid-chunk with the next range's
/// bytes after it.
pub fn seek_to(&mut self, offset: u64, remaining: u64, exact_end: bool) {
self.buf.clear();
self.used = 0;
self.at = offset;
self.left = remaining;
self.exact_end = exact_end;
}
///
/// Fallible, and that matters: a truncated scratch file must fail the build rather than silently
/// shorten the archive.
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Result > {
if self.left == 0 {
if self.exact_end && (self.used < self.buf.len() || self.at < self.end) {
return err(format!(
"a tile chunk in {} holds bytes past its last entry",
self.spill.path.display()
));
}
return Ok(None);
}
self.fill(ENTRY_HEADER_BYTES)?;
let head: &[u8; ENTRY_HEADER_BYTES] = self.buf[self.used..self.used + ENTRY_HEADER_BYTES]
.try_into()
.expect("a header's worth of bytes");
let header = entry_header(head)?;
// The fixed arenas first: their length is a pure function of the header, so one fill
// covers them. Names follow with inline lengths and are pulled one at a time — their
// byte length is known only once the previous one is read.
let fixed =
payload_bytes(header.features, header.parts, header.coords, header.ids) as usize;
self.fill(ENTRY_HEADER_BYTES + fixed)?;
let body = self.used + ENTRY_HEADER_BYTES;
let spare = self.spare.take();
let mut entry = decode_fixed(&header, &self.buf[body..body + fixed], spare)?;
self.used = body + fixed;
// Counted, not differenced: `fill` compacts the buffer when it tops up, so a span across
// two buffer positions is not the difference of two offsets.
let mut consumed = ENTRY_HEADER_BYTES + fixed;
for _ in 0..header.names {
self.fill(4)?;
let len = u32::from_le_bytes(
self.buf[self.used..self.used + 4].try_into().expect("4 bytes"),
) as usize;
if len > 1 << 20 {
return err("a tile chunk entry names a megabyte-plus string".to_string());
}
self.fill(4 + len)?;
let bytes = &self.buf[self.used + 4..self.used + 4 + len];
entry.names.push(
std::str::from_utf8(bytes)
.map_err(|_| Error("a tile chunk entry's name is not UTF-8".to_string()))?
.to_string(),
);
self.used += 4 + len;
consumed += 4 + len;
}
// Every name index must land in the table just read: a dangling index would decode to
// the wrong label on device with no other symptom.
for feature in &entry.layer.features {
if feature.name_idx as usize > entry.names.len() {
return err("a tile chunk feature names past its entry's table".to_string());
}
}
// The turn-lane section, after the names when the flag is set: one record per feature,
// each a u8 forward count, u8 backward count, then that many u16 masks each.
if header.has_turns {
entry.turn_lanes.reserve(header.features);
for _ in 0..header.features {
self.fill(2)?;
let (fwd, bwd) =
(self.buf[self.used] as usize, self.buf[self.used + 1] as usize);
let bytes = 2 + 2 * (fwd + bwd);
self.fill(bytes)?;
let read = |off: usize, n: usize| -> Vec {
(0..n)
.map(|k| {
let o = self.used + off + k * 2;
u16::from_le_bytes([self.buf[o], self.buf[o + 1]])
})
.collect()
};
let forward = read(2, fwd);
let backward = read(2 + 2 * fwd, bwd);
entry.turn_lanes.push(tilecodec::mamaps::body::LaneTurns { forward, backward });
self.used += bytes;
consumed += bytes;
}
}
// The building section, after the turn-lane section when the flag is set: one
// BUILDING_BYTES record per feature, dense.
if header.has_buildings {
entry.buildings.reserve(header.features);
for _ in 0..header.features {
self.fill(BUILDING_BYTES)?;
let o = self.used;
let u16_at = |k: usize| u16::from_le_bytes([self.buf[o + k], self.buf[o + k + 1]]);
let u32_at = |k: usize| {
u32::from_le_bytes([
self.buf[o + k],
self.buf[o + k + 1],
self.buf[o + k + 2],
self.buf[o + k + 3],
])
};
entry.buildings.push(BuildingAttrs {
height: u16_at(0),
min_height: u16_at(2),
roof_height: u16_at(4),
roof_shape: self.buf[o + 6],
roof_direction: self.buf[o + 7],
roof_orientation: self.buf[o + 8],
building_colour: u32_at(9),
roof_colour: u32_at(13),
});
self.used += BUILDING_BYTES;
consumed += BUILDING_BYTES;
}
}
// The carriageway section, last, when the flag is set: one [`CARRIAGEWAY_RECORD_LEN`]
// record per feature, dense.
if header.has_carriageways {
entry.carriageways.reserve(header.features);
for _ in 0..header.features {
self.fill(CARRIAGEWAY_RECORD_LEN)?;
let o = self.used;
entry.carriageways.push(Carriageway {
forward: self.buf[o],
backward: self.buf[o + 1],
solid_dividers: u32::from_le_bytes([
self.buf[o + 2],
self.buf[o + 3],
self.buf[o + 4],
self.buf[o + 5],
]),
});
self.used += CARRIAGEWAY_RECORD_LEN;
consumed += CARRIAGEWAY_RECORD_LEN;
}
}
self.left -= 1;
self.spill.read.fetch_add(consumed as u64, Ordering::Relaxed);
self.spill.read_entries.fetch_add(1, Ordering::Relaxed);
Ok(Some(((header.tile, header.layer_id), entry)))
}
/// Make at least `need` unread bytes available in `buf`.
fn fill(&mut self, need: usize) -> Result<()> {
if self.buf.len() - self.used >= need {
return Ok(());
}
if self.used > 0 {
self.buf.copy_within(self.used.., 0);
let keep = self.buf.len() - self.used;
self.buf.truncate(keep);
self.used = 0;
}
// An entry larger than the window is read whole rather than in pieces: the decode wants it
// contiguous, and one such entry is bounded by `MAX_ENTRY_BYTES`.
let want = need.max(self.window) - self.buf.len();
let take = want.min((self.end - self.at) as usize);
if self.buf.len() + take < need {
return err(format!(
"a tile chunk in {} ends {} byte(s) into a {need}-byte read",
self.spill.path.display(),
self.buf.len() + take
));
}
let base = self.buf.len();
self.buf.resize(base + take, 0);
match (&self.spill.file, &self.spill.anon) {
(Some(f), None) => {
tilecodec::pmtiles::read_exact_at(f, &mut self.buf[base..], self.at)
.map_err(|e| Error(format!("reading {}: {e}", self.spill.path.display())))?;
}
(None, Some(a)) => {
a.lock()
.expect("the anon tile chunk spill")
.read_at(self.at, &mut self.buf[base..])
.map_err(|e| Error(format!("reading {}: {e}", self.spill.path.display())))?;
}
_ => {
return err(format!(
"a tile chunk spill for {} has no backend",
self.spill.path.display()
))
}
}
self.at += take as u64;
Ok(())
}
/// Take back an entry whose arenas the merge has drained, to reuse on the next decode.
///
/// The merge hands this back the emptied [`ChunkEntry`] it just concatenated into its
/// accumulator; [`Self::next`] then fills these buffers instead of allocating fresh ones. Every
/// arena is cleared (so no stale value can leak into a later entry) but its capacity is kept,
/// which is the whole point. A single slot is enough: the merge consumes one of a reader's
/// entries and immediately decodes the next, so the buffers are back in hand exactly when the
/// next decode wants them.
pub fn recycle(&mut self, mut entry: ChunkEntry) {
entry.layer.features.clear();
entry.layer.parts.clear();
entry.layer.coords.clear();
entry.names.clear();
entry.ids.clear();
entry.turn_lanes.clear();
entry.buildings.clear();
entry.carriageways.clear();
self.spare = Some(entry);
}
}
/// How many bytes one entry encodes to, or an error when an arena is past what a `u32` addresses.
fn entry_bytes(entry: &ChunkEntry) -> Result {
let layer = &entry.layer;
let count = |what: &str, n: usize| -> Result {
if n > u32::MAX as usize {
return err(format!("a tile chunk entry holds {n} {what}, which no header can address"));
}
Ok(n)
};
let features = count("feature(s)", layer.features.len())?;
let parts = count("part(s)", layer.parts.len())?;
let coords = count("coordinate(s)", layer.coords.len())?;
let ids = count("id(s)", entry.ids.len())?;
let mut names_bytes = 0u64;
for name in &entry.names {
names_bytes += 4 + name.len() as u64;
}
let mut turns_bytes = 0u64;
for turns in &entry.turn_lanes {
turns_bytes += 2 + 2 * (turns.forward.len() as u64 + turns.backward.len() as u64);
}
// The building section: one fixed record per feature when present.
let buildings_bytes = if entry.buildings.is_empty() {
0
} else {
entry.buildings.len() as u64 * BUILDING_BYTES as u64
};
// And the carriageway section, likewise fixed-width and one per feature.
let carriageways_bytes = if entry.carriageways.is_empty() {
0
} else {
entry.carriageways.len() as u64 * CARRIAGEWAY_RECORD_LEN as u64
};
Ok(ENTRY_HEADER_BYTES as u64
+ payload_bytes(features, parts, coords, ids)
+ names_bytes
+ turns_bytes
+ buildings_bytes
+ carriageways_bytes)
}
/// The payload width implied by an entry's four counts. A pure function of the header, which is
/// what lets a reader validate before it allocates. Names ride after the fixed arenas and are
/// counted in the header's own `names` field.
fn payload_bytes(features: usize, parts: usize, coords: usize, ids: usize) -> u64 {
features as u64 * FEATURE_BYTES as u64
+ parts as u64 * PART_BYTES as u64
+ coords as u64 * COORD_BYTES as u64
+ ids as u64 * ID_BYTES as u64
}
include!("tilespill_part1_s1.rs");