@tanstack/db-ivm 0.1.24 → 0.1.26
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/dist/cjs/d2.d.cts +3 -3
- package/dist/cjs/graph.d.cts +2 -2
- package/dist/cjs/hashing/index.d.cts +2 -2
- package/dist/cjs/index.cjs +42 -42
- package/dist/cjs/index.d.cts +6 -6
- package/dist/cjs/indexes.cjs +6 -3
- package/dist/cjs/indexes.cjs.map +1 -1
- package/dist/cjs/indexes.d.cts +38 -8
- package/dist/cjs/multiset.cjs +12 -4
- package/dist/cjs/multiset.cjs.map +1 -1
- package/dist/cjs/multiset.d.cts +9 -4
- package/dist/cjs/operators/concat.d.cts +2 -2
- package/dist/cjs/operators/consolidate.d.cts +2 -2
- package/dist/cjs/operators/count.cjs.map +1 -1
- package/dist/cjs/operators/count.d.cts +11 -5
- package/dist/cjs/operators/debug.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/filterBy.cjs.map +1 -1
- package/dist/cjs/operators/filterBy.d.cts +2 -2
- package/dist/cjs/operators/groupBy.cjs.map +1 -1
- package/dist/cjs/operators/groupBy.d.cts +5 -2
- package/dist/cjs/operators/groupedOrderBy.d.cts +8 -2
- package/dist/cjs/operators/groupedTopKWithFractionalIndex.d.cts +34 -7
- package/dist/cjs/operators/index.d.cts +21 -21
- package/dist/cjs/operators/join.cjs.map +1 -1
- package/dist/cjs/operators/join.d.cts +49 -10
- package/dist/cjs/operators/keying.d.cts +5 -2
- package/dist/cjs/operators/map.d.cts +3 -3
- package/dist/cjs/operators/negate.d.cts +3 -3
- package/dist/cjs/operators/orderBy.d.cts +20 -5
- package/dist/cjs/operators/output.d.cts +3 -3
- package/dist/cjs/operators/pipe.d.cts +1 -1
- package/dist/cjs/operators/reduce.cjs.map +1 -1
- package/dist/cjs/operators/reduce.d.cts +29 -5
- package/dist/cjs/operators/tap.d.cts +3 -3
- package/dist/cjs/operators/topK.cjs.map +1 -1
- package/dist/cjs/operators/topK.d.cts +5 -2
- package/dist/cjs/operators/topKArray.d.cts +11 -2
- package/dist/cjs/operators/topKState.d.cts +44 -8
- package/dist/cjs/operators/topKWithFractionalIndex.d.cts +41 -8
- package/dist/cjs/operators/topKWithFractionalIndexBTree.d.cts +20 -5
- package/dist/cjs/types.d.cts +6 -3
- package/dist/cjs/utils.cjs +9 -7
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +7 -4
- package/dist/esm/index.js +15 -15
- package/dist/esm/indexes.js +6 -3
- package/dist/esm/indexes.js.map +1 -1
- package/dist/esm/multiset.d.ts +1 -2
- package/dist/esm/multiset.js +12 -4
- package/dist/esm/multiset.js.map +1 -1
- package/dist/esm/operators/count.d.ts +1 -1
- package/dist/esm/operators/count.js.map +1 -1
- package/dist/esm/operators/filterBy.d.ts +1 -1
- package/dist/esm/operators/filterBy.js.map +1 -1
- package/dist/esm/operators/groupBy.js.map +1 -1
- package/dist/esm/operators/join.d.ts +6 -6
- package/dist/esm/operators/join.js.map +1 -1
- package/dist/esm/operators/reduce.d.ts +1 -1
- package/dist/esm/operators/reduce.js.map +1 -1
- package/dist/esm/operators/topK.d.ts +2 -2
- package/dist/esm/operators/topK.js.map +1 -1
- package/dist/esm/operators/topKArray.js +1 -1
- package/dist/esm/utils.d.ts +3 -3
- package/dist/esm/utils.js +9 -7
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/indexes.ts +8 -3
- package/src/multiset.ts +13 -7
- package/src/operators/count.ts +2 -2
- package/src/operators/filterBy.ts +1 -1
- package/src/operators/groupBy.ts +1 -2
- package/src/operators/join.ts +6 -6
- package/src/operators/reduce.ts +2 -2
- package/src/operators/topK.ts +4 -4
- package/src/utils.ts +14 -10
|
@@ -14,7 +14,7 @@ interface TopKOptions {
|
|
|
14
14
|
* @param options - An optional object containing limit and offset properties
|
|
15
15
|
* @returns A piped operator that limits the number of results
|
|
16
16
|
*/
|
|
17
|
-
export declare function topK<KType extends T extends KeyValue<infer K, infer _V> ? K : never, V1Type extends T extends KeyValue<KType, infer V> ? V : never, T>(comparator: (a: V1Type, b: V1Type) => number, options?: TopKOptions): PipedOperator<T, T>;
|
|
17
|
+
export declare function topK<KType extends (T extends KeyValue<infer K, infer _V> ? K : never), V1Type extends (T extends KeyValue<KType, infer V> ? V : never), T>(comparator: (a: V1Type, b: V1Type) => number, options?: TopKOptions): PipedOperator<T, T>;
|
|
18
18
|
/**
|
|
19
19
|
* Limits the number of results based on a comparator, with optional offset.
|
|
20
20
|
* This works on a keyed stream, where the key is the first element of the tuple
|
|
@@ -27,5 +27,8 @@ export declare function topK<KType extends T extends KeyValue<infer K, infer _V>
|
|
|
27
27
|
* @param options - An optional object containing limit and offset properties
|
|
28
28
|
* @returns A piped operator that orders the elements and limits the number of results
|
|
29
29
|
*/
|
|
30
|
-
export declare function topKWithIndex<KType extends T extends KeyValue<infer K, infer _V> ? K : never, V1Type extends T extends KeyValue<KType, infer V> ? V : never, T>(comparator: (a: V1Type, b: V1Type) => number, options?: TopKOptions): PipedOperator<T, KeyValue<KType, [
|
|
30
|
+
export declare function topKWithIndex<KType extends (T extends KeyValue<infer K, infer _V> ? K : never), V1Type extends (T extends KeyValue<KType, infer V> ? V : never), T>(comparator: (a: V1Type, b: V1Type) => number, options?: TopKOptions): PipedOperator<T, KeyValue<KType, [
|
|
31
|
+
V1Type,
|
|
32
|
+
number
|
|
33
|
+
]>>;
|
|
31
34
|
export {};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export type FractionalIndex = string;
|
|
2
|
-
export type IndexedValue<V> = [
|
|
2
|
+
export type IndexedValue<V> = [
|
|
3
|
+
V,
|
|
4
|
+
FractionalIndex
|
|
5
|
+
];
|
|
3
6
|
export declare function indexedValue<V>(value: V, index: FractionalIndex): IndexedValue<V>;
|
|
4
7
|
export declare function getValue<V>(indexedVal: IndexedValue<V>): V;
|
|
5
8
|
export declare function getIndex<V>(indexedVal: IndexedValue<V>): FractionalIndex;
|
|
@@ -7,7 +10,13 @@ export declare function getIndex<V>(indexedVal: IndexedValue<V>): FractionalInde
|
|
|
7
10
|
* Creates a comparator for [key, value] tuples that first compares values,
|
|
8
11
|
* then uses the row key as a stable tie-breaker.
|
|
9
12
|
*/
|
|
10
|
-
export declare function createKeyedComparator<K extends string | number, T>(comparator: (a: T, b: T) => number): (a: [
|
|
13
|
+
export declare function createKeyedComparator<K extends string | number, T>(comparator: (a: T, b: T) => number): (a: [
|
|
14
|
+
K,
|
|
15
|
+
T
|
|
16
|
+
], b: [
|
|
17
|
+
K,
|
|
18
|
+
T
|
|
19
|
+
]) => number;
|
|
11
20
|
export type TopKChanges<V> = {
|
|
12
21
|
/** Indicates which element moves into the topK (if any) */
|
|
13
22
|
moveIn: IndexedValue<V> | null;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MultiSet } from
|
|
2
|
-
import { IndexedValue, TopK, TopKChanges, TopKMoveChanges } from
|
|
1
|
+
import { MultiSet } from "../multiset.cjs";
|
|
2
|
+
import { IndexedValue, TopK, TopKChanges, TopKMoveChanges } from "./topKArray.cjs";
|
|
3
3
|
/** Apply a keyed batch's retractions before additions. An outer join can emit
|
|
4
4
|
* a replacement in the opposite order; key multiplicity alone would hide it.
|
|
5
5
|
* Consolidate first so a transient value added and removed in this same turn
|
|
@@ -7,7 +7,16 @@ import { IndexedValue, TopK, TopKChanges, TopKMoveChanges } from './topKArray.js
|
|
|
7
7
|
* identity: map can allocate a fresh value for each side of a cancelling pair.
|
|
8
8
|
* No state survives the run.
|
|
9
9
|
*/
|
|
10
|
-
export declare function topKBatch<K, T>(messages: Array<MultiSet<[
|
|
10
|
+
export declare function topKBatch<K, T>(messages: Array<MultiSet<[
|
|
11
|
+
K,
|
|
12
|
+
T
|
|
13
|
+
]>>): Generator<[
|
|
14
|
+
[
|
|
15
|
+
K,
|
|
16
|
+
T
|
|
17
|
+
],
|
|
18
|
+
number
|
|
19
|
+
], void, unknown>;
|
|
11
20
|
/**
|
|
12
21
|
* Helper class that manages the state for a single topK window.
|
|
13
22
|
* Encapsulates the multiplicity tracking and topK data structure,
|
|
@@ -18,27 +27,54 @@ export declare function topKBatch<K, T>(messages: Array<MultiSet<[K, T]>>): Gene
|
|
|
18
27
|
*/
|
|
19
28
|
export declare class TopKState<K extends string | number, T> {
|
|
20
29
|
#private;
|
|
21
|
-
constructor(topK: TopK<[
|
|
30
|
+
constructor(topK: TopK<[
|
|
31
|
+
K,
|
|
32
|
+
T
|
|
33
|
+
]>);
|
|
22
34
|
get size(): number;
|
|
23
35
|
get isEmpty(): boolean;
|
|
24
36
|
/**
|
|
25
37
|
* Process an element update (insert or delete based on multiplicity change).
|
|
26
38
|
* Returns the changes to the topK window.
|
|
27
39
|
*/
|
|
28
|
-
processElement(key: K, value: T, multiplicity: number): TopKChanges<[
|
|
40
|
+
processElement(key: K, value: T, multiplicity: number): TopKChanges<[
|
|
41
|
+
K,
|
|
42
|
+
T
|
|
43
|
+
]>;
|
|
29
44
|
/**
|
|
30
45
|
* Move the topK window. Only works with TopKArray implementation.
|
|
31
46
|
*/
|
|
32
47
|
move(options: {
|
|
33
48
|
offset?: number;
|
|
34
49
|
limit?: number;
|
|
35
|
-
}): TopKMoveChanges<[
|
|
50
|
+
}): TopKMoveChanges<[
|
|
51
|
+
K,
|
|
52
|
+
T
|
|
53
|
+
]>;
|
|
36
54
|
}
|
|
37
55
|
/**
|
|
38
56
|
* Handles a moveIn change by adding it to the result array.
|
|
39
57
|
*/
|
|
40
|
-
export declare function handleMoveIn<K extends string | number, T>(moveIn: IndexedValue<[
|
|
58
|
+
export declare function handleMoveIn<K extends string | number, T>(moveIn: IndexedValue<[
|
|
59
|
+
K,
|
|
60
|
+
T
|
|
61
|
+
]> | null, result: Array<[
|
|
62
|
+
[
|
|
63
|
+
K,
|
|
64
|
+
IndexedValue<T>
|
|
65
|
+
],
|
|
66
|
+
number
|
|
67
|
+
]>): void;
|
|
41
68
|
/**
|
|
42
69
|
* Handles a moveOut change by adding it to the result array.
|
|
43
70
|
*/
|
|
44
|
-
export declare function handleMoveOut<K extends string | number, T>(moveOut: IndexedValue<[
|
|
71
|
+
export declare function handleMoveOut<K extends string | number, T>(moveOut: IndexedValue<[
|
|
72
|
+
K,
|
|
73
|
+
T
|
|
74
|
+
]> | null, result: Array<[
|
|
75
|
+
[
|
|
76
|
+
K,
|
|
77
|
+
IndexedValue<T>
|
|
78
|
+
],
|
|
79
|
+
number
|
|
80
|
+
]>): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from
|
|
2
|
-
import { IndexedValue, TopK } from
|
|
3
|
-
import { PipedOperator } from
|
|
1
|
+
import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from "../graph.cjs";
|
|
2
|
+
import { IndexedValue, TopK } from "./topKArray.cjs";
|
|
3
|
+
import { PipedOperator } from "../types.cjs";
|
|
4
4
|
export interface TopKWithFractionalIndexOptions {
|
|
5
5
|
limit?: number;
|
|
6
6
|
offset?: number;
|
|
@@ -15,10 +15,31 @@ export interface TopKWithFractionalIndexOptions {
|
|
|
15
15
|
* This operator maintains fractional indices for sorted elements
|
|
16
16
|
* and only updates indices when elements move position
|
|
17
17
|
*/
|
|
18
|
-
export declare class TopKWithFractionalIndexOperator<K extends string | number, T> extends UnaryOperator<[
|
|
18
|
+
export declare class TopKWithFractionalIndexOperator<K extends string | number, T> extends UnaryOperator<[
|
|
19
|
+
K,
|
|
20
|
+
T
|
|
21
|
+
], [
|
|
22
|
+
K,
|
|
23
|
+
IndexedValue<T>
|
|
24
|
+
]> {
|
|
19
25
|
#private;
|
|
20
|
-
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
21
|
-
|
|
26
|
+
constructor(id: number, inputA: DifferenceStreamReader<[
|
|
27
|
+
K,
|
|
28
|
+
T
|
|
29
|
+
]>, output: DifferenceStreamWriter<[
|
|
30
|
+
K,
|
|
31
|
+
IndexedValue<T>
|
|
32
|
+
]>, comparator: (a: T, b: T) => number, options: TopKWithFractionalIndexOptions);
|
|
33
|
+
protected createTopK(offset: number, limit: number, comparator: (a: [
|
|
34
|
+
K,
|
|
35
|
+
T
|
|
36
|
+
], b: [
|
|
37
|
+
K,
|
|
38
|
+
T
|
|
39
|
+
]) => number): TopK<[
|
|
40
|
+
K,
|
|
41
|
+
T
|
|
42
|
+
]>;
|
|
22
43
|
/**
|
|
23
44
|
* Moves the topK window based on the provided offset and limit.
|
|
24
45
|
* Any changes to the topK are sent to the output.
|
|
@@ -28,7 +49,13 @@ export declare class TopKWithFractionalIndexOperator<K extends string | number,
|
|
|
28
49
|
limit?: number;
|
|
29
50
|
}): void;
|
|
30
51
|
run(): void;
|
|
31
|
-
processElement(key: K, value: T, multiplicity: number, result: Array<[
|
|
52
|
+
processElement(key: K, value: T, multiplicity: number, result: Array<[
|
|
53
|
+
[
|
|
54
|
+
K,
|
|
55
|
+
IndexedValue<T>
|
|
56
|
+
],
|
|
57
|
+
number
|
|
58
|
+
]>): void;
|
|
32
59
|
}
|
|
33
60
|
/**
|
|
34
61
|
* Limits the number of results based on a comparator, with optional offset.
|
|
@@ -40,4 +67,10 @@ export declare class TopKWithFractionalIndexOperator<K extends string | number,
|
|
|
40
67
|
* @param options - An optional object containing limit and offset properties
|
|
41
68
|
* @returns A piped operator that orders the elements and limits the number of results
|
|
42
69
|
*/
|
|
43
|
-
export declare function topKWithFractionalIndex<KType extends string | number, T>(comparator: (a: T, b: T) => number, options?: TopKWithFractionalIndexOptions): PipedOperator<[
|
|
70
|
+
export declare function topKWithFractionalIndex<KType extends string | number, T>(comparator: (a: T, b: T) => number, options?: TopKWithFractionalIndexOptions): PipedOperator<[
|
|
71
|
+
KType,
|
|
72
|
+
T
|
|
73
|
+
], [
|
|
74
|
+
KType,
|
|
75
|
+
IndexedValue<T>
|
|
76
|
+
]>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { TopKWithFractionalIndexOperator, TopKWithFractionalIndexOptions } from
|
|
2
|
-
import { IndexedValue, TopK } from
|
|
3
|
-
import { PipedOperator } from
|
|
1
|
+
import { TopKWithFractionalIndexOperator, TopKWithFractionalIndexOptions } from "./topKWithFractionalIndex.cjs";
|
|
2
|
+
import { IndexedValue, TopK } from "./topKArray.cjs";
|
|
3
|
+
import { PipedOperator } from "../types.cjs";
|
|
4
4
|
export declare function loadBTree(): Promise<void>;
|
|
5
5
|
/**
|
|
6
6
|
* Operator for fractional indexed topK operations
|
|
@@ -8,7 +8,16 @@ export declare function loadBTree(): Promise<void>;
|
|
|
8
8
|
* and only updates indices when elements move position
|
|
9
9
|
*/
|
|
10
10
|
export declare class TopKWithFractionalIndexBTreeOperator<K extends string | number, T> extends TopKWithFractionalIndexOperator<K, T> {
|
|
11
|
-
protected createTopK(offset: number, limit: number, comparator: (a: [
|
|
11
|
+
protected createTopK(offset: number, limit: number, comparator: (a: [
|
|
12
|
+
K,
|
|
13
|
+
T
|
|
14
|
+
], b: [
|
|
15
|
+
K,
|
|
16
|
+
T
|
|
17
|
+
]) => number): TopK<[
|
|
18
|
+
K,
|
|
19
|
+
T
|
|
20
|
+
]>;
|
|
12
21
|
}
|
|
13
22
|
/**
|
|
14
23
|
* Limits the number of results based on a comparator, with optional offset.
|
|
@@ -25,4 +34,10 @@ export declare class TopKWithFractionalIndexBTreeOperator<K extends string | num
|
|
|
25
34
|
* @param options - An optional object containing limit and offset properties
|
|
26
35
|
* @returns A piped operator that orders the elements and limits the number of results
|
|
27
36
|
*/
|
|
28
|
-
export declare function topKWithFractionalIndexBTree<KType extends string | number, T>(comparator: (a: T, b: T) => number, options?: TopKWithFractionalIndexOptions): PipedOperator<[
|
|
37
|
+
export declare function topKWithFractionalIndexBTree<KType extends string | number, T>(comparator: (a: T, b: T) => number, options?: TopKWithFractionalIndexOptions): PipedOperator<[
|
|
38
|
+
KType,
|
|
39
|
+
T
|
|
40
|
+
], [
|
|
41
|
+
KType,
|
|
42
|
+
IndexedValue<T>
|
|
43
|
+
]>;
|
package/dist/cjs/types.d.cts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { MultiSet, MultiSetArray } from
|
|
2
|
-
import { BinaryOperator, DifferenceStreamReader, DifferenceStreamWriter, UnaryOperator } from
|
|
3
|
-
export type KeyValue<K, V> = [
|
|
1
|
+
import { MultiSet, MultiSetArray } from "./multiset.cjs";
|
|
2
|
+
import { BinaryOperator, DifferenceStreamReader, DifferenceStreamWriter, UnaryOperator } from "./graph.cjs";
|
|
3
|
+
export type KeyValue<K, V> = [
|
|
4
|
+
K,
|
|
5
|
+
V
|
|
6
|
+
];
|
|
4
7
|
export interface IOperator<_T> {
|
|
5
8
|
run: () => void;
|
|
6
9
|
hasPendingWork: () => boolean;
|
package/dist/cjs/utils.cjs
CHANGED
|
@@ -28,15 +28,17 @@ function binarySearch(array, value, comparator) {
|
|
|
28
28
|
}
|
|
29
29
|
return low;
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
let nextObjectId = 0;
|
|
33
|
-
function getStringId(value) {
|
|
31
|
+
function getStringId(value, referenceIds) {
|
|
34
32
|
if (value === null) return `null`;
|
|
35
33
|
if (value === void 0) return `undefined`;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const type = typeof value;
|
|
35
|
+
if (type === `object` || type === `function` || type === `symbol`) {
|
|
36
|
+
const reference = value;
|
|
37
|
+
let id = referenceIds.get(reference);
|
|
38
|
+
if (id === void 0) referenceIds.set(reference, id = referenceIds.size);
|
|
39
|
+
return `ref_${id}`;
|
|
40
|
+
}
|
|
41
|
+
return `${type}_${String(value)}`;
|
|
40
42
|
}
|
|
41
43
|
function diffHalfOpen(a, b) {
|
|
42
44
|
const [a1, a2] = a;
|
package/dist/cjs/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["import { isTemporal } from './hashing/temporal.js'\n\n/**\n * Simple assertion function for runtime checks.\n * Throws an error if the condition is false.\n */\nexport function assert(\n condition: unknown,\n message?: string,\n): asserts condition {\n if (!condition) {\n throw new Error(message || `Assertion failed`)\n }\n}\n\n// JS engines have various limits on how many args can be passed to a function\n// with a spread operator, so we need to split the operation into chunks\n// 32767 is the max for Chrome 14, all others are higher\n// TODO: investigate the performance of this and other approaches\nconst chunkSize = 30000\nexport function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>) {\n if (other.length <= chunkSize) {\n array.push(...other)\n } else {\n for (let i = 0; i < other.length; i += chunkSize) {\n const chunk = other.slice(i, i + chunkSize)\n array.push(...chunk)\n }\n }\n}\n\nexport function binarySearch<T>(\n array: Array<T>,\n value: T,\n comparator: (a: T, b: T) => number,\n): number {\n let low = 0\n let high = array.length\n while (low < high) {\n const mid = Math.floor((low + high) / 2)\n const comparison = comparator(array[mid]!, value)\n if (comparison < 0) {\n low = mid + 1\n } else if (comparison > 0) {\n high = mid\n } else {\n return mid\n }\n }\n return low\n}\n\nconst objectIds = new WeakMap<object, number>()\nlet nextObjectId = 0\n\n/**\n * A string identity for composite keys: primitives by value, objects by\n * reference.\n */\nexport function getStringId(value: unknown): string {\n if (value === null) return `null`\n if (value === undefined) return `undefined`\n if (typeof value !== `object`) return `str_${String(value)}`\n let id = objectIds.get(value)\n if (id === undefined) objectIds.set(value, (id = nextObjectId++))\n return `obj_${id}`\n}\n\nexport function* concatIterable<T>(\n ...iterables: Array<Iterable<T>>\n): Iterable<T> {\n for (const iterable of iterables) {\n yield* iterable\n }\n}\n\nexport function* mapIterable<T, U>(\n it: Iterable<T>,\n fn: (t: T) => U,\n): Iterable<U> {\n for (const t of it) {\n yield fn(t)\n }\n}\n\nexport type HRange = [number, number] // half-open [start, end[ i.e. end is exclusive\n\n/**\n * Computes the difference between two half-open ranges.\n * @param a - The first half-open range\n * @param b - The second half-open range\n * @returns The difference between the two ranges\n */\nexport function diffHalfOpen(a: HRange, b: HRange) {\n const [a1, a2] = a\n const [b1, b2] = b\n\n // A \\ B can be up to two segments (left and right of the overlap)\n const onlyInA: Array<number> = [\n ...range(a1, Math.min(a2, b1)), // left side of A outside B\n ...range(Math.max(a1, b2), a2), // right side of A outside B\n ]\n\n // B \\ A similarly\n const onlyInB: Array<number> = [\n ...range(b1, Math.min(b2, a1)),\n ...range(Math.max(b1, a2), b2),\n ]\n\n return { onlyInA, onlyInB }\n}\n\nfunction range(start: number, end: number): Array<number> {\n const out: Array<number> = []\n for (let i = start; i < end; i++) out.push(i)\n return out\n}\n\n/**\n * Compares two keys (string | number) in a consistent, deterministic way.\n * Handles mixed types by ordering strings before numbers.\n */\nexport function compareKeys(a: string | number, b: string | number): number {\n // Same type: compare directly\n if (typeof a === typeof b) {\n if (typeof a === `number` && typeof b === `number`) {\n const aIsNaN = Number.isNaN(a)\n const bIsNaN = Number.isNaN(b)\n if (aIsNaN || bIsNaN) {\n if (aIsNaN && bIsNaN) return 0\n return aIsNaN ? 1 : -1\n }\n }\n if (a < b) return -1\n if (a > b) return 1\n return 0\n }\n // Different types: strings come before numbers\n return typeof a === `string` ? -1 : 1\n}\n\ntype CanonicalValue =\n | readonly [`undefined`]\n | readonly [`null`]\n | readonly [`boolean`, boolean]\n | readonly [`number`, number | `NaN` | `Infinity` | `-Infinity`]\n | readonly [`bigint`, string]\n | readonly [`string`, string]\n | readonly [`date`, number | `Invalid`]\n | readonly [`temporal`, string, string]\n | readonly [`regexp`, string, string]\n | readonly [`bytes`, Array<number>]\n | readonly [`array`, Array<CanonicalValue>]\n | readonly [`map`, Array<readonly [CanonicalValue, CanonicalValue]>]\n | readonly [`set`, Array<CanonicalValue>]\n | readonly [`object`, Array<readonly [string, CanonicalValue]>]\n\n/**\n * Serializes a supported query value into one canonical key.\n *\n * JSON's native encoding is not suitable for relation keys: it merges BigInt\n * with strings when a replacer is used, merges NaN with null, drops undefined,\n * and depends on object insertion order. Ordinary JSON values keep their\n * established wire form. Values that need richer types use a reserved prefix\n * plus a structural, type-tagged encoding.\n */\nexport function serializeValue(value: unknown): string {\n if (isJsonSafeStructuralValue(value, new Set())) {\n return JSON.stringify(toStableJsonValue(value))\n }\n\n return `~${JSON.stringify(toCanonicalValue(value, new Set()))}`\n}\n\ntype JsonValue =\n | null\n | boolean\n | number\n | string\n | Array<JsonValue>\n | { [key: string]: JsonValue }\n\nfunction isJsonSafeStructuralValue(\n value: unknown,\n ancestors: Set<object>,\n): boolean {\n if (value === null) return true\n\n switch (typeof value) {\n case `boolean`:\n case `string`:\n return true\n case `number`:\n return Number.isFinite(value)\n case `undefined`:\n case `bigint`:\n case `symbol`:\n case `function`:\n return false\n }\n\n return withAcyclicValue(value, ancestors, () => {\n if (\n value instanceof Date ||\n value instanceof RegExp ||\n value instanceof Uint8Array ||\n isTemporal(value) ||\n value instanceof Map ||\n value instanceof Set\n ) {\n return false\n }\n\n return Array.isArray(value)\n ? value.every((item) => isJsonSafeStructuralValue(item, ancestors))\n : Object.keys(value).every((key) =>\n isJsonSafeStructuralValue(\n (value as Record<string, unknown>)[key],\n ancestors,\n ),\n )\n })\n}\n\nfunction toStableJsonValue(value: unknown): JsonValue {\n if (\n value === null ||\n typeof value === `boolean` ||\n typeof value === `number` ||\n typeof value === `string`\n ) {\n return value\n }\n\n if (Array.isArray(value)) {\n return value.map(toStableJsonValue)\n }\n\n return Object.fromEntries(\n Object.keys(value as object)\n .sort()\n .map((key) => [\n key,\n toStableJsonValue((value as Record<string, unknown>)[key]),\n ]),\n )\n}\n\nfunction toCanonicalValue(\n value: unknown,\n ancestors: Set<object>,\n): CanonicalValue {\n if (value === undefined) return [`undefined`]\n if (value === null) return [`null`]\n\n switch (typeof value) {\n case `boolean`:\n return [`boolean`, value]\n case `number`:\n if (Number.isNaN(value)) return [`number`, `NaN`]\n if (value === Infinity) return [`number`, `Infinity`]\n if (value === -Infinity) return [`number`, `-Infinity`]\n return [`number`, value === 0 ? 0 : value]\n case `bigint`:\n return [`bigint`, value.toString()]\n case `string`:\n return [`string`, value]\n case `symbol`:\n case `function`:\n throw new TypeError(\n `Cannot serialize ${typeof value} as a structural relation key`,\n )\n }\n\n return withAcyclicValue(value, ancestors, () => {\n if (value instanceof Date) {\n const timestamp = value.getTime()\n return Number.isNaN(timestamp)\n ? ([`date`, `Invalid`] as const)\n : ([`date`, timestamp] as const)\n }\n\n if (value instanceof RegExp) {\n return [`regexp`, value.source, value.flags]\n }\n\n if (value instanceof Uint8Array) {\n return [`bytes`, Array.from(value)]\n }\n\n if (isTemporal(value)) {\n return [`temporal`, value[Symbol.toStringTag], value.toString()]\n }\n\n if (Array.isArray(value)) {\n return [`array`, value.map((item) => toCanonicalValue(item, ancestors))]\n }\n\n if (value instanceof Map) {\n const entries = [...value.entries()].map(\n ([key, entryValue]) =>\n [\n toCanonicalValue(key, ancestors),\n toCanonicalValue(entryValue, ancestors),\n ] as const,\n )\n entries.sort((left, right) =>\n compareSerializedValues(JSON.stringify(left), JSON.stringify(right)),\n )\n return [`map`, entries]\n }\n\n if (value instanceof Set) {\n const entries = [...value].map((entry) =>\n toCanonicalValue(entry, ancestors),\n )\n entries.sort((left, right) =>\n compareSerializedValues(JSON.stringify(left), JSON.stringify(right)),\n )\n return [`set`, entries]\n }\n\n const entries = Object.keys(value)\n .sort()\n .map(\n (key) =>\n [\n key,\n toCanonicalValue(\n (value as Record<string, unknown>)[key],\n ancestors,\n ),\n ] as const,\n )\n return [`object`, entries]\n })\n}\n\nfunction compareSerializedValues(left: string, right: string): number {\n return left < right ? -1 : left > right ? 1 : 0\n}\n\nfunction withAcyclicValue<T>(\n value: object,\n ancestors: Set<object>,\n encode: () => T,\n): T {\n if (ancestors.has(value)) {\n throw new TypeError(`Cannot serialize a cyclic structural relation key`)\n }\n\n ancestors.add(value)\n try {\n return encode()\n } finally {\n ancestors.delete(value)\n }\n}\n"],"names":["isTemporal","entries"],"mappings":";;;AAmBA,MAAM,YAAY;AACX,SAAS,iBAAiB,OAAuB,OAA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MAAM,UAClB,wBAAwB,KAAK,UAAU,IAAI,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,MAAA;AAErE,aAAO,CAAC,OAAOA,QAAO;AAAA,IACxB;AAEA,UAAM,UAAU,OAAO,KAAK,KAAK,EAC9B,OACA;AAAA,MACC,CAAC,QACC;AAAA,QACE;AAAA,QACA;AAAA,UACG,MAAkC,GAAG;AAAA,UACtC;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAEN,WAAO,CAAC,UAAU,OAAO;AAAA,EAC3B,CAAC;AACH;AAEA,SAAS,wBAAwB,MAAc,OAAuB;AACpE,SAAO,OAAO,QAAQ,KAAK,OAAO,QAAQ,IAAI;AAChD;AAEA,SAAS,iBACP,OACA,WACA,QACG;AACH,MAAI,UAAU,IAAI,KAAK,GAAG;AACxB,UAAM,IAAI,UAAU,mDAAmD;AAAA,EACzE;AAEA,YAAU,IAAI,KAAK;AACnB,MAAI;AACF,WAAO,OAAA;AAAA,EACT,UAAA;AACE,cAAU,OAAO,KAAK;AAAA,EACxB;AACF;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["import { isTemporal } from './hashing/temporal.js'\n\n/**\n * Simple assertion function for runtime checks.\n * Throws an error if the condition is false.\n */\nexport function assert(\n condition: unknown,\n message?: string,\n): asserts condition {\n if (!condition) {\n throw new Error(message || `Assertion failed`)\n }\n}\n\n// JS engines have various limits on how many args can be passed to a function\n// with a spread operator, so we need to split the operation into chunks\n// 32767 is the max for Chrome 14, all others are higher\n// TODO: investigate the performance of this and other approaches\nconst chunkSize = 30000\nexport function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>) {\n if (other.length <= chunkSize) {\n array.push(...other)\n } else {\n for (let i = 0; i < other.length; i += chunkSize) {\n const chunk = other.slice(i, i + chunkSize)\n array.push(...chunk)\n }\n }\n}\n\nexport function binarySearch<T>(\n array: Array<T>,\n value: T,\n comparator: (a: T, b: T) => number,\n): number {\n let low = 0\n let high = array.length\n while (low < high) {\n const mid = Math.floor((low + high) / 2)\n const comparison = comparator(array[mid]!, value)\n if (comparison < 0) {\n low = mid + 1\n } else if (comparison > 0) {\n high = mid\n } else {\n return mid\n }\n }\n return low\n}\n\n/**\n * A string identity within one consolidation: primitives by value, objects,\n * functions, and symbols by identity.\n */\nexport function getStringId(\n value: unknown,\n referenceIds: Map<object | symbol, number>,\n): string {\n if (value === null) return `null`\n if (value === undefined) return `undefined`\n const type = typeof value\n if (type === `object` || type === `function` || type === `symbol`) {\n const reference = value as object | symbol\n let id = referenceIds.get(reference)\n if (id === undefined) referenceIds.set(reference, (id = referenceIds.size))\n return `ref_${id}`\n }\n return `${type}_${String(value)}`\n}\n\nexport function* concatIterable<T>(\n ...iterables: Array<Iterable<T>>\n): Iterable<T> {\n for (const iterable of iterables) {\n yield* iterable\n }\n}\n\nexport function* mapIterable<T, U>(\n it: Iterable<T>,\n fn: (t: T) => U,\n): Iterable<U> {\n for (const t of it) {\n yield fn(t)\n }\n}\n\nexport type HRange = [number, number] // half-open [start, end[ i.e. end is exclusive\n\n/**\n * Computes the difference between two half-open ranges.\n * @param a - The first half-open range\n * @param b - The second half-open range\n * @returns The difference between the two ranges\n */\nexport function diffHalfOpen(a: HRange, b: HRange) {\n const [a1, a2] = a\n const [b1, b2] = b\n\n // A \\ B can be up to two segments (left and right of the overlap)\n const onlyInA: Array<number> = [\n ...range(a1, Math.min(a2, b1)), // left side of A outside B\n ...range(Math.max(a1, b2), a2), // right side of A outside B\n ]\n\n // B \\ A similarly\n const onlyInB: Array<number> = [\n ...range(b1, Math.min(b2, a1)),\n ...range(Math.max(b1, a2), b2),\n ]\n\n return { onlyInA, onlyInB }\n}\n\nfunction range(start: number, end: number): Array<number> {\n const out: Array<number> = []\n for (let i = start; i < end; i++) out.push(i)\n return out\n}\n\n/**\n * Compares two keys (string | number) in a consistent, deterministic way.\n * Handles mixed types by ordering strings before numbers.\n */\nexport function compareKeys(a: string | number, b: string | number): number {\n // Same type: compare directly\n if (typeof a === typeof b) {\n if (typeof a === `number` && typeof b === `number`) {\n const aIsNaN = Number.isNaN(a)\n const bIsNaN = Number.isNaN(b)\n if (aIsNaN || bIsNaN) {\n if (aIsNaN && bIsNaN) return 0\n return aIsNaN ? 1 : -1\n }\n }\n if (a < b) return -1\n if (a > b) return 1\n return 0\n }\n // Different types: strings come before numbers\n return typeof a === `string` ? -1 : 1\n}\n\ntype CanonicalValue =\n | readonly [`undefined`]\n | readonly [`null`]\n | readonly [`boolean`, boolean]\n | readonly [`number`, number | `NaN` | `Infinity` | `-Infinity`]\n | readonly [`bigint`, string]\n | readonly [`string`, string]\n | readonly [`date`, number | `Invalid`]\n | readonly [`temporal`, string, string]\n | readonly [`regexp`, string, string]\n | readonly [`bytes`, Array<number>]\n | readonly [`array`, Array<CanonicalValue>]\n | readonly [`map`, Array<readonly [CanonicalValue, CanonicalValue]>]\n | readonly [`set`, Array<CanonicalValue>]\n | readonly [`object`, Array<readonly [string, CanonicalValue]>]\n\n/**\n * Serializes a supported query value into one canonical key.\n *\n * JSON's native encoding is not suitable for relation keys: it merges BigInt\n * with strings when a replacer is used, merges NaN with null, drops undefined,\n * and depends on object insertion order. Ordinary JSON values keep their\n * established wire form. Values that need richer types use a reserved prefix\n * plus a structural, type-tagged encoding.\n */\nexport function serializeValue(value: unknown): string {\n if (isJsonSafeStructuralValue(value, new Set())) {\n return JSON.stringify(toStableJsonValue(value))\n }\n\n return `~${JSON.stringify(toCanonicalValue(value, new Set()))}`\n}\n\ntype JsonValue =\n | null\n | boolean\n | number\n | string\n | Array<JsonValue>\n | { [key: string]: JsonValue }\n\nfunction isJsonSafeStructuralValue(\n value: unknown,\n ancestors: Set<object>,\n): boolean {\n if (value === null) return true\n\n switch (typeof value) {\n case `boolean`:\n case `string`:\n return true\n case `number`:\n return Number.isFinite(value)\n case `undefined`:\n case `bigint`:\n case `symbol`:\n case `function`:\n return false\n }\n\n return withAcyclicValue(value, ancestors, () => {\n if (\n value instanceof Date ||\n value instanceof RegExp ||\n value instanceof Uint8Array ||\n isTemporal(value) ||\n value instanceof Map ||\n value instanceof Set\n ) {\n return false\n }\n\n return Array.isArray(value)\n ? value.every((item) => isJsonSafeStructuralValue(item, ancestors))\n : Object.keys(value).every((key) =>\n isJsonSafeStructuralValue(\n (value as Record<string, unknown>)[key],\n ancestors,\n ),\n )\n })\n}\n\nfunction toStableJsonValue(value: unknown): JsonValue {\n if (\n value === null ||\n typeof value === `boolean` ||\n typeof value === `number` ||\n typeof value === `string`\n ) {\n return value\n }\n\n if (Array.isArray(value)) {\n return value.map(toStableJsonValue)\n }\n\n return Object.fromEntries(\n Object.keys(value as object)\n .sort()\n .map((key) => [\n key,\n toStableJsonValue((value as Record<string, unknown>)[key]),\n ]),\n )\n}\n\nfunction toCanonicalValue(\n value: unknown,\n ancestors: Set<object>,\n): CanonicalValue {\n if (value === undefined) return [`undefined`]\n if (value === null) return [`null`]\n\n switch (typeof value) {\n case `boolean`:\n return [`boolean`, value]\n case `number`:\n if (Number.isNaN(value)) return [`number`, `NaN`]\n if (value === Infinity) return [`number`, `Infinity`]\n if (value === -Infinity) return [`number`, `-Infinity`]\n return [`number`, value === 0 ? 0 : value]\n case `bigint`:\n return [`bigint`, value.toString()]\n case `string`:\n return [`string`, value]\n case `symbol`:\n case `function`:\n throw new TypeError(\n `Cannot serialize ${typeof value} as a structural relation key`,\n )\n }\n\n return withAcyclicValue(value, ancestors, () => {\n if (value instanceof Date) {\n const timestamp = value.getTime()\n return Number.isNaN(timestamp)\n ? ([`date`, `Invalid`] as const)\n : ([`date`, timestamp] as const)\n }\n\n if (value instanceof RegExp) {\n return [`regexp`, value.source, value.flags]\n }\n\n if (value instanceof Uint8Array) {\n return [`bytes`, Array.from(value)]\n }\n\n if (isTemporal(value)) {\n return [`temporal`, value[Symbol.toStringTag], value.toString()]\n }\n\n if (Array.isArray(value)) {\n return [`array`, value.map((item) => toCanonicalValue(item, ancestors))]\n }\n\n if (value instanceof Map) {\n const entries = [...value.entries()].map(\n ([key, entryValue]) =>\n [\n toCanonicalValue(key, ancestors),\n toCanonicalValue(entryValue, ancestors),\n ] as const,\n )\n entries.sort((left, right) =>\n compareSerializedValues(JSON.stringify(left), JSON.stringify(right)),\n )\n return [`map`, entries]\n }\n\n if (value instanceof Set) {\n const entries = [...value].map((entry) =>\n toCanonicalValue(entry, ancestors),\n )\n entries.sort((left, right) =>\n compareSerializedValues(JSON.stringify(left), JSON.stringify(right)),\n )\n return [`set`, entries]\n }\n\n const entries = Object.keys(value)\n .sort()\n .map(\n (key) =>\n [\n key,\n toCanonicalValue(\n (value as Record<string, unknown>)[key],\n ancestors,\n ),\n ] as const,\n )\n return [`object`, entries]\n })\n}\n\nfunction compareSerializedValues(left: string, right: string): number {\n return left < right ? -1 : left > right ? 1 : 0\n}\n\nfunction withAcyclicValue<T>(\n value: object,\n ancestors: Set<object>,\n encode: () => T,\n): T {\n if (ancestors.has(value)) {\n throw new TypeError(`Cannot serialize a cyclic structural relation key`)\n }\n\n ancestors.add(value)\n try {\n return encode()\n } finally {\n ancestors.delete(value)\n }\n}\n"],"names":["isTemporal","entries"],"mappings":";;;AAmBA,MAAM,YAAY;AACX,SAAS,iBAAiB,OAAuB,OAAuB;AAC7E,MAAI,MAAM,UAAU,WAAW;AAC7B,UAAM,KAAK,GAAG,KAAK;AAAA,EACrB,OAAO;AACL,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,WAAW;AAChD,YAAM,QAAQ,MAAM,MAAM,GAAG,IAAI,SAAS;AAC1C,YAAM,KAAK,GAAG,KAAK;AAAA,IACrB;AAAA,EACF;AACF;AAEO,SAAS,aACd,OACA,OACA,YACQ;AACR,MAAI,MAAM;AACV,MAAI,OAAO,MAAM;AACjB,SAAO,MAAM,MAAM;AACjB,UAAM,MAAM,KAAK,OAAO,MAAM,QAAQ,CAAC;AACvC,UAAM,aAAa,WAAW,MAAM,GAAG,GAAI,KAAK;AAChD,QAAI,aAAa,GAAG;AAClB,YA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I;AAChD;AAEA,SAAS,iBACP,OACA,WACA,QACG;AACH,MAAI,UAAU,IAAI,KAAK,GAAG;AACxB,UAAM,IAAI,UAAU,mDAAmD;AAAA,EACzE;AAEA,YAAU,IAAI,KAAK;AACnB,MAAI;AACF,WAAO,OAAA;AAAA,EACT,UAAA;AACE,cAAU,OAAO,KAAK;AAAA,EACxB;AACF;;;;;;;"}
|
package/dist/cjs/utils.d.cts
CHANGED
|
@@ -6,13 +6,16 @@ export declare function assert(condition: unknown, message?: string): asserts co
|
|
|
6
6
|
export declare function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>): void;
|
|
7
7
|
export declare function binarySearch<T>(array: Array<T>, value: T, comparator: (a: T, b: T) => number): number;
|
|
8
8
|
/**
|
|
9
|
-
* A string identity
|
|
10
|
-
*
|
|
9
|
+
* A string identity within one consolidation: primitives by value, objects,
|
|
10
|
+
* functions, and symbols by identity.
|
|
11
11
|
*/
|
|
12
|
-
export declare function getStringId(value: unknown): string;
|
|
12
|
+
export declare function getStringId(value: unknown, referenceIds: Map<object | symbol, number>): string;
|
|
13
13
|
export declare function concatIterable<T>(...iterables: Array<Iterable<T>>): Iterable<T>;
|
|
14
14
|
export declare function mapIterable<T, U>(it: Iterable<T>, fn: (t: T) => U): Iterable<U>;
|
|
15
|
-
export type HRange = [
|
|
15
|
+
export type HRange = [
|
|
16
|
+
number,
|
|
17
|
+
number
|
|
18
|
+
];
|
|
16
19
|
/**
|
|
17
20
|
* Computes the difference between two half-open ranges.
|
|
18
21
|
* @param a - The first half-open range
|
package/dist/esm/index.js
CHANGED
|
@@ -2,27 +2,27 @@ import { D2, RootStreamBuilder, StreamBuilder } from "./d2.js";
|
|
|
2
2
|
import { MultiSet } from "./multiset.js";
|
|
3
3
|
import { compareKeys, serializeValue } from "./utils.js";
|
|
4
4
|
import { registerOpaqueHash } from "./hashing/hash.js";
|
|
5
|
-
import { groupBy, groupByOperators } from "./operators/groupBy.js";
|
|
6
|
-
import { pipe } from "./operators/pipe.js";
|
|
7
|
-
import { MapOperator, map } from "./operators/map.js";
|
|
8
|
-
import { TapOperator, tap } from "./operators/tap.js";
|
|
9
|
-
import { FilterOperator, filter } from "./operators/filter.js";
|
|
10
|
-
import { NegateOperator, negate } from "./operators/negate.js";
|
|
11
5
|
import { ConcatOperator, concat } from "./operators/concat.js";
|
|
12
|
-
import { DebugOperator, debug } from "./operators/debug.js";
|
|
13
|
-
import { OutputOperator, output } from "./operators/output.js";
|
|
14
6
|
import { ConsolidateOperator, consolidate } from "./operators/consolidate.js";
|
|
15
|
-
import { JoinOperator, antiJoin, fullJoin, innerJoin, join, leftJoin, rightJoin } from "./operators/join.js";
|
|
16
|
-
import { ReduceOperator, reduce } from "./operators/reduce.js";
|
|
17
7
|
import { CountOperator, count } from "./operators/count.js";
|
|
8
|
+
import { DebugOperator, debug } from "./operators/debug.js";
|
|
18
9
|
import { DistinctOperator, distinct } from "./operators/distinct.js";
|
|
19
|
-
import {
|
|
20
|
-
import { topK, topKWithIndex } from "./operators/topK.js";
|
|
21
|
-
import { TopKWithFractionalIndexOperator, topKWithFractionalIndex } from "./operators/topKWithFractionalIndex.js";
|
|
10
|
+
import { FilterOperator, filter } from "./operators/filter.js";
|
|
22
11
|
import { GroupedTopKWithFractionalIndexOperator, groupedTopKWithFractionalIndex } from "./operators/groupedTopKWithFractionalIndex.js";
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
12
|
+
import { JoinOperator, antiJoin, fullJoin, innerJoin, join, leftJoin, rightJoin } from "./operators/join.js";
|
|
13
|
+
import { MapOperator, map } from "./operators/map.js";
|
|
14
|
+
import { NegateOperator, negate } from "./operators/negate.js";
|
|
15
|
+
import { OutputOperator, output } from "./operators/output.js";
|
|
16
|
+
import { ReduceOperator, reduce } from "./operators/reduce.js";
|
|
17
|
+
import { TapOperator, tap } from "./operators/tap.js";
|
|
18
|
+
import { TopKWithFractionalIndexOperator, topKWithFractionalIndex } from "./operators/topKWithFractionalIndex.js";
|
|
25
19
|
import { filterBy } from "./operators/filterBy.js";
|
|
20
|
+
import { groupBy, groupByOperators } from "./operators/groupBy.js";
|
|
21
|
+
import { groupedOrderByWithFractionalIndex } from "./operators/groupedOrderBy.js";
|
|
22
|
+
import { keyBy, rekey, unkey } from "./operators/keying.js";
|
|
23
|
+
import { orderBy, orderByWithFractionalIndex, orderByWithFractionalIndexBase, orderByWithIndex } from "./operators/orderBy.js";
|
|
24
|
+
import { pipe } from "./operators/pipe.js";
|
|
25
|
+
import { topK, topKWithIndex } from "./operators/topK.js";
|
|
26
26
|
export {
|
|
27
27
|
ConcatOperator,
|
|
28
28
|
ConsolidateOperator,
|
package/dist/esm/indexes.js
CHANGED
|
@@ -12,7 +12,7 @@ class PrefixMap extends Map {
|
|
|
12
12
|
if (isSingleValue(valueMapOrSingleValue)) {
|
|
13
13
|
const [currentValue, currentMultiplicity] = valueMapOrSingleValue;
|
|
14
14
|
const currentPrefix = getPrefix(currentValue);
|
|
15
|
-
if (currentPrefix
|
|
15
|
+
if (!isSamePrefix(currentPrefix, prefix)) {
|
|
16
16
|
throw new Error(`Mismatching prefixes, this should never happen`);
|
|
17
17
|
}
|
|
18
18
|
if (currentValue === value || hash(currentValue) === hash(value)) {
|
|
@@ -266,7 +266,7 @@ class Index {
|
|
|
266
266
|
}
|
|
267
267
|
const newPrefix = getPrefix(newValue);
|
|
268
268
|
const currentPrefix = getPrefix(currentValue);
|
|
269
|
-
if (currentPrefix
|
|
269
|
+
if (isSamePrefix(currentPrefix, newPrefix) && (currentValue === newValue || hash(currentValue) === hash(newValue))) {
|
|
270
270
|
const newMultiplicity = currentMultiplicity + multiplicity;
|
|
271
271
|
if (newMultiplicity === 0) {
|
|
272
272
|
this.#inner.delete(key);
|
|
@@ -282,7 +282,7 @@ class Index {
|
|
|
282
282
|
this.#inner.set(key, valueMap);
|
|
283
283
|
} else {
|
|
284
284
|
const prefixMap = new PrefixMap();
|
|
285
|
-
if (currentPrefix
|
|
285
|
+
if (isSamePrefix(currentPrefix, newPrefix)) {
|
|
286
286
|
const valueMap = new ValueMap();
|
|
287
287
|
valueMap.set(hash(currentValue), currentSingleValue);
|
|
288
288
|
valueMap.set(hash(newValue), [newValue, multiplicity]);
|
|
@@ -338,6 +338,9 @@ class Index {
|
|
|
338
338
|
return new MultiSet(result);
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
|
+
function isSamePrefix(a, b) {
|
|
342
|
+
return a === b || Number.isNaN(a) && Number.isNaN(b);
|
|
343
|
+
}
|
|
341
344
|
function getPrefix(value) {
|
|
342
345
|
if (Array.isArray(value) && (typeof value[0] === `string` || typeof value[0] === `number` || typeof value[0] === `bigint`)) {
|
|
343
346
|
return value[0];
|
package/dist/esm/indexes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexes.js","sources":["../../src/indexes.ts"],"sourcesContent":["/**\n * # Optimized Index Data Structure\n *\n * Multi-level index that adapts storage strategy based on data patterns to minimize memory\n * usage, eliminate wasteful lookups, and avoid hashing whenever possible.\n *\n * ## Storage Strategy\n *\n * **Single value**: `IndexMap['key'] → [value, multiplicity]` (no hashing needed)\n *\n * **Multiple unprefixed values**: Direct ValueMap (avoids NO_PREFIX lookup)\n * ```\n * IndexMap['key'] → ValueMap { hash(value1) → [value1, mult1], ... }\n * ```\n *\n * **Values with prefixes**: PrefixMap uses prefix keys directly (no hashing)\n * ```\n * IndexMap['key'] → PrefixMap { 'prefix1' → [value1, mult1], NO_PREFIX → ValueMap{...} }\n * ```\n *\n * **Multiple values per prefix**: ValueMap within PrefixMap (hash only suffixes)\n * ```\n * PrefixMap['prefix'] → ValueMap { hash(suffix1) → [full_value1, mult1], ... }\n * ```\n *\n * ## Dynamic Evolution\n *\n * Structure automatically evolves as data is added:\n * - Single → ValueMap (when both values unprefixed)\n * - Single → PrefixMap (when at least one prefixed)\n * - ValueMap → PrefixMap (adding prefixed value to unprefixed)\n *\n * Prefixes extracted from array values: `['prefix', 'suffix']` → prefix='prefix'\n */\n\nimport { MultiSet } from './multiset.js'\nimport { hash } from './hashing/index.js'\nimport type { Hash } from './hashing/index.js'\n\n// We use a symbol to represent the absence of a prefix, unprefixed values a stored\n// against this key.\nconst NO_PREFIX = Symbol(`NO_PREFIX`)\ntype NO_PREFIX = typeof NO_PREFIX\n\n// A single value is a tuple of the value and the multiplicity.\ntype SingleValue<TValue> = [TValue, number]\n\n// Base map type for the index. Stores single values, prefix maps, or value maps against a key.\ntype IndexMap<TKey, TValue, TPrefix> = Map<\n TKey,\n SingleValue<TValue> | PrefixMap<TValue, TPrefix> | ValueMap<TValue>\n>\n\n// Second level map type for the index, stores single values or value maps against a prefix.\nclass PrefixMap<TValue, TPrefix> extends Map<\n TPrefix | NO_PREFIX,\n SingleValue<TValue> | ValueMap<TValue>\n> {\n /**\n * Add a value to the PrefixMap. Returns true if the map becomes empty after the operation.\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const prefix = getPrefix<TValue, TPrefix>(value)\n const valueMapOrSingleValue = this.get(prefix)\n\n if (isSingleValue(valueMapOrSingleValue)) {\n const [currentValue, currentMultiplicity] = valueMapOrSingleValue\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n if (currentPrefix !== prefix) {\n throw new Error(`Mismatching prefixes, this should never happen`)\n }\n\n if (currentValue === value || hash(currentValue) === hash(value)) {\n // Same value, update multiplicity\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(prefix)\n } else {\n this.set(prefix, [value, newMultiplicity])\n }\n } else {\n // Different suffixes, need to create ValueMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), valueMapOrSingleValue)\n valueMap.set(hash(value), [value, multiplicity])\n this.set(prefix, valueMap)\n }\n } else if (valueMapOrSingleValue === undefined) {\n // No existing value for this prefix\n this.set(prefix, [value, multiplicity])\n } else {\n // Existing ValueMap\n const isEmpty = valueMapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.delete(prefix)\n }\n }\n\n return this.size === 0\n }\n}\n\n// Third level map type for the index, stores single values or value maps against a hash.\nclass ValueMap<TValue> extends Map<Hash, [TValue, number]> {\n /**\n * Add a value to the ValueMap. Returns true if the map becomes empty after the operation.\n * @param value - The full value to store\n * @param multiplicity - The multiplicity to add\n * @param hashKey - Optional hash key to use instead of hashing the full value (used when in PrefixMap context)\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const key = hash(value)\n const currentValue = this.get(key)\n\n if (currentValue) {\n const [, currentMultiplicity] = currentValue\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(key)\n } else {\n this.set(key, [value, newMultiplicity])\n }\n } else {\n this.set(key, [value, multiplicity])\n }\n\n return this.size === 0\n }\n}\n\n/**\n * A map from a difference collection trace's keys -> (value, multiplicities) that changed.\n * Used in operations like join and reduce where the operation needs to\n * exploit the key-value structure of the data to run efficiently.\n */\nexport class Index<TKey, TValue, TPrefix = any> {\n /*\n * This index maintains a nested map of keys -> (value, multiplicities), where:\n * - initially the values are stored against the key as a single value tuple\n * - when a key gets additional values, the values are stored against the key in a\n * prefix map\n * - the prefix is extract where possible from values that are structured as\n * [rowPrimaryKey, rowValue], as they are in the Tanstack DB query pipeline.\n * - only when there are multiple values for a given prefix do we fall back to a\n * hash to identify identical values, storing them in a third level value map.\n */\n #inner: IndexMap<TKey, TValue, TPrefix>\n #consolidatedMultiplicity: Map<TKey, number> = new Map() // sum of multiplicities per key\n\n constructor() {\n this.#inner = new Map()\n }\n\n /**\n * Create an Index from multiple MultiSet messages.\n * @param messages - Array of MultiSet messages to build the index from.\n * @returns A new Index containing all the data from the messages.\n */\n static fromMultiSets<K, V>(messages: Array<MultiSet<[K, V]>>): Index<K, V> {\n const index = new Index<K, V>()\n\n for (const message of messages) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n index.addValue(key, [value, multiplicity])\n }\n }\n\n return index\n }\n\n /**\n * This method returns a string representation of the index.\n * @param indent - Whether to indent the string representation.\n * @returns A string representation of the index.\n */\n toString(indent = false): string {\n return `Index(${JSON.stringify(\n [...this.entries()],\n undefined,\n indent ? 2 : undefined,\n )})`\n }\n\n /**\n * The size of the index.\n */\n get size(): number {\n return this.#inner.size\n }\n\n /**\n * This method checks if the index has a given key.\n * @param key - The key to check.\n * @returns True if the index has the key, false otherwise.\n */\n has(key: TKey): boolean {\n return this.#inner.has(key)\n }\n\n /**\n * Check if a key has presence (non-zero consolidated multiplicity).\n * @param key - The key to check.\n * @returns True if the key has non-zero consolidated multiplicity, false otherwise.\n */\n hasPresence(key: TKey): boolean {\n return (this.#consolidatedMultiplicity.get(key) || 0) !== 0\n }\n\n /**\n * Get the consolidated multiplicity (sum of multiplicities) for a key.\n * @param key - The key to get the consolidated multiplicity for.\n * @returns The consolidated multiplicity for the key.\n */\n getConsolidatedMultiplicity(key: TKey): number {\n return this.#consolidatedMultiplicity.get(key) || 0\n }\n\n /**\n * Get all keys that have presence (non-zero consolidated multiplicity).\n * @returns An iterator of keys with non-zero consolidated multiplicity.\n */\n getPresenceKeys(): Iterable<TKey> {\n return this.#consolidatedMultiplicity.keys()\n }\n\n /**\n * This method returns all values for a given key.\n * @param key - The key to get the values for.\n * @returns An array of value tuples [value, multiplicity].\n */\n get(key: TKey): Array<[TValue, number]> {\n return [...this.getIterator(key)]\n }\n\n /**\n * This method returns an iterator over all values for a given key.\n * @param key - The key to get the values for.\n * @returns An iterator of value tuples [value, multiplicity].\n */\n *getIterator(key: TKey): Iterable<[TValue, number]> {\n const mapOrSingleValue = this.#inner.get(key)\n if (isSingleValue(mapOrSingleValue)) {\n yield mapOrSingleValue\n } else if (mapOrSingleValue === undefined) {\n return\n } else if (mapOrSingleValue instanceof ValueMap) {\n // Direct ValueMap - all values have NO_PREFIX\n for (const valueTuple of mapOrSingleValue.values()) {\n yield valueTuple\n }\n } else {\n // PrefixMap - iterate through all prefixes\n for (const singleValueOrValueMap of mapOrSingleValue.values()) {\n if (isSingleValue(singleValueOrValueMap)) {\n yield singleValueOrValueMap\n } else {\n for (const valueTuple of singleValueOrValueMap.values()) {\n yield valueTuple\n }\n }\n }\n }\n }\n\n /**\n * This returns an iterator that iterates over all key-value pairs.\n * @returns An iterable of all key-value pairs (and their multiplicities) in the index.\n */\n *entries(): Iterable<[TKey, [TValue, number]]> {\n for (const key of this.#inner.keys()) {\n for (const valueTuple of this.getIterator(key)) {\n yield [key, valueTuple]\n }\n }\n }\n\n /**\n * This method only iterates over the keys and not over the values.\n * Hence, it is more efficient than the `#entries` method.\n * It returns an iterator that you can use if you need to iterate over the values for a given key.\n * @returns An iterator of all *keys* in the index and their corresponding value iterator.\n */\n *entriesIterators(): Iterable<[TKey, Iterable<[TValue, number]>]> {\n for (const key of this.#inner.keys()) {\n yield [key, this.getIterator(key)]\n }\n }\n\n /**\n * This method adds a value to the index.\n * @param key - The key to add the value to.\n * @param valueTuple - The value tuple [value, multiplicity] to add to the index.\n */\n addValue(key: TKey, valueTuple: SingleValue<TValue>) {\n const [value, multiplicity] = valueTuple\n // If the multiplicity is 0, do nothing\n if (multiplicity === 0) return\n\n // Update consolidated multiplicity tracking\n const newConsolidatedMultiplicity =\n (this.#consolidatedMultiplicity.get(key) || 0) + multiplicity\n if (newConsolidatedMultiplicity === 0) {\n this.#consolidatedMultiplicity.delete(key)\n } else {\n this.#consolidatedMultiplicity.set(key, newConsolidatedMultiplicity)\n }\n\n const mapOrSingleValue = this.#inner.get(key)\n\n if (mapOrSingleValue === undefined) {\n // First value for this key\n this.#inner.set(key, valueTuple)\n return\n }\n\n if (isSingleValue(mapOrSingleValue)) {\n // Handle transition from single value to map\n this.#handleSingleValueTransition(\n key,\n mapOrSingleValue,\n value,\n multiplicity,\n )\n return\n }\n\n if (mapOrSingleValue instanceof ValueMap) {\n // Handle existing ValueMap\n const prefix = getPrefix<TValue, TPrefix>(value)\n if (prefix !== NO_PREFIX) {\n // Convert ValueMap to PrefixMap since we have a prefixed value\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n prefixMap.set(NO_PREFIX, mapOrSingleValue)\n prefixMap.set(prefix, valueTuple)\n this.#inner.set(key, prefixMap)\n } else {\n // Add to existing ValueMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n } else {\n // Handle existing PrefixMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n }\n\n /**\n * Handle the transition from a single value to either a ValueMap or PrefixMap\n */\n #handleSingleValueTransition(\n key: TKey,\n currentSingleValue: SingleValue<TValue>,\n newValue: TValue,\n multiplicity: number,\n ) {\n const [currentValue, currentMultiplicity] = currentSingleValue\n\n // Check for exact same value (reference equality)\n if (currentValue === newValue) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Get prefixes for both values\n const newPrefix = getPrefix<TValue, TPrefix>(newValue)\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n // Check if they're the same value by prefix/suffix comparison\n if (\n currentPrefix === newPrefix &&\n (currentValue === newValue || hash(currentValue) === hash(newValue))\n ) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Different values - choose appropriate map type\n if (currentPrefix === NO_PREFIX && newPrefix === NO_PREFIX) {\n // Both have NO_PREFIX, use ValueMap directly\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n this.#inner.set(key, valueMap)\n } else {\n // At least one has a prefix, use PrefixMap\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n\n if (currentPrefix === newPrefix) {\n // Same prefix, different suffixes - need ValueMap within PrefixMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n prefixMap.set(currentPrefix, valueMap)\n } else {\n // Different prefixes - store as separate single values\n prefixMap.set(currentPrefix, currentSingleValue)\n prefixMap.set(newPrefix, [newValue, multiplicity])\n }\n\n this.#inner.set(key, prefixMap)\n }\n }\n\n /**\n * This method appends another index to the current index.\n * @param other - The index to append to the current index.\n */\n append(other: Index<TKey, TValue>): void {\n for (const [key, value] of other.entries()) {\n this.addValue(key, value)\n }\n }\n\n /**\n * This method joins two indexes.\n * @param other - The index to join with the current index.\n * @returns A multiset of the joined values.\n */\n join<TValue2>(\n other: Index<TKey, TValue2>,\n ): MultiSet<[TKey, [TValue, TValue2]]> {\n const result: Array<[[TKey, [TValue, TValue2]], number]> = []\n // We want to iterate over the smaller of the two indexes to reduce the\n // number of operations we need to do.\n if (this.size <= other.size) {\n for (const [key, valueIt] of this.entriesIterators()) {\n if (!other.has(key)) continue\n const otherValues = other.get(key)\n for (const [val1, mul1] of valueIt) {\n for (const [val2, mul2] of otherValues) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n } else {\n for (const [key, otherValueIt] of other.entriesIterators()) {\n if (!this.has(key)) continue\n const values = this.get(key)\n for (const [val2, mul2] of otherValueIt) {\n for (const [val1, mul1] of values) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n }\n\n return new MultiSet(result)\n }\n}\n\n/**\n * This function extracts the prefix from a value.\n * @param value - The value to extract the prefix from.\n * @returns The prefix and the suffix.\n */\nfunction getPrefix<TValue, TPrefix>(value: TValue): TPrefix | NO_PREFIX {\n // If the value is an array and the first element is a string or number, then the\n // first element is the prefix. This is used to distinguish between values without\n // the need for hashing unless there are multiple values for the same prefix.\n if (\n Array.isArray(value) &&\n (typeof value[0] === `string` ||\n typeof value[0] === `number` ||\n typeof value[0] === `bigint`)\n ) {\n return value[0] as TPrefix\n }\n return NO_PREFIX\n}\n\n/**\n * This function checks if a value is a single value.\n * @param value - The value to check.\n * @returns True if the value is a single value, false otherwise.\n */\nfunction isSingleValue<TValue>(\n value: SingleValue<TValue> | unknown,\n): value is SingleValue<TValue> {\n return Array.isArray(value)\n}\n"],"names":[],"mappings":";;AAyCA,MAAM,mCAAmB,WAAW;AAapC,MAAM,kBAAmC,IAGvC;AAAA;AAAA;AAAA;AAAA,EAIA,SAAS,OAAe,cAA+B;AACrD,QAAI,iBAAiB,EAAG,QAAO,KAAK,SAAS;AAE7C,UAAM,SAAS,UAA2B,KAAK;AAC/C,UAAM,wBAAwB,KAAK,IAAI,MAAM;AAE7C,QAAI,cAAc,qBAAqB,GAAG;AACxC,YAAM,CAAC,cAAc,mBAAmB,IAAI;AAC5C,YAAM,gBAAgB,UAA2B,YAAY;AAE7D,UAAI,kBAAkB,QAAQ;AAC5B,cAAM,IAAI,MAAM,gDAAgD;AAAA,MAClE;AAEA,UAAI,iBAAiB,SAAS,KAAK,YAAY,MAAM,KAAK,KAAK,GAAG;AAEhE,cAAM,kBAAkB,sBAAsB;AAC9C,YAAI,oBAAoB,GAAG;AACzB,eAAK,OAAO,MAAM;AAAA,QACpB,OAAO;AACL,eAAK,IAAI,QAAQ,CAAC,OAAO,eAAe,CAAC;AAAA,QAC3C;AAAA,MACF,OAAO;AAEL,cAAM,WAAW,IAAI,SAAA;AACrB,iBAAS,IAAI,KAAK,YAAY,GAAG,qBAAqB;AACtD,iBAAS,IAAI,KAAK,KAAK,GAAG,CAAC,OAAO,YAAY,CAAC;AAC/C,aAAK,IAAI,QAAQ,QAAQ;AAAA,MAC3B;AAAA,IACF,WAAW,0BAA0B,QAAW;AAE9C,WAAK,IAAI,QAAQ,CAAC,OAAO,YAAY,CAAC;AAAA,IACxC,OAAO;AAEL,YAAM,UAAU,sBAAsB,SAAS,OAAO,YAAY;AAClE,UAAI,SAAS;AAC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AA,MACF;AAAA,IACF;AAEA,WAAO,IAAI,SAAS,MAAM;AAAA,EAC5B;AACF;AAOA,SAAS,UAA2B,OAAoC;AAItE,MACE,MAAM,QAAQ,KAAK,MAClB,OAAO,MAAM,CAAC,MAAM,YACnB,OAAO,MAAM,CAAC,MAAM,YACpB,OAAO,MAAM,CAAC,MAAM,WACtB;AACA,WAAO,MAAM,CAAC;AAAA,EAChB;AACA,SAAO;AACT;AAOA,SAAS,cACP,OAC8B;AAC9B,SAAO,MAAM,QAAQ,KAAK;AAC5B;"}
|
|
1
|
+
{"version":3,"file":"indexes.js","sources":["../../src/indexes.ts"],"sourcesContent":["/**\n * # Optimized Index Data Structure\n *\n * Multi-level index that adapts storage strategy based on data patterns to minimize memory\n * usage, eliminate wasteful lookups, and avoid hashing whenever possible.\n *\n * ## Storage Strategy\n *\n * **Single value**: `IndexMap['key'] → [value, multiplicity]` (no hashing needed)\n *\n * **Multiple unprefixed values**: Direct ValueMap (avoids NO_PREFIX lookup)\n * ```\n * IndexMap['key'] → ValueMap { hash(value1) → [value1, mult1], ... }\n * ```\n *\n * **Values with prefixes**: PrefixMap uses prefix keys directly (no hashing)\n * ```\n * IndexMap['key'] → PrefixMap { 'prefix1' → [value1, mult1], NO_PREFIX → ValueMap{...} }\n * ```\n *\n * **Multiple values per prefix**: ValueMap within PrefixMap (hash only suffixes)\n * ```\n * PrefixMap['prefix'] → ValueMap { hash(suffix1) → [full_value1, mult1], ... }\n * ```\n *\n * ## Dynamic Evolution\n *\n * Structure automatically evolves as data is added:\n * - Single → ValueMap (when both values unprefixed)\n * - Single → PrefixMap (when at least one prefixed)\n * - ValueMap → PrefixMap (adding prefixed value to unprefixed)\n *\n * Prefixes extracted from array values: `['prefix', 'suffix']` → prefix='prefix'\n */\n\nimport { MultiSet } from './multiset.js'\nimport { hash } from './hashing/index.js'\nimport type { Hash } from './hashing/index.js'\n\n// We use a symbol to represent the absence of a prefix, unprefixed values a stored\n// against this key.\nconst NO_PREFIX = Symbol(`NO_PREFIX`)\ntype NO_PREFIX = typeof NO_PREFIX\n\n// A single value is a tuple of the value and the multiplicity.\ntype SingleValue<TValue> = [TValue, number]\n\n// Base map type for the index. Stores single values, prefix maps, or value maps against a key.\ntype IndexMap<TKey, TValue, TPrefix> = Map<\n TKey,\n SingleValue<TValue> | PrefixMap<TValue, TPrefix> | ValueMap<TValue>\n>\n\n// Second level map type for the index, stores single values or value maps against a prefix.\nclass PrefixMap<TValue, TPrefix> extends Map<\n TPrefix | NO_PREFIX,\n SingleValue<TValue> | ValueMap<TValue>\n> {\n /**\n * Add a value to the PrefixMap. Returns true if the map becomes empty after the operation.\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const prefix = getPrefix<TValue, TPrefix>(value)\n const valueMapOrSingleValue = this.get(prefix)\n\n if (isSingleValue(valueMapOrSingleValue)) {\n const [currentValue, currentMultiplicity] = valueMapOrSingleValue\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n if (!isSamePrefix(currentPrefix, prefix)) {\n throw new Error(`Mismatching prefixes, this should never happen`)\n }\n\n if (currentValue === value || hash(currentValue) === hash(value)) {\n // Same value, update multiplicity\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(prefix)\n } else {\n this.set(prefix, [value, newMultiplicity])\n }\n } else {\n // Different suffixes, need to create ValueMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), valueMapOrSingleValue)\n valueMap.set(hash(value), [value, multiplicity])\n this.set(prefix, valueMap)\n }\n } else if (valueMapOrSingleValue === undefined) {\n // No existing value for this prefix\n this.set(prefix, [value, multiplicity])\n } else {\n // Existing ValueMap\n const isEmpty = valueMapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.delete(prefix)\n }\n }\n\n return this.size === 0\n }\n}\n\n// Third level map type for the index, stores single values or value maps against a hash.\nclass ValueMap<TValue> extends Map<Hash, [TValue, number]> {\n /**\n * Add a value to the ValueMap. Returns true if the map becomes empty after the operation.\n * @param value - The full value to store\n * @param multiplicity - The multiplicity to add\n * @param hashKey - Optional hash key to use instead of hashing the full value (used when in PrefixMap context)\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const key = hash(value)\n const currentValue = this.get(key)\n\n if (currentValue) {\n const [, currentMultiplicity] = currentValue\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(key)\n } else {\n this.set(key, [value, newMultiplicity])\n }\n } else {\n this.set(key, [value, multiplicity])\n }\n\n return this.size === 0\n }\n}\n\n/**\n * A map from a difference collection trace's keys -> (value, multiplicities) that changed.\n * Used in operations like join and reduce where the operation needs to\n * exploit the key-value structure of the data to run efficiently.\n */\nexport class Index<TKey, TValue, TPrefix = any> {\n /*\n * This index maintains a nested map of keys -> (value, multiplicities), where:\n * - initially the values are stored against the key as a single value tuple\n * - when a key gets additional values, the values are stored against the key in a\n * prefix map\n * - the prefix is extract where possible from values that are structured as\n * [rowPrimaryKey, rowValue], as they are in the Tanstack DB query pipeline.\n * - only when there are multiple values for a given prefix do we fall back to a\n * hash to identify identical values, storing them in a third level value map.\n */\n #inner: IndexMap<TKey, TValue, TPrefix>\n #consolidatedMultiplicity: Map<TKey, number> = new Map() // sum of multiplicities per key\n\n constructor() {\n this.#inner = new Map()\n }\n\n /**\n * Create an Index from multiple MultiSet messages.\n * @param messages - Array of MultiSet messages to build the index from.\n * @returns A new Index containing all the data from the messages.\n */\n static fromMultiSets<K, V>(messages: Array<MultiSet<[K, V]>>): Index<K, V> {\n const index = new Index<K, V>()\n\n for (const message of messages) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n index.addValue(key, [value, multiplicity])\n }\n }\n\n return index\n }\n\n /**\n * This method returns a string representation of the index.\n * @param indent - Whether to indent the string representation.\n * @returns A string representation of the index.\n */\n toString(indent = false): string {\n return `Index(${JSON.stringify(\n [...this.entries()],\n undefined,\n indent ? 2 : undefined,\n )})`\n }\n\n /**\n * The size of the index.\n */\n get size(): number {\n return this.#inner.size\n }\n\n /**\n * This method checks if the index has a given key.\n * @param key - The key to check.\n * @returns True if the index has the key, false otherwise.\n */\n has(key: TKey): boolean {\n return this.#inner.has(key)\n }\n\n /**\n * Check if a key has presence (non-zero consolidated multiplicity).\n * @param key - The key to check.\n * @returns True if the key has non-zero consolidated multiplicity, false otherwise.\n */\n hasPresence(key: TKey): boolean {\n return (this.#consolidatedMultiplicity.get(key) || 0) !== 0\n }\n\n /**\n * Get the consolidated multiplicity (sum of multiplicities) for a key.\n * @param key - The key to get the consolidated multiplicity for.\n * @returns The consolidated multiplicity for the key.\n */\n getConsolidatedMultiplicity(key: TKey): number {\n return this.#consolidatedMultiplicity.get(key) || 0\n }\n\n /**\n * Get all keys that have presence (non-zero consolidated multiplicity).\n * @returns An iterator of keys with non-zero consolidated multiplicity.\n */\n getPresenceKeys(): Iterable<TKey> {\n return this.#consolidatedMultiplicity.keys()\n }\n\n /**\n * This method returns all values for a given key.\n * @param key - The key to get the values for.\n * @returns An array of value tuples [value, multiplicity].\n */\n get(key: TKey): Array<[TValue, number]> {\n return [...this.getIterator(key)]\n }\n\n /**\n * This method returns an iterator over all values for a given key.\n * @param key - The key to get the values for.\n * @returns An iterator of value tuples [value, multiplicity].\n */\n *getIterator(key: TKey): Iterable<[TValue, number]> {\n const mapOrSingleValue = this.#inner.get(key)\n if (isSingleValue(mapOrSingleValue)) {\n yield mapOrSingleValue\n } else if (mapOrSingleValue === undefined) {\n return\n } else if (mapOrSingleValue instanceof ValueMap) {\n // Direct ValueMap - all values have NO_PREFIX\n for (const valueTuple of mapOrSingleValue.values()) {\n yield valueTuple\n }\n } else {\n // PrefixMap - iterate through all prefixes\n for (const singleValueOrValueMap of mapOrSingleValue.values()) {\n if (isSingleValue(singleValueOrValueMap)) {\n yield singleValueOrValueMap\n } else {\n for (const valueTuple of singleValueOrValueMap.values()) {\n yield valueTuple\n }\n }\n }\n }\n }\n\n /**\n * This returns an iterator that iterates over all key-value pairs.\n * @returns An iterable of all key-value pairs (and their multiplicities) in the index.\n */\n *entries(): Iterable<[TKey, [TValue, number]]> {\n for (const key of this.#inner.keys()) {\n for (const valueTuple of this.getIterator(key)) {\n yield [key, valueTuple]\n }\n }\n }\n\n /**\n * This method only iterates over the keys and not over the values.\n * Hence, it is more efficient than the `#entries` method.\n * It returns an iterator that you can use if you need to iterate over the values for a given key.\n * @returns An iterator of all *keys* in the index and their corresponding value iterator.\n */\n *entriesIterators(): Iterable<[TKey, Iterable<[TValue, number]>]> {\n for (const key of this.#inner.keys()) {\n yield [key, this.getIterator(key)]\n }\n }\n\n /**\n * This method adds a value to the index.\n * @param key - The key to add the value to.\n * @param valueTuple - The value tuple [value, multiplicity] to add to the index.\n */\n addValue(key: TKey, valueTuple: SingleValue<TValue>) {\n const [value, multiplicity] = valueTuple\n // If the multiplicity is 0, do nothing\n if (multiplicity === 0) return\n\n // Update consolidated multiplicity tracking\n const newConsolidatedMultiplicity =\n (this.#consolidatedMultiplicity.get(key) || 0) + multiplicity\n if (newConsolidatedMultiplicity === 0) {\n this.#consolidatedMultiplicity.delete(key)\n } else {\n this.#consolidatedMultiplicity.set(key, newConsolidatedMultiplicity)\n }\n\n const mapOrSingleValue = this.#inner.get(key)\n\n if (mapOrSingleValue === undefined) {\n // First value for this key\n this.#inner.set(key, valueTuple)\n return\n }\n\n if (isSingleValue(mapOrSingleValue)) {\n // Handle transition from single value to map\n this.#handleSingleValueTransition(\n key,\n mapOrSingleValue,\n value,\n multiplicity,\n )\n return\n }\n\n if (mapOrSingleValue instanceof ValueMap) {\n // Handle existing ValueMap\n const prefix = getPrefix<TValue, TPrefix>(value)\n if (prefix !== NO_PREFIX) {\n // Convert ValueMap to PrefixMap since we have a prefixed value\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n prefixMap.set(NO_PREFIX, mapOrSingleValue)\n prefixMap.set(prefix, valueTuple)\n this.#inner.set(key, prefixMap)\n } else {\n // Add to existing ValueMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n } else {\n // Handle existing PrefixMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n }\n\n /**\n * Handle the transition from a single value to either a ValueMap or PrefixMap\n */\n #handleSingleValueTransition(\n key: TKey,\n currentSingleValue: SingleValue<TValue>,\n newValue: TValue,\n multiplicity: number,\n ) {\n const [currentValue, currentMultiplicity] = currentSingleValue\n\n // Check for exact same value (reference equality)\n if (currentValue === newValue) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Get prefixes for both values\n const newPrefix = getPrefix<TValue, TPrefix>(newValue)\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n // Check if they're the same value by prefix/suffix comparison\n if (\n isSamePrefix(currentPrefix, newPrefix) &&\n (currentValue === newValue || hash(currentValue) === hash(newValue))\n ) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Different values - choose appropriate map type\n if (currentPrefix === NO_PREFIX && newPrefix === NO_PREFIX) {\n // Both have NO_PREFIX, use ValueMap directly\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n this.#inner.set(key, valueMap)\n } else {\n // At least one has a prefix, use PrefixMap\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n\n if (isSamePrefix(currentPrefix, newPrefix)) {\n // Same prefix, different suffixes - need ValueMap within PrefixMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n prefixMap.set(currentPrefix, valueMap)\n } else {\n // Different prefixes - store as separate single values\n prefixMap.set(currentPrefix, currentSingleValue)\n prefixMap.set(newPrefix, [newValue, multiplicity])\n }\n\n this.#inner.set(key, prefixMap)\n }\n }\n\n /**\n * This method appends another index to the current index.\n * @param other - The index to append to the current index.\n */\n append(other: Index<TKey, TValue>): void {\n for (const [key, value] of other.entries()) {\n this.addValue(key, value)\n }\n }\n\n /**\n * This method joins two indexes.\n * @param other - The index to join with the current index.\n * @returns A multiset of the joined values.\n */\n join<TValue2>(\n other: Index<TKey, TValue2>,\n ): MultiSet<[TKey, [TValue, TValue2]]> {\n const result: Array<[[TKey, [TValue, TValue2]], number]> = []\n // We want to iterate over the smaller of the two indexes to reduce the\n // number of operations we need to do.\n if (this.size <= other.size) {\n for (const [key, valueIt] of this.entriesIterators()) {\n if (!other.has(key)) continue\n const otherValues = other.get(key)\n for (const [val1, mul1] of valueIt) {\n for (const [val2, mul2] of otherValues) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n } else {\n for (const [key, otherValueIt] of other.entriesIterators()) {\n if (!this.has(key)) continue\n const values = this.get(key)\n for (const [val2, mul2] of otherValueIt) {\n for (const [val1, mul1] of values) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n }\n\n return new MultiSet(result)\n }\n}\n\n/**\n * This function extracts the prefix from a value.\n * @param value - The value to extract the prefix from.\n * @returns The prefix and the suffix.\n */\n// Prefixes are Map keys, so they compare as a Map does: NaN equals NaN.\nfunction isSamePrefix(a: unknown, b: unknown): boolean {\n return a === b || (Number.isNaN(a) && Number.isNaN(b))\n}\n\nfunction getPrefix<TValue, TPrefix>(value: TValue): TPrefix | NO_PREFIX {\n // If the value is an array and the first element is a string or number, then the\n // first element is the prefix. This is used to distinguish between values without\n // the need for hashing unless there are multiple values for the same prefix.\n if (\n Array.isArray(value) &&\n (typeof value[0] === `string` ||\n typeof value[0] === `number` ||\n typeof value[0] === `bigint`)\n ) {\n return value[0] as TPrefix\n }\n return NO_PREFIX\n}\n\n/**\n * This function checks if a value is a single value.\n * @param value - The value to check.\n * @returns True if the value is a single value, false otherwise.\n */\nfunction isSingleValue<TValue>(\n value: SingleValue<TValue> | unknown,\n): value is SingleValue<TValue> {\n return Array.isArray(value)\n}\n"],"names":[],"mappings":";;AAyCA,MAAM,mCAAmB,WAAW;AAapC,MAAM,kBAAmC,IAGvC;AAAA;AAAA;AAAA;AAAA,EAIA,SAAS,OAAe,cAA+B;AACrD,QAAI,iBAAiB,EAAG,QAAO,KAAK,SAAS;AAE7C,UAAM,SAAS,UAA2B,KAAK;AAC/C,UAAM,wBAAwB,KAAK,IAAI,MAAM;AAE7C,QAAI,cAAc,qBAAqB,GAAG;AACxC,YAAM,CAAC,cAAc,mBAAmB,IAAI;AAC5C,YAAM,gBAAgB,UAA2B,YAAY;AAE7D,UAAI,CAAC,aAAa,eAAe,MAAM,GAAG;AACxC,cAAM,IAAI,MAAM,gDAAgD;AAAA,MAClE;AAEA,UAAI,iBAAiB,SAAS,KAAK,YAAY,MAAM,KAAK,KAAK,GAAG;AAEhE,cAAM,kBAAkB,sBAAsB;AAC9C,YAAI,oBAAoB,GAAG;AACzB,eAAK,OAAO,MAAM;AAAA,QACpB,OAAO;AACL,eAAK,IAAI,QAAQ,CAAC,OAAO,eAAe,CAAC;AAAA,QAC3C;AAAA,MACF,OAAO;AAEL,cAAM,WAAW,IAAI,SAAA;AACrB,iBAAS,IAAI,KAAK,YAAY,GAAG,qBAAqB;AACtD,iBAAS,IAAI,KAAK,KAAK,GAAG,CAAC,OAAO,YAAY,CAAC;AAC/C,aAAK,IAAI,QAAQ,QAAQ;AAAA,MAC3B;AAAA,IACF,WAAW,0BAA0B,QAAW;AAE9C,WAAK,IAAI,QAAQ,CAAC,OAAO,YAAY,CAAC;AAAA,IACxC,OAAO;AAEL,YAAM,UAAU,sBAAsB,SAAS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AAO,IAAI,CAAC;AAAA,YAChD;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO,IAAI,SAAS,MAAM;AAAA,EAC5B;AACF;AAQA,SAAS,aAAa,GAAY,GAAqB;AACrD,SAAO,MAAM,KAAM,OAAO,MAAM,CAAC,KAAK,OAAO,MAAM,CAAC;AACtD;AAEA,SAAS,UAA2B,OAAoC;AAItE,MACE,MAAM,QAAQ,KAAK,MAClB,OAAO,MAAM,CAAC,MAAM,YACnB,OAAO,MAAM,CAAC,MAAM,YACpB,OAAO,MAAM,CAAC,MAAM,WACtB;AACA,WAAO,MAAM,CAAC;AAAA,EAChB;AACA,SAAO;AACT;AAOA,SAAS,cACP,OAC8B;AAC9B,SAAO,MAAM,QAAQ,KAAK;AAC5B;"}
|
package/dist/esm/multiset.d.ts
CHANGED
|
@@ -35,8 +35,7 @@ export declare class MultiSet<T> {
|
|
|
35
35
|
* - Keyed: every record is a `[key, value]` pair with a string or number
|
|
36
36
|
* key. Keys and primitive values compare by value, and object values by
|
|
37
37
|
* reference. A value that is an array of length 2 is a join tuple, and
|
|
38
|
-
* its two elements compare by the same rule.
|
|
39
|
-
* has known collisions: https://github.com/TanStack/db/issues/1948
|
|
38
|
+
* its two elements compare by the same rule.
|
|
40
39
|
* - Unkeyed, one primitive type: every record is a string, or every record
|
|
41
40
|
* is a number. Records compare as `Map` keys do, and a `-0` record is
|
|
42
41
|
* returned as `0`.
|
package/dist/esm/multiset.js
CHANGED
|
@@ -55,8 +55,7 @@ class MultiSet {
|
|
|
55
55
|
* - Keyed: every record is a `[key, value]` pair with a string or number
|
|
56
56
|
* key. Keys and primitive values compare by value, and object values by
|
|
57
57
|
* reference. A value that is an array of length 2 is a join tuple, and
|
|
58
|
-
* its two elements compare by the same rule.
|
|
59
|
-
* has known collisions: https://github.com/TanStack/db/issues/1948
|
|
58
|
+
* its two elements compare by the same rule.
|
|
60
59
|
* - Unkeyed, one primitive type: every record is a string, or every record
|
|
61
60
|
* is a number. Records compare as `Map` keys do, and a `-0` record is
|
|
62
61
|
* returned as `0`.
|
|
@@ -71,10 +70,19 @@ class MultiSet {
|
|
|
71
70
|
if (inner.every(
|
|
72
71
|
([data]) => Array.isArray(data) && data.length === 2 && (typeof data[0] === `string` || typeof data[0] === `number`)
|
|
73
72
|
)) {
|
|
73
|
+
const referenceIds = /* @__PURE__ */ new Map();
|
|
74
74
|
return consolidateBy(inner, (data) => {
|
|
75
75
|
const [key, value] = data;
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
let valueId;
|
|
77
|
+
if (Array.isArray(value) && value.length === 2) {
|
|
78
|
+
const firstId = getStringId(value[0], referenceIds);
|
|
79
|
+
const secondId = getStringId(value[1], referenceIds);
|
|
80
|
+
valueId = `tuple_${firstId.length}:${firstId}${secondId}`;
|
|
81
|
+
} else {
|
|
82
|
+
valueId = `leaf_${getStringId(value, referenceIds)}`;
|
|
83
|
+
}
|
|
84
|
+
const keyId = getStringId(key, referenceIds);
|
|
85
|
+
return `${keyId.length}:${keyId}${valueId}`;
|
|
78
86
|
});
|
|
79
87
|
}
|
|
80
88
|
let primitiveType;
|