@rocicorp/zero 1.13.0-canary.2 → 1.13.0-canary.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/out/z2s/src/compiler.d.ts.map +1 -1
  2. package/out/z2s/src/compiler.js +6 -6
  3. package/out/z2s/src/compiler.js.map +1 -1
  4. package/out/zero/package.js +1 -1
  5. package/out/zero/package.js.map +1 -1
  6. package/out/zero-cache/src/server/replicator.js +3 -3
  7. package/out/zero-cache/src/server/replicator.js.map +1 -1
  8. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +14 -4
  9. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  10. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +9 -0
  11. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  12. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +23 -1
  13. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  14. package/out/zero-cache/src/services/replicator/write-worker-client.js +17 -3
  15. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  16. package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
  17. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  18. package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
  19. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  20. package/out/zero-client/src/client/custom.js +1 -1
  21. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  22. package/out/zero-client/src/client/version.js +1 -1
  23. package/out/zero-client/src/client/zero.js +1 -1
  24. package/out/zero-protocol/src/ast.d.ts +2 -2
  25. package/out/zero-protocol/src/ast.js +2 -2
  26. package/out/zero-protocol/src/ast.js.map +1 -1
  27. package/out/zero-protocol/src/down.d.ts +27 -27
  28. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  29. package/out/zql/src/ivm/aggregate.js +5 -4
  30. package/out/zql/src/ivm/aggregate.js.map +1 -1
  31. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  32. package/out/zql/src/ivm/cap.js +116 -63
  33. package/out/zql/src/ivm/cap.js.map +1 -1
  34. package/out/zql/src/ivm/deferred-input.js +1 -1
  35. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  36. package/out/zql/src/ivm/exists.js +136 -73
  37. package/out/zql/src/ivm/exists.js.map +1 -1
  38. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  39. package/out/zql/src/ivm/fan-in.js +3 -3
  40. package/out/zql/src/ivm/fan-in.js.map +1 -1
  41. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  42. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  43. package/out/zql/src/ivm/fan-out.js +47 -12
  44. package/out/zql/src/ivm/fan-out.js.map +1 -1
  45. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  46. package/out/zql/src/ivm/filter-operators.js +98 -13
  47. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  48. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  49. package/out/zql/src/ivm/filter-push.js +5 -13
  50. package/out/zql/src/ivm/filter-push.js.map +1 -1
  51. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  52. package/out/zql/src/ivm/filter.js +3 -2
  53. package/out/zql/src/ivm/filter.js.map +1 -1
  54. package/out/zql/src/ivm/flipped-join.js +1 -0
  55. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  56. package/out/zql/src/ivm/group-by.js +1 -1
  57. package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
  58. package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
  59. package/out/zql/src/ivm/join-index-tap.js +9 -7
  60. package/out/zql/src/ivm/join-index-tap.js.map +1 -1
  61. package/out/zql/src/ivm/join.d.ts.map +1 -1
  62. package/out/zql/src/ivm/join.js +72 -67
  63. package/out/zql/src/ivm/join.js.map +1 -1
  64. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  65. package/out/zql/src/ivm/memory-source.js +163 -57
  66. package/out/zql/src/ivm/memory-source.js.map +1 -1
  67. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  68. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  69. package/out/zql/src/ivm/push-accumulated.js +23 -28
  70. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  71. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  72. package/out/zql/src/ivm/skip.js +6 -9
  73. package/out/zql/src/ivm/skip.js.map +1 -1
  74. package/out/zql/src/ivm/stream.d.ts +58 -0
  75. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  76. package/out/zql/src/ivm/stream.js +256 -1
  77. package/out/zql/src/ivm/stream.js.map +1 -1
  78. package/out/zql/src/ivm/take-gate.d.ts +1 -1
  79. package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
  80. package/out/zql/src/ivm/take-gate.js +11 -21
  81. package/out/zql/src/ivm/take-gate.js.map +1 -1
  82. package/out/zql/src/ivm/take.d.ts.map +1 -1
  83. package/out/zql/src/ivm/take.js +243 -217
  84. package/out/zql/src/ivm/take.js.map +1 -1
  85. package/out/zql/src/query/query.d.ts +4 -4
  86. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"take.js","names":[],"sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeRemoveChange,\n type Change,\n type EditChange,\n} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, valueToKey, type Comparator, type Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport type {TakeBoundProvider, TakeGate} from './take-gate.ts';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: string): TakeState | undefined;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n destroy(): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\ntype DirtyPartitionState = {\n constraint: Constraint | undefined;\n /**\n * The bound to report to upstream TakeGate via getBound() until Phase 2\n * reconciliation completes.\n *\n * When removals reduce partition size below limit in Phase 1, normal getBound()\n * would return undefined (unbounded), which would cause upstream TakeGate to open\n * and allow unbounded parent pushes through. Preserving gateBound keeps TakeGate\n * capped at the pre-removal boundary until Phase 2 refills the deficit.\n */\n gateBound: Row | undefined;\n};\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #takeGate: TakeGate | undefined;\n /**\n * Partitions that incurred removals in Phase 1 and require deficit refills in\n * Phase 2 (reconcile). Keyed by `takeStateKey` (see `getTakeStateKey`), matching\n * `#storage` keys: `'[\"take\"]'` when unpartitioned, `'take:' + valueToKey(value)` for one\n * partition column, or `'[\"take\", ...partitionValues]'` for more.\n */\n readonly #dirtyPartitions = new Map<string, DirtyPartitionState>();\n\n #output: Output = throwOutput;\n\n setTakeGate(gate: TakeGate): void {\n this.#takeGate = gate;\n }\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Take requires sorted input');\n assertOrderingIncludesPK(sort, input.getSchema().primaryKey);\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (\n this.#partitionKey &&\n !constraintContainsPartitionKey(constraint, this.#partitionKey)\n ) {\n return undefined;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, constraint);\n const dirty = this.#dirtyPartitions.get(takeStateKey);\n if (dirty) {\n // While dirty in Phase 1, return gateBound (the bound prior to removals) so\n // upstream operators (e.g. TakeGate) remain capped rather than unbounding during push.\n return dirty.gateBound;\n }\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState || takeState.size < this.#limit) {\n return undefined;\n }\n return takeState.bound;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintContainsPartitionKey(req.constraint, this.#partitionKey)),\n 'Partitioned take does not allow unpartitioned fetches',\n );\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) {\n yield* this.#initialFetch(req, takeStateKey);\n }\n return;\n }\n if (takeState.bound === undefined) {\n return;\n }\n let count = 0;\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) {\n return;\n }\n if (\n this.#rowHiddenFromFetch &&\n this.getSchema().compareRows(\n this.#rowHiddenFromFetch,\n inputNode.row,\n ) === 0\n ) {\n continue;\n }\n yield inputNode;\n count++;\n if (count >= this.#limit) {\n return;\n }\n }\n }\n\n *#initialFetch(\n req: FetchRequest,\n takeStateKey: string,\n ): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n // The caller (fetch) has already checked that there is no state for\n // takeStateKey.\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(takeStateKey, size, bound);\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let constraint: Constraint | undefined;\n if (takeState) {\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n constraint: Constraint | undefined;\n };\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (takeState.size < this.#limit) {\n if (this.#dirtyPartitions.has(takeStateKey)) {\n // While dirty with an unresolved deficit in Phase 1, only admit rows that\n // sort strictly before the current window boundary. We refuse to expand the\n // bound forward during Phase 1 so that Phase 2 can backfill earlier candidate\n // rows from storage in sort order.\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n }\n const nextSize = takeState.size + 1;\n if (nextSize === this.#limit) {\n this.#dirtyPartitions.delete(takeStateKey);\n }\n this.#setTakeState(\n takeStateKey,\n nextSize,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change[ChangeIndex.NODE].row) < 0\n ? change[ChangeIndex.NODE].row\n : takeState.bound,\n );\n yield* this.#output.push(change, this);\n return;\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n // added row < bound\n let beforeBoundNode: Node | undefined;\n let boundNode: Node | undefined;\n if (this.#limit === 1) {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n boundNode = node;\n break;\n }\n } else {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (boundNode === undefined) {\n boundNode = node;\n } else {\n beforeBoundNode = node;\n break;\n }\n }\n }\n assert(\n boundNode !== undefined,\n 'Take: boundNode must be found during fetch',\n );\n const removeChange = makeRemoveChange(boundNode);\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0\n ? change[ChangeIndex.NODE].row\n : beforeBoundNode.row,\n );\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n removeChange,\n );\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (takeState.bound === undefined) {\n // change is after bound\n return;\n }\n const compToBound = compareRows(\n change[ChangeIndex.NODE].row,\n takeState.bound,\n );\n if (compToBound > 0 || (compToBound < 0 && this.#limit === 1)) {\n // change is not in window\n return;\n }\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n\n const finalBound =\n takeState.size - 1 === 0\n ? undefined\n : compToBound < 0\n ? takeState.bound\n : beforeBoundNode?.row;\n if (!this.#dirtyPartitions.has(takeStateKey)) {\n // Snapshot the pre-removal bound as the gate bound on the first removal that\n // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return\n // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits.\n this.#dirtyPartitions.set(takeStateKey, {\n constraint,\n gateBound: takeState.bound,\n });\n }\n this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) {\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0\n ) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n *#pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.OLD_NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(\n change[ChangeIndex.OLD_NODE].row,\n takeState.bound,\n );\n const newCmp = compareRows(change[ChangeIndex.NODE].row, takeState.bound);\n\n const replaceBoundAndForwardChange = () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n change[ChangeIndex.NODE].row,\n );\n return this.#output.push(change, this);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n yield* this.#output.push(change, this);\n return;\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n assert(\n beforeBoundNode !== undefined,\n 'Take: beforeBoundNode must be found during fetch',\n );\n\n this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row);\n yield* this.#output.push(change, this);\n return;\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n let newBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n newBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n newBoundNode.row,\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n );\n yield* this.#output.push(makeAddChange(newBoundNode), this);\n return;\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n let oldBoundNode: Node | undefined;\n let newBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (oldBoundNode === undefined) {\n oldBoundNode = node;\n } else {\n newBoundNode = node;\n break;\n }\n }\n assert(\n oldBoundNode !== undefined,\n 'Take: oldBoundNode must be found during fetch',\n );\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n makeRemoveChange(oldBoundNode),\n );\n yield* this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this);\n\n return;\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n yield* this.#output.push(change, this);\n return;\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n let afterBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n afterBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n afterBoundNode !== undefined,\n 'Take: afterBoundNode must be found during fetch',\n );\n\n // The new row is the new bound. Use an edit change.\n if (compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n yield* this.#output.push(\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n this,\n );\n this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row);\n yield* this.#output.push(makeAddChange(afterBoundNode), this);\n return;\n }\n\n unreachable();\n }\n\n *#pushWithRowHiddenFromFetch(row: Row, change: Change) {\n this.#rowHiddenFromFetch = row;\n try {\n yield* this.#output.push(change, this);\n } finally {\n this.#rowHiddenFromFetch = undefined;\n }\n }\n\n #setTakeState(takeStateKey: string, size: number, bound: Row | undefined) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n *reconcile(_pusher?: InputBase): Stream<'yield'> {\n if (this.#dirtyPartitions.size > 0) {\n const dirty = [...this.#dirtyPartitions.entries()];\n this.#dirtyPartitions.clear();\n\n for (const [takeStateKey, {constraint}] of dirty) {\n const takeState = this.#storage.get(takeStateKey);\n assert(\n takeState !== undefined,\n 'Take: dirty partition must exist in storage',\n );\n const deficit = this.#limit - takeState.size;\n assert(deficit > 0, 'Take: dirty partition must have a deficit');\n\n const toPush: Node[] = [];\n this.#takeGate?.open();\n try {\n const stream = this.#input.fetch({\n start: takeState.bound\n ? {\n row: takeState.bound,\n basis: 'after',\n }\n : undefined,\n constraint,\n });\n\n for (const node of stream) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n toPush.push(node);\n if (toPush.length === deficit) {\n break;\n }\n }\n } finally {\n this.#takeGate?.close();\n }\n\n let currentSize = takeState.size;\n for (const node of toPush) {\n currentSize++;\n this.#setTakeState(takeStateKey, currentSize, node.row);\n yield* this.#output.push(makeAddChange(node), this);\n }\n }\n }\n\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n if (partitionKey?.length === 1 && rowOrConstraint) {\n return 'take:' + valueToKey(rowOrConstraint[partitionKey[0]]);\n }\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nexport function constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function constraintContainsPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function makePartitionKeyComparator(\n partitionKey: PartitionKey,\n): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmEA,IAAa,OAAb,MAAyD;CACvD;CACA;CACA;CACA;CACA;CAEA;CAEA;;;;;;;CAOA,mCAA4B,IAAI,IAAiC;CAEjE,UAAkB;CAElB,YAAY,MAAsB;EAChC,KAAK,YAAY;CACnB;CAEA,YACE,OACA,SACA,OACA,cACA;E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AAM;EACf,KAAK,MAAM,OAAO,cAAc;GAC9B,MAAM,MAAM,cAAc,EAAE,MAAM,EAAE,IAAI;GACxC,IAAI,QAAQ,GACV,OAAO;EAEX;EACA,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"take.js","names":[],"sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeRemoveChange,\n type Change,\n type EditChange,\n} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, valueToKey, type Comparator, type Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {\n andFinally,\n andThen,\n doneResult,\n eachStream,\n type Stream,\n} from './stream.ts';\nimport type {TakeBoundProvider, TakeGate} from './take-gate.ts';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: string): TakeState | undefined;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n destroy(): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\ntype DirtyPartitionState = {\n constraint: Constraint | undefined;\n /**\n * The bound to report to upstream TakeGate via getBound() until Phase 2\n * reconciliation completes.\n *\n * When removals reduce partition size below limit in Phase 1, normal getBound()\n * would return undefined (unbounded), which would cause upstream TakeGate to open\n * and allow unbounded parent pushes through. Preserving gateBound keeps TakeGate\n * capped at the pre-removal boundary until Phase 2 refills the deficit.\n */\n gateBound: Row | undefined;\n};\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #takeGate: TakeGate | undefined;\n /**\n * Partitions that incurred removals in Phase 1 and require deficit refills in\n * Phase 2 (reconcile). Keyed by `takeStateKey` (see `getTakeStateKey`), matching\n * `#storage` keys: `'[\"take\"]'` when unpartitioned, `'take:' + valueToKey(value)` for one\n * partition column, or `'[\"take\", ...partitionValues]'` for more.\n */\n readonly #dirtyPartitions = new Map<string, DirtyPartitionState>();\n\n #output: Output = throwOutput;\n\n setTakeGate(gate: TakeGate): void {\n this.#takeGate = gate;\n }\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Take requires sorted input');\n assertOrderingIncludesPK(sort, input.getSchema().primaryKey);\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (\n this.#partitionKey &&\n !constraintContainsPartitionKey(constraint, this.#partitionKey)\n ) {\n return undefined;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, constraint);\n const dirty = this.#dirtyPartitions.get(takeStateKey);\n if (dirty) {\n // While dirty in Phase 1, return gateBound (the bound prior to removals) so\n // upstream operators (e.g. TakeGate) remain capped rather than unbounding during push.\n return dirty.gateBound;\n }\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState || takeState.size < this.#limit) {\n return undefined;\n }\n return takeState.bound;\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintContainsPartitionKey(req.constraint, this.#partitionKey)),\n 'Partitioned take does not allow unpartitioned fetches',\n );\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) {\n return this.#initialFetch(req, takeStateKey);\n }\n return emptyArray;\n }\n if (takeState.bound === undefined) {\n return emptyArray;\n }\n return new TakeNodes(\n this.#input.fetch(req)[Symbol.iterator](),\n this.getSchema().compareRows,\n takeState.bound,\n this.#limit,\n this.#getRowHiddenFromFetch,\n );\n }\n\n readonly #getRowHiddenFromFetch = (): Row | undefined =>\n this.#rowHiddenFromFetch;\n\n *#initialFetch(\n req: FetchRequest,\n takeStateKey: string,\n ): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n // The caller (fetch) has already checked that there is no state for\n // takeStateKey.\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(takeStateKey, size, bound);\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let constraint: Constraint | undefined;\n if (takeState) {\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n constraint: Constraint | undefined;\n };\n }\n\n push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n return this.#pushEditChange(change);\n }\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.NODE].row,\n );\n if (!takeState) {\n return emptyArray;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (takeState.size < this.#limit) {\n if (this.#dirtyPartitions.has(takeStateKey)) {\n // While dirty with an unresolved deficit in Phase 1, only admit rows that\n // sort strictly before the current window boundary. We refuse to expand the\n // bound forward during Phase 1 so that Phase 2 can backfill earlier candidate\n // rows from storage in sort order.\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return emptyArray;\n }\n }\n const nextSize = takeState.size + 1;\n if (nextSize === this.#limit) {\n this.#dirtyPartitions.delete(takeStateKey);\n }\n this.#setTakeState(\n takeStateKey,\n nextSize,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change[ChangeIndex.NODE].row) < 0\n ? change[ChangeIndex.NODE].row\n : takeState.bound,\n );\n return this.#output.push(change, this);\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return emptyArray;\n }\n // added row < bound\n // The bound node and, unless the limit is 1, the node before it.\n return this.#withFirst(\n this.#limit === 1\n ? {\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n }\n : {\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n },\n this.#limit === 1 ? 1 : 2,\n false,\n ([boundNode, beforeBoundNode]) => {\n assert(\n boundNode !== undefined,\n 'Take: boundNode must be found during fetch',\n );\n const removeChange = makeRemoveChange(boundNode);\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0\n ? change[ChangeIndex.NODE].row\n : beforeBoundNode.row,\n );\n return andThen(\n this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n removeChange,\n ),\n () => this.#output.push(change, this),\n );\n },\n );\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (takeState.bound === undefined) {\n // change is after bound\n return emptyArray;\n }\n const compToBound = compareRows(\n change[ChangeIndex.NODE].row,\n takeState.bound,\n );\n if (compToBound > 0 || (compToBound < 0 && this.#limit === 1)) {\n // change is not in window\n return emptyArray;\n }\n const {bound} = takeState;\n return this.#withFirst(\n {\n start: {\n row: bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n },\n 1,\n false,\n ([beforeBoundNode]) => {\n const finalBound =\n takeState.size - 1 === 0\n ? undefined\n : compToBound < 0\n ? bound\n : beforeBoundNode?.row;\n if (!this.#dirtyPartitions.has(takeStateKey)) {\n // Snapshot the pre-removal bound as the gate bound on the first removal that\n // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return\n // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits.\n this.#dirtyPartitions.set(takeStateKey, {\n constraint,\n gateBound: bound,\n });\n }\n this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);\n return this.#output.push(change, this);\n },\n );\n }\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0\n ) {\n return this.#output.push(change, this);\n }\n return emptyArray;\n }\n\n /**\n * Reads the first `n` nodes of a fetch from the input and calls `rest` with\n * them (fewer if the fetch has fewer). With `gated`, the take gate is open\n * while the fetch is read.\n *\n * `rest` runs right away unless the fetch yields. Then the stream returned\n * holds the yields, with what is left of the fetch and `rest` behind them\n * (see `andThen` in stream.ts).\n */\n #withFirst(\n req: FetchRequest,\n n: number,\n gated: boolean,\n rest: (nodes: Node[]) => Stream<'yield'>,\n ): Stream<'yield'> {\n const nodes: Node[] = [];\n let closeGate = gated;\n if (gated) {\n this.#takeGate?.open();\n }\n try {\n const it = this.#input.fetch(req)[Symbol.iterator]();\n for (;;) {\n const r = it.next();\n if (r.done) {\n break;\n }\n if (r.value === 'yield') {\n closeGate = false;\n return this.#withFirstRest(it, nodes, n, gated, rest);\n }\n nodes.push(r.value);\n if (nodes.length === n) {\n it.return?.();\n break;\n }\n }\n } finally {\n if (closeGate) {\n this.#takeGate?.close();\n }\n }\n return rest(nodes);\n }\n\n /** #withFirst from the 'yield' it stopped at. */\n *#withFirstRest(\n it: Iterator<Node | 'yield'>,\n nodes: Node[],\n n: number,\n gated: boolean,\n rest: (nodes: Node[]) => Stream<'yield'>,\n ): Stream<'yield'> {\n let finished = false;\n try {\n yield 'yield';\n for (;;) {\n const r = it.next();\n if (r.done) {\n finished = true;\n break;\n }\n if (r.value === 'yield') {\n yield 'yield';\n continue;\n }\n nodes.push(r.value);\n if (nodes.length === n) {\n break;\n }\n }\n } finally {\n if (!finished) {\n it.return?.();\n }\n if (gated) {\n this.#takeGate?.close();\n }\n }\n yield* rest(nodes);\n }\n\n #pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.OLD_NODE].row,\n );\n if (!takeState) {\n return emptyArray;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {bound} = takeState;\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(change[ChangeIndex.OLD_NODE].row, bound);\n const newCmp = compareRows(change[ChangeIndex.NODE].row, bound);\n\n const replaceBoundAndForwardChange = () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n change[ChangeIndex.NODE].row,\n );\n return this.#output.push(change, this);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n return this.#output.push(change, this);\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n return replaceBoundAndForwardChange();\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n return this.#withFirst(\n {\n start: {\n row: bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n },\n 1,\n false,\n ([beforeBoundNode]) => {\n assert(\n beforeBoundNode !== undefined,\n 'Take: beforeBoundNode must be found during fetch',\n );\n\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode.row,\n );\n return this.#output.push(change, this);\n },\n );\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n return this.#withFirst(\n {\n start: {\n row: bound,\n basis: 'at',\n },\n constraint,\n },\n 1,\n true,\n ([newBoundNode]) => {\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (\n compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0\n ) {\n return replaceBoundAndForwardChange();\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n return andThen(\n this.#pushWithRowHiddenFromFetch(\n newBoundNode.row,\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n ),\n () => this.#output.push(makeAddChange(newBoundNode), this),\n );\n },\n );\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return emptyArray;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n return this.#withFirst(\n {\n start: {\n row: bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n },\n 2,\n false,\n ([oldBoundNode, newBoundNode]) => {\n assert(\n oldBoundNode !== undefined,\n 'Take: oldBoundNode must be found during fetch',\n );\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n return andThen(\n this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n makeRemoveChange(oldBoundNode),\n ),\n () =>\n this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this),\n );\n },\n );\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n return this.#output.push(change, this);\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n return this.#withFirst(\n {\n start: {\n row: bound,\n basis: 'after',\n },\n constraint,\n },\n 1,\n true,\n ([afterBoundNode]) => {\n assert(\n afterBoundNode !== undefined,\n 'Take: afterBoundNode must be found during fetch',\n );\n\n // The new row is the new bound. Use an edit change.\n if (\n compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0\n ) {\n return replaceBoundAndForwardChange();\n }\n\n return andThen(\n this.#output.push(\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n this,\n ),\n () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n afterBoundNode.row,\n );\n return this.#output.push(makeAddChange(afterBoundNode), this);\n },\n );\n },\n );\n }\n\n unreachable();\n }\n\n #pushWithRowHiddenFromFetch(row: Row, change: Change): Stream<'yield'> {\n this.#rowHiddenFromFetch = row;\n let s: Stream<'yield'>;\n try {\n s = this.#output.push(change, this);\n } catch (e) {\n this.#rowHiddenFromFetch = undefined;\n throw e;\n }\n return andFinally(s, this.#showHiddenRow);\n }\n\n readonly #showHiddenRow = (): void => {\n this.#rowHiddenFromFetch = undefined;\n };\n\n #setTakeState(takeStateKey: string, size: number, bound: Row | undefined) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n reconcile(_pusher?: InputBase): Stream<'yield'> {\n if (this.#dirtyPartitions.size === 0) {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n const dirty = [...this.#dirtyPartitions.entries()];\n this.#dirtyPartitions.clear();\n let i = 0;\n return andThen(\n eachStream(() =>\n i === dirty.length ? undefined : this.#refill(dirty[i++]),\n ),\n () =>\n this.#output.reconcile ? this.#output.reconcile(this) : emptyArray,\n );\n }\n\n /** Refills a dirty partition with the rows that follow its bound. */\n #refill([takeStateKey, {constraint}]: [\n string,\n DirtyPartitionState,\n ]): Stream<'yield'> {\n const takeState = this.#storage.get(takeStateKey);\n assert(\n takeState !== undefined,\n 'Take: dirty partition must exist in storage',\n );\n const deficit = this.#limit - takeState.size;\n assert(deficit > 0, 'Take: dirty partition must have a deficit');\n\n return this.#withFirst(\n {\n start: takeState.bound\n ? {\n row: takeState.bound,\n basis: 'after',\n }\n : undefined,\n constraint,\n },\n deficit,\n true,\n toPush => {\n let size = takeState.size;\n let j = 0;\n return eachStream(() => {\n if (j === toPush.length) {\n return undefined;\n }\n const node = toPush[j++];\n this.#setTakeState(takeStateKey, ++size, node.row);\n return this.#output.push(makeAddChange(node), this);\n });\n },\n );\n }\n}\n\n/**\n * The rows of a hydrated Take: its input up to the bound or the limit. An\n * iterator rather than a generator, see `mapNodes` in stream.ts.\n */\nclass TakeNodes implements IterableIterator<Node | 'yield'> {\n readonly #it: Iterator<Node | 'yield'>;\n readonly #compareRows: Comparator;\n readonly #bound: Row;\n readonly #limit: number;\n readonly #getHidden: () => Row | undefined;\n #count = 0;\n #done = false;\n\n constructor(\n it: Iterator<Node | 'yield'>,\n compareRows: Comparator,\n bound: Row,\n limit: number,\n getHidden: () => Row | undefined,\n ) {\n this.#it = it;\n this.#compareRows = compareRows;\n this.#bound = bound;\n this.#limit = limit;\n this.#getHidden = getHidden;\n }\n\n [Symbol.iterator](): this {\n return this;\n }\n\n next(): IteratorResult<Node | 'yield'> {\n if (this.#done) {\n return doneResult;\n }\n if (this.#count >= this.#limit) {\n return this.return();\n }\n for (;;) {\n let r: IteratorResult<Node | 'yield'>;\n try {\n r = this.#it.next();\n } catch (e) {\n // A fetch that threw is over, as a generator's is.\n this.#done = true;\n throw e;\n }\n if (r.done) {\n this.#done = true;\n return r;\n }\n const node = r.value;\n if (node === 'yield') {\n return r;\n }\n let past: boolean;\n let hidden: boolean;\n try {\n past = this.#compareRows(this.#bound, node.row) < 0;\n const hiddenRow = this.#getHidden();\n hidden =\n !past &&\n hiddenRow !== undefined &&\n this.#compareRows(hiddenRow, node.row) === 0;\n } catch (e) {\n // As a `for...of` does when its body throws: close the input, and\n // report this error rather than one from closing it.\n this.#done = true;\n try {\n this.#it.return?.();\n } catch {\n // The first error wins.\n }\n throw e;\n }\n if (past) {\n return this.return();\n }\n if (hidden) {\n continue;\n }\n this.#count++;\n return r;\n }\n }\n\n return(): IteratorResult<Node | 'yield'> {\n if (!this.#done) {\n this.#done = true;\n this.#it.return?.();\n }\n return doneResult;\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n if (partitionKey?.length === 1 && rowOrConstraint) {\n return 'take:' + valueToKey(rowOrConstraint[partitionKey[0]]);\n }\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nexport function constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function constraintContainsPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function makePartitionKeyComparator(\n partitionKey: PartitionKey,\n): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA0EA,IAAa,OAAb,MAAyD;CACvD;CACA;CACA;CACA;CACA;CAEA;CAEA;;;;;;;CAOA,mCAA4B,IAAI,IAAiC;CAEjE,UAAkB;CAElB,YAAY,MAAsB;EAChC,KAAK,YAAY;CACnB;CAEA,YACE,OACA,SACA,OACA,cACA;EACA,OAAO,SAAS,GAAG,4BAA4B;EAC/C,MAAM,EAAC,SAAQ,MAAM,UAAU;EAC/B,OAAO,SAAS,KAAA,GAAW,4BAA4B;EACvD,yBAAyB,MAAM,MAAM,UAAU,CAAC,CAAC,UAAU;EAC3D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AK;GACL,OAAO;EACT;CACF;CAEA,SAAyC;EACvC,IAAI,CAAC,KAAK,OAAO;GACf,KAAK,QAAQ;GACb,KAAK,IAAI,SAAS;EACpB;EACA,OAAO;CACT;AACF;AAEA,SAAS,gBACP,cACA,iBACQ;CACR,IAAI,cAAc,WAAW,KAAK,iBAChC,OAAO,UAAU,WAAW,gBAAgB,aAAa,GAAG;CAI9D,MAAM,kBAA2B,CAAC;CAElC,IAAI,gBAAgB,iBAClB,KAAK,MAAM,OAAO,cAChB,gBAAgB,KAAK,gBAAgB,IAAI;CAI7C,OAAO,KAAK,UAAU,CAAC,QAAQ,GAAG,eAAe,CAAC;AACpD;AAEA,SAAgB,8BACd,YACA,cACS;CACT,IAAI,eAAe,KAAA,KAAa,iBAAiB,KAAA,GAC/C,OAAO,eAAe;CAExB,IAAI,aAAa,WAAW,OAAO,KAAK,UAAU,CAAC,CAAC,QAClD,OAAO;CAET,KAAK,MAAM,OAAO,cAChB,IAAI,CAAC,OAAO,YAAY,GAAG,GACzB,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,+BACd,YACA,cACS;CACT,IAAI,eAAe,KAAA,KAAa,iBAAiB,KAAA,GAC/C,OAAO;CAET,KAAK,MAAM,OAAO,cAChB,IAAI,CAAC,OAAO,YAAY,GAAG,GACzB,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,2BACd,cACY;CACZ,QAAQ,GAAG,MAAM;EACf,KAAK,MAAM,OAAO,cAAc;GAC9B,MAAM,MAAM,cAAc,EAAE,MAAM,EAAE,IAAI;GACxC,IAAI,QAAQ,GACV,OAAO;EAEX;EACA,OAAO;CACT;AACF"}
@@ -428,8 +428,8 @@ export interface Query<TTable extends keyof TSchema['tables'] & string, TSchema
428
428
  min<TSelector extends OrderedSelector<PullTableSchema<TTable, TSchema>>>(field: TSelector): Query<TTable, TSchema, AggregateResult<FieldTSType<TTable, TSchema, TSelector & string> | null>>;
429
429
  /**
430
430
  * The minimum of the number and string leaves at a path inside a `json()`
431
- * column. Numbers order before strings (as in JS and SQLite), so a path
432
- * holding both yields its smallest number. Other leaves are skipped.
431
+ * column. Strings order before numbers (as in Postgres's jsonb), so a path
432
+ * holding both yields its smallest string. Other leaves are skipped.
433
433
  */
434
434
  min<TColumnType, const P extends readonly (string | number)[]>(selector: (builder: JsonAggregateBuilder<TTable, TSchema>) => ColumnRef<TColumnType, P, TTable> & JsonLeafOf<ValueAtPath<TColumnType, P>, number | string>): Query<TTable, TSchema, AggregateResult<Extract<ValueAtPath<TColumnType, P>, number | string> | null>>;
435
435
  /**
@@ -439,8 +439,8 @@ export interface Query<TTable extends keyof TSchema['tables'] & string, TSchema
439
439
  max<TSelector extends OrderedSelector<PullTableSchema<TTable, TSchema>>>(field: TSelector): Query<TTable, TSchema, AggregateResult<FieldTSType<TTable, TSchema, TSelector & string> | null>>;
440
440
  /**
441
441
  * The maximum of the number and string leaves at a path inside a `json()`
442
- * column. Numbers order before strings, so a path holding both yields its
443
- * largest string. Other leaves are skipped.
442
+ * column. Strings order before numbers, so a path holding both yields its
443
+ * largest number. Other leaves are skipped.
444
444
  */
445
445
  max<TColumnType, const P extends readonly (string | number)[]>(selector: (builder: JsonAggregateBuilder<TTable, TSchema>) => ColumnRef<TColumnType, P, TTable> & JsonLeafOf<ValueAtPath<TColumnType, P>, number | string>): Query<TTable, TSchema, AggregateResult<Extract<ValueAtPath<TColumnType, P>, number | string> | null>>;
446
446
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocicorp/zero",
3
- "version": "1.13.0-canary.2",
3
+ "version": "1.13.0-canary.4",
4
4
  "description": "Zero is a web framework for serverless web development.",
5
5
  "homepage": "https://zero.rocicorp.dev",
6
6
  "bugs": {