-
Notifications
You must be signed in to change notification settings - Fork 131
fix(db): ensure deterministic iteration order for collections and indexes #958
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
Conversation
🦋 Changeset detectedLatest commit: 3181eee The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: +176 B (+0.2%) Total Size: 87.3 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.35 kB ℹ️ View Unchanged
|
0c47d64 to
5f4b251
Compare
kevin-dp
left a comment
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.
Perfect! Love how focused this PR is with small surgical fixes.
1047564 to
4c5cda6
Compare
5f4b251 to
60c1709
Compare
…exes - SortedMap: add key-based tie-breaking for deterministic ordering - SortedMap: optimize to skip value comparison when no comparator provided - BTreeIndex: sort keys within same indexed value for deterministic order - BTreeIndex: add fast paths for empty/single-key sets - CollectionStateManager: always use SortedMap for deterministic iteration - Extract compareKeys utility to utils/comparison.ts - Add comprehensive tests for deterministic ordering behavior
88ec7fd to
7bfa01d
Compare
Summary
This PR ensures deterministic iteration order for collections and indexes when multiple items have equal sort values. It builds on #957 which added stable tie-breaking for ORDER BY operations in the query engine.
Changes:
SortedMapforsyncedData, ensuring deterministic iteration even without a customcomparefunctioncompareKeystoutils/comparison.tsfor reuse across modulesProblem
When multiple rows share the same indexed value (e.g., same priority), their iteration order was previously non-deterministic:
SortedMapreturned any position among equal valuesBTreeIndex.valueMapusedSetwhich iterates in insertion ordercomparefunction usedMapwith insertion-order iterationThis caused issues with live queries using
orderBy+limitwhere window operations (setWindow) could produce inconsistent results.Solution
All three components now use key-based tie-breaking to ensure deterministic ordering:
compareKeys(a, b)Test plan
deterministic-ordering.test.ts) covering:take()/takeReversed()with equal indexed valuescomparefunctioncurrentStateAsChangeswithorderByandlimitNote: This PR is stacked on #957 and should be merged after it.