Stream StudyCachables in QueryHelper - #7896
Merged
Merged
Conversation
labkey-jeckels
approved these changes
Jul 30, 2026
labkey-jeckels
left a comment
Contributor
There was a problem hiding this comment.
Very minor suggestions
| private final List<DatasetDefinition> _nullCohortDatasets; | ||
|
|
||
| private DatasetCollections(Collection<DatasetDefinition> collection) | ||
| private DatasetCollections(Map<Integer, DatasetDefinition> stream) |
Contributor
There was a problem hiding this comment.
Suggested change
| private DatasetCollections(Map<Integer, DatasetDefinition> stream) | |
| private DatasetCollections(Map<Integer, DatasetDefinition> map) |
| private DatasetCollections(Map<Integer, DatasetDefinition> stream) | ||
| { | ||
| super(collection); | ||
| super(stream); |
Contributor
There was a problem hiding this comment.
Suggested change
| super(stream); | |
| super(map); |
Comment on lines
+59
to
+65
| try (Stream<T> stream = getTableSelector(key).uncachedStream(_objectClass)) | ||
| { | ||
| return createCollections(Collections.unmodifiableMap(stream | ||
| .peek(StudyCachable::lock) | ||
| .toList() | ||
| )); | ||
| .collect(LabKeyCollectors.toLinkedMap(StudyCachable::getPrimaryKey, v -> v)) | ||
| )); | ||
| } |
Contributor
There was a problem hiding this comment.
Marginal benefit from closing the stream/ResultSet prior to creating the collections.
Suggested change
| try (Stream<T> stream = getTableSelector(key).uncachedStream(_objectClass)) | |
| { | |
| return createCollections(Collections.unmodifiableMap(stream | |
| .peek(StudyCachable::lock) | |
| .toList() | |
| )); | |
| .collect(LabKeyCollectors.toLinkedMap(StudyCachable::getPrimaryKey, v -> v)) | |
| )); | |
| } | |
| Map<K, T> map; | |
| try (Stream<T> stream = getTableSelector(key).uncachedStream(_objectClass)) | |
| { | |
| map = stream.peek(StudyCachable::lock) | |
| .collect(LabKeyCollectors.toLinkedMap(StudyCachable::getPrimaryKey, v -> v)) | |
| )); | |
| } | |
| return createCollections(Collections.unmodifiableMap(map)); |
Contributor
Author
There was a problem hiding this comment.
Yeah, that's exactly what I intended, but not what my fingers typed. Should be fixed up now.
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.
Rationale
QueryHelper()retrieved objects from the database via a cached stream (backed by a list), copied those objects into another list, and then enumerated the second list to create the map that it actually caches. This meant it (temporarily) held three in-memory copies of the collection at initialization time. Large studies (e.g., many study visits) also produced a warning at retrieval time. This switches to retrieving the objects via an uncached stream, and passing that stream through to the map-creation code path, eliminating the warning and two collection copies. It's still caching a (potentially large) map, but that's an acceptable trade-off for commonly used objects.