Repository navigation
feat(data): read GeoPackage, FlatGeobuf and GeoParquet a chunk at a time #1131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
430cada
d2cc31b
3186740
a2b4c28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,9 @@ pub struct GpkgSource { | |
| reproject: Reproject, | ||
| /// A copy written for a file opened from memory, removed when the source is dropped. | ||
| temp: Option<PathBuf>, | ||
| /// The database file and its identity when opened ([`file_identity`]), for a traversal on a | ||
| /// connection of its own to check it reads the same file. | ||
| file: Option<(PathBuf, Option<(u64, u64)>)>, | ||
| } | ||
|
|
||
| impl Drop for GpkgSource { | ||
|
|
@@ -69,7 +72,10 @@ fn feature_tables(conn: &Connection) -> Result<Vec<String>> { | |
|
|
||
| impl GpkgSource { | ||
| pub fn open(path: &Path, layer: Option<&str>) -> Result<Self> { | ||
| Self::open_conn(open_db(path)?, layer, None) | ||
| // The identity is taken before the database is opened: a file put in its place after | ||
| // this leaves it describing the file opened, which a traversal then refuses to swap. | ||
| let id = file_identity(path); | ||
| Self::open_conn(open_db(path)?, layer, None, id) | ||
| } | ||
|
|
||
| /// Opens a GeoPackage held in memory, through a temporary copy (SQLite reads files). The copy | ||
|
|
@@ -100,7 +106,8 @@ impl GpkgSource { | |
| return Err(e.into()); | ||
| } | ||
| drop(f); | ||
| match open_db(&p).and_then(|c| Self::open_conn(c, layer, Some(p.clone()))) { | ||
| let id = file_identity(&p); | ||
| match open_db(&p).and_then(|c| Self::open_conn(c, layer, Some(p.clone()), id)) { | ||
| Ok(s) => Ok(s), | ||
| Err(e) => { | ||
| let _ = std::fs::remove_file(&p); | ||
|
|
@@ -109,7 +116,8 @@ impl GpkgSource { | |
| } | ||
| } | ||
|
|
||
| fn open_conn(conn: Connection, layer: Option<&str>, temp: Option<PathBuf>) -> Result<Self> { | ||
| /// A source on an open database; `id` is the file's identity taken before it was opened. | ||
| fn open_conn(conn: Connection, layer: Option<&str>, temp: Option<PathBuf>, id: Option<(u64, u64)>) -> Result<Self> { | ||
| let tables = feature_tables(&conn)?; | ||
| let table = match layer { | ||
| Some(l) => tables | ||
|
|
@@ -212,7 +220,8 @@ impl GpkgSource { | |
| indexed: rtree.is_some(), | ||
| notes, | ||
| }; | ||
| let mut src = GpkgSource { info, conn: Mutex::new(Some(conn)), table, geom, pk, rtree, reproject, temp }; | ||
| let file = conn.path().filter(|p| !p.is_empty()).map(|p| (PathBuf::from(p), id)); | ||
| let mut src = GpkgSource { info, conn: Mutex::new(Some(conn)), table, geom, pk, rtree, reproject, temp, file }; | ||
| if src.info.geometry_type == GeometryType::None { | ||
| src.info.geometry_type = src.peek_kind(); | ||
| } | ||
|
|
@@ -368,6 +377,63 @@ impl FeatureSource for GpkgSource { | |
| } | ||
| Ok(out) | ||
| } | ||
|
|
||
| /// Every table in a file is scanned in full, whatever its index. | ||
| fn visits_chunks(&self) -> bool { | ||
| self.file.is_some() | ||
| } | ||
|
|
||
| /// A scan in table order: the envelope in each geometry's header tests the extent before | ||
| /// decoding, as a read without the spatial index does, with the same statement. It runs on a | ||
| /// read-only connection of its own, one statement and so one snapshot of the database, so | ||
| /// the map's reads of the layer go on and nothing is locked while the visitor runs. A file | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc is wrong about locking (Bugs, ~80% confidence). The scan's
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right: one open |
||
| /// put in place of the one opened is refused (where the system says files' identities). | ||
| fn visit_chunks(&self, extent: Option<Extent>, bytes: usize, visit: &mut dyn FnMut(Vec<Feature>) -> Result<()>) -> Result<Batch> { | ||
| let mut chunks = super::chunk_memory::Chunker::new(bytes, visit)?; | ||
| let Some((path, id)) = &self.file else { return Err(DataError::Format("GeoPackage: no file to read".into())) }; | ||
| let same_file = || { | ||
| if id.is_some() && file_identity(path) != *id { | ||
| return Err(DataError::Format(format!("{}: the file was replaced since it was added; add it again", path.display()))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading error for a deleted file (Bugs, low, ~55% confidence).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed: a removed file should fail as missing (the I/O error), keeping "replaced" for a real identity mismatch. That goes into the next pull request of this stack with a test, along with the locking wording above. |
||
| } | ||
| Ok(()) | ||
| }; | ||
| same_file()?; | ||
| let conn = open_db(path)?; | ||
| same_file()?; | ||
| let req = Request { extent, limit: usize::MAX }; | ||
| let native = extent.map(|e| self.reproject.extent_from_lonlat(&e)); | ||
| let sql = format!("SELECT {} FROM {}", self.select_list(), quote(&self.table)); | ||
| let mut st = conn.prepare(&sql).map_err(err)?; | ||
| let mut rows = st.query([]).map_err(err)?; | ||
| let mut out = Batch { complete: true, ..Batch::default() }; | ||
| while let Some(r) = rows.next().map_err(err)? { | ||
| match self.feature(r, native.as_ref(), &req).map_err(err)? { | ||
| Row::Feature(f) => { | ||
| out.matched += 1; | ||
| chunks.push(f)?; | ||
| } | ||
| Row::Skip => {} | ||
| Row::Bad => out.skipped += 1, | ||
| } | ||
| } | ||
| chunks.flush()?; | ||
| Ok(out) | ||
| } | ||
| } | ||
|
|
||
| /// A file's device and inode, which tell it from another file put at its path since; `None` | ||
| /// where the system doesn't say them. | ||
| fn file_identity(path: &Path) -> Option<(u64, u64)> { | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::MetadataExt; | ||
| std::fs::metadata(path).ok().map(|m| (m.dev(), m.ino())) | ||
| } | ||
| #[cfg(not(unix))] | ||
| { | ||
| let _ = path; | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// WGS 84 as OGC WKT, the coordinate system GeoPackages are written in. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance (medium confidence). For an extent over an indexed FlatGeobuf,
numbersholds 8 bytes per matched feature for the whole traversal, regardless of the chunk budget. On a large indexed file with a broad extent, that is an allocation proportional to the layer size, which cuts against the "exports no longer hold the whole layer" goal. The doc comment already says it holds the numbers, so this is a conscious trade-off. If it's acceptable, consider noting in thevisit_chunkscontract that the id list is unbounded. If not, the ids could be recomputed by walking the index per chunk.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept, and now stated in the trait contract too (3186740): "a provider may hold … an index's list of matches (FlatGeobuf, for an extent) besides". The export the next pull request adds reads with no extent, which builds no list; an extent visit holds 8 bytes per match, against about 355 bytes per decoded feature, as a read already does.