Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/data/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ fn bounded(text: &str, limit: usize) -> String {
}

/// `text` cut to `limit` bytes at a character, an ellipsis marking the cut.
fn cut_with_ellipsis(text: &mut String, limit: usize) {
pub(crate) fn cut_with_ellipsis(text: &mut String, limit: usize) {
if text.len() > limit {
let mut end = limit.saturating_sub(3);
while !text.is_char_boundary(end) {
Expand Down
79 changes: 66 additions & 13 deletions crates/data/src/esripbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const MAX_TEXT_BYTES: usize = 512 << 20;
fn row_bytes(features: usize, fields: usize) -> usize {
use std::mem::size_of;
// Declared, kept or not, as a column, a field and a name to tell repeats by.
let field = size_of::<(String, u64, bool)>() + size_of::<Option<(usize, Kind)>>() + size_of::<Field>() + size_of::<String>();
let field = size_of::<Declared>() + size_of::<Option<(usize, Kind)>>() + size_of::<Field>() + size_of::<String>();
let row = size_of::<crate::Feature>().saturating_add(fields.saturating_mul(size_of::<Value>()));
features.saturating_mul(row).saturating_add(fields.saturating_mul(field))
}
Expand Down Expand Up @@ -92,17 +92,29 @@ const LAST_FIELD_TYPE: u64 = 16;
pub struct Page {
/// Its features, in longitude/latitude.
pub dataset: Dataset,
/// What was left out or assumed while reading it.
/// What was left out or assumed while reading it, but for what its features lost
/// ([`losses`](Self::losses)).
pub report: ReadReport,
/// The service holds more features than it sent (`exceededTransferLimit`): the next page
/// starts after these.
pub exceeded_transfer_limit: bool,
/// The field holding the features' object ids (`esriFieldTypeOID`), by its place in
/// `dataset.fields`.
pub object_id_field: Option<usize>,
/// What its features lost, counted (also said in `report`): a reader joining pages sums them
/// and says each once ([`Losses::messages`]).
/// What its features lost, counted: a reader joining pages sums them and says each once
/// ([`Losses::messages`]), as [`read`] says a single page's.
pub losses: Losses,
/// About the memory its fields, rows, text values and geometries take, in bytes, as counted
/// against [`MAX_ROW_BYTES`], [`MAX_TEXT_BYTES`] and [`MAX_GEOMETRY_BYTES`]: a reader joining
/// pages sums them against a budget of its own.
pub memory: usize,
/// Features it holds past the limit it was read within ([`read_page_within`]), not read: what
/// they lost isn't counted.
pub left_out: usize,
/// A hash of how many features it holds and of its first and last features as they were sent,
/// taken before any limit: a reader of pages tells a page sent again by it when the features
/// have no object id.
pub fingerprint: u64,
}

/// What a page's features lost or had assumed, counted.
Expand Down Expand Up @@ -133,7 +145,7 @@ impl Losses {
self.with_zm = self.with_zm.saturating_add(other.with_zm);
}

/// What they say, one message per kind (as [`read_page`] says them).
/// What they say, one message per kind (as [`read`] says them).
pub fn messages(&self) -> Vec<String> {
let mut out = Vec::new();
if self.no_crs {
Expand Down Expand Up @@ -167,6 +179,7 @@ impl Losses {
/// Reads an answer as a layer named `name`, saying when the service held more features than it sent.
pub fn read(bytes: &[u8], name: &str) -> Result<(Dataset, ReadReport)> {
let mut page = read_page(bytes, name)?;
page.report.messages.extend(page.losses.messages());
if page.exceeded_transfer_limit {
page.report.messages.push(format!(
"the service sent {} features only (its record limit); the rest aren't in this answer",
Expand All @@ -186,6 +199,13 @@ pub fn is_esripbf(bytes: &[u8]) -> bool {

/// Reads one page of an answer as a layer named `name`.
pub fn read_page(bytes: &[u8], name: &str) -> Result<Page> {
read_page_within(bytes, name, usize::MAX)
}

/// Reads one page of an answer as a layer named `name`, its first `limit` features only (the
/// rest are counted in [`Page::left_out`]): a reader joining pages up to a number of features
/// counts what those it keeps lost, and no others.
pub fn read_page_within(bytes: &[u8], name: &str, limit: usize) -> Result<Page> {
if bytes.is_empty() {
return Err(invalid("the answer is empty: the service refused the query (ask for it with f=json to see why)"));
}
Expand Down Expand Up @@ -227,6 +247,18 @@ pub fn read_page(bytes: &[u8], name: &str) -> Result<Page> {
return Err(too_many_rows(features.len(), declared.len()));
}
}
let fingerprint = {
use std::hash::Hasher as _;
let mut hash = std::collections::hash_map::DefaultHasher::new();
hash.write_usize(features.len());
for f in [features.first(), features.last()].into_iter().flatten() {
hash.write(f);
}
hash.finish()
};
let left_out = features.len().saturating_sub(limit);
features.truncate(limit);
let rows = row_bytes(features.len(), declared.len());
// A point layer's type is the format's default, which an answer leaves out.
let shape_kind = geometry_type.unwrap_or(POINT);
// Refused whether or not a feature has coordinates, so such a layer isn't read as empty.
Expand All @@ -243,7 +275,7 @@ pub fn read_page(bytes: &[u8], name: &str) -> Result<Page> {
let mut names = std::collections::HashSet::new();
let (mut repeated, mut not_read, mut unknown) = (0usize, 0usize, 0usize);
let mut object_id_field = None;
for (name, number, utf8) in declared {
for Declared { name, number, utf8, alias } in declared {
tally.not_utf8 += usize::from(!utf8);
let Some(read_as) = kind(number) else {
not_read += 1;
Expand All @@ -261,7 +293,7 @@ pub fn read_page(bytes: &[u8], name: &str) -> Result<Page> {
object_id_field = Some(fields.len());
}
columns.push(Some((fields.len(), read_as)));
fields.push(Field::new(name, read_as.field_type()));
fields.push(Field { name, ty: read_as.field_type(), alias });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
if repeated > 0 {
report.messages.push(format!("{repeated} fields repeat an earlier field's name and are left out"));
Expand Down Expand Up @@ -331,9 +363,11 @@ pub fn read_page(bytes: &[u8], name: &str) -> Result<Page> {
unprojected,
with_zm,
};
report.messages.extend(losses.messages());
ds.zm_dropped = with_zm > 0;
Ok(Page { dataset: ds, report, exceeded_transfer_limit: more, object_id_field, losses })
let memory = rows
.saturating_add(MAX_GEOMETRY_BYTES.saturating_sub(tally.geometry_room))
.saturating_add(MAX_TEXT_BYTES.saturating_sub(tally.text_room));
Ok(Page { dataset: ds, report, exceeded_transfer_limit: more, object_id_field, losses, memory, left_out, fingerprint })
}

/// A reader of protocol buffer fields, which refuses what runs past its bytes.
Expand Down Expand Up @@ -575,10 +609,22 @@ fn read_transform(bytes: &[u8]) -> Result<Transform> {
Ok(t)
}

/// A field's name, its type's number (`FieldType`) and whether the name was valid UTF-8.
/// The name is taken from `tally`'s text room before it's made, twice: it's kept lowercase too.
fn field(bytes: &[u8], tally: &mut Tally) -> Result<(String, u64, bool)> {
/// A field as an answer declares it.
struct Declared {
name: String,
/// Its type's number (`FieldType`).
number: u64,
/// Whether its name and alias were valid UTF-8.
utf8: bool,
/// None when empty or the name itself.
alias: Option<String>,
}

/// A field as declared. The name is taken from `tally`'s text room before it's made, twice: it's
/// kept lowercase too; the alias once.
fn field(bytes: &[u8], tally: &mut Tally) -> Result<Declared> {
let (mut name, mut number, mut utf8) = (String::new(), 0, true);
let (mut alias, mut alias_utf8) = (String::new(), true);
let mut r = Reader::new(bytes);
while !r.done() {
match r.key()? {
Expand All @@ -589,10 +635,17 @@ fn field(bytes: &[u8], tally: &mut Tally) -> Result<(String, u64, bool)> {
name = String::from_utf8_lossy(raw).into_owned();
}
(2, 0) => number = r.varint()?,
(3, 2) => {
let raw = r.message()?;
tally.spend_text(lossy_len(raw))?;
alias_utf8 = std::str::from_utf8(raw).is_ok();
alias = String::from_utf8_lossy(raw).into_owned();
}
(_, wire) => r.skip(wire)?,
}
}
Ok((name, number, utf8))
let alias = Some(alias).filter(|a| !a.is_empty() && *a != name);
Ok(Declared { name, number, utf8: utf8 && alias_utf8, alias })
}

/// The code of an answer's coordinate system (its `latestWkid` when RustGIS knows it, else its
Expand Down
49 changes: 48 additions & 1 deletion crates/data/src/esripbf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::raster::Crs;
// Encoding, as `FeatureCollection.proto` lays answers out: shared with the engine's tests.
#[path = "../../../../tests/support/esripbf.rs"]
mod encode;
pub(crate) use encode::page;
pub(crate) use encode::{page, page_with_z};
// Named, so they come before the reader's own functions of the same names (`field`, `feature`).
use encode::{
answer, double, feature, field, geometry, lonlat, message, null, number, packed, real, single, sint64, text, transform, uint, uint64,
Expand Down Expand Up @@ -166,6 +166,53 @@ fn z_and_m_are_dropped_and_said_even_when_they_are_all_alike() {
assert_eq!(report.messages, [crate::zm_note(Some(2))]);
}

#[test]
fn field_aliases_are_kept_unless_empty_or_the_name() {
let declared =
|name: &str, alias: &str| message(13, &[message(1, name.as_bytes()), number(2, 4), message(3, alias.as_bytes())].concat());
let bytes = answer(&[
lonlat(),
transform(false, 1.0, 0.0, 0.0),
declared("POP_2020", "Population 2020"),
declared("NAME", "NAME"),
declared("CODE", ""),
field("PLAIN", 4),
feature(&[geometry(&[], &[1, 2])]),
]);
let (ds, report) = read(&bytes, "t").unwrap();
let aliases: Vec<_> = ds.fields.iter().map(|f| f.alias.as_deref()).collect();
assert_eq!(aliases, [Some("Population 2020"), None, None, None]);
assert_eq!(ds.fields[0].display(), "Population 2020");
assert!(report.messages.is_empty(), "{:?}", report.messages);
// An alias that isn't UTF-8 is read as it can be, and said.
let bytes = answer(&[lonlat(), message(13, &[message(1, b"A"), number(2, 4), message(3, b"Pop\xff")].concat())]);
let (ds, report) = read(&bytes, "t").unwrap();
assert_eq!(ds.fields[0].alias.as_deref(), Some("Pop\u{fffd}"));
assert!(report.messages.iter().any(|m| m.contains("aren't valid UTF-8")), "{:?}", report.messages);
}

#[test]
fn a_page_read_within_a_limit_counts_what_the_features_kept_lost() {
let bytes = answer(&[
number(10, 1),
lonlat(),
transform(false, 1.0, 0.0, 0.0),
feature(&[geometry(&[], &[4, 6, 100])]),
feature(&[geometry(&[], &[1, 1, 100])]),
feature(&[geometry(&[], &[2, 2, 100])]),
]);
let page = read_page_within(&bytes, "t", 1).unwrap();
assert_eq!((page.dataset.features.len(), page.left_out, page.losses.with_zm), (1, 2, 1));
assert_eq!(page.losses.messages(), [crate::zm_note(Some(1))]);
assert!(page.report.messages.is_empty(), "the losses are said apart: {:?}", page.report.messages);
// Within a limit it doesn't reach, the whole page.
let page = read_page_within(&bytes, "t", 3).unwrap();
assert_eq!((page.dataset.features.len(), page.left_out, page.losses.with_zm), (3, 0, 3));
let all = read_page(&bytes, "t").unwrap();
assert_eq!((all.left_out, all.memory), (0, page.memory));
assert!(read_page_within(&bytes, "t", 1).unwrap().memory < all.memory);
}

#[test]
fn web_mercator_answers_are_turned_to_longitude_latitude() {
let bytes = answer(&[
Expand Down
Loading
Loading