MCPcopy Create free account
hub / github.com/TanStack/db / updateRoutingIndex

Function updateRoutingIndex

packages/db/src/query/live/collection-config-builder.ts:1601–1735  ·  view source on GitHub ↗
(
  state: IncludesOutputState,
  correlationKey: unknown,
  childChanges: Map<unknown, Changes<any>>,
)

Source from the content-addressed store, hash-verified

1599}
1600
1601function updateRoutingIndex(
1602 state: IncludesOutputState,
1603 correlationKey: unknown,
1604 childChanges: Map<unknown, Changes<any>>,
1605): void {
1606 if (!state.nestedSetups) return
1607
1608 for (let i = 0; i < state.nestedSetups.length; i++) {
1609 const setup = state.nestedSetups[i]!
1610 let childToNested = setup.routingChildToNested.get(state)
1611 if (!childToNested) {
1612 childToNested = new Map()
1613 setup.routingChildToNested.set(state, childToNested)
1614 }
1615 for (const [childKey, change] of childChanges) {
1616 if (change.inserts > 0) {
1617 // Read the nested routing key from the INCLUDES_ROUTING stamp.
1618 // Must use the composite routing key (not raw correlationKey) to match
1619 // how nested buffers are keyed by computeRoutingKey.
1620 const nestedRouting =
1621 change.value[INCLUDES_ROUTING]?.[setup.compilationResult.fieldName]
1622 const nestedCorrelationKey = nestedRouting?.correlationKey
1623 const nestedParentContext = nestedRouting?.parentContext ?? null
1624 const nestedRoutingKey = computeRoutingKey(
1625 nestedCorrelationKey,
1626 nestedParentContext,
1627 )
1628
1629 // An update (inserts > 0 && deletes > 0) can change a child row's nested
1630 // correlation key (e.g. a price range's regionId changes). The change
1631 // only carries the NEW key, so drop the row's previous reference for
1632 // THIS setup using the recorded mapping before re-routing it.
1633 //
1634 // This relies on the compiler stamping the FULL INCLUDES_ROUTING map on
1635 // every emitted row (one entry per nested include field), so for an
1636 // unrelated nested include the recomputed nestedRoutingKey equals the
1637 // recorded one and the guard below is a no-op — a change to one nested
1638 // include never disturbs the recorded key of another on the same row.
1639 const perParent = childToNested.get(correlationKey)
1640 const prevNestedKey = perParent?.get(childKey)
1641 if (prevNestedKey !== undefined && prevNestedKey !== nestedRoutingKey) {
1642 removeChildKeyFromRoute(
1643 setup,
1644 state,
1645 correlationKey,
1646 prevNestedKey,
1647 childKey,
1648 )
1649 perParent!.delete(childKey)
1650 }
1651
1652 if (nestedCorrelationKey != null) {
1653 let stateRoutes = setup.routingIndex.get(nestedRoutingKey)
1654 if (!stateRoutes) {
1655 stateRoutes = new Map()
1656 setup.routingIndex.set(nestedRoutingKey, stateRoutes)
1657 }
1658 let parents = stateRoutes.get(state)

Callers 1

flushIncludesStateFunction · 0.85

Calls 7

computeRoutingKeyFunction · 0.85
removeChildKeyFromRouteFunction · 0.85
seedParentFromSnapshotFunction · 0.85
getMethod · 0.45
setMethod · 0.45
deleteMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected