-
Notifications
You must be signed in to change notification settings - Fork 227
fix(db): deduplicate loadedSubsets and join key requests #1554
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
Open
v-anton
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
v-anton:fix/loaded-subsets-dedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@tanstack/db": patch | ||
| --- | ||
|
|
||
| fix(db): prevent unbounded loadedSubsets growth in subscription and join lazy loading |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { createLiveQueryCollection, eq } from '../../src/query/index.js' | ||
| import { createCollection } from '../../src/collection/index.js' | ||
| import { BasicIndex } from '../../src/indexes/basic-index.js' | ||
| import { extractSimpleComparisons } from '../../src/query/expression-helpers.js' | ||
| import { flushPromises } from '../utils.js' | ||
| import type { | ||
| ChangeMessageOrDeleteKeyMessage, | ||
| LoadSubsetOptions, | ||
| } from '../../src/types.js' | ||
|
|
||
| type Parent = { | ||
| id: number | ||
| name: string | ||
| } | ||
|
|
||
| type Child = { | ||
| id: number | ||
| parentId: number | ||
| title: string | ||
| } | ||
|
|
||
| const sampleParents: Array<Parent> = [ | ||
| { id: 1, name: `Parent A` }, | ||
| { id: 2, name: `Parent B` }, | ||
| { id: 3, name: `Parent C` }, | ||
| ] | ||
|
|
||
| const sampleChildren: Array<Child> = [ | ||
| { id: 10, parentId: 1, title: `Child A1` }, | ||
| { id: 11, parentId: 1, title: `Child A2` }, | ||
| { id: 20, parentId: 2, title: `Child B1` }, | ||
| ] | ||
|
|
||
| describe(`loadedSubsets deduplication`, () => { | ||
| function createParentsCollection() { | ||
| return createCollection<Parent>({ | ||
| id: `dedup-parents`, | ||
| getKey: (p) => p.id, | ||
| sync: { | ||
| sync: ({ begin, write, commit, markReady }) => { | ||
| begin() | ||
| for (const parent of sampleParents) { | ||
| write({ type: `insert`, value: parent }) | ||
| } | ||
| commit() | ||
| markReady() | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function createChildrenCollectionWithTracking() { | ||
| const loadSubsetCalls: Array<LoadSubsetOptions> = [] | ||
|
|
||
| const collection = createCollection<Child>({ | ||
| id: `dedup-children`, | ||
| getKey: (child) => child.id, | ||
| syncMode: `on-demand`, | ||
| autoIndex: `eager`, | ||
| defaultIndexType: BasicIndex, | ||
| sync: { | ||
| sync: ({ begin, write, commit, markReady }) => { | ||
| begin() | ||
| for (const child of sampleChildren) { | ||
| write({ type: `insert`, value: child }) | ||
| } | ||
| commit() | ||
| markReady() | ||
| return { | ||
| loadSubset: vi.fn((options: LoadSubsetOptions) => { | ||
| loadSubsetCalls.push(options) | ||
| return Promise.resolve() | ||
| }), | ||
| } | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| return { collection, loadSubsetCalls } | ||
| } | ||
|
|
||
| it(`should not grow loadedSubsets when requestSnapshot is called with the same predicate`, async () => { | ||
| const parents = createParentsCollection() | ||
| const { collection: children, loadSubsetCalls } = | ||
| createChildrenCollectionWithTracking() | ||
|
|
||
| const liveQuery = createLiveQueryCollection((q) => | ||
| q | ||
| .from({ p: parents }) | ||
| .join({ c: children }, ({ p, c }) => eq(c.parentId, p.id)) | ||
| .select(({ p, c }) => ({ | ||
| parentId: p.id, | ||
| parentName: p.name, | ||
| childId: c.id, | ||
| childTitle: c.title, | ||
| })), | ||
| ) | ||
|
|
||
| await liveQuery.preload() | ||
|
|
||
| const initialCallCount = loadSubsetCalls.length | ||
| expect(initialCallCount).toBeGreaterThan(0) | ||
|
|
||
| const firstCall = loadSubsetCalls[0]! | ||
| expect(firstCall.where).toBeDefined() | ||
|
|
||
| const filters = extractSimpleComparisons(firstCall.where) | ||
| expect(filters).toEqual([ | ||
| { | ||
| field: [`parentId`], | ||
| operator: `in`, | ||
| value: expect.arrayContaining([1, 2, 3]), | ||
| }, | ||
| ]) | ||
|
|
||
| // Trigger a second preload — this re-runs the pipeline with the same | ||
| // predicates, so the dedup layer should prevent any new loadSubset calls. | ||
| await liveQuery.preload() | ||
|
|
||
| expect(loadSubsetCalls.length).toBe(initialCallCount) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it(`should deduplicate join key requests across pipeline batches`, async () => { | ||
| let parentBegin: () => void | ||
| let parentWrite: (msg: ChangeMessageOrDeleteKeyMessage<Parent, number>) => void | ||
| let parentCommit: () => void | ||
|
|
||
| const parents = createCollection<Parent>({ | ||
| id: `dedup-parents-sync`, | ||
| getKey: (p) => p.id, | ||
| sync: { | ||
| sync: ({ begin, write, commit, markReady }) => { | ||
| parentBegin = begin | ||
| parentWrite = write | ||
| parentCommit = commit | ||
|
|
||
| begin() | ||
| for (const parent of sampleParents) { | ||
| write({ type: `insert`, value: parent }) | ||
| } | ||
| commit() | ||
| markReady() | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const { collection: children, loadSubsetCalls } = | ||
| createChildrenCollectionWithTracking() | ||
|
|
||
| const liveQuery = createLiveQueryCollection((q) => | ||
| q | ||
| .from({ p: parents }) | ||
| .join({ c: children }, ({ p, c }) => eq(c.parentId, p.id)) | ||
| .select(({ p, c }) => ({ | ||
| parentId: p.id, | ||
| parentName: p.name, | ||
| childId: c.id, | ||
| childTitle: c.title, | ||
| })), | ||
| ) | ||
|
|
||
| await liveQuery.preload() | ||
|
|
||
| const callCountAfterPreload = loadSubsetCalls.length | ||
|
|
||
| parentBegin!() | ||
| parentWrite!({ type: `insert`, value: { id: 4, name: `Parent D` } }) | ||
| parentCommit!() | ||
| await flushPromises() | ||
|
|
||
| const newCalls = loadSubsetCalls.slice(callCountAfterPreload) | ||
| expect(newCalls.length).toBeGreaterThan(0) | ||
|
|
||
| let inFilterCount = 0 | ||
| for (const call of newCalls) { | ||
| if (!call.where) continue | ||
| const filters = extractSimpleComparisons(call.where) | ||
| for (const filter of filters) { | ||
| if (filter.operator === `in`) { | ||
| inFilterCount++ | ||
| const values = filter.value as Array<number> | ||
| expect(values).toContain(4) | ||
| expect(values).not.toContain(1) | ||
| expect(values).not.toContain(2) | ||
| expect(values).not.toContain(3) | ||
| } | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| expect(inFilterCount).toBeGreaterThan(0) | ||
| }) | ||
| }) | ||
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.
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.
loadedJoinKeysis updated before async load success, which can suppress needed retries.requestSnapshotcan returntrueeven if the underlying asyncloadSubsetlater rejects. Adding keys immediately means failed keys won’t be retried in later batches.Suggested fix
🤖 Prompt for AI Agents