Repository navigation
fix(iterator): only query when the cache is empty - #484
Open
dudanogueira wants to merge 3 commits into
Open
dudanogueira wants to merge 3 commits into
dudanogueira wants to merge 3 commits into
Conversation
collection.iterator() sent one Search request per yielded object, fetching 100 objects and discarding 99. Refill the cache only once it is exhausted, so iterating N objects takes ceil(N / 100) + 1 requests. Fixes weaviate#483
There was a problem hiding this comment.
Orca Security Scan Summary
| Status | Check | Issues by priority | |
|---|---|---|---|
| Infrastructure as Code | View in Orca | ||
| SAST | View in Orca | ||
| Secrets | View in Orca | ||
| Vulnerabilities | View in Orca |
tsmith023
reviewed
Sep 30, 2026
Comment on lines
+18
to
+23
| if (this.cache.length == 0) { | ||
| return { | ||
| done: true, | ||
| value: undefined, | ||
| }; | ||
| } |
Member
There was a problem hiding this comment.
imo, the double nesting is somewhat confusing here
can we have it so that we do?
if (this.cache.length == 0) { this.cache = await fetch }
if (this.cache.length == 0) { return done }
Contributor
Author
There was a problem hiding this comment.
Done in df0b955, flattened to the two sequential checks as suggested. Thanks!
Move the iterator unit test to packages/test/node/iterator to follow the monorepo layout.
This branch has not been deployed
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.
Fixes #483
collection.iterator()sent oneSearchrequest for every object it returned. Each call tonext()fetched a page of 100 objects after the last UUID, kept only the first and threw the rest away. Results were still correct, but iterating over N objects took N round trips and moved about 100 × N objects over the wire, including vectors whenincludeVectoris set.Approach
The iterator already had a
cachefield; it was just being overwritten on every call. Nownext()only queries when the cache is empty, then returns objects from it one at a time, moving theaftercursor to the UUID of each object it returns. When a page is used up, the next query starts after the last object of that page. The loop still stops on the first empty page, so iterating over N objects takesceil(N / 100) + 1requests instead of N + 1.This doesn't stop early on a page with fewer than 100 objects. That would save the final empty request, but only by assuming the server always fills a page when more data exists. Stopping on an empty page is the rule the iterator already relied on.
Risks
(limit, after)sequence.next()calls earlier, so a change made mid-iteration may not show up until the next page. This is normal for cursor pagination.Testing
New
src/collections/iterator/unit.test.tsuses a mockedqueryfunction, so it needs no server:ceil(N / 100) + 1query calls.(100, undefined),(100, 'uuid-99'),(100, 'uuid-199'),(100, 'uuid-249').5 of the 7 cases fail on the previous implementation.