@rocicorp/zero 1.12.0-canary.1 → 1.12.0-canary.3

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 (36) hide show
  1. package/out/zero/package.js +1 -1
  2. package/out/zero/package.js.map +1 -1
  3. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +18 -0
  4. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  5. package/out/zero-cache/src/services/view-syncer/client-handler.js +46 -20
  6. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  7. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  8. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  9. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +4 -3
  10. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  11. package/out/zero-cache/src/services/view-syncer/snapshotter.js +5 -2
  12. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  13. package/out/zero-client/src/client/version.js +1 -1
  14. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  15. package/out/zero-protocol/src/poke.d.ts +6 -6
  16. package/out/zero-protocol/src/up.d.ts +60 -60
  17. package/out/zql/src/ivm/cap.js +2 -4
  18. package/out/zql/src/ivm/cap.js.map +1 -1
  19. package/out/zql/src/ivm/take.js +2 -4
  20. package/out/zql/src/ivm/take.js.map +1 -1
  21. package/out/zqlite/src/database-storage.d.ts.map +1 -1
  22. package/out/zqlite/src/database-storage.js +38 -6
  23. package/out/zqlite/src/database-storage.js.map +1 -1
  24. package/out/zqlite/src/fetch-sql.d.ts +27 -0
  25. package/out/zqlite/src/fetch-sql.d.ts.map +1 -0
  26. package/out/zqlite/src/fetch-sql.js +112 -0
  27. package/out/zqlite/src/fetch-sql.js.map +1 -0
  28. package/out/zqlite/src/query-builder.d.ts +11 -1
  29. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  30. package/out/zqlite/src/query-builder.js +22 -7
  31. package/out/zqlite/src/query-builder.js.map +1 -1
  32. package/out/zqlite/src/table-source.d.ts +9 -2
  33. package/out/zqlite/src/table-source.d.ts.map +1 -1
  34. package/out/zqlite/src/table-source.js +11 -14
  35. package/out/zqlite/src/table-source.js.map +1 -1
  36. package/package.json +1 -1
@@ -67,7 +67,7 @@ var Take = class {
67
67
  const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);
68
68
  const takeState = this.#storage.get(takeStateKey);
69
69
  if (!takeState) {
70
- if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) yield* this.#initialFetch(req);
70
+ if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) yield* this.#initialFetch(req, takeStateKey);
71
71
  return;
72
72
  }
73
73
  if (takeState.bound === void 0) return;
@@ -84,13 +84,11 @@ var Take = class {
84
84
  if (count >= this.#limit) return;
85
85
  }
86
86
  }
87
- *#initialFetch(req) {
87
+ *#initialFetch(req, takeStateKey) {
88
88
  assert(req.start === void 0, "Start should be undefined");
89
89
  assert(!req.reverse, "Reverse should be false");
90
90
  if (this.#limit === 0) return;
91
91
  assert(constraintMatchesPartitionKey(req.constraint, this.#partitionKey), "Constraint should match partition key");
92
- const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);
93
- assert(this.#storage.get(takeStateKey) === void 0, "Take state should be undefined");
94
92
  let size = 0;
95
93
  let bound;
96
94
  let downstreamEarlyReturn = true;
@@ -1 +1 @@
1
- {"version":3,"file":"take.js","names":["#input","#storage","#limit","#partitionKey","#partitionKeyComparator","#dirtyPartitions","#takeGate","#output","#initialFetch","#rowHiddenFromFetch","#setTakeState","#pushEditChange","#getStateAndConstraint","#pushWithRowHiddenFromFetch"],"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, 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, or `'[\"take\", ...partitionValues]'`\n * when partitioned.\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);\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(req: FetchRequest): 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 const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n assert(\n this.#storage.get(takeStateKey) === undefined,\n 'Take state should be undefined',\n );\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 // 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":";;;;;;;;;;;;;;;;;;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S;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"}
1
+ {"version":3,"file":"take.js","names":["#input","#storage","#limit","#partitionKey","#partitionKeyComparator","#dirtyPartitions","#takeGate","#output","#initialFetch","#rowHiddenFromFetch","#setTakeState","#pushEditChange","#getStateAndConstraint","#pushWithRowHiddenFromFetch"],"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, 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, or `'[\"take\", ...partitionValues]'`\n * when partitioned.\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 // 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,KAAKM,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,EAAE,UAAU;EAC3D,MAAM,UAAU,IAAI;EACpB,KAAKN,SAAS;EACd,KAAKC,WAAW;EAChB,KAAKC,SAAS;EACd,KAAKC,gBAAgB;EACrB,KAAKC,0BACH,gBAAgB,2BAA2B,YAAY;CAC3D;CAEA,UAAU,QAAsB;EAC9B,KAAKG,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAKP,OAAO,UAAU;CAC/B;CAEA,SAAS,YAA0C;EACjD,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f,OAAO,KAAKA,QAAQ,UAAU,IAAI;CAEtC;AACF;AAEA,SAAS,gBACP,cACA,iBACQ;CAGR,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,EAAE,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"}
@@ -1 +1 @@
1
- {"version":3,"file":"database-storage.d.ts","sourceRoot":"","sources":["../../../../zqlite/src/database-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,+BAA+B,CAAC;AAG3D,OAAO,EAAC,QAAQ,EAAC,MAAM,SAAS,CAAC;AAEjC,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,aAAa,IAAI,OAAO,CAAC;IAEzB,gDAAgD;IAChD,OAAO,IAAI,IAAI,CAAC;CACjB;AAcD,eAAO,MAAM,oBAAoB,wJAQ9B,CAAC;AAOJ,qBAAa,eAAe;;IAC1B,MAAM,CAAC,MAAM,CACX,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,MAAM,EACZ,OAAO;;;KAAiB,GACvB,eAAe,CAiBjB;IAOD,YAAY,EAAE,EAAE,QAAQ,EAAE,OAAO;;;KAAiB,EAiCjD;IAED,KAAK,SAGJ;IA+DD,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,CAuBzD;CACF"}
1
+ {"version":3,"file":"database-storage.d.ts","sourceRoot":"","sources":["../../../../zqlite/src/database-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,+BAA+B,CAAC;AAG3D,OAAO,EAAC,QAAQ,EAAC,MAAM,SAAS,CAAC;AAEjC,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,aAAa,IAAI,OAAO,CAAC;IAEzB,gDAAgD;IAChD,OAAO,IAAI,IAAI,CAAC;CACjB;AAcD,eAAO,MAAM,oBAAoB,wJAQ9B,CAAC;AAeJ,qBAAa,eAAe;;IAC1B,MAAM,CAAC,MAAM,CACX,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,MAAM,EACZ,OAAO;;;KAAiB,GACvB,eAAe,CAiBjB;IAOD,YAAY,EAAE,EAAE,QAAQ,EAAE,OAAO;;;KAAiB,EAiCjD;IAED,KAAK,SAGJ;IA0DD,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,CA4DzD;CACF"}
@@ -9,6 +9,13 @@ var CREATE_STORAGE_TABLE = `
9
9
  PRIMARY KEY(clientGroupID, op, key)
10
10
  )
11
11
  `;
12
+ /**
13
+ * The most values each operator's storage keeps in memory. The storage is in
14
+ * SQLite so that operator state can spill to disk, so only a few values per
15
+ * operator are kept: profiles of the view-syncer gain as much from 64 as from
16
+ * 1,024.
17
+ */
18
+ var MAX_CACHED_ENTRIES_PER_OP = 64;
12
19
  var defaultOptions = {
13
20
  commitInterval: 5e3,
14
21
  compactionThresholdBytes: 50 * 1024 * 1024
@@ -68,10 +75,10 @@ var DatabaseStorage = class DatabaseStorage {
68
75
  this.#checkpoint();
69
76
  this.#db.close();
70
77
  }
71
- #get(cgID, opID, key, def) {
78
+ #get(cgID, opID, key) {
72
79
  this.#maybeCheckpoint();
73
80
  const row = this.#stmts.get.get(cgID, opID, key);
74
- return row ? JSON.parse(row.val) : def;
81
+ return row ? JSON.parse(row.val) : void 0;
75
82
  }
76
83
  #set(cgID, opID, key, val) {
77
84
  this.#maybeCheckpoint();
@@ -107,8 +114,10 @@ var DatabaseStorage = class DatabaseStorage {
107
114
  }
108
115
  }
109
116
  createClientGroupStorage(cgID) {
117
+ const caches = /* @__PURE__ */ new Set();
110
118
  const destroy = () => {
111
119
  this.#stmts.clear.run(cgID);
120
+ for (const cache of caches) cache.clear();
112
121
  this.#checkpoint();
113
122
  this.#db.compact(this.#options.compactionThresholdBytes);
114
123
  };
@@ -117,12 +126,35 @@ var DatabaseStorage = class DatabaseStorage {
117
126
  return {
118
127
  createStorage: () => {
119
128
  const opID = nextOpID++;
129
+ const cache = /* @__PURE__ */ new Map();
130
+ caches.add(cache);
131
+ const cacheSet = (key, val) => {
132
+ if (cache.size >= MAX_CACHED_ENTRIES_PER_OP && !cache.has(key)) cache.clear();
133
+ cache.set(key, val);
134
+ };
120
135
  return {
121
- get: (key, def) => this.#get(cgID, opID, key, def),
122
- set: (key, val) => this.#set(cgID, opID, key, val),
123
- del: (key) => this.#del(cgID, opID, key),
136
+ get: (key, def) => {
137
+ let val = cache.get(key);
138
+ if (val === void 0 && !cache.has(key)) {
139
+ val = this.#get(cgID, opID, key);
140
+ cacheSet(key, val);
141
+ }
142
+ return val === void 0 ? def : val;
143
+ },
144
+ set: (key, val) => {
145
+ this.#set(cgID, opID, key, val);
146
+ cacheSet(key, val);
147
+ },
148
+ del: (key) => {
149
+ this.#del(cgID, opID, key);
150
+ cacheSet(key, void 0);
151
+ },
124
152
  scan: (opts) => this.#scan(cgID, opID, opts),
125
- destroy: () => this.#destroy(cgID, opID)
153
+ destroy: () => {
154
+ this.#destroy(cgID, opID);
155
+ cache.clear();
156
+ caches.delete(cache);
157
+ }
126
158
  };
127
159
  },
128
160
  destroy
@@ -1 +1 @@
1
- {"version":3,"file":"database-storage.js","names":["#stmts","#options","#db","#checkpoint","#maybeCheckpoint","#numWrites","#scan","#get","#set","#del","#destroy"],"sources":["../../../../zqlite/src/database-storage.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {JSONValue} from '../../shared/src/json.ts';\nimport type {Storage} from '../../zql/src/ivm/operator.ts';\nimport type {Stream} from '../../zql/src/ivm/stream.ts';\nimport type {Statement} from './db.ts';\nimport {Database} from './db.ts';\n\nexport interface ClientGroupStorage {\n /** Creates a {@link Storage} instance for a single operator. */\n createStorage(): Storage;\n\n /** Deletes all storage for the client group. */\n destroy(): void;\n}\n\ntype Statements = {\n get: Statement;\n set: Statement;\n del: Statement;\n scan: Statement;\n clear: Statement;\n clearOp: Statement;\n commit: Statement;\n begin: Statement;\n};\n\n// Exported for testing.\nexport const CREATE_STORAGE_TABLE = `\n CREATE TABLE storage (\n clientGroupID TEXT,\n op NUMBER,\n key TEXT,\n val TEXT,\n PRIMARY KEY(clientGroupID, op, key)\n )\n `;\n\nconst defaultOptions = {\n commitInterval: 5_000,\n compactionThresholdBytes: 50 * 1024 * 1024,\n};\n\nexport class DatabaseStorage {\n static create(\n lc: LogContext,\n path: string,\n options = defaultOptions,\n ): DatabaseStorage {\n // SQLite is used for ephemeral storage (i.e. similar to RAM) that can spill to\n // disk to avoid consuming too much memory. Each worker thread gets its own\n // database (file) and acts as the single reader/writer of the DB, so\n // `locking_mode` is set to `EXCLUSIVE` for performance. Similarly, since\n // durability is not important, `synchronous` is set to `OFF` for performance.\n const db = new Database(lc, path);\n db.unsafeMode(true); // Allows journal_mode = OFF\n db.pragma('locking_mode = EXCLUSIVE');\n db.pragma('foreign_keys = OFF');\n db.pragma('journal_mode = OFF');\n db.pragma('synchronous = OFF');\n db.pragma('auto_vacuum = INCREMENTAL');\n\n db.prepare(CREATE_STORAGE_TABLE).run();\n lc.debug?.(`Created DatabaseStorage backed by ${path}`);\n return new DatabaseStorage(db, options);\n }\n\n readonly #stmts: Statements;\n readonly #options: typeof defaultOptions;\n readonly #db: Database;\n #numWrites = 0;\n\n constructor(db: Database, options = defaultOptions) {\n this.#stmts = {\n get: db.prepare(`\n SELECT val FROM storage WHERE\n clientGroupID = ? AND op = ? AND key = ?\n `),\n set: db.prepare(`\n INSERT INTO storage (clientGroupID, op, key, val)\n VALUES(?, ?, ?, ?)\n ON CONFLICT(clientGroupID, op, key) \n DO \n UPDATE SET val = excluded.val\n `),\n del: db.prepare(`\n DELETE FROM storage WHERE\n clientGroupID = ? AND op = ? AND key = ?\n `),\n scan: db.prepare(`\n SELECT key, val FROM storage WHERE\n clientGroupID = ? AND op = ? AND key >= ?\n `),\n clear: db.prepare(`\n DELETE FROM storage WHERE clientGroupID = ?\n `),\n clearOp: db.prepare(`\n DELETE FROM storage WHERE clientGroupID = ? AND op = ?\n `),\n commit: db.prepare('COMMIT'),\n begin: db.prepare('BEGIN'),\n };\n this.#stmts.begin.run();\n this.#options = options;\n this.#db = db;\n }\n\n close() {\n this.#checkpoint();\n this.#db.close();\n }\n\n #get(\n cgID: string,\n opID: number,\n key: string,\n def?: JSONValue,\n ): JSONValue | undefined {\n this.#maybeCheckpoint();\n const row = this.#stmts.get.get<{val: string}>(cgID, opID, key);\n return row ? JSON.parse(row.val) : def;\n }\n\n #set(cgID: string, opID: number, key: string, val: JSONValue) {\n this.#maybeCheckpoint();\n this.#stmts.set.run(cgID, opID, key, JSON.stringify(val));\n }\n\n #del(cgID: string, opID: number, key: string) {\n this.#maybeCheckpoint();\n this.#stmts.del.run(cgID, opID, key);\n }\n\n #destroy(cgID: string, opID: number) {\n this.#maybeCheckpoint();\n this.#stmts.clearOp.run(cgID, opID);\n }\n\n /**\n * We don't need to commit every single write to the DB\n * since we're not concerned with durability.\n * Waiting on commits can be expensive, so we commit\n * every `COMMIT_INTERVAL` writes.\n */\n #maybeCheckpoint() {\n if (++this.#numWrites >= this.#options.commitInterval) {\n this.#checkpoint();\n }\n }\n\n #checkpoint() {\n this.#stmts.commit.run();\n this.#stmts.begin.run();\n this.#numWrites = 0;\n }\n\n *#scan(\n cgID: string,\n opID: number,\n opts: {prefix: string} = {prefix: ''},\n ): Stream<[string, JSONValue]> {\n const {prefix} = opts;\n for (const {key, val} of this.#stmts.scan.iterate<{\n key: string;\n val: string;\n }>(cgID, opID, prefix)) {\n if (!key.startsWith(prefix)) {\n return;\n }\n yield [key, JSON.parse(val)];\n }\n }\n\n createClientGroupStorage(cgID: string): ClientGroupStorage {\n const destroy = () => {\n this.#stmts.clear.run(cgID);\n this.#checkpoint();\n this.#db.compact(this.#options.compactionThresholdBytes);\n };\n this.#stmts.clear.run(cgID);\n\n let nextOpID = 1;\n return {\n createStorage: () => {\n const opID = nextOpID++;\n return {\n get: (key, def?) => this.#get(cgID, opID, key, def),\n set: (key, val) => this.#set(cgID, opID, key, val),\n del: key => this.#del(cgID, opID, key),\n scan: opts => this.#scan(cgID, opID, opts),\n destroy: () => this.#destroy(cgID, opID),\n };\n },\n\n destroy,\n };\n }\n}\n"],"mappings":";;AA2BA,IAAa,uBAAuB;;;;;;;;;AAUpC,IAAM,iBAAiB;CACrB,gBAAgB;CAChB,0BAA0B,KAAK,OAAO;AACxC;AAEA,IAAa,kBAAb,MAAa,gBAAgB;CAC3B,OAAO,OACL,IACA,MACA,UAAU,gBACO;EAMjB,MAAM,KAAK,IAAI,SAAS,IAAI,IAAI;EAChC,GAAG,WAAW,IAAI;EAClB,GAAG,OAAO,0BAA0B;EACpC,GAAG,OAAO,oBAAoB;EAC9B,GAAG,OAAO,oBAAoB;EAC9B,GAAG,OAAO,mBAAm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AAKE,YAAY;CAErB;CAEA,cAAc;EACZ,KAAKH,OAAO,OAAO,IAAI;EACvB,KAAKA,OAAO,MAAM,IAAI;EACtB,KAAKK,aAAa;CACpB;CAEA,CAACC,MACC,MACA,MACA,OAAyB,EAAC,QAAQ,GAAE,GACP;EAC7B,MAAM,EAAC,WAAU;EACjB,KAAK,MAAM,EAAC,KAAK,SAAQ,KAAKN,OAAO,KAAK,QAGvC,MAAM,MAAM,MAAM,GAAG;GACtB,IAAI,CAAC,IAAI,WAAW,MAAM,GACxB;GAEF,MAAM,CAAC,KAAK,KAAK,MAAM,GAAG,CAAC;EAC7B;CACF;CAEA,yBAAyB,MAAkC;EACzD,MAAM,gBAAgB;GACpB,KAAKA,OAAO,MAAM,IAAI,IAAI;GAC1B,KAAKG,YAAY;GACjB,KAAKD,IAAI,QAAQ,KAAKD,SAAS,wBAAwB;EACzD;EACA,KAAKD,OAAO,MAAM,IAAI,IAAI;EAE1B,IAAI,WAAW;EACf,OAAO;GACL,qBAAqB;IACnB,MAAM,OAAO;IACb,OAAO;KACL,MAAM,KAAK,QAAS,KAAKO,KAAK,MAAM,MAAM,KAAK,GAAG;KAClD,MAAM,KAAK,QAAQ,KAAKC,KAAK,MAAM,MAAM,KAAK,GAAG;KACjD,MAAK,QAAO,KAAKC,KAAK,MAAM,MAAM,GAAG;KACrC,OAAM,SAAQ,KAAKH,MAAM,MAAM,MAAM,IAAI;KACzC,eAAe,KAAKI,SAAS,MAAM,IAAI;IACzC;GACF;GAEA;EACF;CACF;AACF"}
1
+ {"version":3,"file":"database-storage.js","names":["#stmts","#options","#db","#checkpoint","#maybeCheckpoint","#numWrites","#scan","#get","#set","#del","#destroy"],"sources":["../../../../zqlite/src/database-storage.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {JSONValue} from '../../shared/src/json.ts';\nimport type {Storage} from '../../zql/src/ivm/operator.ts';\nimport type {Stream} from '../../zql/src/ivm/stream.ts';\nimport type {Statement} from './db.ts';\nimport {Database} from './db.ts';\n\nexport interface ClientGroupStorage {\n /** Creates a {@link Storage} instance for a single operator. */\n createStorage(): Storage;\n\n /** Deletes all storage for the client group. */\n destroy(): void;\n}\n\ntype Statements = {\n get: Statement;\n set: Statement;\n del: Statement;\n scan: Statement;\n clear: Statement;\n clearOp: Statement;\n commit: Statement;\n begin: Statement;\n};\n\n// Exported for testing.\nexport const CREATE_STORAGE_TABLE = `\n CREATE TABLE storage (\n clientGroupID TEXT,\n op NUMBER,\n key TEXT,\n val TEXT,\n PRIMARY KEY(clientGroupID, op, key)\n )\n `;\n\n/**\n * The most values each operator's storage keeps in memory. The storage is in\n * SQLite so that operator state can spill to disk, so only a few values per\n * operator are kept: profiles of the view-syncer gain as much from 64 as from\n * 1,024.\n */\nconst MAX_CACHED_ENTRIES_PER_OP = 64;\n\nconst defaultOptions = {\n commitInterval: 5_000,\n compactionThresholdBytes: 50 * 1024 * 1024,\n};\n\nexport class DatabaseStorage {\n static create(\n lc: LogContext,\n path: string,\n options = defaultOptions,\n ): DatabaseStorage {\n // SQLite is used for ephemeral storage (i.e. similar to RAM) that can spill to\n // disk to avoid consuming too much memory. Each worker thread gets its own\n // database (file) and acts as the single reader/writer of the DB, so\n // `locking_mode` is set to `EXCLUSIVE` for performance. Similarly, since\n // durability is not important, `synchronous` is set to `OFF` for performance.\n const db = new Database(lc, path);\n db.unsafeMode(true); // Allows journal_mode = OFF\n db.pragma('locking_mode = EXCLUSIVE');\n db.pragma('foreign_keys = OFF');\n db.pragma('journal_mode = OFF');\n db.pragma('synchronous = OFF');\n db.pragma('auto_vacuum = INCREMENTAL');\n\n db.prepare(CREATE_STORAGE_TABLE).run();\n lc.debug?.(`Created DatabaseStorage backed by ${path}`);\n return new DatabaseStorage(db, options);\n }\n\n readonly #stmts: Statements;\n readonly #options: typeof defaultOptions;\n readonly #db: Database;\n #numWrites = 0;\n\n constructor(db: Database, options = defaultOptions) {\n this.#stmts = {\n get: db.prepare(`\n SELECT val FROM storage WHERE\n clientGroupID = ? AND op = ? AND key = ?\n `),\n set: db.prepare(`\n INSERT INTO storage (clientGroupID, op, key, val)\n VALUES(?, ?, ?, ?)\n ON CONFLICT(clientGroupID, op, key) \n DO \n UPDATE SET val = excluded.val\n `),\n del: db.prepare(`\n DELETE FROM storage WHERE\n clientGroupID = ? AND op = ? AND key = ?\n `),\n scan: db.prepare(`\n SELECT key, val FROM storage WHERE\n clientGroupID = ? AND op = ? AND key >= ?\n `),\n clear: db.prepare(`\n DELETE FROM storage WHERE clientGroupID = ?\n `),\n clearOp: db.prepare(`\n DELETE FROM storage WHERE clientGroupID = ? AND op = ?\n `),\n commit: db.prepare('COMMIT'),\n begin: db.prepare('BEGIN'),\n };\n this.#stmts.begin.run();\n this.#options = options;\n this.#db = db;\n }\n\n close() {\n this.#checkpoint();\n this.#db.close();\n }\n\n #get(cgID: string, opID: number, key: string): JSONValue | undefined {\n this.#maybeCheckpoint();\n const row = this.#stmts.get.get<{val: string}>(cgID, opID, key);\n return row ? JSON.parse(row.val) : undefined;\n }\n\n #set(cgID: string, opID: number, key: string, val: JSONValue) {\n this.#maybeCheckpoint();\n this.#stmts.set.run(cgID, opID, key, JSON.stringify(val));\n }\n\n #del(cgID: string, opID: number, key: string) {\n this.#maybeCheckpoint();\n this.#stmts.del.run(cgID, opID, key);\n }\n\n #destroy(cgID: string, opID: number) {\n this.#maybeCheckpoint();\n this.#stmts.clearOp.run(cgID, opID);\n }\n\n /**\n * We don't need to commit every single write to the DB\n * since we're not concerned with durability.\n * Waiting on commits can be expensive, so we commit\n * every `COMMIT_INTERVAL` writes.\n */\n #maybeCheckpoint() {\n if (++this.#numWrites >= this.#options.commitInterval) {\n this.#checkpoint();\n }\n }\n\n #checkpoint() {\n this.#stmts.commit.run();\n this.#stmts.begin.run();\n this.#numWrites = 0;\n }\n\n *#scan(\n cgID: string,\n opID: number,\n opts: {prefix: string} = {prefix: ''},\n ): Stream<[string, JSONValue]> {\n const {prefix} = opts;\n for (const {key, val} of this.#stmts.scan.iterate<{\n key: string;\n val: string;\n }>(cgID, opID, prefix)) {\n if (!key.startsWith(prefix)) {\n return;\n }\n yield [key, JSON.parse(val)];\n }\n }\n\n createClientGroupStorage(cgID: string): ClientGroupStorage {\n // The caches of the group's live operator storages.\n const caches = new Set<Map<string, JSONValue | undefined>>();\n const destroy = () => {\n this.#stmts.clear.run(cgID);\n for (const cache of caches) {\n cache.clear();\n }\n this.#checkpoint();\n this.#db.compact(this.#options.compactionThresholdBytes);\n };\n this.#stmts.clear.run(cgID);\n\n let nextOpID = 1;\n return {\n createStorage: () => {\n const opID = nextOpID++;\n // The operator's most recently read and written values, `undefined`\n // for a key known to be absent. Operators read the same key again and\n // again (Cap and Take read their state on every fetch and push), so\n // this saves most of the SQLite reads. Like MemoryStorage, `get`\n // returns the stored value itself, which operators do not mutate.\n const cache = new Map<string, JSONValue | undefined>();\n caches.add(cache);\n const cacheSet = (key: string, val: JSONValue | undefined) => {\n if (cache.size >= MAX_CACHED_ENTRIES_PER_OP && !cache.has(key)) {\n cache.clear();\n }\n cache.set(key, val);\n };\n return {\n get: (key, def?) => {\n let val = cache.get(key);\n if (val === undefined && !cache.has(key)) {\n val = this.#get(cgID, opID, key);\n cacheSet(key, val);\n }\n return val === undefined ? def : val;\n },\n // The cache is updated only once the database is, so a failed write\n // leaves it consistent with the database.\n set: (key, val) => {\n this.#set(cgID, opID, key, val);\n cacheSet(key, val);\n },\n del: key => {\n this.#del(cgID, opID, key);\n cacheSet(key, undefined);\n },\n scan: opts => this.#scan(cgID, opID, opts),\n destroy: () => {\n this.#destroy(cgID, opID);\n cache.clear();\n caches.delete(cache);\n },\n };\n },\n\n destroy,\n };\n }\n}\n"],"mappings":";;AA2BA,IAAa,uBAAuB;;;;;;;;;;;;;;;AAgBpC,IAAM,4BAA4B;AAElC,IAAM,iBAAiB;CACrB,gBAAgB;CAChB,0BAA0B,KAAK,OAAO;AACxC;AAEA,IAAa,kBAAb,MAAa,gBAAgB;CAC3B,OAAO,OACL,IAC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M,IAAI,GAAG,GAAG;OACxC,MAAM,KAAKO,KAAK,MAAM,MAAM,GAAG;OAC/B,SAAS,KAAK,GAAG;MACnB;MACA,OAAO,QAAQ,KAAA,IAAY,MAAM;KACnC;KAGA,MAAM,KAAK,QAAQ;MACjB,KAAKC,KAAK,MAAM,MAAM,KAAK,GAAG;MAC9B,SAAS,KAAK,GAAG;KACnB;KACA,MAAK,QAAO;MACV,KAAKC,KAAK,MAAM,MAAM,GAAG;MACzB,SAAS,KAAK,KAAA,CAAS;KACzB;KACA,OAAM,SAAQ,KAAKH,MAAM,MAAM,MAAM,IAAI;KACzC,eAAe;MACb,KAAKI,SAAS,MAAM,IAAI;MACxB,MAAM,MAAM;MACZ,OAAO,OAAO,KAAK;KACrB;IACF;GACF;GAEA;EACF;CACF;AACF"}
@@ -0,0 +1,27 @@
1
+ import type { Ordering } from '../../zero-protocol/src/ast.ts';
2
+ import type { SchemaValue } from '../../zero-schema/src/table-schema.ts';
3
+ import type { FetchRequest } from '../../zql/src/ivm/operator.ts';
4
+ import { type NoSubqueryCondition } from './query-builder.ts';
5
+ export type FetchSQL = {
6
+ readonly text: string;
7
+ readonly values: readonly unknown[];
8
+ };
9
+ /**
10
+ * Builds the SELECTs of {@link buildSelectQueries} for the fetches of one
11
+ * connection, formatting the SQL text only once per fetch shape.
12
+ *
13
+ * Building and formatting the SQL is a fixed cost of every fetch that is
14
+ * comparable to running the statement for the small fetches IVM does most.
15
+ * The text depends only on the fetch's shape, though: which columns it
16
+ * constrains, how many entries each multi-constraint has, its direction,
17
+ * which columns of its start row are NULL, and its `req.filter`. The values
18
+ * are bound to `?` placeholders, so a shape's text is built once with a
19
+ * {@link Binding} in place of each value, and each fetch only reads its values
20
+ * from those.
21
+ */
22
+ export declare class FetchSQLCache {
23
+ #private;
24
+ constructor(tableName: string, columns: Record<string, SchemaValue>, filters: NoSubqueryCondition | undefined, order: Ordering | undefined);
25
+ get(req: FetchRequest): FetchSQL[];
26
+ }
27
+ //# sourceMappingURL=fetch-sql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-sql.d.ts","sourceRoot":"","sources":["../../../../zqlite/src/fetch-sql.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,gCAAgC,CAAC;AAE7D,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,uCAAuC,CAAC;AAEvE,OAAO,KAAK,EACV,YAAY,EAEb,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAIL,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAE5B,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,CAAC;CACrC,CAAC;AAYF;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;;IAOxB,YACE,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EACpC,OAAO,EAAE,mBAAmB,GAAG,SAAS,EACxC,KAAK,EAAE,QAAQ,GAAG,SAAS,EAM5B;IAED,GAAG,CAAC,GAAG,EAAE,YAAY,GAAG,QAAQ,EAAE,CAcjC;CA2GF"}
@@ -0,0 +1,112 @@
1
+ import { assignProperty } from "../../shared/src/objects.js";
2
+ import { format } from "./internal/sql.js";
3
+ import { Binding, buildSelectQueries, toSQLiteType } from "./query-builder.js";
4
+ //#region ../zqlite/src/fetch-sql.ts
5
+ /**
6
+ * More shapes than a connection is expected to fetch. Past it the cache is
7
+ * cleared, which bounds it if some fetch parameter that is part of the shape
8
+ * (a `req.filter` that is not the same object every time, say) never repeats.
9
+ */
10
+ var MAX_SHAPES = 256;
11
+ var filterIDs = /* @__PURE__ */ new WeakMap();
12
+ var nextFilterID = 0;
13
+ /**
14
+ * Builds the SELECTs of {@link buildSelectQueries} for the fetches of one
15
+ * connection, formatting the SQL text only once per fetch shape.
16
+ *
17
+ * Building and formatting the SQL is a fixed cost of every fetch that is
18
+ * comparable to running the statement for the small fetches IVM does most.
19
+ * The text depends only on the fetch's shape, though: which columns it
20
+ * constrains, how many entries each multi-constraint has, its direction,
21
+ * which columns of its start row are NULL, and its `req.filter`. The values
22
+ * are bound to `?` placeholders, so a shape's text is built once with a
23
+ * {@link Binding} in place of each value, and each fetch only reads its values
24
+ * from those.
25
+ */
26
+ var FetchSQLCache = class {
27
+ #tableName;
28
+ #columns;
29
+ #filters;
30
+ #order;
31
+ #templates = /* @__PURE__ */ new Map();
32
+ constructor(tableName, columns, filters, order) {
33
+ this.#tableName = tableName;
34
+ this.#columns = columns;
35
+ this.#filters = filters;
36
+ this.#order = order;
37
+ }
38
+ get(req) {
39
+ const key = this.#shapeKey(req);
40
+ let templates = this.#templates.get(key);
41
+ if (templates === void 0) {
42
+ templates = this.#buildTemplates(req);
43
+ if (this.#templates.size >= MAX_SHAPES) this.#templates.clear();
44
+ this.#templates.set(key, templates);
45
+ }
46
+ return templates.map(({ text, values }) => ({
47
+ text,
48
+ values: values.map((v) => v instanceof Binding ? v.get(req) : v)
49
+ }));
50
+ }
51
+ #shapeKey(req) {
52
+ const { constraint, multiConstraints, start, filter } = req;
53
+ let key = req.reverse ? "r" : "f";
54
+ if (constraint) for (const k in constraint) key += k + "\0";
55
+ key += "\0";
56
+ if (multiConstraints) for (const mc of multiConstraints) {
57
+ key += mc.length + "\0";
58
+ if (mc.length > 0) for (const k in mc[0]) key += k + "\0";
59
+ key += "\0";
60
+ }
61
+ key += "\0";
62
+ if (start) {
63
+ key += start.basis;
64
+ for (const [field] of this.#order ?? []) key += isNullish(start.row[field]) ? "0" : "1";
65
+ }
66
+ key += "\0";
67
+ if (filter) {
68
+ let id = filterIDs.get(filter);
69
+ if (id === void 0) {
70
+ id = nextFilterID++;
71
+ filterIDs.set(filter, id);
72
+ }
73
+ key += id;
74
+ }
75
+ return key;
76
+ }
77
+ #buildTemplates(req) {
78
+ const columns = this.#columns;
79
+ const binding = (get, field) => {
80
+ const { type } = columns[field];
81
+ return new Binding((req) => toSQLiteType(get(req), type));
82
+ };
83
+ let constraint;
84
+ if (req.constraint) {
85
+ const probe = {};
86
+ for (const k in req.constraint) assignProperty(probe, k, binding((req) => req.constraint[k], k));
87
+ constraint = probe;
88
+ }
89
+ const multiConstraints = req.multiConstraints?.map((mc, i) => mc.map((entry, j) => {
90
+ const probe = {};
91
+ for (const k in entry) assignProperty(probe, k, binding((req) => req.multiConstraints[i][j][k], k));
92
+ return probe;
93
+ }));
94
+ let start;
95
+ if (req.start) {
96
+ const row = {};
97
+ for (const [field] of this.#order ?? []) assignProperty(row, field, isNullish(req.start.row[field]) ? null : binding((req) => req.start.row[field], field));
98
+ start = {
99
+ basis: req.start.basis,
100
+ row
101
+ };
102
+ }
103
+ return buildSelectQueries(this.#tableName, columns, constraint, this.#filters, this.#order, req.reverse, start, multiConstraints, req.filter).map((query) => format(query));
104
+ }
105
+ };
106
+ function isNullish(v) {
107
+ return v === null || v === void 0;
108
+ }
109
+ //#endregion
110
+ export { FetchSQLCache };
111
+
112
+ //# sourceMappingURL=fetch-sql.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-sql.js","names":["#tableName","#columns","#filters","#order","#templates","#shapeKey","#buildTemplates"],"sources":["../../../../zqlite/src/fetch-sql.ts"],"sourcesContent":["import {assignProperty} from '../../shared/src/objects.ts';\nimport type {Ordering} from '../../zero-protocol/src/ast.ts';\nimport type {Row} from '../../zero-protocol/src/data.ts';\nimport type {SchemaValue} from '../../zero-schema/src/table-schema.ts';\nimport type {Constraint} from '../../zql/src/ivm/constraint.ts';\nimport type {\n FetchRequest,\n MultiConstraint,\n} from '../../zql/src/ivm/operator.ts';\nimport {format} from './internal/sql.ts';\nimport {\n Binding,\n buildSelectQueries,\n toSQLiteType,\n type NoSubqueryCondition,\n} from './query-builder.ts';\n\nexport type FetchSQL = {\n readonly text: string;\n readonly values: readonly unknown[];\n};\n\n/**\n * More shapes than a connection is expected to fetch. Past it the cache is\n * cleared, which bounds it if some fetch parameter that is part of the shape\n * (a `req.filter` that is not the same object every time, say) never repeats.\n */\nconst MAX_SHAPES = 256;\n\nconst filterIDs = new WeakMap<NoSubqueryCondition, number>();\nlet nextFilterID = 0;\n\n/**\n * Builds the SELECTs of {@link buildSelectQueries} for the fetches of one\n * connection, formatting the SQL text only once per fetch shape.\n *\n * Building and formatting the SQL is a fixed cost of every fetch that is\n * comparable to running the statement for the small fetches IVM does most.\n * The text depends only on the fetch's shape, though: which columns it\n * constrains, how many entries each multi-constraint has, its direction,\n * which columns of its start row are NULL, and its `req.filter`. The values\n * are bound to `?` placeholders, so a shape's text is built once with a\n * {@link Binding} in place of each value, and each fetch only reads its values\n * from those.\n */\nexport class FetchSQLCache {\n readonly #tableName: string;\n readonly #columns: Record<string, SchemaValue>;\n readonly #filters: NoSubqueryCondition | undefined;\n readonly #order: Ordering | undefined;\n readonly #templates = new Map<string, readonly FetchSQL[]>();\n\n constructor(\n tableName: string,\n columns: Record<string, SchemaValue>,\n filters: NoSubqueryCondition | undefined,\n order: Ordering | undefined,\n ) {\n this.#tableName = tableName;\n this.#columns = columns;\n this.#filters = filters;\n this.#order = order;\n }\n\n get(req: FetchRequest): FetchSQL[] {\n const key = this.#shapeKey(req);\n let templates = this.#templates.get(key);\n if (templates === undefined) {\n templates = this.#buildTemplates(req);\n if (this.#templates.size >= MAX_SHAPES) {\n this.#templates.clear();\n }\n this.#templates.set(key, templates);\n }\n return templates.map(({text, values}) => ({\n text,\n values: values.map(v => (v instanceof Binding ? v.get(req) : v)),\n }));\n }\n\n // Column names are never empty and never contain NUL, so terminating each\n // with NUL and each list with an extra NUL keeps distinct shapes distinct.\n #shapeKey(req: FetchRequest): string {\n const {constraint, multiConstraints, start, filter} = req;\n let key = req.reverse ? 'r' : 'f';\n if (constraint) {\n for (const k in constraint) {\n key += k + '\\0';\n }\n }\n key += '\\0';\n if (multiConstraints) {\n for (const mc of multiConstraints) {\n key += mc.length + '\\0';\n if (mc.length > 0) {\n for (const k in mc[0]) {\n key += k + '\\0';\n }\n }\n key += '\\0';\n }\n }\n key += '\\0';\n if (start) {\n key += start.basis;\n // Only the order's columns of the start row are read.\n for (const [field] of this.#order ?? []) {\n key += isNullish(start.row[field]) ? '0' : '1';\n }\n }\n key += '\\0';\n if (filter) {\n let id = filterIDs.get(filter);\n if (id === undefined) {\n id = nextFilterID++;\n filterIDs.set(filter, id);\n }\n key += id;\n }\n return key;\n }\n\n #buildTemplates(req: FetchRequest): FetchSQL[] {\n const columns = this.#columns;\n const binding = (\n get: (req: FetchRequest) => unknown,\n field: string,\n ): Binding => {\n const {type} = columns[field];\n return new Binding(req => toSQLiteType(get(req), type));\n };\n\n let constraint: Constraint | undefined;\n if (req.constraint) {\n const probe: Record<string, unknown> = {};\n for (const k in req.constraint) {\n assignProperty(\n probe,\n k,\n binding(req => req.constraint![k], k),\n );\n }\n constraint = probe as Constraint;\n }\n const multiConstraints = req.multiConstraints?.map(\n (mc, i): MultiConstraint =>\n mc.map((entry, j) => {\n const probe: Record<string, unknown> = {};\n for (const k in entry) {\n assignProperty(\n probe,\n k,\n binding(req => req.multiConstraints![i][j][k], k),\n );\n }\n return probe as Row;\n }),\n );\n let start: FetchRequest['start'];\n if (req.start) {\n const row: Record<string, unknown> = {};\n for (const [field] of this.#order ?? []) {\n assignProperty(\n row,\n field,\n isNullish(req.start.row[field])\n ? null\n : binding(req => req.start!.row[field], field),\n );\n }\n start = {basis: req.start.basis, row: row as Row};\n }\n\n return buildSelectQueries(\n this.#tableName,\n columns,\n constraint,\n this.#filters,\n this.#order,\n req.reverse,\n start,\n multiConstraints,\n req.filter,\n ).map(query => format(query));\n }\n}\n\nfunction isNullish(v: unknown): boolean {\n return v === null || v === undefined;\n}\n"],"mappings":";;;;;;;;;AA2BA,IAAM,aAAa;AAEnB,IAAM,4BAAY,IAAI,QAAqC;AAC3D,IAAI,eAAe;;;;;;;;;;;;;;AAenB,IAAa,gBAAb,MAA2B;CACzB;CACA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AAM,UAAU,KAAKF;EACrB,MAAM,WACJ,KACA,UACY;GACZ,MAAM,EAAC,SAAQ,QAAQ;GACvB,OAAO,IAAI,SAAQ,QAAO,aAAa,IAAI,GAAG,GAAG,IAAI,CAAC;EACxD;EAEA,IAAI;EACJ,IAAI,IAAI,YAAY;GAClB,MAAM,QAAiC,CAAC;GACxC,KAAK,MAAM,KAAK,IAAI,YAClB,eACE,OACA,GACA,SAAQ,QAAO,IAAI,WAAY,IAAI,CAAC,CACtC;GAEF,aAAa;EACf;EACA,MAAM,mBAAmB,IAAI,kBAAkB,KAC5C,IAAI,MACH,GAAG,KAAK,OAAO,MAAM;GACnB,MAAM,QAAiC,CAAC;GACxC,KAAK,MAAM,KAAK,OACd,eACE,OACA,GACA,SAAQ,QAAO,IAAI,iBAAkB,GAAG,GAAG,IAAI,CAAC,CAClD;GAEF,OAAO;EACT,CAAC,CACL;EACA,IAAI;EACJ,IAAI,IAAI,OAAO;GACb,MAAM,MAA+B,CAAC;GACtC,KAAK,MAAM,CAAC,UAAU,KAAKE,UAAU,CAAC,GACpC,eACE,KACA,OACA,UAAU,IAAI,MAAM,IAAI,MAAM,IAC1B,OACA,SAAQ,QAAO,IAAI,MAAO,IAAI,QAAQ,KAAK,CACjD;GAEF,QAAQ;IAAC,OAAO,IAAI,MAAM;IAAY;GAAU;EAClD;EAEA,OAAO,mBACL,KAAKH,YACL,SACA,YACA,KAAKE,UACL,KAAKC,QACL,IAAI,SACJ,OACA,kBACA,IAAI,MACN,EAAE,KAAI,UAAS,OAAO,KAAK,CAAC;CAC9B;AACF;AAEA,SAAS,UAAU,GAAqB;CACtC,OAAO,MAAM,QAAQ,MAAM,KAAA;AAC7B"}
@@ -2,7 +2,7 @@ import type { SQLQuery } from '@databases/sql';
2
2
  import type { Condition, Ordering } from '../../zero-protocol/src/ast.ts';
3
3
  import type { SchemaValue, ValueType } from '../../zero-schema/src/table-schema.ts';
4
4
  import type { Constraint } from '../../zql/src/ivm/constraint.ts';
5
- import type { MultiConstraint, Start } from '../../zql/src/ivm/operator.ts';
5
+ import type { FetchRequest, MultiConstraint, Start } from '../../zql/src/ivm/operator.ts';
6
6
  /**
7
7
  * Condition type without correlated subqueries.
8
8
  * This matches the output of transformFilters from zql/builder/filter.ts
@@ -47,4 +47,14 @@ export declare function orderByToSQL(order: Ordering, reverse: boolean): SQLQuer
47
47
  */
48
48
  export declare function filtersToSQL(filters: NoSubqueryCondition): SQLQuery;
49
49
  export declare function toSQLiteType(v: unknown, type: ValueType): unknown;
50
+ /**
51
+ * A placeholder for a fetch's value that {@link buildSelectQueries} passes
52
+ * through to the bindings of its SELECTs, where `toSQLiteType` would convert
53
+ * the value itself. It lets a fetch's SQL text be built once and reused for
54
+ * every fetch of the same shape (see `fetch-sql.ts`).
55
+ */
56
+ export declare class Binding {
57
+ readonly get: (req: FetchRequest) => unknown;
58
+ constructor(get: (req: FetchRequest) => unknown);
59
+ }
50
60
  //# sourceMappingURL=query-builder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"query-builder.d.ts","sourceRoot":"","sources":["../../../../zqlite/src/query-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAE7C,OAAO,KAAK,EACV,SAAS,EACT,QAAQ,EAGT,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EACV,WAAW,EACX,SAAS,EACV,MAAM,uCAAuC,CAAC;AAC/C,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,iCAAiC,CAAC;AAChE,OAAO,KAAK,EAAC,eAAe,EAAE,KAAK,EAAC,MAAM,+BAA+B,CAAC;AAG1E;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,OAAO,CACvC,SAAS,EACT;IAAC,IAAI,EAAE,oBAAoB,CAAA;CAAC,CAC7B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,C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,EAAE,OAAO,EAAE,OAAO,GAAG,QAAQ,CAmBxE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,QAAQ,CAuBnE;AAgGD,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAWjE;AAED;;;;;GAKG;AACH,qBAAa,OAAO;IAClB,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,OAAO,CAAC;IAC7C,YAAY,GAAG,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,OAAO,EAE9C;CACF"}
@@ -87,7 +87,7 @@ function constraintsRejectNull(constraint, multiConstraints, field) {
87
87
  function constraintsToSQL(constraint, columns) {
88
88
  if (!constraint) return [];
89
89
  const constraints = [];
90
- for (const [key, value] of Object.entries(constraint)) constraints.push(sql`${sql.ident(key)} = ${toSQLiteType(value, columns[key].type)}`);
90
+ for (const [key, value] of Object.entries(constraint)) constraints.push(sql`${sql.ident(key)} = ${toSQLiteBinding(value, columns[key].type)}`);
91
91
  return constraints;
92
92
  }
93
93
  /**
@@ -112,10 +112,10 @@ function multiConstraintToSQL(multiConstraint, columns) {
112
112
  if (keys.length === 1) {
113
113
  const key = keys[0];
114
114
  const colType = columns[key].type;
115
- return sql`${sql.ident(key)} IN (${sql.join(multiConstraint.map((c) => sql`${toSQLiteType(c[key], colType)}`), sql`,`)})`;
115
+ return sql`${sql.ident(key)} IN (${sql.join(multiConstraint.map((c) => sql`${toSQLiteBinding(c[key], colType)}`), sql`,`)})`;
116
116
  }
117
117
  const colList = sql`(${sql.join(keys.map((k) => sql.ident(k)), sql`,`)})`;
118
- const rows = multiConstraint.map((c) => sql`(${sql.join(keys.map((k) => sql`${toSQLiteType(c[k], columns[k].type)}`), sql`,`)})`);
118
+ const rows = multiConstraint.map((c) => sql`(${sql.join(keys.map((k) => sql`${toSQLiteBinding(c[k], columns[k].type)}`), sql`,`)})`);
119
119
  return sql`${colList} IN (VALUES ${sql.join(rows, sql`,`)})`;
120
120
  }
121
121
  function orderByToSQL(order, reverse) {
@@ -173,6 +173,21 @@ function toSQLiteType(v, type) {
173
173
  case "json": return JSON.stringify(v);
174
174
  }
175
175
  }
176
+ /**
177
+ * A placeholder for a fetch's value that {@link buildSelectQueries} passes
178
+ * through to the bindings of its SELECTs, where `toSQLiteType` would convert
179
+ * the value itself. It lets a fetch's SQL text be built once and reused for
180
+ * every fetch of the same shape (see `fetch-sql.ts`).
181
+ */
182
+ var Binding = class {
183
+ get;
184
+ constructor(get) {
185
+ this.get = get;
186
+ }
187
+ };
188
+ function toSQLiteBinding(v, type) {
189
+ return v instanceof Binding ? v : toSQLiteType(v, type);
190
+ }
176
191
  function nullableAwareEquality(field, value, columnType) {
177
192
  if (value === null) return sql`${sql.ident(field)} IS NULL`;
178
193
  return columnType.optional === true ? sql`${sql.ident(field)} IS ${value}` : sql`${sql.ident(field)} = ${value}`;
@@ -226,7 +241,7 @@ function gatherStartConstraints(start, reverse, order, columnTypes) {
226
241
  const [iField, iDirection] = order[i];
227
242
  for (let j = 0; j <= i; j++) if (j === i) {
228
243
  const columnType = columnTypes[iField];
229
- const constraintValue = toSQLiteType(from[iField] ?? null, columnType.type);
244
+ const constraintValue = toSQLiteBinding(from[iField] ?? null, columnType.type);
230
245
  const operator = iDirection === "asc" ? reverse ? "<" : ">" : reverse ? ">" : "<";
231
246
  let nullGroupExcluded = false;
232
247
  if (i === 0) {
@@ -237,14 +252,14 @@ function gatherStartConstraints(start, reverse, order, columnTypes) {
237
252
  } else {
238
253
  const [jField] = order[j];
239
254
  const columnType = columnTypes[jField];
240
- const value = toSQLiteType(from[jField] ?? null, columnType.type);
255
+ const value = toSQLiteBinding(from[jField] ?? null, columnType.type);
241
256
  group.push(nullableAwareEquality(jField, value, columnType));
242
257
  }
243
258
  constraints.push(sql`(${sql.join(group, sql` AND `)})`);
244
259
  }
245
260
  if (basis === "at") constraints.push(sql`(${sql.join(order.map(([field]) => {
246
261
  const columnType = columnTypes[field];
247
- return nullableAwareEquality(field, toSQLiteType(from[field] ?? null, columnType.type), columnType);
262
+ return nullableAwareEquality(field, toSQLiteBinding(from[field] ?? null, columnType.type), columnType);
248
263
  }), sql` AND `)})`);
249
264
  const lexicographicStart = sql`(${sql.join(constraints, sql` OR `)})`;
250
265
  if (leadingBound === void 0) return {
@@ -257,6 +272,6 @@ function gatherStartConstraints(start, reverse, order, columnTypes) {
257
272
  };
258
273
  }
259
274
  //#endregion
260
- export { buildSelectQueries, buildSelectQuery, toSQLiteType };
275
+ export { Binding, buildSelectQueries, buildSelectQuery, toSQLiteType };
261
276
 
262
277
  //# sourceMappingURL=query-builder.js.map