Skip to content

Commit 9badb35

Browse files
Mavrick-Fclaude
andcommitted
fix(engine): count unread rows when a streamed export reads its layer whole
A streamed layer exported to a format written from a whole read went through `load_all`, which dropped the read report, so the notice of rows that couldn't be read never showed. The layer's `load_all_counted` returns the count with the dataset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a107a07 commit 9badb35

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎crates/engine/src/cmd/share.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn export_layer(s: &mut Session, p: &Value) -> CmdResult {
8484
// A streamed layer exports its whole source, not the features read for the view: a chunk at
8585
// a time where the source and format allow, so its size isn't limited by memory.
8686
let chunks = if l.is_streamed() && rustgis_data::export::streams(&ext) { l.chunk_export()? } else { None };
87-
let whole = if l.is_streamed() && chunks.is_none() { Some(l.load_all()?) } else { None };
87+
let (whole, unread) = if l.is_streamed() && chunks.is_none() { l.load_all_counted().map(|(ds, n)| (Some(ds), n))? } else { (None, 0) };
8888
let ds = chunks.as_ref().map(|(_, layer)| layer).or(whole.as_ref()).unwrap_or(&l.dataset);
8989
// GeoJSON, KML and GPX are longitude/latitude; the others keep the layer's own system unless
9090
// told another.
@@ -99,7 +99,7 @@ fn export_layer(s: &mut Session, p: &Value) -> CmdResult {
9999
}
100100
None => {
101101
let left_out = rustgis_data::io::write_path_in(ds, Path::new(&path), crs).map_err(|e| e.to_string())?;
102-
(ds.features.len().saturating_sub(left_out), left_out, 0)
102+
(ds.features.len().saturating_sub(left_out), left_out, unread)
103103
}
104104
};
105105
// FlatGeobuf, KML, GPX and Shapefile hold features with geometry only (a Shapefile, only

‎crates/engine/src/tests.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20123,6 +20123,29 @@ impl Drop for GpkgRegressionFixture {
2012320123
}
2012420124
}
2012520125

20126+
/// A streamed layer's export says how many of its source's rows couldn't be read, written a
20127+
/// chunk at a time (GeoJSON) or from a whole read (KML) alike.
20128+
#[cfg(not(target_arch = "wasm32"))]
20129+
#[test]
20130+
fn a_streamed_export_counts_the_rows_it_could_not_read() {
20131+
let fixture = GpkgRegressionFixture::new("unread-rows");
20132+
let path = fixture.path();
20133+
let db = rusqlite::Connection::open(&path).unwrap();
20134+
db.execute_batch("INSERT INTO roads (fid, geom, name) VALUES (4, X'4750000300000000DEAD', 'bad');").unwrap();
20135+
drop(db);
20136+
let mut s = Session::new(Project::default());
20137+
let loaded = s.run("layer.addData", &json!({"path": path, "stream": true})).unwrap();
20138+
let id = loaded["layer"].as_u64().unwrap() as u32;
20139+
assert!(s.map().layer(id).unwrap().is_streamed());
20140+
for ext in ["geojson", "kml"] {
20141+
let mark = s.notice_mark();
20142+
let exported = s.run("share.exportLayer", &json!({"layer": id, "path": fixture.dir.join(format!("out.{ext}"))})).unwrap();
20143+
assert_eq!(exported["features"].as_u64(), Some(3), "{ext}");
20144+
let notices: Vec<_> = s.notices_since(mark).iter().map(|n| n.text.clone()).collect();
20145+
assert!(notices.iter().any(|n| n.contains("1 rows couldn't be read")), "{ext}: {notices:?}");
20146+
}
20147+
}
20148+
2012620149
/// Whole reads must refuse an oversized provider instead of returning its capped sample (#1108).
2012720150
#[cfg(not(target_arch = "wasm32"))]
2012820151
#[test]

‎crates/map/src/lib.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,20 @@ impl Layer {
544544
/// Every feature of a streamed layer's source (up to the read limit), for tools and for
545545
/// loading the layer into memory.
546546
pub fn load_all(&self) -> Result<Dataset, String> {
547-
let Some(copy) = self.open_source()? else { return Ok(self.dataset.clone()) };
548-
let (mut ds, _) = copy.read_all()?;
547+
self.load_all_counted().map(|(ds, _)| ds)
548+
}
549+
550+
/// [`Layer::load_all`], with the number of rows of the source that couldn't be read.
551+
pub fn load_all_counted(&self) -> Result<(Dataset, usize), String> {
552+
let Some(copy) = self.open_source()? else { return Ok((self.dataset.clone(), 0)) };
553+
let (mut ds, report) = copy.read_all()?;
549554
ds.name = self.dataset.name.clone();
550555
// (A project that lists no fields keeps the source's, as `sync_source` gives it.)
551556
if !self.dataset.fields.is_empty() {
552557
ds.fields = self.dataset.fields.clone();
553558
}
554559
ds.source = self.dataset.source.clone();
555-
Ok(ds)
560+
Ok((ds, report.skipped))
556561
}
557562

558563
/// A streamed layer's source, to export whole a chunk at a time, with a dataset of no

0 commit comments

Comments
 (0)