@tanstack/db-ivm 0.1.25 → 0.2.0
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.
- package/README.md +5 -6
- package/dist/cjs/d2.d.cts +3 -3
- package/dist/cjs/graph.d.cts +2 -2
- package/dist/cjs/hashing/hash.cjs +58 -3
- package/dist/cjs/hashing/hash.cjs.map +1 -1
- package/dist/cjs/hashing/hash.d.cts +9 -0
- package/dist/cjs/hashing/index.d.cts +2 -2
- package/dist/cjs/hashing/murmur.cjs +26 -15
- package/dist/cjs/hashing/murmur.cjs.map +1 -1
- package/dist/cjs/hashing/murmur.d.cts +2 -0
- package/dist/cjs/index.cjs +25 -51
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +6 -6
- package/dist/cjs/indexes.cjs +52 -16
- package/dist/cjs/indexes.cjs.map +1 -1
- package/dist/cjs/indexes.d.cts +42 -9
- package/dist/cjs/multiset.d.cts +8 -2
- package/dist/cjs/operators/concat.d.cts +2 -2
- package/dist/cjs/operators/consolidate.d.cts +2 -2
- package/dist/cjs/operators/distinct.d.cts +2 -2
- package/dist/cjs/operators/filter.d.cts +3 -3
- package/dist/cjs/operators/groupBy.cjs +33 -36
- package/dist/cjs/operators/groupBy.cjs.map +1 -1
- package/dist/cjs/operators/groupBy.d.cts +6 -7
- package/dist/cjs/operators/groupedOrderBy.d.cts +8 -2
- package/dist/cjs/operators/groupedTopKWithFractionalIndex.cjs +3 -9
- package/dist/cjs/operators/groupedTopKWithFractionalIndex.cjs.map +1 -1
- package/dist/cjs/operators/groupedTopKWithFractionalIndex.d.cts +24 -11
- package/dist/cjs/operators/index.d.cts +15 -21
- package/dist/cjs/operators/join.cjs +0 -20
- package/dist/cjs/operators/join.cjs.map +1 -1
- package/dist/cjs/operators/join.d.cts +29 -30
- package/dist/cjs/operators/map.d.cts +3 -3
- package/dist/cjs/operators/orderBy.cjs +2 -77
- package/dist/cjs/operators/orderBy.cjs.map +1 -1
- package/dist/cjs/operators/orderBy.d.cts +10 -24
- package/dist/cjs/operators/output.d.cts +3 -3
- package/dist/cjs/operators/pipe.d.cts +1 -1
- package/dist/cjs/operators/reduce.cjs +36 -5
- package/dist/cjs/operators/reduce.cjs.map +1 -1
- package/dist/cjs/operators/reduce.d.cts +38 -5
- package/dist/cjs/operators/tap.d.cts +3 -3
- package/dist/cjs/operators/topKArray.cjs +9 -0
- package/dist/cjs/operators/topKArray.cjs.map +1 -1
- package/dist/cjs/operators/topKArray.d.cts +17 -2
- package/dist/cjs/operators/topKState.cjs +3 -0
- package/dist/cjs/operators/topKState.cjs.map +1 -1
- package/dist/cjs/operators/topKState.d.cts +45 -8
- package/dist/cjs/operators/topKWithFractionalIndex.cjs +5 -9
- package/dist/cjs/operators/topKWithFractionalIndex.cjs.map +1 -1
- package/dist/cjs/operators/topKWithFractionalIndex.d.cts +37 -9
- package/dist/cjs/types.d.cts +6 -3
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +4 -8
- package/dist/esm/hashing/hash.d.ts +9 -0
- package/dist/esm/hashing/hash.js +60 -5
- package/dist/esm/hashing/hash.js.map +1 -1
- package/dist/esm/hashing/murmur.d.ts +2 -0
- package/dist/esm/hashing/murmur.js +26 -15
- package/dist/esm/hashing/murmur.js.map +1 -1
- package/dist/esm/index.js +11 -37
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/indexes.d.ts +4 -1
- package/dist/esm/indexes.js +52 -16
- package/dist/esm/indexes.js.map +1 -1
- package/dist/esm/operators/groupBy.d.ts +1 -5
- package/dist/esm/operators/groupBy.js +33 -36
- package/dist/esm/operators/groupBy.js.map +1 -1
- package/dist/esm/operators/groupedTopKWithFractionalIndex.d.ts +1 -6
- package/dist/esm/operators/groupedTopKWithFractionalIndex.js +3 -9
- package/dist/esm/operators/groupedTopKWithFractionalIndex.js.map +1 -1
- package/dist/esm/operators/index.d.ts +0 -6
- package/dist/esm/operators/join.d.ts +1 -26
- package/dist/esm/operators/join.js +1 -21
- package/dist/esm/operators/join.js.map +1 -1
- package/dist/esm/operators/orderBy.d.ts +2 -22
- package/dist/esm/operators/orderBy.js +3 -78
- package/dist/esm/operators/orderBy.js.map +1 -1
- package/dist/esm/operators/reduce.d.ts +11 -2
- package/dist/esm/operators/reduce.js +36 -5
- package/dist/esm/operators/reduce.js.map +1 -1
- package/dist/esm/operators/topKArray.d.ts +6 -0
- package/dist/esm/operators/topKArray.js +10 -1
- package/dist/esm/operators/topKArray.js.map +1 -1
- package/dist/esm/operators/topKState.d.ts +1 -0
- package/dist/esm/operators/topKState.js +3 -0
- package/dist/esm/operators/topKState.js.map +1 -1
- package/dist/esm/operators/topKWithFractionalIndex.d.ts +7 -3
- package/dist/esm/operators/topKWithFractionalIndex.js +6 -10
- package/dist/esm/operators/topKWithFractionalIndex.js.map +1 -1
- package/dist/esm/utils.d.ts +0 -7
- package/dist/esm/utils.js.map +1 -1
- package/package.json +3 -5
- package/src/hashing/hash.ts +87 -5
- package/src/hashing/murmur.ts +31 -17
- package/src/indexes.ts +65 -16
- package/src/operators/groupBy.ts +48 -59
- package/src/operators/groupedTopKWithFractionalIndex.ts +4 -15
- package/src/operators/index.ts +0 -6
- package/src/operators/join.ts +1 -91
- package/src/operators/orderBy.ts +6 -141
- package/src/operators/reduce.ts +62 -5
- package/src/operators/topKArray.ts +15 -0
- package/src/operators/topKState.ts +7 -0
- package/src/operators/topKWithFractionalIndex.ts +15 -16
- package/src/utils.ts +0 -30
- package/dist/cjs/operators/count.cjs +0 -35
- package/dist/cjs/operators/count.cjs.map +0 -1
- package/dist/cjs/operators/count.d.cts +0 -13
- package/dist/cjs/operators/debug.cjs +0 -39
- package/dist/cjs/operators/debug.cjs.map +0 -1
- package/dist/cjs/operators/debug.d.cts +0 -16
- package/dist/cjs/operators/filterBy.cjs +0 -19
- package/dist/cjs/operators/filterBy.cjs.map +0 -1
- package/dist/cjs/operators/filterBy.d.cts +0 -9
- package/dist/cjs/operators/keying.cjs +0 -16
- package/dist/cjs/operators/keying.cjs.map +0 -1
- package/dist/cjs/operators/keying.d.cts +0 -19
- package/dist/cjs/operators/negate.cjs +0 -27
- package/dist/cjs/operators/negate.cjs.map +0 -1
- package/dist/cjs/operators/negate.d.cts +0 -13
- package/dist/cjs/operators/topK.cjs +0 -39
- package/dist/cjs/operators/topK.cjs.map +0 -1
- package/dist/cjs/operators/topK.d.cts +0 -31
- package/dist/cjs/operators/topKWithFractionalIndexBTree.d.cts +0 -28
- package/dist/esm/operators/count.d.ts +0 -13
- package/dist/esm/operators/count.js +0 -35
- package/dist/esm/operators/count.js.map +0 -1
- package/dist/esm/operators/debug.d.ts +0 -16
- package/dist/esm/operators/debug.js +0 -39
- package/dist/esm/operators/debug.js.map +0 -1
- package/dist/esm/operators/filterBy.d.ts +0 -9
- package/dist/esm/operators/filterBy.js +0 -19
- package/dist/esm/operators/filterBy.js.map +0 -1
- package/dist/esm/operators/keying.d.ts +0 -19
- package/dist/esm/operators/keying.js +0 -16
- package/dist/esm/operators/keying.js.map +0 -1
- package/dist/esm/operators/negate.d.ts +0 -13
- package/dist/esm/operators/negate.js +0 -27
- package/dist/esm/operators/negate.js.map +0 -1
- package/dist/esm/operators/topK.d.ts +0 -31
- package/dist/esm/operators/topK.js +0 -39
- package/dist/esm/operators/topK.js.map +0 -1
- package/dist/esm/operators/topKWithFractionalIndexBTree.d.ts +0 -28
- package/src/operators/count.ts +0 -51
- package/src/operators/debug.ts +0 -57
- package/src/operators/filterBy.ts +0 -30
- package/src/operators/keying.ts +0 -34
- package/src/operators/negate.ts +0 -32
- package/src/operators/topK.ts +0 -91
- package/src/operators/topKWithFractionalIndexBTree.ts +0 -297
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
11
|
-
export * from
|
|
12
|
-
export * from
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export
|
|
16
|
-
export * from './topKWithFractionalIndex.js';
|
|
17
|
-
export * from './groupedTopKWithFractionalIndex.js';
|
|
18
|
-
export * from './orderBy.js';
|
|
19
|
-
export * from './groupedOrderBy.js';
|
|
20
|
-
export * from './filterBy.js';
|
|
21
|
-
export { groupBy, groupByOperators } from './groupBy.js';
|
|
1
|
+
export * from "./pipe.cjs";
|
|
2
|
+
export * from "./map.cjs";
|
|
3
|
+
export * from "./tap.cjs";
|
|
4
|
+
export * from "./filter.cjs";
|
|
5
|
+
export * from "./concat.cjs";
|
|
6
|
+
export * from "./output.cjs";
|
|
7
|
+
export * from "./consolidate.cjs";
|
|
8
|
+
export * from "./join.cjs";
|
|
9
|
+
export * from "./reduce.cjs";
|
|
10
|
+
export * from "./distinct.cjs";
|
|
11
|
+
export * from "./topKWithFractionalIndex.cjs";
|
|
12
|
+
export * from "./groupedTopKWithFractionalIndex.cjs";
|
|
13
|
+
export * from "./orderBy.cjs";
|
|
14
|
+
export * from "./groupedOrderBy.cjs";
|
|
15
|
+
export { groupBy, groupByOperators } from "./groupBy.cjs";
|
|
@@ -130,26 +130,6 @@ function join(other, type = `inner`) {
|
|
|
130
130
|
return output;
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
|
-
function innerJoin(other) {
|
|
134
|
-
return join(other, `inner`);
|
|
135
|
-
}
|
|
136
|
-
function antiJoin(other) {
|
|
137
|
-
return join(other, `anti`);
|
|
138
|
-
}
|
|
139
|
-
function leftJoin(other) {
|
|
140
|
-
return join(other, `left`);
|
|
141
|
-
}
|
|
142
|
-
function rightJoin(other) {
|
|
143
|
-
return join(other, `right`);
|
|
144
|
-
}
|
|
145
|
-
function fullJoin(other) {
|
|
146
|
-
return join(other, `full`);
|
|
147
|
-
}
|
|
148
133
|
exports.JoinOperator = JoinOperator;
|
|
149
|
-
exports.antiJoin = antiJoin;
|
|
150
|
-
exports.fullJoin = fullJoin;
|
|
151
|
-
exports.innerJoin = innerJoin;
|
|
152
134
|
exports.join = join;
|
|
153
|
-
exports.leftJoin = leftJoin;
|
|
154
|
-
exports.rightJoin = rightJoin;
|
|
155
135
|
//# sourceMappingURL=join.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"join.cjs","sources":["../../../src/operators/join.ts"],"sourcesContent":["/**\n * # Direct Join Algorithms for Incremental View Maintenance\n *\n * High-performance join operations implementing all join types (inner, left, right, full, anti)\n * with minimal state and optimized performance.\n *\n * ## Algorithm\n *\n * For each tick, the algorithm processes incoming changes (deltas) and emits join results:\n *\n * 1. **Build deltas**: Create delta indexes from input messages using `Index.fromMultiSet()`\n * 2. **Inner results**: Emit `ΔA⋈B_old + A_old⋈ΔB + ΔA⋈ΔB` (matched pairs)\n * 3. **Outer results**: For unmatched rows, emit null-extended tuples:\n * - New unmatched rows from deltas (when opposite side empty)\n * - Presence transitions: when key goes `0→>0` (retract nulls) or `>0→0` (emit nulls)\n * 4. **Update state**: Append deltas to indexes (consolidated multiplicity tracking automatic)\n *\n * **Consolidated multiplicity tracking** enables O(1) presence checks instead of scanning index buckets.\n *\n * ## State\n *\n * **Indexes** store the actual data:\n * - `indexA: Index<K, V1>` - all left-side rows accumulated over time\n * - `indexB: Index<K, V2>` - all right-side rows accumulated over time\n *\n * **Consolidated multiplicity tracking** (built into Index):\n * - Each Index maintains sum of multiplicities per key internally\n * - Provides O(1) presence checks: `index.hasPresence(key)` and `index.getConsolidatedMultiplicity(key)`\n * - Avoids scanning entire index buckets just to check if key has any rows\n *\n * ## Join Types\n *\n * - **Inner**: Standard delta terms only\n * - **Outer**: Inner results + null-extended unmatched rows with transition handling\n * - **Anti**: Unmatched rows only (no inner results)\n *\n * ## Key Optimizations\n *\n * - **No temp copying**: Uses `(A⊎ΔA)⋈ΔB = A⋈ΔB ⊎ ΔA⋈ΔB` distributive property\n * - **Early-out checks**: Skip phases when no deltas present\n * - **Zero-entry pruning**: Keep maps compact, O(distinct keys) memory\n * - **Final presence logic**: Avoid emit→retract churn within same tick\n *\n * ## Correctness\n *\n * - **Ordering**: Pre-append snapshots for emissions, post-emit state updates\n * - **Presence**: Key matched iff mass ≠ 0, transitions trigger null handling\n * - **Bag semantics**: Proper multiplicity handling including negatives\n */\n\nimport { BinaryOperator, DifferenceStreamWriter } from '../graph.js'\nimport { StreamBuilder } from '../d2.js'\nimport { MultiSet } from '../multiset.js'\nimport { Index } from '../indexes.js'\nimport type { DifferenceStreamReader } from '../graph.js'\nimport type { IStreamBuilder, KeyValue, PipedOperator } from '../types.js'\n\n/**\n * Type of join to perform\n */\nexport type JoinType = `inner` | `left` | `right` | `full` | `anti`\n\n/**\n * Operator that joins two input streams using direct join algorithms\n */\nexport class JoinOperator<K, V1, V2> extends BinaryOperator<\n [K, V1] | [K, V2] | [K, [V1, V2]] | [K, [V1 | null, V2 | null]]\n> {\n #indexA = new Index<K, V1>()\n #indexB = new Index<K, V2>()\n #mode: JoinType\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V1]>,\n inputB: DifferenceStreamReader<[K, V2]>,\n output: DifferenceStreamWriter<any>,\n mode: JoinType = `inner`,\n ) {\n super(id, inputA, inputB, output)\n this.#mode = mode\n }\n\n run(): void {\n // Build deltas from input messages\n const deltaA = Index.fromMultiSets<K, V1>(\n this.inputAMessages() as Array<MultiSet<[K, V1]>>,\n )\n const deltaB = Index.fromMultiSets<K, V2>(\n this.inputBMessages() as Array<MultiSet<[K, V2]>>,\n )\n\n // Early-out if nothing changed\n if (deltaA.size === 0 && deltaB.size === 0) return\n\n const results = new MultiSet<any>()\n\n // Emit inner results (all modes except anti)\n if (this.#mode !== `anti`) {\n this.emitInnerResults(deltaA, deltaB, results)\n }\n\n // Emit left outer/anti results\n if (\n this.#mode === `left` ||\n this.#mode === `full` ||\n this.#mode === `anti`\n ) {\n this.emitLeftOuterResults(deltaA, deltaB, results)\n }\n\n // Emit right outer results\n if (this.#mode === `right` || this.#mode === `full`) {\n this.emitRightOuterResults(deltaA, deltaB, results)\n }\n\n // Update state and send results\n // IMPORTANT: All emissions use pre-append snapshots of indexA/indexB.\n // Now append ALL deltas to indices - this happens unconditionally for every key,\n // regardless of whether presence flipped. Consolidated multiplicity tracking is automatic.\n this.#indexA.append(deltaA)\n this.#indexB.append(deltaB)\n\n // Send results\n if (results.getInner().length > 0) {\n this.output.sendData(results)\n }\n }\n\n private emitInnerResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit the three standard delta terms: ΔA⋈B_old, A_old⋈ΔB, ΔA⋈ΔB\n if (deltaA.size > 0) results.extend(deltaA.join(this.#indexB))\n if (deltaB.size > 0) results.extend(this.#indexA.join(deltaB))\n if (deltaA.size > 0 && deltaB.size > 0) results.extend(deltaA.join(deltaB))\n }\n\n private emitLeftOuterResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit unmatched left rows from deltaA\n if (deltaA.size > 0) {\n for (const [key, valueIterator] of deltaA.entriesIterators()) {\n const currentMultiplicityB =\n this.#indexB.getConsolidatedMultiplicity(key)\n const deltaMultiplicityB = deltaB.getConsolidatedMultiplicity(key)\n const finalMultiplicityB = currentMultiplicityB + deltaMultiplicityB\n\n if (finalMultiplicityB === 0) {\n for (const [value, multiplicity] of valueIterator) {\n if (multiplicity !== 0) {\n results.add([key, [value, null]], multiplicity)\n }\n }\n }\n }\n }\n\n // Handle presence transitions from right side changes\n if (deltaB.size > 0) {\n for (const key of deltaB.getPresenceKeys()) {\n const before = this.#indexB.getConsolidatedMultiplicity(key)\n const deltaMult = deltaB.getConsolidatedMultiplicity(key)\n if (deltaMult === 0) continue\n const after = before + deltaMult\n\n // Skip transition handling if presence doesn't flip (both zero or both non-zero)\n // Note: Index updates happen later regardless - we're only skipping null-extension emissions here\n if ((before === 0) === (after === 0)) continue\n\n // Determine the type of transition:\n // - 0 → non-zero: Right becomes non-empty, left rows transition from unmatched to matched\n // → RETRACT previously emitted null-extended rows (emit with negative multiplicity)\n // - non-zero → 0: Right becomes empty, left rows transition from matched to unmatched\n // → EMIT new null-extended rows (emit with positive multiplicity)\n const transitioningToMatched = before === 0\n\n for (const [value, multiplicity] of this.#indexA.getIterator(key)) {\n if (multiplicity !== 0) {\n results.add(\n [key, [value, null]],\n transitioningToMatched ? -multiplicity : +multiplicity,\n )\n }\n }\n }\n }\n }\n\n private emitRightOuterResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit unmatched right rows from deltaB\n if (deltaB.size > 0) {\n for (const [key, valueIterator] of deltaB.entriesIterators()) {\n const currentMultiplicityA =\n this.#indexA.getConsolidatedMultiplicity(key)\n const deltaMultiplicityA = deltaA.getConsolidatedMultiplicity(key)\n const finalMultiplicityA = currentMultiplicityA + deltaMultiplicityA\n\n if (finalMultiplicityA === 0) {\n for (const [value, multiplicity] of valueIterator) {\n if (multiplicity !== 0) {\n results.add([key, [null, value]], multiplicity)\n }\n }\n }\n }\n }\n\n // Handle presence transitions from left side changes\n if (deltaA.size > 0) {\n for (const key of deltaA.getPresenceKeys()) {\n const before = this.#indexA.getConsolidatedMultiplicity(key)\n const deltaMult = deltaA.getConsolidatedMultiplicity(key)\n if (deltaMult === 0) continue\n const after = before + deltaMult\n\n // Skip transition handling if presence doesn't flip (both zero or both non-zero)\n // Note: Index updates happen later regardless - we're only skipping null-extension emissions here\n if ((before === 0) === (after === 0)) continue\n\n // Determine the type of transition:\n // - 0 → non-zero: Left becomes non-empty, right rows transition from unmatched to matched\n // → RETRACT previously emitted null-extended rows (emit with negative multiplicity)\n // - non-zero → 0: Left becomes empty, right rows transition from matched to unmatched\n // → EMIT new null-extended rows (emit with positive multiplicity)\n const transitioningToMatched = before === 0\n\n for (const [value, multiplicity] of this.#indexB.getIterator(key)) {\n if (multiplicity !== 0) {\n results.add(\n [key, [null, value]],\n transitioningToMatched ? -multiplicity : +multiplicity,\n )\n }\n }\n }\n }\n }\n}\n\n/**\n * Joins two input streams\n * @param other - The other stream to join with\n * @param type - The type of join to perform\n */\nexport function join<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n type: JoinType = `inner`,\n): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {\n return (\n stream: IStreamBuilder<T>,\n ): IStreamBuilder<KeyValue<K, [V1 | null, V2 | null]>> => {\n if (stream.graph !== other.graph) {\n throw new Error(`Cannot join streams from different graphs`)\n }\n const output = new StreamBuilder<KeyValue<K, [V1 | null, V2 | null]>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<K, [V1 | null, V2 | null]>>(),\n )\n const operator = new JoinOperator<K, V1, V2>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<K, V1>>,\n other.connectReader(),\n output.writer,\n type,\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n\n/**\n * Joins two input streams (inner join)\n * @param other - The other stream to join with\n */\nexport function innerJoin<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n): PipedOperator<T, KeyValue<K, [V1, V2]>> {\n return join(other, `inner`) as unknown as PipedOperator<\n T,\n KeyValue<K, [V1, V2]>\n >\n}\n\n/**\n * Joins two input streams (anti join)\n * @param other - The other stream to join with\n */\nexport function antiJoin<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n): PipedOperator<T, KeyValue<K, [V1, null]>> {\n return join(other, `anti`) as unknown as PipedOperator<\n T,\n KeyValue<K, [V1, null]>\n >\n}\n\n/**\n * Joins two input streams (left join)\n * @param other - The other stream to join with\n */\nexport function leftJoin<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n): PipedOperator<T, KeyValue<K, [V1, V2 | null]>> {\n return join(other, `left`) as unknown as PipedOperator<\n T,\n KeyValue<K, [V1, V2 | null]>\n >\n}\n\n/**\n * Joins two input streams (right join)\n * @param other - The other stream to join with\n */\nexport function rightJoin<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n): PipedOperator<T, KeyValue<K, [V1 | null, V2]>> {\n return join(other, `right`) as unknown as PipedOperator<\n T,\n KeyValue<K, [V1 | null, V2]>\n >\n}\n\n/**\n * Joins two input streams (full join)\n * @param other - The other stream to join with\n */\nexport function fullJoin<\n K,\n V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {\n return join(other, `full`) as unknown as PipedOperator<\n T,\n KeyValue<K, [V1 | null, V2 | null]>\n >\n}\n"],"names":["BinaryOperator","Index","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;AAiEO,MAAM,qBAAgCA,MAAAA,eAE3C;AAAA,EACA,UAAU,IAAIC,QAAAA,MAAA;AAAA,EACd,UAAU,IAAIA,QAAAA,MAAA;AAAA,EACd;AAAA,EAEA,YACE,IACA,QACA,QACA,QACA,OAAiB,SACjB;AACA,UAAM,IAAI,QAAQ,QAAQ,MAAM;AAChC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,MAAY;AAEV,UAAM,SAASA,QAAAA,MAAM;AAAA,MACnB,KAAK,eAAA;AAAA,IAAe;AAEtB,UAAM,SAASA,QAAAA,MAAM;AAAA,MACnB,KAAK,eAAA;AAAA,IAAe;AAItB,QAAI,OAAO,SAAS,KAAK,OAAO,SAAS,EAAG;AAE5C,UAAM,UAAU,IAAIC,kBAAA;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W,CAAC,OAAO,YAAY,KAAK,KAAK,QAAQ,YAAY,GAAG,GAAG;AACjE,cAAI,iBAAiB,GAAG;AACtB,oBAAQ;AAAA,cACN,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;AAAA,cACnB,yBAAyB,CAAC,eAAe,CAAC;AAAA,YAAA;AAAA,UAE9C;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAOO,SAAS,KAMd,OACA,OAAiB,SACsC;AACvD,SAAO,CACL,WACwD;AACxD,QAAI,OAAO,UAAU,MAAM,OAAO;AAChC,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC7D;AACA,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA4D;AAElE,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,MAAM,cAAA;AAAA,MACN,OAAO;AAAA,MACP;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;AAMO,SAAS,UAMd,OACyC;AACzC,SAAO,KAAK,OAAO,OAAO;AAI5B;AAMO,SAAS,SAMd,OAC2C;AAC3C,SAAO,KAAK,OAAO,MAAM;AAI3B;AAMO,SAAS,SAMd,OACgD;AAChD,SAAO,KAAK,OAAO,MAAM;AAI3B;AAMO,SAAS,UAMd,OACgD;AAChD,SAAO,KAAK,OAAO,OAAO;AAI5B;AAMO,SAAS,SAMd,OACuD;AACvD,SAAO,KAAK,OAAO,MAAM;AAI3B;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"join.cjs","sources":["../../../src/operators/join.ts"],"sourcesContent":["/**\n * # Direct Join Algorithms for Incremental View Maintenance\n *\n * High-performance join operations implementing all join types (inner, left, right, full, anti)\n * with minimal state and optimized performance.\n *\n * ## Algorithm\n *\n * For each tick, the algorithm processes incoming changes (deltas) and emits join results:\n *\n * 1. **Build deltas**: Create delta indexes from input messages using `Index.fromMultiSet()`\n * 2. **Inner results**: Emit `ΔA⋈B_old + A_old⋈ΔB + ΔA⋈ΔB` (matched pairs)\n * 3. **Outer results**: For unmatched rows, emit null-extended tuples:\n * - New unmatched rows from deltas (when opposite side empty)\n * - Presence transitions: when key goes `0→>0` (retract nulls) or `>0→0` (emit nulls)\n * 4. **Update state**: Append deltas to indexes (consolidated multiplicity tracking automatic)\n *\n * **Consolidated multiplicity tracking** enables O(1) presence checks instead of scanning index buckets.\n *\n * ## State\n *\n * **Indexes** store the actual data:\n * - `indexA: Index<K, V1>` - all left-side rows accumulated over time\n * - `indexB: Index<K, V2>` - all right-side rows accumulated over time\n *\n * **Consolidated multiplicity tracking** (built into Index):\n * - Each Index maintains sum of multiplicities per key internally\n * - Provides O(1) presence checks: `index.hasPresence(key)` and `index.getConsolidatedMultiplicity(key)`\n * - Avoids scanning entire index buckets just to check if key has any rows\n *\n * ## Join Types\n *\n * - **Inner**: Standard delta terms only\n * - **Outer**: Inner results + null-extended unmatched rows with transition handling\n * - **Anti**: Unmatched rows only (no inner results)\n *\n * ## Key Optimizations\n *\n * - **No temp copying**: Uses `(A⊎ΔA)⋈ΔB = A⋈ΔB ⊎ ΔA⋈ΔB` distributive property\n * - **Early-out checks**: Skip phases when no deltas present\n * - **Zero-entry pruning**: Keep maps compact, O(distinct keys) memory\n * - **Final presence logic**: Avoid emit→retract churn within same tick\n *\n * ## Correctness\n *\n * - **Ordering**: Pre-append snapshots for emissions, post-emit state updates\n * - **Presence**: Key matched iff mass ≠ 0, transitions trigger null handling\n * - **Bag semantics**: Proper multiplicity handling including negatives\n */\n\nimport { BinaryOperator, DifferenceStreamWriter } from '../graph.js'\nimport { StreamBuilder } from '../d2.js'\nimport { MultiSet } from '../multiset.js'\nimport { Index } from '../indexes.js'\nimport type { DifferenceStreamReader } from '../graph.js'\nimport type { IStreamBuilder, KeyValue, PipedOperator } from '../types.js'\n\n/**\n * Type of join to perform\n */\nexport type JoinType = `inner` | `left` | `right` | `full` | `anti`\n\n/**\n * Operator that joins two input streams using direct join algorithms\n */\nexport class JoinOperator<K, V1, V2> extends BinaryOperator<\n [K, V1] | [K, V2] | [K, [V1, V2]] | [K, [V1 | null, V2 | null]]\n> {\n #indexA = new Index<K, V1>()\n #indexB = new Index<K, V2>()\n #mode: JoinType\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V1]>,\n inputB: DifferenceStreamReader<[K, V2]>,\n output: DifferenceStreamWriter<any>,\n mode: JoinType = `inner`,\n ) {\n super(id, inputA, inputB, output)\n this.#mode = mode\n }\n\n run(): void {\n // Build deltas from input messages\n const deltaA = Index.fromMultiSets<K, V1>(\n this.inputAMessages() as Array<MultiSet<[K, V1]>>,\n )\n const deltaB = Index.fromMultiSets<K, V2>(\n this.inputBMessages() as Array<MultiSet<[K, V2]>>,\n )\n\n // Early-out if nothing changed\n if (deltaA.size === 0 && deltaB.size === 0) return\n\n const results = new MultiSet<any>()\n\n // Emit inner results (all modes except anti)\n if (this.#mode !== `anti`) {\n this.emitInnerResults(deltaA, deltaB, results)\n }\n\n // Emit left outer/anti results\n if (\n this.#mode === `left` ||\n this.#mode === `full` ||\n this.#mode === `anti`\n ) {\n this.emitLeftOuterResults(deltaA, deltaB, results)\n }\n\n // Emit right outer results\n if (this.#mode === `right` || this.#mode === `full`) {\n this.emitRightOuterResults(deltaA, deltaB, results)\n }\n\n // Update state and send results\n // IMPORTANT: All emissions use pre-append snapshots of indexA/indexB.\n // Now append ALL deltas to indices - this happens unconditionally for every key,\n // regardless of whether presence flipped. Consolidated multiplicity tracking is automatic.\n this.#indexA.append(deltaA)\n this.#indexB.append(deltaB)\n\n // Send results\n if (results.getInner().length > 0) {\n this.output.sendData(results)\n }\n }\n\n private emitInnerResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit the three standard delta terms: ΔA⋈B_old, A_old⋈ΔB, ΔA⋈ΔB\n if (deltaA.size > 0) results.extend(deltaA.join(this.#indexB))\n if (deltaB.size > 0) results.extend(this.#indexA.join(deltaB))\n if (deltaA.size > 0 && deltaB.size > 0) results.extend(deltaA.join(deltaB))\n }\n\n private emitLeftOuterResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit unmatched left rows from deltaA\n if (deltaA.size > 0) {\n for (const [key, valueIterator] of deltaA.entriesIterators()) {\n const currentMultiplicityB =\n this.#indexB.getConsolidatedMultiplicity(key)\n const deltaMultiplicityB = deltaB.getConsolidatedMultiplicity(key)\n const finalMultiplicityB = currentMultiplicityB + deltaMultiplicityB\n\n if (finalMultiplicityB === 0) {\n for (const [value, multiplicity] of valueIterator) {\n if (multiplicity !== 0) {\n results.add([key, [value, null]], multiplicity)\n }\n }\n }\n }\n }\n\n // Handle presence transitions from right side changes\n if (deltaB.size > 0) {\n for (const key of deltaB.getPresenceKeys()) {\n const before = this.#indexB.getConsolidatedMultiplicity(key)\n const deltaMult = deltaB.getConsolidatedMultiplicity(key)\n if (deltaMult === 0) continue\n const after = before + deltaMult\n\n // Skip transition handling if presence doesn't flip (both zero or both non-zero)\n // Note: Index updates happen later regardless - we're only skipping null-extension emissions here\n if ((before === 0) === (after === 0)) continue\n\n // Determine the type of transition:\n // - 0 → non-zero: Right becomes non-empty, left rows transition from unmatched to matched\n // → RETRACT previously emitted null-extended rows (emit with negative multiplicity)\n // - non-zero → 0: Right becomes empty, left rows transition from matched to unmatched\n // → EMIT new null-extended rows (emit with positive multiplicity)\n const transitioningToMatched = before === 0\n\n for (const [value, multiplicity] of this.#indexA.getIterator(key)) {\n if (multiplicity !== 0) {\n results.add(\n [key, [value, null]],\n transitioningToMatched ? -multiplicity : +multiplicity,\n )\n }\n }\n }\n }\n }\n\n private emitRightOuterResults(\n deltaA: Index<K, V1>,\n deltaB: Index<K, V2>,\n results: MultiSet<any>,\n ): void {\n // Emit unmatched right rows from deltaB\n if (deltaB.size > 0) {\n for (const [key, valueIterator] of deltaB.entriesIterators()) {\n const currentMultiplicityA =\n this.#indexA.getConsolidatedMultiplicity(key)\n const deltaMultiplicityA = deltaA.getConsolidatedMultiplicity(key)\n const finalMultiplicityA = currentMultiplicityA + deltaMultiplicityA\n\n if (finalMultiplicityA === 0) {\n for (const [value, multiplicity] of valueIterator) {\n if (multiplicity !== 0) {\n results.add([key, [null, value]], multiplicity)\n }\n }\n }\n }\n }\n\n // Handle presence transitions from left side changes\n if (deltaA.size > 0) {\n for (const key of deltaA.getPresenceKeys()) {\n const before = this.#indexA.getConsolidatedMultiplicity(key)\n const deltaMult = deltaA.getConsolidatedMultiplicity(key)\n if (deltaMult === 0) continue\n const after = before + deltaMult\n\n // Skip transition handling if presence doesn't flip (both zero or both non-zero)\n // Note: Index updates happen later regardless - we're only skipping null-extension emissions here\n if ((before === 0) === (after === 0)) continue\n\n // Determine the type of transition:\n // - 0 → non-zero: Left becomes non-empty, right rows transition from unmatched to matched\n // → RETRACT previously emitted null-extended rows (emit with negative multiplicity)\n // - non-zero → 0: Left becomes empty, right rows transition from matched to unmatched\n // → EMIT new null-extended rows (emit with positive multiplicity)\n const transitioningToMatched = before === 0\n\n for (const [value, multiplicity] of this.#indexB.getIterator(key)) {\n if (multiplicity !== 0) {\n results.add(\n [key, [null, value]],\n transitioningToMatched ? -multiplicity : +multiplicity,\n )\n }\n }\n }\n }\n }\n}\n\n/**\n * Joins two input streams\n * @param other - The other stream to join with\n * @param type - The type of join to perform\n */\nexport function join<\n K,\n V1 extends (T extends KeyValue<infer _KT, infer VT> ? VT : never),\n V2,\n T,\n>(\n other: IStreamBuilder<KeyValue<K, V2>>,\n type: JoinType = `inner`,\n): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {\n return (\n stream: IStreamBuilder<T>,\n ): IStreamBuilder<KeyValue<K, [V1 | null, V2 | null]>> => {\n if (stream.graph !== other.graph) {\n throw new Error(`Cannot join streams from different graphs`)\n }\n const output = new StreamBuilder<KeyValue<K, [V1 | null, V2 | null]>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<K, [V1 | null, V2 | null]>>(),\n )\n const operator = new JoinOperator<K, V1, V2>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<K, V1>>,\n other.connectReader(),\n output.writer,\n type,\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["BinaryOperator","Index","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;AAiEO,MAAM,qBAAgCA,MAAAA,eAE3C;AAAA,EACA,UAAU,IAAIC,QAAAA,MAAA;AAAA,EACd,UAAU,IAAIA,QAAAA,MAAA;AAAA,EACd;AAAA,EAEA,YACE,IACA,QACA,QACA,QACA,OAAiB,SACjB;AACA,UAAM,IAAI,QAAQ,QAAQ,MAAM;AAChC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,MAAY;AAEV,UAAM,SAASA,QAAAA,MAAM;AAAA,MACnB,KAAK,eAAA;AAAA,IAAe;AAEtB,UAAM,SAASA,QAAAA,MAAM;AAAA,MACnB,KAAK,eAAA;AAAA,IAAe;AAItB,QAAI,OAAO,SAAS,KAAK,OAAO,SAAS,EAAG;AAE5C,UAAM,UAAU,IAAIC,kBAAA;AAGpB,QAAI,KAAK,UAAU,QAAQ;AACzB,WAAK,iBAAiB,QAAQ,QAAQ,OAAO;AAAA,IAC/C;AAGA,QACE,KAAK,UAAU,UACf,KAAK,UAAU,UACf,KAAK,UAAU,QACf;AACA,WAAK,qBAAqB,QAAQ,QAAQ,OAAO;AAAA,IACnD;AAGA,QAAI,KAAK,UAAU,WAAW,KAAK,UAAU,QAAQ;AACnD,WAAK,sBAAsB,QAAQ,QAAQ,OAAO;AAAA,IACpD;AAMA,SAAK,QAAQ,OAAO,MAAM;AAC1B,SAAK,QAAQ,OAAO,MAAM;AAG1B,QAAI,QAAQ,WAAW,SAAS,GAAG;AACjC,WAAK,OAAO,SAAS,OAAO;AAAA,IAC9B;AAA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AA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA4D;AAElE,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,MAAM,cAAA;AAAA,MACN,OAAO;AAAA,MACP;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BinaryOperator, DifferenceStreamWriter, DifferenceStreamReader } from
|
|
2
|
-
import { IStreamBuilder, KeyValue, PipedOperator } from
|
|
1
|
+
import { BinaryOperator, DifferenceStreamWriter, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { IStreamBuilder, KeyValue, PipedOperator } from "../types.cjs";
|
|
3
3
|
/**
|
|
4
4
|
* Type of join to perform
|
|
5
5
|
*/
|
|
@@ -10,9 +10,30 @@ export type JoinType = `inner` | `left` | `right` | `full` | `anti`;
|
|
|
10
10
|
export declare class JoinOperator<K, V1, V2> extends BinaryOperator<[
|
|
11
11
|
K,
|
|
12
12
|
V1
|
|
13
|
-
] | [
|
|
13
|
+
] | [
|
|
14
|
+
K,
|
|
15
|
+
V2
|
|
16
|
+
] | [
|
|
17
|
+
K,
|
|
18
|
+
[
|
|
19
|
+
V1,
|
|
20
|
+
V2
|
|
21
|
+
]
|
|
22
|
+
] | [
|
|
23
|
+
K,
|
|
24
|
+
[
|
|
25
|
+
V1 | null,
|
|
26
|
+
V2 | null
|
|
27
|
+
]
|
|
28
|
+
]> {
|
|
14
29
|
#private;
|
|
15
|
-
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
30
|
+
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
31
|
+
K,
|
|
32
|
+
V1
|
|
33
|
+
]>, inputB: DifferenceStreamReader<[
|
|
34
|
+
K,
|
|
35
|
+
V2
|
|
36
|
+
]>, output: DifferenceStreamWriter<any>, mode?: JoinType);
|
|
16
37
|
run(): void;
|
|
17
38
|
private emitInnerResults;
|
|
18
39
|
private emitLeftOuterResults;
|
|
@@ -23,29 +44,7 @@ export declare class JoinOperator<K, V1, V2> extends BinaryOperator<[
|
|
|
23
44
|
* @param other - The other stream to join with
|
|
24
45
|
* @param type - The type of join to perform
|
|
25
46
|
*/
|
|
26
|
-
export declare function join<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>, type?: JoinType): PipedOperator<T, KeyValue<K, [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*/
|
|
31
|
-
export declare function innerJoin<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>): PipedOperator<T, KeyValue<K, [V1, V2]>>;
|
|
32
|
-
/**
|
|
33
|
-
* Joins two input streams (anti join)
|
|
34
|
-
* @param other - The other stream to join with
|
|
35
|
-
*/
|
|
36
|
-
export declare function antiJoin<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>): PipedOperator<T, KeyValue<K, [V1, null]>>;
|
|
37
|
-
/**
|
|
38
|
-
* Joins two input streams (left join)
|
|
39
|
-
* @param other - The other stream to join with
|
|
40
|
-
*/
|
|
41
|
-
export declare function leftJoin<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>): PipedOperator<T, KeyValue<K, [V1, V2 | null]>>;
|
|
42
|
-
/**
|
|
43
|
-
* Joins two input streams (right join)
|
|
44
|
-
* @param other - The other stream to join with
|
|
45
|
-
*/
|
|
46
|
-
export declare function rightJoin<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>): PipedOperator<T, KeyValue<K, [V1 | null, V2]>>;
|
|
47
|
-
/**
|
|
48
|
-
* Joins two input streams (full join)
|
|
49
|
-
* @param other - The other stream to join with
|
|
50
|
-
*/
|
|
51
|
-
export declare function fullJoin<K, V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never, V2, T>(other: IStreamBuilder<KeyValue<K, V2>>): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>>;
|
|
47
|
+
export declare function join<K, V1 extends (T extends KeyValue<infer _KT, infer VT> ? VT : never), V2, T>(other: IStreamBuilder<KeyValue<K, V2>>, type?: JoinType): PipedOperator<T, KeyValue<K, [
|
|
48
|
+
V1 | null,
|
|
49
|
+
V2 | null
|
|
50
|
+
]>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DifferenceStreamWriter, LinearUnaryOperator, DifferenceStreamReader } from
|
|
2
|
-
import { PipedOperator } from
|
|
3
|
-
import { MultiSet } from
|
|
1
|
+
import { DifferenceStreamWriter, LinearUnaryOperator, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { PipedOperator } from "../types.cjs";
|
|
3
|
+
import { MultiSet } from "../multiset.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* Operator that applies a function to each element in the input stream
|
|
6
6
|
*/
|
|
@@ -1,73 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const topK = require("./topK.cjs");
|
|
4
3
|
const topKWithFractionalIndex = require("./topKWithFractionalIndex.cjs");
|
|
5
|
-
const map = require("./map.cjs");
|
|
6
|
-
const join = require("./join.cjs");
|
|
7
4
|
const consolidate = require("./consolidate.cjs");
|
|
8
|
-
function
|
|
9
|
-
const limit = options?.limit ?? Infinity;
|
|
10
|
-
const offset = options?.offset ?? 0;
|
|
11
|
-
const comparator = options?.comparator ?? ((a, b) => {
|
|
12
|
-
if (a === b) return 0;
|
|
13
|
-
if (a < b) return -1;
|
|
14
|
-
return 1;
|
|
15
|
-
});
|
|
16
|
-
return (stream) => {
|
|
17
|
-
return stream.pipe(
|
|
18
|
-
map.map(
|
|
19
|
-
([key, value]) => [
|
|
20
|
-
null,
|
|
21
|
-
[
|
|
22
|
-
key,
|
|
23
|
-
valueExtractor(
|
|
24
|
-
value
|
|
25
|
-
)
|
|
26
|
-
]
|
|
27
|
-
]
|
|
28
|
-
),
|
|
29
|
-
topK.topK((a, b) => comparator(a[1], b[1]), { limit, offset }),
|
|
30
|
-
map.map(([_, [key]]) => [key, null]),
|
|
31
|
-
join.innerJoin(stream),
|
|
32
|
-
map.map(([key, value]) => {
|
|
33
|
-
return [key, value[1]];
|
|
34
|
-
}),
|
|
35
|
-
consolidate.consolidate()
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
function orderByWithIndex(valueExtractor, options) {
|
|
40
|
-
const limit = options?.limit ?? Infinity;
|
|
41
|
-
const offset = options?.offset ?? 0;
|
|
42
|
-
const comparator = options?.comparator ?? ((a, b) => {
|
|
43
|
-
if (a === b) return 0;
|
|
44
|
-
if (a < b) return -1;
|
|
45
|
-
return 1;
|
|
46
|
-
});
|
|
47
|
-
return (stream) => {
|
|
48
|
-
return stream.pipe(
|
|
49
|
-
map.map(
|
|
50
|
-
([key, value]) => [
|
|
51
|
-
null,
|
|
52
|
-
[
|
|
53
|
-
key,
|
|
54
|
-
valueExtractor(
|
|
55
|
-
value
|
|
56
|
-
)
|
|
57
|
-
]
|
|
58
|
-
]
|
|
59
|
-
),
|
|
60
|
-
topK.topKWithIndex((a, b) => comparator(a[1], b[1]), { limit, offset }),
|
|
61
|
-
map.map(([_, [[key], index]]) => [key, index]),
|
|
62
|
-
join.innerJoin(stream),
|
|
63
|
-
map.map(([key, [index, value]]) => {
|
|
64
|
-
return [key, [value, index]];
|
|
65
|
-
}),
|
|
66
|
-
consolidate.consolidate()
|
|
67
|
-
);
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
function orderByWithFractionalIndexBase(topKFunction, valueExtractor, options) {
|
|
5
|
+
function orderByWithFractionalIndex(valueExtractor, options) {
|
|
71
6
|
const limit = options?.limit ?? Infinity;
|
|
72
7
|
const offset = options?.offset ?? 0;
|
|
73
8
|
const setSizeCallback = options?.setSizeCallback;
|
|
@@ -79,7 +14,7 @@ function orderByWithFractionalIndexBase(topKFunction, valueExtractor, options) {
|
|
|
79
14
|
});
|
|
80
15
|
return (stream) => {
|
|
81
16
|
return stream.pipe(
|
|
82
|
-
|
|
17
|
+
topKWithFractionalIndex.topKWithFractionalIndex(
|
|
83
18
|
(a, b) => comparator(valueExtractor(a), valueExtractor(b)),
|
|
84
19
|
{
|
|
85
20
|
limit,
|
|
@@ -92,15 +27,5 @@ function orderByWithFractionalIndexBase(topKFunction, valueExtractor, options) {
|
|
|
92
27
|
);
|
|
93
28
|
};
|
|
94
29
|
}
|
|
95
|
-
function orderByWithFractionalIndex(valueExtractor, options) {
|
|
96
|
-
return orderByWithFractionalIndexBase(
|
|
97
|
-
topKWithFractionalIndex.topKWithFractionalIndex,
|
|
98
|
-
valueExtractor,
|
|
99
|
-
options
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
exports.orderBy = orderBy;
|
|
103
30
|
exports.orderByWithFractionalIndex = orderByWithFractionalIndex;
|
|
104
|
-
exports.orderByWithFractionalIndexBase = orderByWithFractionalIndexBase;
|
|
105
|
-
exports.orderByWithIndex = orderByWithIndex;
|
|
106
31
|
//# sourceMappingURL=orderBy.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orderBy.cjs","sources":["../../../src/operators/orderBy.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"orderBy.cjs","sources":["../../../src/operators/orderBy.ts"],"sourcesContent":["import { topKWithFractionalIndex } from './topKWithFractionalIndex.js'\nimport { consolidate } from './consolidate.js'\nimport type { IStreamBuilder, KeyValue } from '../types.js'\nimport type { SetSizeCallback } from './topKWithFractionalIndex.js'\n\nexport interface OrderByOptions<Ve> {\n comparator?: (a: Ve, b: Ve) => number\n limit?: number\n offset?: number\n}\n\ntype OrderByWithFractionalIndexOptions<Ve> = OrderByOptions<Ve> & {\n setSizeCallback?: SetSizeCallback\n setWindowFn?: (\n windowFn: (options: { offset?: number; limit?: number }) => void,\n ) => void\n}\n\n/**\n * Orders the elements and limits the number of results, with optional offset and\n * annotates the value with a fractional index.\n * This requires a keyed stream, and uses the `topKWithFractionalIndex` operator to order all the elements.\n *\n * @param valueExtractor - A function that extracts the value to order by from the element\n * @param options - An optional object containing comparator, limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function orderByWithFractionalIndex<\n T extends KeyValue<unknown, unknown>,\n Ve = unknown,\n>(\n valueExtractor: (\n value: T extends KeyValue<unknown, infer V> ? V : never,\n ) => Ve,\n options?: OrderByWithFractionalIndexOptions<Ve>,\n) {\n type KeyType = T extends KeyValue<infer K, unknown> ? K : never\n type ValueType = T extends KeyValue<unknown, infer V> ? V : never\n\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n const setSizeCallback = options?.setSizeCallback\n const setWindowFn = options?.setWindowFn\n const comparator =\n options?.comparator ??\n ((a, b) => {\n // Default to JS like ordering\n if (a === b) return 0\n if (a < b) return -1\n return 1\n })\n\n return (\n stream: IStreamBuilder<T>,\n ): IStreamBuilder<[KeyType, [ValueType, string]]> => {\n return stream.pipe(\n topKWithFractionalIndex(\n (a: ValueType, b: ValueType) =>\n comparator(valueExtractor(a), valueExtractor(b)),\n {\n limit,\n offset,\n setSizeCallback,\n setWindowFn,\n },\n ),\n consolidate(),\n )\n }\n}\n"],"names":["topKWithFractionalIndex","consolidate"],"mappings":";;;;AA2BO,SAAS,2BAId,gBAGA,SACA;AAIA,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAClC,QAAM,kBAAkB,SAAS;AACjC,QAAM,cAAc,SAAS;AAC7B,QAAM,aACJ,SAAS,eACR,CAAC,GAAG,MAAM;AAET,QAAI,MAAM,EAAG,QAAO;AACpB,QAAI,IAAI,EAAG,QAAO;AAClB,WAAO;AAAA,EACT;AAEF,SAAO,CACL,WACmD;AACnD,WAAO,OAAO;AAAA,MACZA,wBAAAA;AAAAA,QACE,CAAC,GAAc,MACb,WAAW,eAAe,CAAC,GAAG,eAAe,CAAC,CAAC;AAAA,QACjD;AAAA,UACE;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QAAA;AAAA,MACF;AAAA,MAEFC,YAAAA,YAAA;AAAA,IAAY;AAAA,EAEhB;AACF;;"}
|
|
@@ -1,37 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { IStreamBuilder, KeyValue } from "../types.cjs";
|
|
2
|
+
import { SetSizeCallback } from "./topKWithFractionalIndex.cjs";
|
|
3
3
|
export interface OrderByOptions<Ve> {
|
|
4
4
|
comparator?: (a: Ve, b: Ve) => number;
|
|
5
5
|
limit?: number;
|
|
6
6
|
offset?: number;
|
|
7
7
|
}
|
|
8
8
|
type OrderByWithFractionalIndexOptions<Ve> = OrderByOptions<Ve> & {
|
|
9
|
-
setSizeCallback?:
|
|
9
|
+
setSizeCallback?: SetSizeCallback;
|
|
10
10
|
setWindowFn?: (windowFn: (options: {
|
|
11
11
|
offset?: number;
|
|
12
12
|
limit?: number;
|
|
13
13
|
}) => void) => void;
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
16
|
-
* Orders the elements and limits the number of results, with optional offset
|
|
17
|
-
* This requires a keyed stream, and uses the `topK` operator to order all the elements.
|
|
18
|
-
*
|
|
19
|
-
* @param valueExtractor - A function that extracts the value to order by from the element
|
|
20
|
-
* @param options - An optional object containing comparator, limit and offset properties
|
|
21
|
-
* @returns A piped operator that orders the elements and limits the number of results
|
|
22
|
-
*/
|
|
23
|
-
export declare function orderBy<T extends KeyValue<unknown, unknown>, Ve = unknown>(valueExtractor: (value: T extends KeyValue<unknown, infer V> ? V : never) => Ve, options?: OrderByOptions<Ve>): (stream: IStreamBuilder<T>) => IStreamBuilder<T>;
|
|
24
|
-
/**
|
|
25
|
-
* Orders the elements and limits the number of results, with optional offset and
|
|
26
|
-
* annotates the value with the index.
|
|
27
|
-
* This requires a keyed stream, and uses the `topKWithIndex` operator to order all the elements.
|
|
28
|
-
*
|
|
29
|
-
* @param valueExtractor - A function that extracts the value to order by from the element
|
|
30
|
-
* @param options - An optional object containing comparator, limit and offset properties
|
|
31
|
-
* @returns A piped operator that orders the elements and limits the number of results
|
|
32
|
-
*/
|
|
33
|
-
export declare function orderByWithIndex<T extends KeyValue<unknown, unknown>, Ve = unknown>(valueExtractor: (value: T extends KeyValue<unknown, infer V> ? V : never) => Ve, options?: OrderByOptions<Ve>): (stream: IStreamBuilder<T>) => IStreamBuilder<KeyValue<T extends KeyValue<infer K, unknown> ? K : never, [T extends KeyValue<unknown, infer V> ? V : never, number]>>;
|
|
34
|
-
export declare function orderByWithFractionalIndexBase<T extends KeyValue<unknown, unknown>, Ve = unknown>(topKFunction: typeof topKWithFractionalIndex, valueExtractor: (value: T extends KeyValue<unknown, infer V> ? V : never) => Ve, options?: OrderByWithFractionalIndexOptions<Ve>): (stream: IStreamBuilder<T>) => IStreamBuilder<[T extends KeyValue<infer K, unknown> ? K : never, [T extends KeyValue<unknown, infer V> ? V : never, string]]>;
|
|
35
15
|
/**
|
|
36
16
|
* Orders the elements and limits the number of results, with optional offset and
|
|
37
17
|
* annotates the value with a fractional index.
|
|
@@ -41,5 +21,11 @@ export declare function orderByWithFractionalIndexBase<T extends KeyValue<unknow
|
|
|
41
21
|
* @param options - An optional object containing comparator, limit and offset properties
|
|
42
22
|
* @returns A piped operator that orders the elements and limits the number of results
|
|
43
23
|
*/
|
|
44
|
-
export declare function orderByWithFractionalIndex<T extends KeyValue<unknown, unknown>, Ve = unknown>(valueExtractor: (value: T extends KeyValue<unknown, infer V> ? V : never) => Ve, options?: OrderByWithFractionalIndexOptions<Ve>): (stream: IStreamBuilder<T>) => IStreamBuilder<[
|
|
24
|
+
export declare function orderByWithFractionalIndex<T extends KeyValue<unknown, unknown>, Ve = unknown>(valueExtractor: (value: T extends KeyValue<unknown, infer V> ? V : never) => Ve, options?: OrderByWithFractionalIndexOptions<Ve>): (stream: IStreamBuilder<T>) => IStreamBuilder<[
|
|
25
|
+
T extends KeyValue<infer K, unknown> ? K : never,
|
|
26
|
+
[
|
|
27
|
+
T extends KeyValue<unknown, infer V> ? V : never,
|
|
28
|
+
string
|
|
29
|
+
]
|
|
30
|
+
]>;
|
|
45
31
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from
|
|
2
|
-
import { PipedOperator } from
|
|
3
|
-
import { MultiSet } from
|
|
1
|
+
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { PipedOperator } from "../types.cjs";
|
|
3
|
+
import { MultiSet } from "../multiset.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* Operator that outputs the messages in the stream
|
|
6
6
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PipedOperator } from
|
|
1
|
+
import { PipedOperator } from "../types.cjs";
|
|
2
2
|
export declare function pipe<T, O>(o1: PipedOperator<T, O>): PipedOperator<T, O>;
|
|
3
3
|
export declare function pipe<T, T2, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, O>): PipedOperator<T, O>;
|
|
4
4
|
export declare function pipe<T, T2, T3, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, O>): PipedOperator<T, O>;
|
|
@@ -4,12 +4,20 @@ const graph = require("../graph.cjs");
|
|
|
4
4
|
const d2 = require("../d2.cjs");
|
|
5
5
|
const multiset = require("../multiset.cjs");
|
|
6
6
|
const indexes = require("../indexes.cjs");
|
|
7
|
+
function sameOutput(left, right) {
|
|
8
|
+
return left === right || left !== left && right !== right;
|
|
9
|
+
}
|
|
10
|
+
function asMapKey(value) {
|
|
11
|
+
return value === 0 ? 0 : value;
|
|
12
|
+
}
|
|
7
13
|
class ReduceOperator extends graph.UnaryOperator {
|
|
8
|
-
|
|
9
|
-
#
|
|
14
|
+
// Reduce never reads presence, so its indexes skip that bookkeeping.
|
|
15
|
+
#index;
|
|
16
|
+
#indexOut = new indexes.Index({ trackPresence: false });
|
|
10
17
|
#f;
|
|
11
|
-
constructor(id, inputA, output, f) {
|
|
18
|
+
constructor(id, inputA, output, f, options) {
|
|
12
19
|
super(id, inputA, output);
|
|
20
|
+
this.#index = new indexes.Index({ ...options, trackPresence: false });
|
|
13
21
|
this.#f = f;
|
|
14
22
|
}
|
|
15
23
|
run() {
|
|
@@ -26,6 +34,28 @@ class ReduceOperator extends graph.UnaryOperator {
|
|
|
26
34
|
const curr = this.#index.get(key);
|
|
27
35
|
const currOut = this.#indexOut.get(key);
|
|
28
36
|
const out = this.#f(curr);
|
|
37
|
+
if (out.length <= 1 && currOut.length <= 1) {
|
|
38
|
+
const before = currOut[0];
|
|
39
|
+
const after = out[0];
|
|
40
|
+
const value = after === void 0 ? void 0 : asMapKey(after[0]);
|
|
41
|
+
if (before !== void 0 && after !== void 0 && sameOutput(before[0], value)) {
|
|
42
|
+
const delta = after[1] - before[1];
|
|
43
|
+
if (delta !== 0) {
|
|
44
|
+
result.push([[key, before[0]], delta]);
|
|
45
|
+
this.#indexOut.addValue(key, [before[0], delta]);
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (before !== void 0) {
|
|
50
|
+
result.push([[key, before[0]], -before[1]]);
|
|
51
|
+
this.#indexOut.addValue(key, [before[0], -before[1]]);
|
|
52
|
+
}
|
|
53
|
+
if (after !== void 0 && after[1] !== 0) {
|
|
54
|
+
result.push([[key, value], after[1]]);
|
|
55
|
+
this.#indexOut.addValue(key, [value, after[1]]);
|
|
56
|
+
}
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
29
59
|
const newOutputMap = /* @__PURE__ */ new Map();
|
|
30
60
|
const oldOutputMap = /* @__PURE__ */ new Map();
|
|
31
61
|
for (const [value, multiplicity] of out) {
|
|
@@ -66,7 +96,7 @@ class ReduceOperator extends graph.UnaryOperator {
|
|
|
66
96
|
}
|
|
67
97
|
}
|
|
68
98
|
}
|
|
69
|
-
function reduce(f) {
|
|
99
|
+
function reduce(f, options) {
|
|
70
100
|
return (stream) => {
|
|
71
101
|
const output = new d2.StreamBuilder(
|
|
72
102
|
stream.graph,
|
|
@@ -76,7 +106,8 @@ function reduce(f) {
|
|
|
76
106
|
stream.graph.getNextOperatorId(),
|
|
77
107
|
stream.connectReader(),
|
|
78
108
|
output.writer,
|
|
79
|
-
f
|
|
109
|
+
f,
|
|
110
|
+
options
|
|
80
111
|
);
|
|
81
112
|
stream.graph.addOperator(operator);
|
|
82
113
|
return output;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reduce.cjs","sources":["../../../src/operators/reduce.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from '../graph.js'\nimport { StreamBuilder } from '../d2.js'\nimport { MultiSet } from '../multiset.js'\nimport { Index } from '../indexes.js'\nimport type { DifferenceStreamReader } from '../graph.js'\nimport type { IStreamBuilder, KeyValue } from '../types.js'\n\n/**\n * Base operator for reduction operations (version-free)\n */\nexport class ReduceOperator<K, V1, V2> extends UnaryOperator<[K, V1], [K, V2]> {\n #index
|
|
1
|
+
{"version":3,"file":"reduce.cjs","sources":["../../../src/operators/reduce.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from '../graph.js'\nimport { StreamBuilder } from '../d2.js'\nimport { MultiSet } from '../multiset.js'\nimport { Index } from '../indexes.js'\nimport type { DifferenceStreamReader } from '../graph.js'\nimport type { IStreamBuilder, KeyValue } from '../types.js'\n\n/** The key identity of a Map: SameValueZero, so NaN equals NaN. */\nfunction sameOutput(left: unknown, right: unknown): boolean {\n return left === right || (left !== left && right !== right)\n}\n\n/** The value a Map stores as a key: -0 becomes 0. */\nfunction asMapKey<T>(value: T): T {\n return (value === 0 ? 0 : value) as T\n}\n\n/**\n * Base operator for reduction operations (version-free)\n */\nexport class ReduceOperator<K, V1, V2> extends UnaryOperator<[K, V1], [K, V2]> {\n // Reduce never reads presence, so its indexes skip that bookkeeping.\n #index: Index<K, V1>\n #indexOut = new Index<K, V2>({ trackPresence: false })\n #f: (values: Array<[V1, number]>) => Array<[V2, number]>\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V1]>,\n output: DifferenceStreamWriter<[K, V2]>,\n f: (values: Array<[V1, number]>) => Array<[V2, number]>,\n options?: ReduceOptions,\n ) {\n super(id, inputA, output)\n this.#index = new Index<K, V1>({ ...options, trackPresence: false })\n this.#f = f\n }\n\n run(): void {\n // Collect all input messages and update the index\n const keysTodo = new Set<K>()\n for (const message of this.inputMessages()) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n this.#index.addValue(key, [value, multiplicity])\n keysTodo.add(key)\n }\n }\n\n // For each key, compute the reduction and delta\n const result: Array<[[K, V2], number]> = []\n for (const key of keysTodo) {\n const curr = this.#index.get(key)\n const currOut = this.#indexOut.get(key)\n const out = this.#f(curr)\n\n // Most keys have at most one output before and after. Compare the two\n // directly, by the SameValueZero identity the maps below use.\n if (out.length <= 1 && currOut.length <= 1) {\n // Earlier outputs were stored as Map keys, so only the new one needs it.\n const before = currOut[0]\n const after = out[0]\n const value = after === undefined ? undefined : asMapKey(after[0])\n if (\n before !== undefined &&\n after !== undefined &&\n sameOutput(before[0], value)\n ) {\n const delta = after[1] - before[1]\n if (delta !== 0) {\n result.push([[key, before[0]], delta])\n this.#indexOut.addValue(key, [before[0], delta])\n }\n continue\n }\n if (before !== undefined) {\n result.push([[key, before[0]], -before[1]])\n this.#indexOut.addValue(key, [before[0], -before[1]])\n }\n if (after !== undefined && after[1] !== 0) {\n result.push([[key, value as V2], after[1]])\n this.#indexOut.addValue(key, [value as V2, after[1]])\n }\n continue\n }\n\n // Create maps for current and previous outputs using values directly as keys\n const newOutputMap = new Map<V2, number>()\n const oldOutputMap = new Map<V2, number>()\n\n // Process new output\n for (const [value, multiplicity] of out) {\n const existing = newOutputMap.get(value) ?? 0\n newOutputMap.set(value, existing + multiplicity)\n }\n\n // Process previous output\n for (const [value, multiplicity] of currOut) {\n const existing = oldOutputMap.get(value) ?? 0\n oldOutputMap.set(value, existing + multiplicity)\n }\n\n // First, emit removals for old values that are no longer present\n for (const [value, multiplicity] of oldOutputMap) {\n if (!newOutputMap.has(value)) {\n // Remove the old value entirely\n result.push([[key, value], -multiplicity])\n this.#indexOut.addValue(key, [value, -multiplicity])\n }\n }\n\n // Then, emit additions for new values that are not present in old\n for (const [value, multiplicity] of newOutputMap) {\n if (!oldOutputMap.has(value)) {\n // Add the new value only if it has non-zero multiplicity\n if (multiplicity !== 0) {\n result.push([[key, value], multiplicity])\n this.#indexOut.addValue(key, [value, multiplicity])\n }\n }\n }\n\n // Finally, emit multiplicity changes for values that were present and are still present\n for (const [value, newMultiplicity] of newOutputMap) {\n const oldMultiplicity = oldOutputMap.get(value)\n if (oldMultiplicity !== undefined) {\n const delta = newMultiplicity - oldMultiplicity\n // Only emit actual changes, i.e. non-zero deltas\n if (delta !== 0) {\n result.push([[key, value], delta])\n this.#indexOut.addValue(key, [value, delta])\n }\n }\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n}\n\n/** @internal groupBy's option; its producer must keep the identity exact. */\nexport type ReduceOptions = {\n /**\n * Input values are `[identity, value]` with a string or number identity\n * that determines the value: two inputs under one key with an equal\n * identity are equal. They merge without a structural hash.\n */\n prefixIdentity?: boolean\n}\n\n/**\n * Reduces the elements in the stream by key (version-free)\n */\nexport function reduce<\n KType extends (T extends KeyValue<infer K, infer _V> ? K : never),\n V1Type extends (T extends KeyValue<KType, infer V> ? V : never),\n R,\n T,\n>(\n f: (values: Array<[V1Type, number]>) => Array<[R, number]>,\n options?: ReduceOptions,\n) {\n return (stream: IStreamBuilder<T>): IStreamBuilder<KeyValue<KType, R>> => {\n const output = new StreamBuilder<KeyValue<KType, R>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<KType, R>>(),\n )\n const operator = new ReduceOperator<KType, V1Type, R>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<KType, V1Type>>,\n output.writer,\n f,\n options,\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["UnaryOperator","Index","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;AAQA,SAAS,WAAW,MAAe,OAAyB;AAC1D,SAAO,SAAS,SAAU,SAAS,QAAQ,UAAU;AACvD;AAGA,SAAS,SAAY,OAAa;AAChC,SAAQ,UAAU,IAAI,IAAI;AAC5B;AAKO,MAAM,uBAAkCA,MAAAA,cAAgC;AAAA;AAAA,EAE7E;AAAA,EACA,YAAY,IAAIC,QAAAA,MAAa,EAAE,eAAe,OAAO;AAAA,EACrD;AAAA,EAEA,YACE,IACA,QACA,QACA,GACA,SACA;AACA,UAAM,IAAI,QAAQ,MAAM;AACxB,SAAK,SAAS,IAAIA,QAAAA,MAAa,EAAE,GAAG,SAAS,eAAe,OAAO;AACnE,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAY;AAEV,UAAM,+BAAe,IAAA;AACrB,eAAW,WAAW,KAAK,iBAAiB;AAC1C,iBAAW,CAAC,MAAM,YAAY,KAAK,QAAQ,YAAY;AACrD,cAAM,CAAC,KAAK,KAAK,IAAI;AACrB,aAAK,OAAO,SAAS,KAAK,CAAC,OAAO,YAAY,CAAC;AAC/C,iBAAS,IAAI,GAAG;AAAA,MAClB;AAAA,IACF;AAGA,UAAM,SAAmC,CAAA;AACzC,eAAW,OAAO,UAAU;AAC1B,YAAM,OAAO,KAAK,OAAO,IAAI,GAAG;AAChC,YAAM,UAAU,KAAK,UAAU,IAAI,GAAG;AACtC,YAAM,MAAM,KAAK,GAAG,IAAI;AAIxB,UAAI,IAAI,UAAU,KAAK,QAAQ,UAAU,GAAG;AAE1C,cAAM,SAAS,QAAQ,CAAC;AACxB,cA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
1
|
-
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from
|
|
2
|
-
import { IStreamBuilder, KeyValue } from
|
|
1
|
+
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { IStreamBuilder, KeyValue } from "../types.cjs";
|
|
3
3
|
/**
|
|
4
4
|
* Base operator for reduction operations (version-free)
|
|
5
5
|
*/
|
|
6
|
-
export declare class ReduceOperator<K, V1, V2> extends UnaryOperator<[
|
|
6
|
+
export declare class ReduceOperator<K, V1, V2> extends UnaryOperator<[
|
|
7
|
+
K,
|
|
8
|
+
V1
|
|
9
|
+
], [
|
|
10
|
+
K,
|
|
11
|
+
V2
|
|
12
|
+
]> {
|
|
7
13
|
#private;
|
|
8
|
-
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
14
|
+
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
15
|
+
K,
|
|
16
|
+
V1
|
|
17
|
+
]>, output: DifferenceStreamWriter<[
|
|
18
|
+
K,
|
|
19
|
+
V2
|
|
20
|
+
]>, f: (values: Array<[
|
|
21
|
+
V1,
|
|
22
|
+
number
|
|
23
|
+
]>) => Array<[
|
|
24
|
+
V2,
|
|
25
|
+
number
|
|
26
|
+
]>, options?: ReduceOptions);
|
|
9
27
|
run(): void;
|
|
10
28
|
}
|
|
29
|
+
/** @internal groupBy's option; its producer must keep the identity exact. */
|
|
30
|
+
export type ReduceOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* Input values are `[identity, value]` with a string or number identity
|
|
33
|
+
* that determines the value: two inputs under one key with an equal
|
|
34
|
+
* identity are equal. They merge without a structural hash.
|
|
35
|
+
*/
|
|
36
|
+
prefixIdentity?: boolean;
|
|
37
|
+
};
|
|
11
38
|
/**
|
|
12
39
|
* Reduces the elements in the stream by key (version-free)
|
|
13
40
|
*/
|
|
14
|
-
export declare function reduce<KType extends T extends KeyValue<infer K, infer _V> ? K : never, V1Type extends T extends KeyValue<KType, infer V> ? V : never, R, T>(f: (values: Array<[
|
|
41
|
+
export declare function reduce<KType extends (T extends KeyValue<infer K, infer _V> ? K : never), V1Type extends (T extends KeyValue<KType, infer V> ? V : never), R, T>(f: (values: Array<[
|
|
42
|
+
V1Type,
|
|
43
|
+
number
|
|
44
|
+
]>) => Array<[
|
|
45
|
+
R,
|
|
46
|
+
number
|
|
47
|
+
]>, options?: ReduceOptions): (stream: IStreamBuilder<T>) => IStreamBuilder<KeyValue<KType, R>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DifferenceStreamWriter, LinearUnaryOperator, DifferenceStreamReader } from
|
|
2
|
-
import { PipedOperator } from
|
|
3
|
-
import { MultiSet } from
|
|
1
|
+
import { DifferenceStreamWriter, LinearUnaryOperator, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { PipedOperator } from "../types.cjs";
|
|
3
|
+
import { MultiSet } from "../multiset.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* Operator that applies a function to each multi-set in the input stream
|
|
6
6
|
*/
|
|
@@ -36,6 +36,15 @@ class TopKArray {
|
|
|
36
36
|
const available = this.#sortedValues.length - offset;
|
|
37
37
|
return Math.max(0, Math.min(limit, available));
|
|
38
38
|
}
|
|
39
|
+
countLeadingInWindow(predicate) {
|
|
40
|
+
let count = 0;
|
|
41
|
+
const end = this.#topKStart + this.size;
|
|
42
|
+
for (let index = this.#topKStart; index < end; index++) {
|
|
43
|
+
if (!predicate(getValue(this.#sortedValues[index]))) break;
|
|
44
|
+
count++;
|
|
45
|
+
}
|
|
46
|
+
return count;
|
|
47
|
+
}
|
|
39
48
|
/**
|
|
40
49
|
* Moves the topK window
|
|
41
50
|
*/
|