@rocicorp/zero 1.13.0-canary.3 → 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 (80) 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/replicator/write-worker-client.js +17 -3
  9. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  10. package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
  11. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  12. package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
  13. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  14. package/out/zero-client/src/client/custom.js +1 -1
  15. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  16. package/out/zero-client/src/client/version.js +1 -1
  17. package/out/zero-client/src/client/zero.js +1 -1
  18. package/out/zero-protocol/src/ast.d.ts +2 -2
  19. package/out/zero-protocol/src/ast.js +2 -2
  20. package/out/zero-protocol/src/ast.js.map +1 -1
  21. package/out/zero-protocol/src/down.d.ts +27 -27
  22. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  23. package/out/zql/src/ivm/aggregate.js +5 -4
  24. package/out/zql/src/ivm/aggregate.js.map +1 -1
  25. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  26. package/out/zql/src/ivm/cap.js +116 -63
  27. package/out/zql/src/ivm/cap.js.map +1 -1
  28. package/out/zql/src/ivm/deferred-input.js +1 -1
  29. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  30. package/out/zql/src/ivm/exists.js +136 -73
  31. package/out/zql/src/ivm/exists.js.map +1 -1
  32. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  33. package/out/zql/src/ivm/fan-in.js +3 -3
  34. package/out/zql/src/ivm/fan-in.js.map +1 -1
  35. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  36. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  37. package/out/zql/src/ivm/fan-out.js +47 -12
  38. package/out/zql/src/ivm/fan-out.js.map +1 -1
  39. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  40. package/out/zql/src/ivm/filter-operators.js +98 -13
  41. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  42. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  43. package/out/zql/src/ivm/filter-push.js +5 -13
  44. package/out/zql/src/ivm/filter-push.js.map +1 -1
  45. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  46. package/out/zql/src/ivm/filter.js +3 -2
  47. package/out/zql/src/ivm/filter.js.map +1 -1
  48. package/out/zql/src/ivm/flipped-join.js +1 -0
  49. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  50. package/out/zql/src/ivm/group-by.js +1 -1
  51. package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
  52. package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
  53. package/out/zql/src/ivm/join-index-tap.js +9 -7
  54. package/out/zql/src/ivm/join-index-tap.js.map +1 -1
  55. package/out/zql/src/ivm/join.d.ts.map +1 -1
  56. package/out/zql/src/ivm/join.js +72 -67
  57. package/out/zql/src/ivm/join.js.map +1 -1
  58. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  59. package/out/zql/src/ivm/memory-source.js +163 -57
  60. package/out/zql/src/ivm/memory-source.js.map +1 -1
  61. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  62. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  63. package/out/zql/src/ivm/push-accumulated.js +23 -28
  64. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  65. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  66. package/out/zql/src/ivm/skip.js +6 -9
  67. package/out/zql/src/ivm/skip.js.map +1 -1
  68. package/out/zql/src/ivm/stream.d.ts +58 -0
  69. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  70. package/out/zql/src/ivm/stream.js +256 -1
  71. package/out/zql/src/ivm/stream.js.map +1 -1
  72. package/out/zql/src/ivm/take-gate.d.ts +1 -1
  73. package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
  74. package/out/zql/src/ivm/take-gate.js +11 -21
  75. package/out/zql/src/ivm/take-gate.js.map +1 -1
  76. package/out/zql/src/ivm/take.d.ts.map +1 -1
  77. package/out/zql/src/ivm/take.js +243 -217
  78. package/out/zql/src/ivm/take.js.map +1 -1
  79. package/out/zql/src/query/query.d.ts +4 -4
  80. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"exists.js","names":[],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n *filter(node: Node): IterableIterator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n exists = yield* this.#fetchExists(node);\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n\n const result =\n (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node));\n return result;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n try {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE: {\n yield* this.#pushWithFilter(change);\n return;\n }\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n yield* this.#pushWithFilter(change);\n return;\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n } else {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n case ChangeType.REMOVE: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 0) {\n if (this.#not) {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n } else {\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[\n ChangeIndex.NODE\n ],\n ],\n },\n }),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n }\n return;\n default:\n unreachable(change);\n }\n } finally {\n this.#inPush = false;\n }\n }\n\n /**\n * Returns whether or not the node's this.#relationshipName\n * relationship passes the exist/not exists filter condition.\n * If the optional `size` is passed it is used.\n * Otherwise, if there is a stored size for the row it is used.\n * Otherwise the size is computed by streaming the node's\n * relationship with this.#relationshipName (this computed size is also\n * stored).\n */\n *#filter(node: Node, exists?: boolean): IterableIterator<'yield', boolean> {\n exists = exists ?? (yield* this.#fetchExists(node));\n return this.#not ? !exists : exists;\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n if (def.length === 1) {\n return valueToKey(node.row[def[0]]);\n }\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (yield* this.#filter(change[ChangeIndex.NODE], exists)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *#fetchExists(node: Node): IterableIterator<'yield', boolean> {\n // While it seems like this should be able to fetch just 1 node\n // to check for exists, we can't because Take does not support\n // early return during initial fetch.\n return (yield* this.#fetchSize(node)) > 0;\n }\n\n *#fetchSize(node: Node): IterableIterator<'yield', number> {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n if (this.#inPush) {\n // Only the size is needed, which Cap can answer from its state.\n let size = 0;\n for (const n of relationship(true)) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n size++;\n }\n }\n return size;\n }\n // During a fetch, keep the children read to count them and make the\n // node's relationship return them, so that the consumer of the node does\n // not fetch them again. The exists subquery is limited, so this holds at\n // most a few nodes. `node.relationships` is created per node by the Join.\n const children: Node[] = [];\n for (const n of relationship()) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n children.push(n);\n }\n }\n node.relationships[this.#relationshipName] = () => children;\n return children.length;\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,IAAa,SAAb,MAA8C;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA,UAAwB;;;;;;;;;CAUxB,UAAU;CAEV,YACE,OACA,kBACA,eACA,MACA,0BACA;EACA,KAAK,SAAS;EACd,KAAK,oBAAoB;EACzB,KAAK,OAAO,gBAAgB,IAAI;EAChC,KAAK,yBAAS,IAAI,IAAI;EACtB,KAAK,4BAA4B;EACjC,OACE,KAAK,OAAO,UAAU,CAAC,CAAC,cAAc,mBAEtC,wBAAwB,kBAC1B;EACA,KAAK,OAAO,SAAS;EACrB,KAAK,iBAAiB;EAGtB,KAAK,eAAe,SAClB,eACA,KAAK,OAAO,UAAU,CAAC,CAAC,UAC1B;CACF;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,UAAU;CACjB;CAEA,cAAc;EACZ,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAY;EACV,KAAK,yBAAS,IAAI,IAAI;EACtB,KAAK,QAAQ,UAAU;CACzB;CAEA,CAAC,OAAO,MAAgD;EACtD,IAAI;EACJ,IAAI,CAAC,KAAK,gBAAgB,CAAC,KAAK,SAAS;GACvC,MAAM,MAAM,KAAK,aAAa,MAAM,KAAK,cAAc;GACvD,SAAS,KAAK,OAAO,IAAI,GAAG;GAC5B,IAAI,WAAW,KAAA,GAAW;IACxB,SAAS,OAAO,KAAK,aAAa,IAAI;IACtC,KAAK,OAAO,IAAI,KAAK,MAAM;GAC7B,OAAO,IAAI,KAAK,2BACd,KAAK,0BAA0B,IAC7B,MACC,KAAK,0BAA0B,IAAI,GAAG,KAAK,KAAK,CACnD;EAEJ;EAIA,QADG,OAAO,KAAK,QAAQ,MAAM,MAAM,OAAO,OAAO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AAK,CAAC;EAGnB,KAAK,cAAc,KAAK,2BAA2B;EACnD,OAAO,SAAS;CAClB;AACF"}
1
+ {"version":3,"file":"exists.js","names":[],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {andFinally, type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n const size = this.#fetchSizeNow(node);\n if (typeof size !== 'number') {\n // The relationship yielded: the rest is read lazily.\n return this.#filterRest(node, key, size);\n }\n exists = size > 0;\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n if (exists === undefined) {\n const size = this.#fetchSizeNow(node);\n if (typeof size !== 'number') {\n return this.#filterRest(node, undefined, size);\n }\n exists = size > 0;\n }\n return (this.#not ? !exists : exists)\n ? this.#output.filter(node)\n : emptyIteratorReturnFalse;\n }\n\n /** filter() from the point where reading the node's relationship yielded. */\n *#filterRest(\n node: Node,\n cacheKey: string | undefined,\n read: PartialRead,\n ): IterableIterator<'yield', boolean> {\n const exists = (yield* this.#fetchSizeRest(node, read)) > 0;\n if (cacheKey !== undefined) {\n this.#cache.set(cacheKey, exists);\n }\n return (this.#not ? !exists : exists) && (yield* this.#output.filter(node));\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n let s: Stream<'yield'>;\n try {\n s = this.#pushInPush(change);\n } catch (e) {\n this.#inPush = false;\n throw e;\n }\n return andFinally(s, this.#endPush);\n }\n\n readonly #endPush = (): void => {\n this.#inPush = false;\n };\n\n #pushInPush(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE:\n return this.#pushWithFilter(change);\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n return this.#pushWithFilter(change);\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return this.#withSize(change[ChangeIndex.NODE], size => {\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n return this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n }\n return this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n return this.#pushWithFilter(change, size > 0);\n });\n case ChangeType.REMOVE:\n return this.#withSize(change[ChangeIndex.NODE], size => {\n if (size === 0) {\n if (this.#not) {\n return this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n return this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.NODE],\n ],\n },\n }),\n this,\n );\n }\n return this.#pushWithFilter(change, size > 0);\n });\n }\n return emptyArray;\n default:\n unreachable(change);\n }\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n if (def.length === 1) {\n return valueToKey(node.row[def[0]]);\n }\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n #pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (exists === undefined) {\n return this.#withSize(change[ChangeIndex.NODE], size =>\n this.#pushIfPasses(change, size > 0),\n );\n }\n return this.#pushIfPasses(change, exists);\n }\n\n #pushIfPasses(change: Change, exists: boolean): Stream<'yield'> {\n return (this.#not ? !exists : exists)\n ? this.#output.push(change, this)\n : emptyArray;\n }\n\n /**\n * Calls `rest` with the size of the node's relationship: right away, or\n * once the stream returned has been read if reading the relationship yields.\n */\n #withSize(\n node: Node,\n rest: (size: number) => Stream<'yield'>,\n ): Stream<'yield'> {\n const size = this.#fetchSizeNow(node);\n return typeof size === 'number'\n ? rest(size)\n : this.#withSizeRest(node, size, rest);\n }\n\n *#withSizeRest(\n node: Node,\n read: PartialRead,\n rest: (size: number) => Stream<'yield'>,\n ): Stream<'yield'> {\n yield* rest(yield* this.#fetchSizeRest(node, read));\n }\n\n /**\n * The size of the node's this.#relationshipName relationship, or, if\n * reading it yields, what was read up to there, for #fetchSizeRest.\n */\n #fetchSizeNow(node: Node): number | PartialRead {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n if (this.#inPush) {\n // Only the size is needed, which Cap can answer from its state.\n let size = 0;\n const it = relationship(true)[Symbol.iterator]();\n for (;;) {\n const r = it.next();\n if (r.done) {\n return size;\n }\n if (r.value === 'yield') {\n return {it, size, children: undefined};\n }\n size++;\n }\n }\n // During a fetch, keep the children read to count them and make the\n // node's relationship return them, so that the consumer of the node does\n // not fetch them again. The exists subquery is limited, so this holds at\n // most a few nodes. `node.relationships` is created per node by the Join.\n const children: Node[] = [];\n const it = relationship()[Symbol.iterator]();\n for (;;) {\n const r = it.next();\n if (r.done) {\n break;\n }\n if (r.value === 'yield') {\n return {it, size: 0, children};\n }\n children.push(r.value);\n }\n node.relationships[this.#relationshipName] = () => children;\n return children.length;\n }\n\n /** #fetchSizeNow from the 'yield' it stopped at. */\n *#fetchSizeRest(\n node: Node,\n {it, size, children}: PartialRead,\n ): IterableIterator<'yield', number> {\n let ended = false;\n try {\n yield 'yield';\n for (;;) {\n const r = it.next();\n if (r.done) {\n ended = true;\n break;\n }\n if (r.value === 'yield') {\n yield 'yield';\n } else if (children) {\n children.push(r.value);\n } else {\n size++;\n }\n }\n } finally {\n // The consumer stopped reading at a 'yield'.\n if (!ended) {\n it.return?.();\n }\n }\n if (children) {\n node.relationships[this.#relationshipName] = () => children;\n return children.length;\n }\n return size;\n }\n}\n\nconst emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);\n\n/** A relationship read that stopped at a 'yield'. */\ntype PartialRead = {\n readonly it: Iterator<Node | 'yield'>;\n /** Nodes counted so far, when only the size is wanted. */\n readonly size: number;\n /** Nodes read so far, when they are kept. */\n readonly children: Node[] | undefined;\n};\n"],"mappings":";;;;;;;;;;;;;AA4BA,IAAa,SAAb,MAA8C;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA,UAAwB;;;;;;;;;CAUxB,UAAU;CAEV,YACE,OACA,kBACA,eACA,MACA,0BACA;EACA,KAAK,SAAS;E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in.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/fan-in.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAC,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACzC,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AAE7C,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC;;;;;;;;;;;;;GAaG;AACH,qBAAa,KAAM,YAAW,cAAc;;IAM1C,YAAY,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,EAOhD;IAED,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAE1C;IAED,OAAO,IAAI,IAAI,CAId;IAED,SAAS,iBAER;IAED,WAAW,IAAI,IAAI,CAElB;IAED,SAAS,IAAI,IAAI,CAEhB;IAED,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAErD;IAED,IAAI,CAAC,MAAM,EAAE,MAAM,oBAGlB;IAED,8BAA8B,CAAC,gBAAgB,EAAE,UAAU,sDAiB1D;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAG7C;IAEA,qBAAqB,IAAI,MAAM,CAAC,OAAO,CAAC,CAOxC;CACF"}
1
+ {"version":3,"file":"fan-in.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/fan-in.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAC,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACzC,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AAE7C,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC;;;;;;;;;;;;;GAaG;AACH,qBAAa,KAAM,YAAW,cAAc;;IAM1C,YAAY,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,EAOhD;IAED,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAE1C;IAED,OAAO,IAAI,IAAI,CAId;IAED,SAAS,iBAER;IAED,WAAW,IAAI,IAAI,CAElB;IAED,SAAS,IAAI,IAAI,CAEhB;IAED,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAErD;IAED,IAAI,CAAC,MAAM,EAAE,MAAM,oBAGlB;IAED,8BAA8B,CAAC,gBAAgB,EAAE,UAAU,sDAiB1D;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAG7C;IAED,qBAAqB,IAAI,MAAM,CAAC,OAAO,CAAC,CAKvC;CACF"}
@@ -64,9 +64,9 @@ var FanIn = class {
64
64
  reconcile(_pusher) {
65
65
  return emptyArray;
66
66
  }
67
- *fanOutDoneReconciling() {
68
- if (this.#inputs.length === 0) return;
69
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
67
+ fanOutDoneReconciling() {
68
+ if (this.#inputs.length === 0) return emptyArray;
69
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
70
70
  }
71
71
  };
72
72
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"fan-in.js","names":[],"sources":["../../../../../zql/src/ivm/fan-in.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {emptyIterator} from '../../../shared/src/iterables.ts';\nimport {emptyArray, identity} from '../../../shared/src/sentinels.ts';\nimport type {ChangeType} from './change-type.ts';\nimport {type Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FanOut} from './fan-out.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport {pushAccumulatedChanges} from './push-accumulated.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * The FanIn operator merges multiple streams into one.\n * It eliminates duplicates and must be paired with a fan-out operator\n * somewhere upstream of the fan-in.\n *\n * issue\n * |\n * fan-out\n * / \\\n * a b\n * \\ /\n * fan-in\n * |\n */\nexport class FanIn implements FilterOperator {\n readonly #inputs: readonly FilterInput[];\n readonly #schema: SourceSchema;\n #output: FilterOutput = throwFilterOutput;\n #accumulatedPushes: Change[] = [];\n\n constructor(fanOut: FanOut, inputs: FilterInput[]) {\n this.#inputs = inputs;\n this.#schema = fanOut.getSchema();\n for (const input of inputs) {\n input.setFilterOutput(this);\n assert(this.#schema === input.getSchema(), `Schema mismatch in fan-in`);\n }\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n destroy(): void {\n for (const input of this.#inputs) {\n input.destroy();\n }\n }\n\n getSchema() {\n return this.#schema;\n }\n\n beginFilter(): void {\n this.#output.beginFilter();\n }\n\n endFilter(): void {\n this.#output.endFilter();\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n return this.#output.filter(node);\n }\n\n push(change: Change) {\n this.#accumulatedPushes.push(change);\n return emptyArray;\n }\n\n fanOutDonePushingToAllBranches(fanOutChangeType: ChangeType) {\n if (this.#inputs.length === 0) {\n assert(\n this.#accumulatedPushes.length === 0,\n 'If there are no inputs then fan-in should not receive any pushes.',\n );\n return emptyIterator;\n }\n\n return pushAccumulatedChanges(\n this.#accumulatedPushes,\n this.#output,\n this,\n fanOutChangeType,\n identity,\n identity,\n );\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n // Reconcile is coordinated by FanOut, which calls fanOutDoneReconciling after all branches complete.\n return emptyArray;\n }\n\n *fanOutDoneReconciling(): Stream<'yield'> {\n if (this.#inputs.length === 0) {\n return;\n }\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAgCA,IAAa,QAAb,MAA6C;CAC3C;CACA;CACA,UAAwB;CACxB,qBAA+B,CAAC;CAEhC,YAAY,QAAgB,QAAuB;EACjD,KAAK,UAAU;EACf,KAAK,UAAU,OAAO,UAAU;EAChC,KAAK,MAAM,SAAS,QAAQ;GAC1B,MAAM,gBAAgB,IAAI;GAC1B,OAAO,KAAK,YAAY,MAAM,UAAU,GAAG,2BAA2B;EACxE;CACF;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,MAAM,SAAS,KAAK,SACvB,MAAM,QAAQ;CAElB;CAEA,YAAY;EACV,OAAO,KAAK;CACd;CAEA,cAAoB;EAClB,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAkB;EAChB,KAAK,QAAQ,UAAU;CACzB;CAEA,OAAO,MAAgD;EACrD,OAAO,KAAK,QAAQ,OAAO,IAAI;CACjC;CAEA,KAAK,QAAgB;EACnB,KAAK,mBAAmB,KAAK,MAAM;EACnC,OAAO;CACT;CAEA,+BAA+B,kBAA8B;EAC3D,IAAI,KAAK,QAAQ,WAAW,GAAG;GAC7B,OACE,KAAK,mBAAmB,WAAW,GACnC,mEACF;GACA,OAAO;EACT;EAEA,OAAO,uBACL,KAAK,oBACL,KAAK,SACL,MACA,kBACA,UACA,QACF;CACF;CAEA,UAAU,SAAqC;EAE7C,OAAO;CACT;CAEA,CAAC,wBAAyC;EACxC,IAAI,KAAK,QAAQ,WAAW,GAC1B;EAEF,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;AACF"}
1
+ {"version":3,"file":"fan-in.js","names":[],"sources":["../../../../../zql/src/ivm/fan-in.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {emptyIterator} from '../../../shared/src/iterables.ts';\nimport {emptyArray, identity} from '../../../shared/src/sentinels.ts';\nimport type {ChangeType} from './change-type.ts';\nimport {type Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FanOut} from './fan-out.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport {pushAccumulatedChanges} from './push-accumulated.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * The FanIn operator merges multiple streams into one.\n * It eliminates duplicates and must be paired with a fan-out operator\n * somewhere upstream of the fan-in.\n *\n * issue\n * |\n * fan-out\n * / \\\n * a b\n * \\ /\n * fan-in\n * |\n */\nexport class FanIn implements FilterOperator {\n readonly #inputs: readonly FilterInput[];\n readonly #schema: SourceSchema;\n #output: FilterOutput = throwFilterOutput;\n #accumulatedPushes: Change[] = [];\n\n constructor(fanOut: FanOut, inputs: FilterInput[]) {\n this.#inputs = inputs;\n this.#schema = fanOut.getSchema();\n for (const input of inputs) {\n input.setFilterOutput(this);\n assert(this.#schema === input.getSchema(), `Schema mismatch in fan-in`);\n }\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n destroy(): void {\n for (const input of this.#inputs) {\n input.destroy();\n }\n }\n\n getSchema() {\n return this.#schema;\n }\n\n beginFilter(): void {\n this.#output.beginFilter();\n }\n\n endFilter(): void {\n this.#output.endFilter();\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n return this.#output.filter(node);\n }\n\n push(change: Change) {\n this.#accumulatedPushes.push(change);\n return emptyArray;\n }\n\n fanOutDonePushingToAllBranches(fanOutChangeType: ChangeType) {\n if (this.#inputs.length === 0) {\n assert(\n this.#accumulatedPushes.length === 0,\n 'If there are no inputs then fan-in should not receive any pushes.',\n );\n return emptyIterator;\n }\n\n return pushAccumulatedChanges(\n this.#accumulatedPushes,\n this.#output,\n this,\n fanOutChangeType,\n identity,\n identity,\n );\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n // Reconcile is coordinated by FanOut, which calls fanOutDoneReconciling after all branches complete.\n return emptyArray;\n }\n\n fanOutDoneReconciling(): Stream<'yield'> {\n if (this.#inputs.length === 0) {\n return emptyArray;\n }\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAgCA,IAAa,QAAb,MAA6C;CAC3C;CACA;CACA,UAAwB;CACxB,qBAA+B,CAAC;CAEhC,YAAY,QAAgB,QAAuB;EACjD,KAAK,UAAU;EACf,KAAK,UAAU,OAAO,UAAU;EAChC,KAAK,MAAM,SAAS,QAAQ;GAC1B,MAAM,gBAAgB,IAAI;GAC1B,OAAO,KAAK,YAAY,MAAM,UAAU,GAAG,2BAA2B;EACxE;CACF;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,MAAM,SAAS,KAAK,SACvB,MAAM,QAAQ;CAElB;CAEA,YAAY;EACV,OAAO,KAAK;CACd;CAEA,cAAoB;EAClB,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAkB;EAChB,KAAK,QAAQ,UAAU;CACzB;CAEA,OAAO,MAAgD;EACrD,OAAO,KAAK,QAAQ,OAAO,IAAI;CACjC;CAEA,KAAK,QAAgB;EACnB,KAAK,mBAAmB,KAAK,MAAM;EACnC,OAAO;CACT;CAEA,+BAA+B,kBAA8B;EAC3D,IAAI,KAAK,QAAQ,WAAW,GAAG;GAC7B,OACE,KAAK,mBAAmB,WAAW,GACnC,mEACF;GACA,OAAO;EACT;EAEA,OAAO,uBACL,KAAK,oBACL,KAAK,SACL,MACA,kBACA,UACA,QACF;CACF;CAEA,UAAU,SAAqC;EAE7C,OAAO;CACT;CAEA,wBAAyC;EACvC,IAAI,KAAK,QAAQ,WAAW,GAC1B,OAAO;EAET,OAAO,KAAK,QAAQ,YAAY,KAAK,QAAQ,UAAU,IAAI,IAAI;CACjE;AACF"}
@@ -3,7 +3,7 @@ import type { Node } from './data.ts';
3
3
  import type { FanIn } from './fan-in.ts';
4
4
  import type { FilterInput, FilterOperator, FilterOutput } from './filter-operators.ts';
5
5
  import type { InputBase } from './operator.ts';
6
- import type { Stream } from './stream.ts';
6
+ import { type Stream } from './stream.ts';
7
7
  /**
8
8
  * Forks a stream into multiple streams.
9
9
  * Is meant to be paired with a `FanIn` operator which will
@@ -19,7 +19,7 @@ export declare class FanOut implements FilterOperator {
19
19
  beginFilter(): void;
20
20
  endFilter(): void;
21
21
  filter(node: Node): IterableIterator<'yield', boolean>;
22
- push(change: Change): Generator<any, void, any>;
22
+ push(change: Change): Stream<'yield'>;
23
23
  reconcile(_pusher: InputBase): Stream<'yield'>;
24
24
  }
25
25
  //# sourceMappingURL=fan-out.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fan-out.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/fan-out.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,YAAY,EACb,MAAM,uBAAu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AE1C;IAED,OAAO,IAAI,IAAI,CASd;IAED,SAAS,uCAER;IAED,WAAW,IAAI,IAAI,CAIlB;IAED,SAAS,IAAI,IAAI,CAIhB;IAED,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAcrD;IAqCD,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAapC;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAe7C;CACF"}
@@ -1,4 +1,7 @@
1
+ import { makeEmptyIteratorWithReturn } from "../../../shared/src/iterables.js";
1
2
  import { must } from "../../../shared/src/must.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
4
+ import { andThen, eachStream } from "./stream.js";
2
5
  //#region ../zql/src/ivm/fan-out.ts
3
6
  /**
4
7
  * Forks a stream into multiple streams.
@@ -35,23 +38,55 @@ var FanOut = class {
35
38
  endFilter() {
36
39
  for (const output of this.#outputs) output.endFilter();
37
40
  }
38
- *filter(node) {
39
- let result = false;
40
- for (const output of this.#outputs) {
41
- result = (yield* output.filter(node)) || result;
42
- if (result) return true;
41
+ filter(node) {
42
+ const outputs = this.#outputs;
43
+ for (let i = 0; i < outputs.length; i++) {
44
+ const it = outputs[i].filter(node);
45
+ const r = it.next();
46
+ if (!r.done) return this.#filterRest(node, i, it);
47
+ if (r.value) return emptyIteratorReturnTrue;
43
48
  }
44
- return result;
49
+ return emptyIteratorReturnFalse;
45
50
  }
46
- *push(change) {
47
- for (const out of this.#outputs) yield* out.push(change, this);
48
- yield* must(this.#fanIn, "fan-out must have a corresponding fan-in set!").fanOutDonePushingToAllBranches(change[0]);
51
+ /** filter() from the branch `i`, whose answer `it` has just yielded. */
52
+ *#filterRest(node, i, it) {
53
+ let answered = false;
54
+ try {
55
+ yield "yield";
56
+ for (;;) {
57
+ const r = it.next();
58
+ if (r.done) {
59
+ answered = true;
60
+ if (r.value) return true;
61
+ break;
62
+ }
63
+ yield r.value;
64
+ }
65
+ } finally {
66
+ if (!answered) it.return?.();
67
+ }
68
+ const outputs = this.#outputs;
69
+ for (i++; i < outputs.length; i++) if (yield* outputs[i].filter(node)) return true;
70
+ return false;
71
+ }
72
+ push(change) {
73
+ const outputs = this.#outputs;
74
+ let i = 0;
75
+ return andThen(eachStream(() => i === outputs.length ? void 0 : outputs[i++].push(change, this)), () => must(this.#fanIn, "fan-out must have a corresponding fan-in set!").fanOutDonePushingToAllBranches(change[0]));
49
76
  }
50
- *reconcile(_pusher) {
51
- for (const output of this.#outputs) if (output.reconcile) yield* output.reconcile(this);
52
- if (this.#fanIn) yield* this.#fanIn.fanOutDoneReconciling();
77
+ reconcile(_pusher) {
78
+ const outputs = this.#outputs;
79
+ let i = 0;
80
+ return andThen(eachStream(() => {
81
+ while (i < outputs.length) {
82
+ const output = outputs[i++];
83
+ if (output.reconcile) return output.reconcile(this);
84
+ }
85
+ }), () => this.#fanIn ? this.#fanIn.fanOutDoneReconciling() : emptyArray);
53
86
  }
54
87
  };
88
+ var emptyIteratorReturnTrue = makeEmptyIteratorWithReturn(true);
89
+ var emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);
55
90
  //#endregion
56
91
  export { FanOut };
57
92
 
@@ -1 +1 @@
1
- {"version":3,"file":"fan-out.js","names":[],"sources":["../../../../../zql/src/ivm/fan-out.ts"],"sourcesContent":["import {must} from '../../../shared/src/must.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport type {Change} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {FanIn} from './fan-in.ts';\nimport type {\n FilterInput,\n FilterOperator,\n FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * Forks a stream into multiple streams.\n * Is meant to be paired with a `FanIn` operator which will\n * later merge the forks back together.\n */\nexport class FanOut implements FilterOperator {\n readonly #input: FilterInput;\n readonly #outputs: FilterOutput[] = [];\n #fanIn: FanIn | undefined;\n #destroyCount: number = 0;\n\n constructor(input: FilterInput) {\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n setFanIn(fanIn: FanIn) {\n this.#fanIn = fanIn;\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#outputs.push(output);\n }\n\n destroy(): void {\n if (this.#destroyCount < this.#outputs.length) {\n ++this.#destroyCount;\n if (this.#destroyCount === this.#outputs.length) {\n this.#input.destroy();\n }\n } else {\n throw new Error('FanOut already destroyed once for each output');\n }\n }\n\n getSchema() {\n return this.#input.getSchema();\n }\n\n beginFilter(): void {\n for (const output of this.#outputs) {\n output.beginFilter();\n }\n }\n\n endFilter(): void {\n for (const output of this.#outputs) {\n output.endFilter();\n }\n }\n\n *filter(node: Node): IterableIterator<'yield', boolean> {\n let result = false;\n for (const output of this.#outputs) {\n result = (yield* output.filter(node)) || result;\n if (result) {\n return true;\n }\n }\n return result;\n }\n\n *push(change: Change) {\n for (const out of this.#outputs) {\n yield* out.push(change, this);\n }\n yield* must(\n this.#fanIn,\n 'fan-out must have a corresponding fan-in set!',\n ).fanOutDonePushingToAllBranches(change[ChangeIndex.TYPE]);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n for (const output of this.#outputs) {\n if (output.reconcile) {\n yield* output.reconcile(this);\n }\n }\n if (this.#fanIn) {\n yield* this.#fanIn.fanOutDoneReconciling();\n }\n }\n}\n"],"mappings":";;;;;;;AAkBA,IAAa,SAAb,MAA8C;CAC5C;CACA,WAAoC,CAAC;CACrC;CACA,gBAAwB;CAExB,YAAY,OAAoB;EAC9B,KAAK,SAAS;EACd,MAAM,gBAAgB,IAAI;CAC5B;CAEA,SAAS,OAAc;EACrB,KAAK,SAAS;CAChB;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,SAAS,KAAK,MAAM;CAC3B;CAEA,UAAgB;EACd,IAAI,KAAK,gBAAgB,KAAK,SAAS,QAAQ;GAC7C,EAAE,KAAK;GACP,IAAI,KAAK,kBAAkB,KAAK,SAAS,QACvC,KAAK,OAAO,QAAQ;EAExB,OACE,MAAM,IAAI,MAAM,+CAA+C;CAEnE;CAEA,YAAY;EACV,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,cAAoB;EAClB,KAAK,MAAM,UAAU,KAAK,UACxB,OAAO,YAAY;CAEvB;CAEA,YAAkB;EAChB,KAAK,MAAM,UAAU,KAAK,UACxB,OAAO,UAAU;CAErB;CAEA,CAAC,OAAO,MAAgD;EACtD,IAAI,SAAS;EACb,KAAK,MAAM,UAAU,KAAK,UAAU;GAClC,UAAU,OAAO,OAAO,OAAO,IAAI,MAAM;GACzC,IAAI,QACF,OAAO;EAEX;EACA,OAAO;CACT;CAEA,CAAC,KAAK,QAAgB;EACpB,KAAK,MAAM,OAAO,KAAK,UACrB,OAAO,IAAI,KAAK,QAAQ,IAAI;EAE9B,OAAO,KACL,KAAK,QACL,+CACF,CAAC,CAAC,+BAA+B,OAAO,EAAiB;CAC3D;CAEA,CAAC,UAAU,SAAqC;EAC9C,KAAK,MAAM,UAAU,KAAK,UACxB,IAAI,OAAO,WACT,OAAO,OAAO,UAAU,IAAI;EAGhC,IAAI,KAAK,QACP,OAAO,KAAK,OAAO,sBAAsB;CAE7C;AACF"}
1
+ {"version":3,"file":"fan-out.js","names":[],"sources":["../../../../../zql/src/ivm/fan-out.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport type {Change} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {FanIn} from './fan-in.ts';\nimport type {\n FilterInput,\n FilterOperator,\n FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport {andThen, eachStream, type Stream} from './stream.ts';\n\n/**\n * Forks a stream into multiple streams.\n * Is meant to be paired with a `FanIn` operator which will\n * later merge the forks back together.\n */\nexport class FanOut implements FilterOperator {\n readonly #input: FilterInput;\n readonly #outputs: FilterOutput[] = [];\n #fanIn: FanIn | undefined;\n #destroyCount: number = 0;\n\n constructor(input: FilterInput) {\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n setFanIn(fanIn: FanIn) {\n this.#fanIn = fanIn;\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#outputs.push(output);\n }\n\n destroy(): void {\n if (this.#destroyCount < this.#outputs.length) {\n ++this.#destroyCount;\n if (this.#destroyCount === this.#outputs.length) {\n this.#input.destroy();\n }\n } else {\n throw new Error('FanOut already destroyed once for each output');\n }\n }\n\n getSchema() {\n return this.#input.getSchema();\n }\n\n beginFilter(): void {\n for (const output of this.#outputs) {\n output.beginFilter();\n }\n }\n\n endFilter(): void {\n for (const output of this.#outputs) {\n output.endFilter();\n }\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n const outputs = this.#outputs;\n for (let i = 0; i < outputs.length; i++) {\n const it = outputs[i].filter(node);\n const r = it.next();\n if (!r.done) {\n // A branch yielded: the rest is read lazily.\n return this.#filterRest(node, i, it);\n }\n if (r.value) {\n return emptyIteratorReturnTrue;\n }\n }\n return emptyIteratorReturnFalse;\n }\n\n /** filter() from the branch `i`, whose answer `it` has just yielded. */\n *#filterRest(\n node: Node,\n i: number,\n it: Iterator<'yield', boolean>,\n ): IterableIterator<'yield', boolean> {\n let answered = false;\n try {\n yield 'yield';\n for (;;) {\n const r = it.next();\n if (r.done) {\n answered = true;\n if (r.value) {\n return true;\n }\n break;\n }\n yield r.value;\n }\n } finally {\n // The consumer stopped reading while the branch was yielding.\n if (!answered) {\n it.return?.();\n }\n }\n const outputs = this.#outputs;\n for (i++; i < outputs.length; i++) {\n if (yield* outputs[i].filter(node)) {\n return true;\n }\n }\n return false;\n }\n\n push(change: Change): Stream<'yield'> {\n const outputs = this.#outputs;\n let i = 0;\n return andThen(\n eachStream(() =>\n i === outputs.length ? undefined : outputs[i++].push(change, this),\n ),\n () =>\n must(\n this.#fanIn,\n 'fan-out must have a corresponding fan-in set!',\n ).fanOutDonePushingToAllBranches(change[ChangeIndex.TYPE]),\n );\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n const outputs = this.#outputs;\n let i = 0;\n return andThen(\n eachStream(() => {\n while (i < outputs.length) {\n const output = outputs[i++];\n if (output.reconcile) {\n return output.reconcile(this);\n }\n }\n return undefined;\n }),\n () => (this.#fanIn ? this.#fanIn.fanOutDoneReconciling() : emptyArray),\n );\n }\n}\n\nconst emptyIteratorReturnTrue = makeEmptyIteratorWithReturn(true);\nconst emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);\n"],"mappings":";;;;;;;;;;AAoBA,IAAa,SAAb,MAA8C;CAC5C;CACA,WAAoC,CAAC;CACrC;CACA,gBAAwB;CAExB,YAAY,OAAoB;EAC9B,KAAK,SAAS;EACd,MAAM,gBAAgB,IAAI;CAC5B;CAEA,SAAS,OAAc;EACrB,KAAK,SAAS;CAChB;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,SAAS,KAAK,MAAM;CAC3B;CAEA,UAAgB;EACd,IAAI,KAAK,gBAAgB,KAAK,SAAS,QAAQ;GAC7C,EAAE,KAAK;GACP,IAAI,KAAK,kBAAkB,KAAK,SAAS,QACvC,KAAK,OAAO,QAAQ;EAExB,OACE,MAAM,IAAI,MAAM,+CAA+C;CAEnE;CAEA,YAAY;EACV,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,cAAoB;EAClB,KAAK,MAAM,UAAU,KAAK,UACxB,OAAO,YAAY;CAEvB;CAEA,YAAkB;EAChB,KAAK,MAAM,UAAU,KAAK,UACxB,OAAO,UAAU;CAErB;CAEA,OAAO,MAAgD;EACrD,MAAM,UAAU,KAAK;EACrB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;GACvC,MAAM,KAAK,QAAQ,EAAE,CAAC,OAAO,IAAI;GACjC,MAAM,IAAI,GAAG,KAAK;GAClB,IAAI,CAAC,EAAE,MAEL,OAAO,KAAK,YAAY,MAAM,GAAG,EAAE;GAErC,IAAI,EAAE,OACJ,OAAO;EAEX;EACA,OAAO;CACT;;CAGA,CAAC,YACC,MACA,GACA,IACoC;EACpC,IAAI,WAAW;EACf,IAAI;GACF,MAAM;GACN,SAAS;IACP,MAAM,IAAI,GAAG,KAAK;IAClB,IAAI,EAAE,MAAM;KACV,WAAW;KACX,IAAI,EAAE,OACJ,OAAO;KAET;IACF;IACA,MAAM,EAAE;GACV;EACF,UAAU;GAER,IAAI,CAAC,UACH,GAAG,SAAS;EAEhB;EACA,MAAM,UAAU,KAAK;EACrB,KAAK,KAAK,IAAI,QAAQ,QAAQ,KAC5B,IAAI,OAAO,QAAQ,EAAE,CAAC,OAAO,IAAI,GAC/B,OAAO;EAGX,OAAO;CACT;CAEA,KAAK,QAAiC;EACpC,MAAM,UAAU,KAAK;EACrB,IAAI,IAAI;EACR,OAAO,QACL,iBACE,MAAM,QAAQ,SAAS,KAAA,IAAY,QAAQ,IAAI,CAAC,KAAK,QAAQ,IAAI,CACnE,SAEE,KACE,KAAK,QACL,+CACF,CAAC,CAAC,+BAA+B,OAAO,EAAiB,CAC7D;CACF;CAEA,UAAU,SAAqC;EAC7C,MAAM,UAAU,KAAK;EACrB,IAAI,IAAI;EACR,OAAO,QACL,iBAAiB;GACf,OAAO,IAAI,QAAQ,QAAQ;IACzB,MAAM,SAAS,QAAQ;IACvB,IAAI,OAAO,WACT,OAAO,OAAO,UAAU,IAAI;GAEhC;EAEF,CAAC,SACM,KAAK,SAAS,KAAK,OAAO,sBAAsB,IAAI,UAC7D;CACF;AACF;AAEA,IAAM,0BAA0B,4BAA4B,IAAI;AAChE,IAAM,2BAA2B,4BAA4B,KAAK"}
@@ -1 +1 @@
1
- {"version":3,"file":"filter-operators.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/filter-operators.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAC3D,OA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C,MAAM,EAAE,MAAM,mBAElB;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAE7C;CACF;AAID,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,eAAe,EACzB,QAAQ,EAAE,CAAC,WAAW,EAAE,WAAW,KAAK,WAAW,EACnD,SAAS,CAAC,EAAE,mBAAmB,GAC9B,KAAK,CAQP"}
@@ -1,5 +1,7 @@
1
1
  import { makeEmptyIteratorWithReturn } from "../../../shared/src/iterables.js";
2
2
  import "./data.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
4
+ import { doneResult } from "./stream.js";
3
5
  //#region ../zql/src/ivm/filter-operators.ts
4
6
  /**
5
7
  * An implementation of FilterOutput that throws if push or filter is called.
@@ -40,29 +42,112 @@ var FilterStart = class {
40
42
  push(change) {
41
43
  return this.#output.push(change, this);
42
44
  }
43
- *reconcile(_pusher) {
44
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
45
+ reconcile(_pusher) {
46
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
45
47
  }
46
- *fetch(req) {
48
+ fetch(req) {
47
49
  const mergedFilter = mergeFilters(req.filter, this.#condition);
48
50
  const childReq = mergedFilter === req.filter ? req : {
49
51
  ...req,
50
52
  filter: mergedFilter
51
53
  };
52
- this.#output.beginFilter();
54
+ return new FilteredNodes(this.#input, childReq, this.#output);
55
+ }
56
+ };
57
+ /**
58
+ * The nodes of FilterStart's input that pass the filters. An iterator rather
59
+ * than a generator, see `mapNodes` in stream.ts.
60
+ *
61
+ * Nothing happens until the first `next()`, so that `beginFilter` and
62
+ * `endFilter` bracket the iteration, and `endFilter` runs however the
63
+ * iteration ends: at the end of the input, on an error or when the consumer
64
+ * stops early.
65
+ */
66
+ var FilteredNodes = class {
67
+ #input;
68
+ #req;
69
+ #output;
70
+ #it = void 0;
71
+ #begun = false;
72
+ #ended = false;
73
+ #pending = void 0;
74
+ #pendingResult = void 0;
75
+ constructor(input, req, output) {
76
+ this.#input = input;
77
+ this.#req = req;
78
+ this.#output = output;
79
+ }
80
+ [Symbol.iterator]() {
81
+ return this;
82
+ }
83
+ next() {
84
+ if (this.#ended) return doneResult;
85
+ let it = this.#it;
53
86
  try {
54
- for (const node of this.#input.fetch(childReq)) {
55
- if (node === "yield") {
56
- yield node;
57
- continue;
87
+ if (it === void 0) {
88
+ this.#output.beginFilter();
89
+ this.#begun = true;
90
+ it = this.#it = this.#input.fetch(this.#req)[Symbol.iterator]();
91
+ }
92
+ for (;;) {
93
+ let r;
94
+ let answer;
95
+ if (this.#pending !== void 0) {
96
+ r = this.#pendingResult;
97
+ answer = this.#pending.next();
98
+ } else {
99
+ r = it.next();
100
+ if (r.done) {
101
+ this.#end();
102
+ return r;
103
+ }
104
+ if (r.value === "yield") return r;
105
+ this.#pending = this.#output.filter(r.value);
106
+ this.#pendingResult = r;
107
+ answer = this.#pending.next();
58
108
  }
59
- if (yield* this.#output.filter(node)) yield node;
109
+ if (!answer.done) return yieldResult;
110
+ this.#pending = void 0;
111
+ this.#pendingResult = void 0;
112
+ if (answer.value) return r;
113
+ }
114
+ } catch (e) {
115
+ try {
116
+ it?.return?.();
117
+ } finally {
118
+ this.#end();
60
119
  }
61
- } finally {
62
- this.#output.endFilter();
120
+ throw e;
121
+ }
122
+ }
123
+ return() {
124
+ if (!this.#ended) {
125
+ const pending = this.#pending;
126
+ this.#pending = void 0;
127
+ this.#pendingResult = void 0;
128
+ try {
129
+ pending?.return?.();
130
+ } finally {
131
+ try {
132
+ this.#it?.return?.();
133
+ } finally {
134
+ this.#end();
135
+ }
136
+ }
137
+ }
138
+ return doneResult;
139
+ }
140
+ #end() {
141
+ if (!this.#ended) {
142
+ this.#ended = true;
143
+ if (this.#begun) this.#output.endFilter();
63
144
  }
64
145
  }
65
146
  };
147
+ var yieldResult = Object.freeze({
148
+ value: "yield",
149
+ done: false
150
+ });
66
151
  function mergeFilters(reqFilter, ownCondition) {
67
152
  if (!ownCondition) return reqFilter;
68
153
  if (!reqFilter) return ownCondition;
@@ -100,8 +185,8 @@ var FilterEnd = class {
100
185
  push(change) {
101
186
  return this.#output.push(change, this);
102
187
  }
103
- *reconcile(_pusher) {
104
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
188
+ reconcile(_pusher) {
189
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
105
190
  }
106
191
  };
107
192
  var returnTrueEmptyIterator = makeEmptyIteratorWithReturn(true);
@@ -1 +1 @@
1
- {"version":3,"file":"filter-operators.js","names":[],"sources":["../../../../../zql/src/ivm/filter-operators.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport type {BuilderDelegate} from '../builder/builder.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FetchRequest, Input, InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The `where` clause of a ZQL query is implemented using a sub-graph of\n * `FilterOperators`. This sub-graph starts with a `FilterStart` operator,\n * that adapts from the normal `Operator` `Output`, to the\n * `FilterOperator` `FilterInput`, and ends with a `FilterEnd` operator that\n * adapts from a `FilterOperator` `FilterOutput` to a normal `Operator` `Input`.\n * `FilterOperator`'s do not have `fetch` instead they have a\n * `filter(node: Node): boolean` method.\n * They also have `push` which is just like normal `Operator` push.\n * Not having a `fetch` means these `FilterOperator`'s cannot modify\n * `Node` `row`s or `relationship`s, but they shouldn't, they should just\n * filter.\n *\n * One exception: during a fetch, `Exists` replaces the relationship it read\n * with a function returning the children it read, so that the consumer does\n * not fetch them again. The relationship yields the same nodes, so other\n * branches of a `FanOut`, which see the same node object, are not affected.\n *\n * This `FilterOperator` abstraction enables much more efficient processing of\n * `fetch` for `where` clauses containing OR conditions.\n *\n * See https://github.com/rocicorp/mono/pull/4339\n */\n\nexport interface FilterInput extends InputBase {\n /** Tell the input where to send its output. */\n setFilterOutput(output: FilterOutput): void;\n}\n\nexport interface FilterOutput extends Output {\n // Lets the operator know that we're in a loop of filtering\n // nodes. E.g., so the operator can cache results for the\n // duration of the loop.\n beginFilter(): void;\n filter(node: Node): IterableIterator<'yield', boolean>;\n endFilter(): void;\n}\n\nexport interface FilterOperator extends FilterInput, FilterOutput {}\n\n/**\n * An implementation of FilterOutput that throws if push or filter is called.\n * It is used as the initial value for for an operator's output before it is\n * set.\n */\nexport const throwFilterOutput: FilterOutput = {\n // oxlint-disable-next-line require-yield\n *push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n // oxlint-disable-next-line require-yield\n *filter(_node: Node): IterableIterator<'yield', boolean> {\n throw new Error('Output not set');\n },\n\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n beginFilter() {},\n endFilter() {},\n};\n\nexport class FilterStart implements FilterInput, Output {\n readonly #input: Input;\n readonly #condition: NoSubqueryCondition | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: Input, condition?: NoSubqueryCondition) {\n this.#input = input;\n this.#condition = condition;\n input.setOutput(this);\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const mergedFilter = mergeFilters(req.filter, this.#condition);\n const childReq =\n mergedFilter === req.filter ? req : {...req, filter: mergedFilter};\n this.#output.beginFilter();\n try {\n for (const node of this.#input.fetch(childReq)) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (yield* this.#output.filter(node)) {\n yield node;\n }\n }\n } finally {\n // finally is important if an exception is thrown or\n // if the stream is not fully consumed.\n this.#output.endFilter();\n }\n }\n}\n\nfunction mergeFilters(\n reqFilter: NoSubqueryCondition | undefined,\n ownCondition: NoSubqueryCondition | undefined,\n): NoSubqueryCondition | undefined {\n if (!ownCondition) {\n return reqFilter;\n }\n if (!reqFilter) {\n return ownCondition;\n }\n return {type: 'and', conditions: [reqFilter, ownCondition]};\n}\n\nexport class FilterEnd implements Input, FilterOutput {\n readonly #start: FilterStart;\n readonly #input: FilterInput;\n\n #output: Output = throwFilterOutput;\n\n constructor(start: FilterStart, input: FilterInput) {\n this.#start = start;\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return this.#start.fetch(req);\n }\n\n beginFilter() {}\n endFilter() {}\n\n filter(_node: Node) {\n return returnTrueEmptyIterator;\n }\n\n setOutput(output: Output) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nconst returnTrueEmptyIterator = makeEmptyIteratorWithReturn(true);\n\nexport function buildFilterPipeline(\n input: Input,\n delegate: BuilderDelegate,\n pipeline: (filterInput: FilterInput) => FilterInput,\n condition?: NoSubqueryCondition,\n): Input {\n const filterStart = new FilterStart(input, condition);\n delegate.addEdge(input, filterStart);\n const middle = pipeline(filterStart);\n delegate.addEdge(filterStart, middle);\n const filterEnd = new FilterEnd(filterStart, middle);\n delegate.addEdge(middle, filterEnd);\n return filterEnd;\n}\n"],"mappings":";;;;;;;;AAsDA,IAAa,oBAAkC;CAE7C,CAAC,KAAK,SAAkC;EACtC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAGA,CAAC,OAAO,OAAiD;EACvD,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAg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MAAM,GAAG;CAC9B;CAEA,cAAc,CAAC;CACf,YAAY,CAAC;CAEb,OAAO,OAAa;EAClB,OAAO;CACT;CAEA,UAAU,QAAgB;EACxB,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,KAAK,QAAgB;EACnB,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CACvC;CAEA,CAAC,UAAU,SAAqC;EAC9C,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;AACF;AAEA,IAAM,0BAA0B,4BAA4B,IAAI;AAEhE,SAAgB,oBACd,OACA,UACA,UACA,WACO;CACP,MAAM,cAAc,IAAI,YAAY,OAAO,SAAS;CACpD,SAAS,QAAQ,OAAO,WAAW;CACnC,MAAM,SAAS,SAAS,WAAW;CACnC,SAAS,QAAQ,aAAa,MAAM;CACpC,MAAM,YAAY,IAAI,UAAU,aAAa,MAAM;CACnD,SAAS,QAAQ,QAAQ,SAAS;CAClC,OAAO;AACT"}
1
+ {"version":3,"file":"filter-operators.js","names":[],"sources":["../../../../../zql/src/ivm/filter-operators.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {BuilderDelegate} from '../builder/builder.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FetchRequest, Input, InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {doneResult, type Stream} from './stream.ts';\n\n/**\n * The `where` clause of a ZQL query is implemented using a sub-graph of\n * `FilterOperators`. This sub-graph starts with a `FilterStart` operator,\n * that adapts from the normal `Operator` `Output`, to the\n * `FilterOperator` `FilterInput`, and ends with a `FilterEnd` operator that\n * adapts from a `FilterOperator` `FilterOutput` to a normal `Operator` `Input`.\n * `FilterOperator`'s do not have `fetch` instead they have a\n * `filter(node: Node): boolean` method.\n * They also have `push` which is just like normal `Operator` push.\n * Not having a `fetch` means these `FilterOperator`'s cannot modify\n * `Node` `row`s or `relationship`s, but they shouldn't, they should just\n * filter.\n *\n * One exception: during a fetch, `Exists` replaces the relationship it read\n * with a function returning the children it read, so that the consumer does\n * not fetch them again. The relationship yields the same nodes, so other\n * branches of a `FanOut`, which see the same node object, are not affected.\n *\n * This `FilterOperator` abstraction enables much more efficient processing of\n * `fetch` for `where` clauses containing OR conditions.\n *\n * See https://github.com/rocicorp/mono/pull/4339\n */\n\nexport interface FilterInput extends InputBase {\n /** Tell the input where to send its output. */\n setFilterOutput(output: FilterOutput): void;\n}\n\nexport interface FilterOutput extends Output {\n // Lets the operator know that we're in a loop of filtering\n // nodes. E.g., so the operator can cache results for the\n // duration of the loop.\n beginFilter(): void;\n filter(node: Node): IterableIterator<'yield', boolean>;\n endFilter(): void;\n}\n\nexport interface FilterOperator extends FilterInput, FilterOutput {}\n\n/**\n * An implementation of FilterOutput that throws if push or filter is called.\n * It is used as the initial value for for an operator's output before it is\n * set.\n */\nexport const throwFilterOutput: FilterOutput = {\n // oxlint-disable-next-line require-yield\n *push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n // oxlint-disable-next-line require-yield\n *filter(_node: Node): IterableIterator<'yield', boolean> {\n throw new Error('Output not set');\n },\n\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n beginFilter() {},\n endFilter() {},\n};\n\nexport class FilterStart implements FilterInput, Output {\n readonly #input: Input;\n readonly #condition: NoSubqueryCondition | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: Input, condition?: NoSubqueryCondition) {\n this.#input = input;\n this.#condition = condition;\n input.setOutput(this);\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const mergedFilter = mergeFilters(req.filter, this.#condition);\n const childReq =\n mergedFilter === req.filter ? req : {...req, filter: mergedFilter};\n return new FilteredNodes(this.#input, childReq, this.#output);\n }\n}\n\n/**\n * The nodes of FilterStart's input that pass the filters. An iterator rather\n * than a generator, see `mapNodes` in stream.ts.\n *\n * Nothing happens until the first `next()`, so that `beginFilter` and\n * `endFilter` bracket the iteration, and `endFilter` runs however the\n * iteration ends: at the end of the input, on an error or when the consumer\n * stops early.\n */\nclass FilteredNodes implements IterableIterator<Node | 'yield'> {\n readonly #input: Input;\n readonly #req: FetchRequest;\n readonly #output: FilterOutput;\n #it: Iterator<Node | 'yield'> | undefined = undefined;\n #begun = false;\n #ended = false;\n // A filter() answer that yielded, and the result holding its node.\n #pending: Iterator<'yield', boolean> | undefined = undefined;\n #pendingResult: IteratorResult<Node | 'yield'> | undefined = undefined;\n\n constructor(input: Input, req: FetchRequest, output: FilterOutput) {\n this.#input = input;\n this.#req = req;\n this.#output = output;\n }\n\n [Symbol.iterator](): this {\n return this;\n }\n\n next(): IteratorResult<Node | 'yield'> {\n if (this.#ended) {\n return doneResult;\n }\n let it = this.#it;\n try {\n if (it === undefined) {\n // A filter that failed to begin is not ended.\n this.#output.beginFilter();\n this.#begun = true;\n it = this.#it = this.#input.fetch(this.#req)[Symbol.iterator]();\n }\n for (;;) {\n let r: IteratorResult<Node | 'yield'>;\n let answer: IteratorResult<'yield', boolean>;\n if (this.#pending !== undefined) {\n // oxlint-disable-next-line typescript/no-non-null-assertion\n r = this.#pendingResult!;\n answer = this.#pending.next();\n } else {\n r = it.next();\n if (r.done) {\n this.#end();\n return r;\n }\n if (r.value === 'yield') {\n return r;\n }\n this.#pending = this.#output.filter(r.value);\n this.#pendingResult = r;\n answer = this.#pending.next();\n }\n if (!answer.done) {\n return yieldResult;\n }\n this.#pending = undefined;\n this.#pendingResult = undefined;\n if (answer.value) {\n return r;\n }\n }\n } catch (e) {\n // As a `for...of` does when its body throws.\n try {\n it?.return?.();\n } finally {\n this.#end();\n }\n throw e;\n }\n }\n\n return(): IteratorResult<Node | 'yield'> {\n if (!this.#ended) {\n // A filter that has yielded is closed first, then the input, as a\n // generator suspended in `yield* filter(node)` inside its `for...of`\n // would close them.\n const pending = this.#pending;\n this.#pending = undefined;\n this.#pendingResult = undefined;\n try {\n pending?.return?.();\n } finally {\n try {\n this.#it?.return?.();\n } finally {\n this.#end();\n }\n }\n }\n return doneResult;\n }\n\n #end() {\n if (!this.#ended) {\n this.#ended = true;\n if (this.#begun) {\n this.#output.endFilter();\n }\n }\n }\n}\n\nconst yieldResult: IteratorResult<Node | 'yield'> = Object.freeze({\n value: 'yield',\n done: false,\n});\n\nfunction mergeFilters(\n reqFilter: NoSubqueryCondition | undefined,\n ownCondition: NoSubqueryCondition | undefined,\n): NoSubqueryCondition | undefined {\n if (!ownCondition) {\n return reqFilter;\n }\n if (!reqFilter) {\n return ownCondition;\n }\n return {type: 'and', conditions: [reqFilter, ownCondition]};\n}\n\nexport class FilterEnd implements Input, FilterOutput {\n readonly #start: FilterStart;\n readonly #input: FilterInput;\n\n #output: Output = throwFilterOutput;\n\n constructor(start: FilterStart, input: FilterInput) {\n this.#start = start;\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return this.#start.fetch(req);\n }\n\n beginFilter() {}\n endFilter() {}\n\n filter(_node: Node) {\n return returnTrueEmptyIterator;\n }\n\n setOutput(output: Output) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n}\n\nconst returnTrueEmptyIterator = makeEmptyIteratorWithReturn(true);\n\nexport function buildFilterPipeline(\n input: Input,\n delegate: BuilderDelegate,\n pipeline: (filterInput: FilterInput) => FilterInput,\n condition?: NoSubqueryCondition,\n): Input {\n const filterStart = new FilterStart(input, condition);\n delegate.addEdge(input, filterStart);\n const middle = pipeline(filterStart);\n delegate.addEdge(filterStart, middle);\n const filterEnd = new FilterEnd(filterStart, middle);\n delegate.addEdge(middle, filterEnd);\n return filterEnd;\n}\n"],"mappings":";;;;;;;;;;AAuDA,IAAa,oBAAkC;CAE7C,CAAC,KAAK,SAAkC;EACtC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAGA,CAAC,OAAO,OAAiD;EACvD,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,cAAc,CAAC;CACf,YAAY,CAAC;AACf;AAEA,IAAa,cAAb,MAAwD;CACtD;CACA;CACA,UAAwB;CAExB,YAAY,OAAc,WAAiC;EACzD,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,MAAM,UAAU,IAAI;CACtB;CAEA,gBAAgB,QAAsB;EACpC,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,KAAK,QAAgB;EACnB,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CACvC;CAEA,UAAU,SAAqC;EAC7C,OAAO,KAAK,QAAQ,YAAY,KAAK,QAAQ,UAAU,IAAI,IAAI;CACjE;CAEA,MAAM,KAA2C;EAC/C,MAAM,eAAe,aAAa,IAAI,QAAQ,KAAK,UAAU;EAC7D,MAAM,WACJ,iBAAiB,IAAI,SAAS,MAAM;GAAC,GAAG;GAAK,QAAQ;EAAY;EACnE,OAAO,IAAI,cAAc,KAAK,QAAQ,UAAU,KAA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MAAM;CACpC,MAAM,YAAY,IAAI,UAAU,aAAa,MAAM;CACnD,SAAS,QAAQ,QAAQ,SAAS;CAClC,OAAO;AACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"filter-push.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/filter-push.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAG5D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAC,SAAS,EAAE,MAAM,EAAC,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,wBAAiB,UAAU,CACzB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,SAAS,EACjB,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,GAChC,MAAM,CAAC,OAAO,CAAC,CAuBjB"}
1
+ {"version":3,"file":"filter-push.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/filter-push.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAG5D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAC,SAAS,EAAE,MAAM,EAAC,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,SAAS,EACjB,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,GAChC,MAAM,CAAC,OAAO,CAAC,CAgBjB"}
@@ -1,22 +1,14 @@
1
1
  import { unreachable } from "../../../shared/src/asserts.js";
2
+ import { emptyArray } from "../../../shared/src/sentinels.js";
2
3
  import { maybeSplitAndPushEditChange } from "./maybe-split-and-push-edit-change.js";
3
4
  //#region ../zql/src/ivm/filter-push.ts
4
- function* filterPush(change, output, pusher, predicate) {
5
- if (!predicate) {
6
- yield* output.push(change, pusher);
7
- return;
8
- }
5
+ function filterPush(change, output, pusher, predicate) {
6
+ if (!predicate) return output.push(change, pusher);
9
7
  switch (change[0]) {
10
8
  case 0:
11
9
  case 1:
12
- if (predicate(change[1].row)) yield* output.push(change, pusher);
13
- break;
14
- case 3:
15
- if (predicate(change[1].row)) yield* output.push(change, pusher);
16
- break;
17
- case 2:
18
- yield* maybeSplitAndPushEditChange(change, predicate, output, pusher);
19
- break;
10
+ case 3: return predicate(change[1].row) ? output.push(change, pusher) : emptyArray;
11
+ case 2: return maybeSplitAndPushEditChange(change, predicate, output, pusher);
20
12
  default: unreachable(change);
21
13
  }
22
14
  }
@@ -1 +1 @@
1
- {"version":3,"file":"filter-push.js","names":[],"sources":["../../../../../zql/src/ivm/filter-push.ts"],"sourcesContent":["import {unreachable} from '../../../shared/src/asserts.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {maybeSplitAndPushEditChange} from './maybe-split-and-push-edit-change.ts';\nimport type {InputBase, Output} from './operator.ts';\nimport type {Stream} from './stream.ts';\n\nexport function* filterPush(\n change: Change,\n output: Output,\n pusher: InputBase,\n predicate?: (row: Row) => boolean,\n): Stream<'yield'> {\n if (!predicate) {\n yield* output.push(change, pusher);\n return;\n }\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.REMOVE:\n if (predicate(change[ChangeIndex.NODE].row)) {\n yield* output.push(change, pusher);\n }\n break;\n case ChangeType.CHILD:\n if (predicate(change[ChangeIndex.NODE].row)) {\n yield* output.push(change, pusher);\n }\n break;\n case ChangeType.EDIT:\n yield* maybeSplitAndPushEditChange(change, predicate, output, pusher);\n break;\n default:\n unreachable(change);\n }\n}\n"],"mappings":";;;AASA,UAAiB,WACf,QACA,QACA,QACA,WACiB;CACjB,IAAI,CAAC,WAAW;EACd,OAAO,OAAO,KAAK,QAAQ,MAAM;EACjC;CACF;CACA,QAAQ,OAAO,IAAf;EACE,KAAK;EACL,KAAK;GACH,IAAI,UAAU,OAAO,EAAiB,CAAC,GAAG,GACxC,OAAO,OAAO,KAAK,QAAQ,MAAM;GAEnC;EACF,KAAK;GACH,IAAI,UAAU,OAAO,EAAiB,CAAC,GAAG,GACxC,OAAO,OAAO,KAAK,QAAQ,MAAM;GAEnC;EACF,KAAK;GACH,OAAO,4BAA4B,QAAQ,WAAW,QAAQ,MAAM;GACpE;EACF,SACE,YAAY,MAAM;CACtB;AACF"}
1
+ {"version":3,"file":"filter-push.js","names":[],"sources":["../../../../../zql/src/ivm/filter-push.ts"],"sourcesContent":["import {unreachable} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {maybeSplitAndPushEditChange} from './maybe-split-and-push-edit-change.ts';\nimport type {InputBase, Output} from './operator.ts';\nimport type {Stream} from './stream.ts';\n\nexport function filterPush(\n change: Change,\n output: Output,\n pusher: InputBase,\n predicate?: (row: Row) => boolean,\n): Stream<'yield'> {\n if (!predicate) {\n return output.push(change, pusher);\n }\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.REMOVE:\n case ChangeType.CHILD:\n return predicate(change[ChangeIndex.NODE].row)\n ? output.push(change, pusher)\n : emptyArray;\n case ChangeType.EDIT:\n return maybeSplitAndPushEditChange(change, predicate, output, pusher);\n default:\n unreachable(change);\n }\n}\n"],"mappings":";;;;AAUA,SAAgB,WACd,QACA,QACA,QACA,WACiB;CACjB,IAAI,CAAC,WACH,OAAO,OAAO,KAAK,QAAQ,MAAM;CAEn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CAAC,CAI9C;CACF"}
1
+ {"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/filter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAC5D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC;;;;GAIG;AACH,qBAAa,MAAO,YAAW,cAAc;;IAM3C,YAAY,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,EAI/D;IAED,WAAW,IAAI,IAAI,CAElB;IAED,SAAS,IAAI,IAAI,CAEhB;IAED,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAKrD;IAED,eAAe,CAAC,MAAM,EAAE,YAAY,QAEnC;IAED,OAAO,IAAI,IAAI,CAEd;IAED,SAAS,IAAI,YAAY,CAExB;IAED,IAAI,CAAC,MAAM,EAAE,MAAM,mBAElB;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAE7C;CACF"}
@@ -1,5 +1,6 @@
1
1
  import { makeEmptyIteratorWithReturn } from "../../../shared/src/iterables.js";
2
2
  import "./data.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
3
4
  import { throwFilterOutput } from "./filter-operators.js";
4
5
  import { filterPush } from "./filter-push.js";
5
6
  //#region ../zql/src/ivm/filter.ts
@@ -39,8 +40,8 @@ var Filter = class {
39
40
  push(change) {
40
41
  return filterPush(change, this.#output, this, this.#predicate);
41
42
  }
42
- *reconcile(_pusher) {
43
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
43
+ reconcile(_pusher) {
44
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
44
45
  }
45
46
  };
46
47
  var emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);
@@ -1 +1 @@
1
- {"version":3,"file":"filter.js","names":[],"sources":["../../../../../zql/src/ivm/filter.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport {filterPush} from './filter-push.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * The Filter operator filters data through a predicate. It is stateless.\n *\n * The predicate must be pure.\n */\nexport class Filter implements FilterOperator {\n readonly #input: FilterInput;\n readonly #predicate: (row: Row) => boolean;\n\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: FilterInput, predicate: (row: Row) => boolean) {\n this.#input = input;\n this.#predicate = predicate;\n input.setFilterOutput(this);\n }\n\n beginFilter(): void {\n this.#output.beginFilter();\n }\n\n endFilter(): void {\n this.#output.endFilter();\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n if (this.#predicate(node.row)) {\n return this.#output.filter(node);\n }\n return emptyIteratorReturnFalse;\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return filterPush(change, this.#output, this, this.#predicate);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nconst emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);\n"],"mappings":";;;;;;;;;;AAoBA,IAAa,SAAb,MAA8C;CAC5C;CACA;CAEA,UAAwB;CAExB,YAAY,OAAoB,WAAkC;EAChE,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,MAAM,gBAAgB,IAAI;CAC5B;CAEA,cAAoB;EAClB,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAkB;EAChB,KAAK,QAAQ,UAAU;CACzB;CAEA,OAAO,MAAgD;EACrD,IAAI,KAAK,WAAW,KAAK,GAAG,GAC1B,OAAO,KAAK,QAAQ,OAAO,IAAI;EAEjC,OAAO;CACT;CAEA,gBAAgB,QAAsB;EACpC,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,KAAK,QAAgB;EACnB,OAAO,WAAW,QAAQ,KAAK,SAAS,MAAM,KAAK,UAAU;CAC/D;CAEA,CAAC,UAAU,SAAqC;EAC9C,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;AACF;AAEA,IAAM,2BAA2B,4BAA4B,KAAK"}
1
+ {"version":3,"file":"filter.js","names":[],"sources":["../../../../../zql/src/ivm/filter.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport {filterPush} from './filter-push.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * The Filter operator filters data through a predicate. It is stateless.\n *\n * The predicate must be pure.\n */\nexport class Filter implements FilterOperator {\n readonly #input: FilterInput;\n readonly #predicate: (row: Row) => boolean;\n\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: FilterInput, predicate: (row: Row) => boolean) {\n this.#input = input;\n this.#predicate = predicate;\n input.setFilterOutput(this);\n }\n\n beginFilter(): void {\n this.#output.beginFilter();\n }\n\n endFilter(): void {\n this.#output.endFilter();\n }\n\n filter(node: Node): IterableIterator<'yield', boolean> {\n if (this.#predicate(node.row)) {\n return this.#output.filter(node);\n }\n return emptyIteratorReturnFalse;\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return filterPush(change, this.#output, this, this.#predicate);\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n}\n\nconst emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false);\n"],"mappings":";;;;;;;;;;;AAqBA,IAAa,SAAb,MAA8C;CAC5C;CACA;CAEA,UAAwB;CAExB,YAAY,OAAoB,WAAkC;EAChE,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,MAAM,gBAAgB,IAAI;CAC5B;CAEA,cAAoB;EAClB,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAkB;EAChB,KAAK,QAAQ,UAAU;CACzB;CAEA,OAAO,MAAgD;EACrD,IAAI,KAAK,WAAW,KAAK,GAAG,GAC1B,OAAO,KAAK,QAAQ,OAAO,IAAI;EAEjC,OAAO;CACT;CAEA,gBAAgB,QAAsB;EACpC,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,KAAK,QAAgB;EACnB,OAAO,WAAW,QAAQ,KAAK,SAAS,MAAM,KAAK,UAAU;CAC/D;CAEA,UAAU,SAAqC;EAC7C,OAAO,KAAK,QAAQ,YAAY,KAAK,QAAQ,UAAU,IAAI,IAAI;CACjE;AACF;AAEA,IAAM,2BAA2B,4BAA4B,KAAK"}
@@ -3,6 +3,7 @@ import { assignProperty } from "../../../shared/src/objects.js";
3
3
  import { binarySearch } from "../../../shared/src/binary-search.js";
4
4
  import { makeAddChange, makeChildChange, makeEditChange, makeRemoveChange } from "./change.js";
5
5
  import { throwOutput } from "./operator.js";
6
+ import "./stream.js";
6
7
  import { constraintsAreCompatible } from "./constraint.js";
7
8
  import { buildJoinConstraint, canonicalKey, generateWithOverlayNoYield, isJoinMatch, rowEqualsForCompoundKey } from "./join-utils.js";
8
9
  import { JoinIndex } from "./join-index.js";