Stream CsvDataTableStore.getRows from disk lazily and mandate closing the returned stream#8156
Merged
Merged
Conversation
… the returned stream readRows(...) buffered every matching CSV file fully into a List before returning list.stream(), so reading back a large data table held all of its rows in memory at once. It now streams rows lazily via a single Spliterator that keeps one file open at a time: rows are produced on demand, the file is closed the moment its last row is read (so a fully-drained stream self-closes), and closing the stream early also releases the open file. Because the returned stream now owns a file handle, DataTableStore.getRows is annotated @MustBeClosed (error_prone_annotations, added as compileOnly) so callers are flagged if they consume it without try-with-resources. All call sites are wrapped accordingly. Alternative to #7858.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed?
CsvDataTableStore.readRows(...)buffered every matching CSV file fully into aListbefore returninglist.stream(), so reading back a large data table held all of its rows in memory at once. It now streams rows lazily from disk via a singleSpliteratorthat keeps one file open at a time:Because the returned stream now owns a file handle,
DataTableStore.getRows(...)is annotated@MustBeClosed. The annotation is already on the compile classpath transitively (via Caffeine); it's added explicitly as acompileOnlydependency since it's CLASS-retention and not needed at runtime. The effect is that IntelliJ flags any call site that consumes the stream withouttry-with-resources, so a leak can't slip through unnoticed. Every call site in the repo is wrapped accordingly.@MustBeClosedcontract, rather thanflatMapover per-file resource-backed streams documented with a Javadoc note.What's your motivation?
Recipes — and the hosts that run them — can read their own data tables back, e.g. to export or aggregate them, and those tables can get very large (one row per method/class across a large repository). Buffering the entire table into a
Listbefore the consumer sees a single row makes peak memory scale with table size; streaming bounds the store side to one row at a time.Checklist