@tanstack/db-ivm 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hashIndex.cjs +83 -0
- package/dist/cjs/hashIndex.cjs.map +1 -0
- package/dist/cjs/hashIndex.d.cts +25 -0
- package/dist/cjs/hashing/hash.cjs +122 -0
- package/dist/cjs/hashing/hash.cjs.map +1 -0
- package/dist/cjs/hashing/hash.d.cts +1 -0
- package/dist/cjs/hashing/index.d.cts +2 -0
- package/dist/cjs/hashing/murmur.cjs +103 -0
- package/dist/cjs/hashing/murmur.cjs.map +1 -0
- package/dist/cjs/hashing/murmur.d.cts +26 -0
- package/dist/cjs/indexes.cjs +97 -45
- package/dist/cjs/indexes.cjs.map +1 -1
- package/dist/cjs/indexes.d.cts +0 -2
- package/dist/cjs/multiset.cjs +2 -1
- package/dist/cjs/multiset.cjs.map +1 -1
- package/dist/cjs/operators/distinct.cjs +2 -2
- package/dist/cjs/operators/distinct.cjs.map +1 -1
- package/dist/cjs/operators/groupBy.cjs +5 -4
- package/dist/cjs/operators/groupBy.cjs.map +1 -1
- package/dist/cjs/operators/groupBy.d.cts +1 -1
- package/dist/cjs/utils.cjs +13 -33
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +7 -4
- package/dist/cjs/valueIndex.cjs +74 -0
- package/dist/cjs/valueIndex.cjs.map +1 -0
- package/dist/cjs/valueIndex.d.cts +17 -0
- package/dist/esm/hashIndex.d.ts +25 -0
- package/dist/esm/hashIndex.js +83 -0
- package/dist/esm/hashIndex.js.map +1 -0
- package/dist/esm/hashing/hash.d.ts +1 -0
- package/dist/esm/hashing/hash.js +122 -0
- package/dist/esm/hashing/hash.js.map +1 -0
- package/dist/esm/hashing/index.d.ts +2 -0
- package/dist/esm/hashing/murmur.d.ts +26 -0
- package/dist/esm/hashing/murmur.js +103 -0
- package/dist/esm/hashing/murmur.js.map +1 -0
- package/dist/esm/indexes.d.ts +0 -2
- package/dist/esm/indexes.js +98 -46
- package/dist/esm/indexes.js.map +1 -1
- package/dist/esm/multiset.js +2 -1
- package/dist/esm/multiset.js.map +1 -1
- package/dist/esm/operators/distinct.js +1 -1
- package/dist/esm/operators/distinct.js.map +1 -1
- package/dist/esm/operators/groupBy.d.ts +1 -1
- package/dist/esm/operators/groupBy.js +5 -4
- package/dist/esm/operators/groupBy.js.map +1 -1
- package/dist/esm/utils.d.ts +7 -4
- package/dist/esm/utils.js +13 -33
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/valueIndex.d.ts +17 -0
- package/dist/esm/valueIndex.js +74 -0
- package/dist/esm/valueIndex.js.map +1 -0
- package/package.json +1 -3
- package/src/hashIndex.ts +94 -0
- package/src/hashing/hash.ts +157 -0
- package/src/hashing/index.ts +2 -0
- package/src/hashing/murmur.ts +128 -0
- package/src/indexes.ts +106 -47
- package/src/multiset.ts +1 -1
- package/src/operators/distinct.ts +4 -4
- package/src/operators/groupBy.ts +7 -4
- package/src/utils.ts +31 -48
- package/src/valueIndex.ts +78 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
+
var _inner;
|
|
10
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
|
+
const utils = require("./utils.cjs");
|
|
12
|
+
const hash = require("./hashing/hash.cjs");
|
|
13
|
+
class HashIndex {
|
|
14
|
+
constructor() {
|
|
15
|
+
__privateAdd(this, _inner);
|
|
16
|
+
__privateSet(this, _inner, new utils.DefaultMap(
|
|
17
|
+
() => new utils.DefaultMap(() => [void 0, 0])
|
|
18
|
+
));
|
|
19
|
+
}
|
|
20
|
+
toString(indent = false) {
|
|
21
|
+
return `HashIndex(${JSON.stringify(
|
|
22
|
+
[...__privateGet(this, _inner)].map(([k, valueMap]) => [k, [...valueMap]]),
|
|
23
|
+
void 0,
|
|
24
|
+
indent ? 2 : void 0
|
|
25
|
+
)})`;
|
|
26
|
+
}
|
|
27
|
+
get(key) {
|
|
28
|
+
const valueMap = __privateGet(this, _inner).get(key);
|
|
29
|
+
return [...valueMap.values()];
|
|
30
|
+
}
|
|
31
|
+
getMultiplicity(key, value) {
|
|
32
|
+
const valueMap = __privateGet(this, _inner).get(key);
|
|
33
|
+
const valueHash = hash.hash(value);
|
|
34
|
+
const [, multiplicity] = valueMap.get(valueHash);
|
|
35
|
+
return multiplicity;
|
|
36
|
+
}
|
|
37
|
+
entries() {
|
|
38
|
+
return __privateGet(this, _inner).entries();
|
|
39
|
+
}
|
|
40
|
+
*entriesIterator() {
|
|
41
|
+
for (const [key, valueMap] of __privateGet(this, _inner).entries()) {
|
|
42
|
+
for (const [_valueHash, [value, multiplicity]] of valueMap.entries()) {
|
|
43
|
+
yield [key, [value, multiplicity]];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
has(key) {
|
|
48
|
+
return __privateGet(this, _inner).has(key);
|
|
49
|
+
}
|
|
50
|
+
delete(key) {
|
|
51
|
+
__privateGet(this, _inner).delete(key);
|
|
52
|
+
}
|
|
53
|
+
get size() {
|
|
54
|
+
return __privateGet(this, _inner).size;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds a value to the index and does not return anything
|
|
58
|
+
* except if the addition caused the value to be removed
|
|
59
|
+
* and the key to be left with only a single value.
|
|
60
|
+
* In that case, we return the single remaining value.
|
|
61
|
+
*/
|
|
62
|
+
addValue(key, value) {
|
|
63
|
+
const [val, multiplicity] = value;
|
|
64
|
+
const valueMap = __privateGet(this, _inner).get(key);
|
|
65
|
+
const valueHash = hash.hash(val);
|
|
66
|
+
const [, existingMultiplicity] = valueMap.get(valueHash);
|
|
67
|
+
const newMultiplicity = existingMultiplicity + multiplicity;
|
|
68
|
+
if (multiplicity !== 0) {
|
|
69
|
+
if (newMultiplicity === 0) {
|
|
70
|
+
valueMap.delete(valueHash);
|
|
71
|
+
if (valueMap.size === 1) {
|
|
72
|
+
return valueMap.entries().next().value[1];
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
valueMap.set(valueHash, [val, newMultiplicity]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
__privateGet(this, _inner).set(key, valueMap);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
_inner = new WeakMap();
|
|
82
|
+
exports.HashIndex = HashIndex;
|
|
83
|
+
//# sourceMappingURL=hashIndex.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hashIndex.cjs","sources":["../../src/hashIndex.ts"],"sourcesContent":["import { DefaultMap } from \"./utils.js\"\nimport { hash } from \"./hashing/index.js\"\nimport type { Hash } from \"./hashing/index.js\"\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 HashIndex<K, V> {\n #inner: DefaultMap<K, DefaultMap<Hash, [V, number]>>\n\n constructor() {\n this.#inner = new DefaultMap<K, DefaultMap<Hash, [V, number]>>(\n () => new DefaultMap<Hash, [V, number]>(() => [undefined as any as V, 0])\n )\n // #inner is as map of:\n // {\n // [key]: {\n // [hash(value)]: [value, multiplicity]\n // }\n // }\n }\n\n toString(indent = false): string {\n return `HashIndex(${JSON.stringify(\n [...this.#inner].map(([k, valueMap]) => [k, [...valueMap]]),\n undefined,\n indent ? 2 : undefined\n )})`\n }\n\n get(key: K): Array<[V, number]> {\n const valueMap = this.#inner.get(key)\n return [...valueMap.values()]\n }\n\n getMultiplicity(key: K, value: V): number {\n const valueMap = this.#inner.get(key)\n const valueHash = hash(value)\n const [, multiplicity] = valueMap.get(valueHash)\n return multiplicity\n }\n\n entries() {\n return this.#inner.entries()\n }\n\n *entriesIterator(): Generator<[K, [V, number]]> {\n for (const [key, valueMap] of this.#inner.entries()) {\n for (const [_valueHash, [value, multiplicity]] of valueMap.entries()) {\n yield [key, [value, multiplicity]]\n }\n }\n }\n\n has(key: K): boolean {\n return this.#inner.has(key)\n }\n\n delete(key: K): void {\n this.#inner.delete(key)\n }\n\n get size(): number {\n return this.#inner.size\n }\n\n /**\n * Adds a value to the index and does not return anything\n * except if the addition caused the value to be removed\n * and the key to be left with only a single value.\n * In that case, we return the single remaining value.\n */\n addValue(key: K, value: [V, number]): [V, number] | void {\n const [val, multiplicity] = value\n const valueMap = this.#inner.get(key)\n const valueHash = hash(val)\n const [, existingMultiplicity] = valueMap.get(valueHash)\n const newMultiplicity = existingMultiplicity + multiplicity\n if (multiplicity !== 0) {\n if (newMultiplicity === 0) {\n valueMap.delete(valueHash)\n if (valueMap.size === 1) {\n // Signal that the key only has a single remaining value\n return valueMap.entries().next().value![1]\n }\n } else {\n valueMap.set(valueHash, [val, newMultiplicity])\n }\n }\n this.#inner.set(key, valueMap)\n }\n}\n"],"names":["DefaultMap","hash"],"mappings":";;;;;;;;;;;;AASO,MAAM,UAAgB;AAAA,EAG3B,cAAc;AAFd;AAGE,uBAAK,QAAS,IAAIA,MAAAA;AAAAA,MAChB,MAAM,IAAIA,MAAAA,WAA8B,MAAM,CAAC,QAAuB,CAAC,CAAC;AAAA,IAAA;AAAA,EAQ5E;AAAA,EAEA,SAAS,SAAS,OAAe;AAC/B,WAAO,aAAa,KAAK;AAAA,MACvB,CAAC,GAAG,mBAAK,OAAM,EAAE,IAAI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/C,QAAI,iBAAiB,GAAG;AACtB,UAAI,oBAAoB,GAAG;AACzB,iBAAS,OAAO,SAAS;AACzB,YAAI,SAAS,SAAS,GAAG;AAEvB,iBAAO,SAAS,QAAA,EAAU,KAAA,EAAO,MAAO,CAAC;AAAA,QAC3C;AAAA,MACF,OAAO;AACL,iBAAS,IAAI,WAAW,CAAC,KAAK,eAAe,CAAC;AAAA,MAChD;AAAA,IACF;AACA,uBAAK,QAAO,IAAI,KAAK,QAAQ;AAAA,EAC/B;AACF;AAnFE;;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DefaultMap } from './utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* A map from a difference collection trace's keys -> (value, multiplicities) that changed.
|
|
4
|
+
* Used in operations like join and reduce where the operation needs to
|
|
5
|
+
* exploit the key-value structure of the data to run efficiently.
|
|
6
|
+
*/
|
|
7
|
+
export declare class HashIndex<K, V> {
|
|
8
|
+
#private;
|
|
9
|
+
constructor();
|
|
10
|
+
toString(indent?: boolean): string;
|
|
11
|
+
get(key: K): Array<[V, number]>;
|
|
12
|
+
getMultiplicity(key: K, value: V): number;
|
|
13
|
+
entries(): MapIterator<[K, DefaultMap<number, [V, number]>]>;
|
|
14
|
+
entriesIterator(): Generator<[K, [V, number]]>;
|
|
15
|
+
has(key: K): boolean;
|
|
16
|
+
delete(key: K): void;
|
|
17
|
+
get size(): number;
|
|
18
|
+
/**
|
|
19
|
+
* Adds a value to the index and does not return anything
|
|
20
|
+
* except if the addition caused the value to be removed
|
|
21
|
+
* and the key to be left with only a single value.
|
|
22
|
+
* In that case, we return the single remaining value.
|
|
23
|
+
*/
|
|
24
|
+
addValue(key: K, value: [V, number]): [V, number] | void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const murmur = require("./murmur.cjs");
|
|
4
|
+
const TRUE = murmur.randomHash();
|
|
5
|
+
const FALSE = murmur.randomHash();
|
|
6
|
+
const NULL = murmur.randomHash();
|
|
7
|
+
const UNDEFINED = murmur.randomHash();
|
|
8
|
+
const KEY = murmur.randomHash();
|
|
9
|
+
const FUNCTIONS = murmur.randomHash();
|
|
10
|
+
const DATE_MARKER = murmur.randomHash();
|
|
11
|
+
const OBJECT_MARKER = murmur.randomHash();
|
|
12
|
+
const ARRAY_MARKER = murmur.randomHash();
|
|
13
|
+
const MAP_MARKER = murmur.randomHash();
|
|
14
|
+
const SET_MARKER = murmur.randomHash();
|
|
15
|
+
const hashCache = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
function hash(input) {
|
|
17
|
+
const hasher = new murmur.MurmurHashStream();
|
|
18
|
+
updateHasher(hasher, input);
|
|
19
|
+
return hasher.digest();
|
|
20
|
+
}
|
|
21
|
+
function hashObject(input) {
|
|
22
|
+
const cachedHash = hashCache.get(input);
|
|
23
|
+
if (cachedHash !== void 0) {
|
|
24
|
+
return cachedHash;
|
|
25
|
+
}
|
|
26
|
+
let valueHash;
|
|
27
|
+
if (input instanceof Date) {
|
|
28
|
+
valueHash = hashDate(input);
|
|
29
|
+
} else {
|
|
30
|
+
let plainObjectInput = input;
|
|
31
|
+
let marker = OBJECT_MARKER;
|
|
32
|
+
if (input instanceof Array) {
|
|
33
|
+
marker = ARRAY_MARKER;
|
|
34
|
+
}
|
|
35
|
+
if (input instanceof Map) {
|
|
36
|
+
marker = MAP_MARKER;
|
|
37
|
+
plainObjectInput = [...input.entries()];
|
|
38
|
+
}
|
|
39
|
+
if (input instanceof Set) {
|
|
40
|
+
marker = SET_MARKER;
|
|
41
|
+
plainObjectInput = [...input.entries()];
|
|
42
|
+
}
|
|
43
|
+
if (input instanceof Buffer || input instanceof Uint8Array || input instanceof File) {
|
|
44
|
+
return cachedReferenceHash(input);
|
|
45
|
+
}
|
|
46
|
+
valueHash = hashPlainObject(plainObjectInput, marker);
|
|
47
|
+
}
|
|
48
|
+
hashCache.set(input, valueHash);
|
|
49
|
+
return valueHash;
|
|
50
|
+
}
|
|
51
|
+
function hashDate(input) {
|
|
52
|
+
const hasher = new murmur.MurmurHashStream();
|
|
53
|
+
hasher.update(DATE_MARKER);
|
|
54
|
+
hasher.update(input.getTime());
|
|
55
|
+
return hasher.digest();
|
|
56
|
+
}
|
|
57
|
+
function hashPlainObject(input, marker) {
|
|
58
|
+
const hasher = new murmur.MurmurHashStream();
|
|
59
|
+
hasher.update(marker);
|
|
60
|
+
const keys = Object.keys(input);
|
|
61
|
+
keys.sort(keySort);
|
|
62
|
+
for (const key of keys) {
|
|
63
|
+
hasher.update(KEY);
|
|
64
|
+
hasher.update(key);
|
|
65
|
+
updateHasher(hasher, input[key]);
|
|
66
|
+
}
|
|
67
|
+
return hasher.digest();
|
|
68
|
+
}
|
|
69
|
+
function updateHasher(hasher, input) {
|
|
70
|
+
if (input === null) {
|
|
71
|
+
hasher.update(NULL);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
switch (typeof input) {
|
|
75
|
+
case `undefined`:
|
|
76
|
+
hasher.update(UNDEFINED);
|
|
77
|
+
return;
|
|
78
|
+
case `boolean`:
|
|
79
|
+
hasher.update(input ? TRUE : FALSE);
|
|
80
|
+
return;
|
|
81
|
+
case `number`:
|
|
82
|
+
hasher.update(isNaN(input) ? NaN : input === 0 ? 0 : input);
|
|
83
|
+
return;
|
|
84
|
+
case `bigint`:
|
|
85
|
+
case `string`:
|
|
86
|
+
case `symbol`:
|
|
87
|
+
hasher.update(input);
|
|
88
|
+
return;
|
|
89
|
+
case `object`:
|
|
90
|
+
hasher.update(getCachedHash(input));
|
|
91
|
+
return;
|
|
92
|
+
case `function`:
|
|
93
|
+
hasher.update(cachedReferenceHash(input));
|
|
94
|
+
return;
|
|
95
|
+
default:
|
|
96
|
+
console.warn(
|
|
97
|
+
`Ignored input during hashing because it is of type ${typeof input} which is not supported`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function getCachedHash(input) {
|
|
102
|
+
let valueHash = hashCache.get(input);
|
|
103
|
+
if (valueHash === void 0) {
|
|
104
|
+
valueHash = hashObject(input);
|
|
105
|
+
}
|
|
106
|
+
return valueHash;
|
|
107
|
+
}
|
|
108
|
+
let nextRefId = 1;
|
|
109
|
+
function cachedReferenceHash(fn) {
|
|
110
|
+
let valueHash = hashCache.get(fn);
|
|
111
|
+
if (valueHash === void 0) {
|
|
112
|
+
valueHash = nextRefId ^ FUNCTIONS;
|
|
113
|
+
nextRefId++;
|
|
114
|
+
hashCache.set(fn, valueHash);
|
|
115
|
+
}
|
|
116
|
+
return valueHash;
|
|
117
|
+
}
|
|
118
|
+
function keySort(a, b) {
|
|
119
|
+
return a.localeCompare(b);
|
|
120
|
+
}
|
|
121
|
+
exports.hash = hash;
|
|
122
|
+
//# sourceMappingURL=hash.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.cjs","sources":["../../../src/hashing/hash.ts"],"sourcesContent":["import { MurmurHashStream, randomHash } from \"./murmur.js\"\nimport type { Hasher } from \"./murmur.js\"\n\n/*\n * Implementation of structural hashing based on the Composites polyfill implementation:\n * https://github.com/tc39/proposal-composites\n */\n\nconst TRUE = randomHash()\nconst FALSE = randomHash()\nconst NULL = randomHash()\nconst UNDEFINED = randomHash()\nconst KEY = randomHash()\nconst FUNCTIONS = randomHash()\nconst DATE_MARKER = randomHash()\nconst OBJECT_MARKER = randomHash()\nconst ARRAY_MARKER = randomHash()\nconst MAP_MARKER = randomHash()\nconst SET_MARKER = randomHash()\n\nconst hashCache = new WeakMap<object, number>()\n\nexport function hash(input: any): number {\n const hasher = new MurmurHashStream()\n updateHasher(hasher, input)\n return hasher.digest()\n}\n\nfunction hashObject(input: object): number {\n const cachedHash = hashCache.get(input)\n if (cachedHash !== undefined) {\n return cachedHash\n }\n\n let valueHash: number | undefined\n if (input instanceof Date) {\n valueHash = hashDate(input)\n } else {\n let plainObjectInput = input\n let marker = OBJECT_MARKER\n\n if (input instanceof Array) {\n marker = ARRAY_MARKER\n }\n\n if (input instanceof Map) {\n marker = MAP_MARKER\n plainObjectInput = [...input.entries()]\n }\n\n if (input instanceof Set) {\n marker = SET_MARKER\n plainObjectInput = [...input.entries()]\n }\n\n if (\n input instanceof Buffer ||\n input instanceof Uint8Array ||\n input instanceof File\n ) {\n // Deeply hashing these objects would be too costly\n // but we also don't want to ignore them\n // so we track them by reference and cache them in a weak map\n return cachedReferenceHash(input)\n }\n\n valueHash = hashPlainObject(plainObjectInput, marker)\n }\n\n hashCache.set(input, valueHash)\n return valueHash\n}\n\nfunction hashDate(input: Date): number {\n const hasher = new MurmurHashStream()\n hasher.update(DATE_MARKER)\n hasher.update(input.getTime())\n return hasher.digest()\n}\n\nfunction hashPlainObject(input: object, marker: number): number {\n const hasher = new MurmurHashStream()\n\n // Mark the type of the input\n hasher.update(marker)\n const keys = Object.keys(input)\n keys.sort(keySort)\n for (const key of keys) {\n hasher.update(KEY)\n hasher.update(key)\n updateHasher(hasher, input[key as keyof typeof input])\n }\n\n return hasher.digest()\n}\n\nfunction updateHasher(hasher: Hasher, input: unknown): void {\n if (input === null) {\n hasher.update(NULL)\n return\n }\n switch (typeof input) {\n case `undefined`:\n hasher.update(UNDEFINED)\n return\n case `boolean`:\n hasher.update(input ? TRUE : FALSE)\n return\n case `number`:\n // Normalize NaNs and -0\n hasher.update(isNaN(input) ? NaN : input === 0 ? 0 : input)\n return\n case `bigint`:\n case `string`:\n case `symbol`:\n hasher.update(input)\n return\n case `object`:\n hasher.update(getCachedHash(input))\n return\n case `function`:\n // Functions are assigned a globally unique ID\n // and that ID is cached in the weak map\n hasher.update(cachedReferenceHash(input))\n return\n default:\n console.warn(\n `Ignored input during hashing because it is of type ${typeof input} which is not supported`\n )\n }\n}\n\nfunction getCachedHash(input: object): number {\n let valueHash = hashCache.get(input)\n if (valueHash === undefined) {\n valueHash = hashObject(input)\n }\n return valueHash\n}\n\nlet nextRefId = 1\nfunction cachedReferenceHash(fn: object): number {\n let valueHash = hashCache.get(fn)\n if (valueHash === undefined) {\n valueHash = nextRefId ^ FUNCTIONS\n nextRefId++\n hashCache.set(fn, valueHash)\n }\n return valueHash\n}\n\n/**\n * Strings sorted lexicographically.\n */\nfunction keySort(a: string, b: string): number {\n return a.localeCompare(b)\n}\n"],"names":["randomHash","MurmurHashStream"],"mappings":";;;AAQA,MAAM,OAAOA,OAAAA,WAAA;AACb,MAAM,QAAQA,OAAAA,WAAA;AACd,MAAM,OAAOA,OAAAA,WAAA;AACb,MAAM,YAAYA,OAAAA,WAAA;AAClB,MAAM,MAAMA,OAAAA,WAAA;AACZ,MAAM,YAAYA,OAAAA,WAAA;AAClB,MAAM,cAAcA,OAAAA,WAAA;AACpB,MAAM,gBAAgBA,OAAAA,WAAA;AACtB,MAAM,eAAeA,OAAAA,WAAA;AACrB,MAAM,aAAaA,OAAAA,WAAA;AACnB,MAAM,aAAaA,OAAAA,WAAA;AAEnB,MAAM,gCAAgB,QAAA;AAEf,SAAS,KAAK,OAAoB;AACvC,QAAM,SAAS,IAAIC,wBAAA;AACnB,eAAa,QAAQ,KAAK;AAC1B,SAAO,OAAO,OAAA;AAChB;AAEA,SAAS,WAAW,OAAuB;AACzC,QAAM,aAAa,UAAU,IAAI,KAAK;AACtC,MAAI,eAAe,QAAW;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI;AACJ,MAAI,iBAAiB,MAAM;AACzB,gBAAY,SAAS,KAAK;AAAA,EAC5B,OAAO;AACL,QAAI,mBAAmB;AACvB,QAAI,SAAS;AAEb,QAAI,iBAAiB,OAAO;AAC1B,eAAS;AAAA,IACX;AAEA,QAAI,iBAAiB,KAAK;AACxB,eAAS;AACT,yBAAmB,CAAC,GAAG,MAAM,SAAS;AAAA,IACxC;AAEA,QAAI,iBAAiB,KAAK;AACxB,eAAS;AACT,yBAAmB,CAAC,GAAG,MAAM,SAAS;AAAA,IACxC;AAEA,QACE,iBAAiB,UACjB,iBAAiB,cACjB,iBA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AI,YAAY,UAAU,IAAI,KAAK;AACnC,MAAI,cAAc,QAAW;AAC3B,gBAAY,WAAW,KAAK;AAAA,EAC9B;AACA,SAAO;AACT;AAEA,IAAI,YAAY;AAChB,SAAS,oBAAoB,IAAoB;AAC/C,MAAI,YAAY,UAAU,IAAI,EAAE;AAChC,MAAI,cAAc,QAAW;AAC3B,gBAAY,YAAY;AACxB;AACA,cAAU,IAAI,IAAI,SAAS;AAAA,EAC7B;AACA,SAAO;AACT;AAKA,SAAS,QAAQ,GAAW,GAAmB;AAC7C,SAAO,EAAE,cAAc,CAAC;AAC1B;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hash(input: any): number;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const RANDOM_SEED = randomHash();
|
|
4
|
+
const STRING_MARKER = randomHash();
|
|
5
|
+
const BIG_INT_MARKER = randomHash();
|
|
6
|
+
const NEG_BIG_INT_MARKER = randomHash();
|
|
7
|
+
const SYMBOL_MARKER = randomHash();
|
|
8
|
+
function randomHash() {
|
|
9
|
+
return Math.random() * (2 ** 31 - 1) >>> 0;
|
|
10
|
+
}
|
|
11
|
+
class MurmurHashStream {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.hash = RANDOM_SEED;
|
|
14
|
+
this.length = 0;
|
|
15
|
+
this.carry = 0;
|
|
16
|
+
this.carryBytes = 0;
|
|
17
|
+
}
|
|
18
|
+
_mix(k1) {
|
|
19
|
+
k1 = Math.imul(k1, 3432918353);
|
|
20
|
+
k1 = k1 << 15 | k1 >>> 17;
|
|
21
|
+
k1 = Math.imul(k1, 461845907);
|
|
22
|
+
this.hash ^= k1;
|
|
23
|
+
this.hash = this.hash << 13 | this.hash >>> 19;
|
|
24
|
+
this.hash = Math.imul(this.hash, 5) + 3864292196;
|
|
25
|
+
}
|
|
26
|
+
_writeByte(byte) {
|
|
27
|
+
this.carry |= (byte & 255) << 8 * this.carryBytes;
|
|
28
|
+
this.carryBytes++;
|
|
29
|
+
this.length++;
|
|
30
|
+
if (this.carryBytes === 4) {
|
|
31
|
+
this._mix(this.carry >>> 0);
|
|
32
|
+
this.carry = 0;
|
|
33
|
+
this.carryBytes = 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
update(chunk) {
|
|
37
|
+
switch (typeof chunk) {
|
|
38
|
+
case `symbol`: {
|
|
39
|
+
this.update(SYMBOL_MARKER);
|
|
40
|
+
const description = chunk.description;
|
|
41
|
+
if (!description) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
for (let i = 0; i < description.length; i++) {
|
|
45
|
+
const code = description.charCodeAt(i);
|
|
46
|
+
this._writeByte(code & 255);
|
|
47
|
+
this._writeByte(code >>> 8 & 255);
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
case `string`:
|
|
52
|
+
this.update(STRING_MARKER);
|
|
53
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
54
|
+
const code = chunk.charCodeAt(i);
|
|
55
|
+
this._writeByte(code & 255);
|
|
56
|
+
this._writeByte(code >>> 8 & 255);
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
case `number`:
|
|
60
|
+
this._writeByte(chunk & 255);
|
|
61
|
+
this._writeByte(chunk >>> 8 & 255);
|
|
62
|
+
this._writeByte(chunk >>> 16 & 255);
|
|
63
|
+
this._writeByte(chunk >>> 24 & 255);
|
|
64
|
+
return;
|
|
65
|
+
case `bigint`: {
|
|
66
|
+
let value = chunk;
|
|
67
|
+
if (value < 0n) {
|
|
68
|
+
value = -value;
|
|
69
|
+
this.update(NEG_BIG_INT_MARKER);
|
|
70
|
+
} else {
|
|
71
|
+
this.update(BIG_INT_MARKER);
|
|
72
|
+
}
|
|
73
|
+
while (value > 0n) {
|
|
74
|
+
this._writeByte(Number(value & 0xffn));
|
|
75
|
+
value >>= 8n;
|
|
76
|
+
}
|
|
77
|
+
if (chunk === 0n) this._writeByte(0);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
throw new TypeError(`Unsupported input type: ${typeof chunk}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
digest() {
|
|
85
|
+
if (this.carryBytes > 0) {
|
|
86
|
+
let k1 = this.carry >>> 0;
|
|
87
|
+
k1 = Math.imul(k1, 3432918353);
|
|
88
|
+
k1 = k1 << 15 | k1 >>> 17;
|
|
89
|
+
k1 = Math.imul(k1, 461845907);
|
|
90
|
+
this.hash ^= k1;
|
|
91
|
+
}
|
|
92
|
+
this.hash ^= this.length;
|
|
93
|
+
this.hash ^= this.hash >>> 16;
|
|
94
|
+
this.hash = Math.imul(this.hash, 2246822507);
|
|
95
|
+
this.hash ^= this.hash >>> 13;
|
|
96
|
+
this.hash = Math.imul(this.hash, 3266489909);
|
|
97
|
+
this.hash ^= this.hash >>> 16;
|
|
98
|
+
return this.hash >>> 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.MurmurHashStream = MurmurHashStream;
|
|
102
|
+
exports.randomHash = randomHash;
|
|
103
|
+
//# sourceMappingURL=murmur.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"murmur.cjs","sources":["../../../src/hashing/murmur.ts"],"sourcesContent":["/*\n * Implementation of murmur hash based on the Composites polyfill implementation:\n * https://github.com/tc39/proposal-composites\n */\n\nconst RANDOM_SEED = randomHash()\nconst STRING_MARKER = randomHash()\nconst BIG_INT_MARKER = randomHash()\nconst NEG_BIG_INT_MARKER = randomHash()\nconst SYMBOL_MARKER = randomHash()\n\nexport type Hash = number\n\nexport function randomHash() {\n return (Math.random() * (2 ** 31 - 1)) >>> 0\n}\n\nexport interface Hasher {\n update: (val: symbol | string | number | bigint) => void\n digest: () => number\n}\n\n/**\n * This implementation of Murmur hash uses a random initial seed and random markers.\n * This means that hashes aren't deterministic across app restarts.\n * This is intentional in the composites polyfill to be resistent to hash-flooding attacks\n * where malicious users would precompute lots of different objects whose hashes collide with each other.\n *\n * Currently, for ts/db-ivm this is fine because we don't persist client state.\n * However, when we will introduce persistence we will either need to store the seeds or remove the randomness\n * to ensure deterministic hashes across app restarts.\n */\nexport class MurmurHashStream implements Hasher {\n private hash: number = RANDOM_SEED\n private length = 0\n private carry = 0\n private carryBytes = 0\n\n private _mix(k1: number): void {\n k1 = Math.imul(k1, 0xcc9e2d51)\n k1 = (k1 << 15) | (k1 >>> 17)\n k1 = Math.imul(k1, 0x1b873593)\n this.hash ^= k1\n this.hash = (this.hash << 13) | (this.hash >>> 19)\n this.hash = Math.imul(this.hash, 5) + 0xe6546b64\n }\n\n private _writeByte(byte: number): void {\n this.carry |= (byte & 0xff) << (8 * this.carryBytes)\n this.carryBytes++\n this.length++\n\n if (this.carryBytes === 4) {\n this._mix(this.carry >>> 0)\n this.carry = 0\n this.carryBytes = 0\n }\n }\n\n update(chunk: symbol | string | number | bigint): void {\n switch (typeof chunk) {\n case `symbol`: {\n this.update(SYMBOL_MARKER)\n const description = chunk.description\n if (!description) {\n return\n }\n\n for (let i = 0; i < description.length; i++) {\n const code = description.charCodeAt(i)\n this._writeByte(code & 0xff)\n this._writeByte((code >>> 8) & 0xff)\n }\n return\n }\n case `string`:\n this.update(STRING_MARKER)\n for (let i = 0; i < chunk.length; i++) {\n const code = chunk.charCodeAt(i)\n this._writeByte(code & 0xff)\n this._writeByte((code >>> 8) & 0xff)\n }\n return\n case `number`:\n this._writeByte(chunk & 0xff)\n this._writeByte((chunk >>> 8) & 0xff)\n this._writeByte((chunk >>> 16) & 0xff)\n this._writeByte((chunk >>> 24) & 0xff)\n return\n case `bigint`: {\n let value = chunk\n if (value < 0n) {\n value = -value\n this.update(NEG_BIG_INT_MARKER)\n } else {\n this.update(BIG_INT_MARKER)\n }\n while (value > 0n) {\n this._writeByte(Number(value & 0xffn))\n value >>= 8n\n }\n if (chunk === 0n) this._writeByte(0)\n return\n }\n default:\n throw new TypeError(`Unsupported input type: ${typeof chunk}`)\n }\n }\n\n digest(): number {\n if (this.carryBytes > 0) {\n let k1 = this.carry >>> 0\n k1 = Math.imul(k1, 0xcc9e2d51)\n k1 = (k1 << 15) | (k1 >>> 17)\n k1 = Math.imul(k1, 0x1b873593)\n this.hash ^= k1\n }\n\n this.hash ^= this.length\n this.hash ^= this.hash >>> 16\n this.hash = Math.imul(this.hash, 0x85ebca6b)\n this.hash ^= this.hash >>> 13\n this.hash = Math.imul(this.hash, 0xc2b2ae35)\n this.hash ^= this.hash >>> 16\n\n return this.hash >>> 0\n }\n}\n"],"names":[],"mappings":";;AAKA,MAAM,cAAc,WAAA;AACpB,MAAM,gBAAgB,WAAA;AACtB,MAAM,iBAAiB,WAAA;AACvB,MAAM,qBAAqB,WAAA;AAC3B,MAAM,gBAAgB,WAAA;AAIf,SAAS,aAAa;AAC3B,SAAQ,KAAK,OAAA,KAAY,KAAK,KAAK,OAAQ;AAC7C;AAiBO,MAAM,iBAAmC;AAAA,EAAzC,cAAA;AACL,SAAQ,OAAe;AACvB,SAAQ,SAAS;AACjB,SAAQ,QAAQ;AAChB,SAAQ,aAAa;AAAA,EAAA;AAAA,EAEb,KAAK,IAAkB;AAC7B,SAAK,KAAK,KAAK,IAAI,UAAU;AAC7B,SAAM,MAAM,KAAO,OAAO;AAC1B,SAAK,KAAK,KAAK,IAAI,SAAU;AAC7B,SAAK,QAAQ;AACb,SAAK,OAAQ,KAAK,QAAQ,KAAO,KAAK,SAAS;AAC/C,SAAK,OAAO,KAAK,KAAK,KAAK,MAAM,CAAC,IAAI;AAAA,EACxC;AAAA,EAEQ,WAAW,MAAoB;AACrC,SAAK,UAAU,OAAO,QAAU,IAAI,KAAK;AACzC,SAAK;AACL,SAAK;AAEL,QAAI,KAAK,eAAe,GAAG;AACzB,WAAK,KAAK,KAAK,UAAU,CAAC;AAC1B,WAAK,QAAQ;AACb,WAAK,aAAa;AAAA,IACpB;AAAA,EACF;AAA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;AAC7B,WAAM,MAAM,KAAO,OAAO;AAC1B,WAAK,KAAK,KAAK,IAAI,SAAU;AAC7B,WAAK,QAAQ;AAAA,IACf;AAEA,SAAK,QAAQ,KAAK;AAClB,SAAK,QAAQ,KAAK,SAAS;AAC3B,SAAK,OAAO,KAAK,KAAK,KAAK,MAAM,UAAU;AAC3C,SAAK,QAAQ,KAAK,SAAS;AAC3B,SAAK,OAAO,KAAK,KAAK,KAAK,MAAM,UAAU;AAC3C,SAAK,QAAQ,KAAK,SAAS;AAE3B,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type Hash = number;
|
|
2
|
+
export declare function randomHash(): number;
|
|
3
|
+
export interface Hasher {
|
|
4
|
+
update: (val: symbol | string | number | bigint) => void;
|
|
5
|
+
digest: () => number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* This implementation of Murmur hash uses a random initial seed and random markers.
|
|
9
|
+
* This means that hashes aren't deterministic across app restarts.
|
|
10
|
+
* This is intentional in the composites polyfill to be resistent to hash-flooding attacks
|
|
11
|
+
* where malicious users would precompute lots of different objects whose hashes collide with each other.
|
|
12
|
+
*
|
|
13
|
+
* Currently, for ts/db-ivm this is fine because we don't persist client state.
|
|
14
|
+
* However, when we will introduce persistence we will either need to store the seeds or remove the randomness
|
|
15
|
+
* to ensure deterministic hashes across app restarts.
|
|
16
|
+
*/
|
|
17
|
+
export declare class MurmurHashStream implements Hasher {
|
|
18
|
+
private hash;
|
|
19
|
+
private length;
|
|
20
|
+
private carry;
|
|
21
|
+
private carryBytes;
|
|
22
|
+
private _mix;
|
|
23
|
+
private _writeByte;
|
|
24
|
+
update(chunk: symbol | string | number | bigint): void;
|
|
25
|
+
digest(): number;
|
|
26
|
+
}
|
package/dist/cjs/indexes.cjs
CHANGED
|
@@ -6,76 +6,102 @@ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot
|
|
|
6
6
|
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
7
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
8
|
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var
|
|
9
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
10
|
+
var _valueIndex, _hashIndex, _Index_instances, entries_fn, entriesIterators_fn;
|
|
10
11
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
12
|
const multiset = require("./multiset.cjs");
|
|
13
|
+
const hashIndex = require("./hashIndex.cjs");
|
|
14
|
+
const valueIndex = require("./valueIndex.cjs");
|
|
12
15
|
const utils = require("./utils.cjs");
|
|
13
16
|
class Index {
|
|
14
17
|
constructor() {
|
|
15
|
-
__privateAdd(this,
|
|
16
|
-
|
|
18
|
+
__privateAdd(this, _Index_instances);
|
|
19
|
+
/*
|
|
20
|
+
* This is a hybrid Index that composes a ValueIndex and a HashIndex.
|
|
21
|
+
* Keys that have only one value are stored in the ValueIndex.
|
|
22
|
+
* Keys that have multiple values are stored in the HashIndex, the hash distinguishes between the values.
|
|
23
|
+
* This reduces the amount of hashes we need to compute since often times only a small portion of the keys are updated
|
|
24
|
+
* so we don't have to hash the keys that are never updated.
|
|
25
|
+
*
|
|
26
|
+
* Note: The `valueIndex` and `hashIndex` have disjoint keys.
|
|
27
|
+
* When a key that has only one value gets a new distinct value,
|
|
28
|
+
* it is added to the `hashIndex` and removed from the `valueIndex` and vice versa.
|
|
29
|
+
*/
|
|
30
|
+
__privateAdd(this, _valueIndex);
|
|
31
|
+
__privateAdd(this, _hashIndex);
|
|
32
|
+
__privateSet(this, _valueIndex, new valueIndex.ValueIndex());
|
|
33
|
+
__privateSet(this, _hashIndex, new hashIndex.HashIndex());
|
|
17
34
|
}
|
|
18
35
|
toString(indent = false) {
|
|
19
|
-
return `Index(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
)})`;
|
|
36
|
+
return `Index(
|
|
37
|
+
${__privateGet(this, _valueIndex).toString(indent)},
|
|
38
|
+
${__privateGet(this, _hashIndex).toString(indent)}
|
|
39
|
+
)`;
|
|
24
40
|
}
|
|
25
41
|
get(key) {
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
if (__privateGet(this, _valueIndex).has(key)) {
|
|
43
|
+
return [__privateGet(this, _valueIndex).get(key)];
|
|
44
|
+
}
|
|
45
|
+
return __privateGet(this, _hashIndex).get(key);
|
|
28
46
|
}
|
|
29
47
|
getMultiplicity(key, value) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return __privateGet(this, _inner).entries();
|
|
35
|
-
}
|
|
36
|
-
keys() {
|
|
37
|
-
return __privateGet(this, _inner).keys();
|
|
48
|
+
if (__privateGet(this, _valueIndex).has(key)) {
|
|
49
|
+
return __privateGet(this, _valueIndex).getMultiplicity(key);
|
|
50
|
+
}
|
|
51
|
+
return __privateGet(this, _hashIndex).getMultiplicity(key, value);
|
|
38
52
|
}
|
|
39
53
|
has(key) {
|
|
40
|
-
return __privateGet(this,
|
|
54
|
+
return __privateGet(this, _valueIndex).has(key) || __privateGet(this, _hashIndex).has(key);
|
|
41
55
|
}
|
|
42
56
|
get size() {
|
|
43
|
-
return __privateGet(this,
|
|
57
|
+
return __privateGet(this, _valueIndex).size + __privateGet(this, _hashIndex).size;
|
|
44
58
|
}
|
|
45
59
|
addValue(key, value) {
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
60
|
+
const containedInValueIndex = __privateGet(this, _valueIndex).has(key);
|
|
61
|
+
const containedInHashIndex = __privateGet(this, _hashIndex).has(key);
|
|
62
|
+
if (containedInHashIndex && containedInValueIndex) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Key ${key} is contained in both the value index and the hash index. This should never happen because they should have disjoint keysets.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (!containedInValueIndex && !containedInHashIndex) {
|
|
68
|
+
__privateGet(this, _valueIndex).addValue(key, value);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (containedInValueIndex) {
|
|
72
|
+
try {
|
|
73
|
+
__privateGet(this, _valueIndex).addValue(key, value);
|
|
74
|
+
} catch {
|
|
75
|
+
const existingValue = __privateGet(this, _valueIndex).get(key);
|
|
76
|
+
__privateGet(this, _valueIndex).delete(key);
|
|
77
|
+
__privateGet(this, _hashIndex).addValue(key, existingValue);
|
|
78
|
+
__privateGet(this, _hashIndex).addValue(key, value);
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (containedInHashIndex) {
|
|
83
|
+
const singleRemainingValue = __privateGet(this, _hashIndex).addValue(key, value);
|
|
84
|
+
if (singleRemainingValue) {
|
|
85
|
+
__privateGet(this, _hashIndex).delete(key);
|
|
86
|
+
__privateGet(this, _valueIndex).addValue(key, singleRemainingValue);
|
|
55
87
|
}
|
|
88
|
+
return;
|
|
56
89
|
}
|
|
57
90
|
}
|
|
58
91
|
append(other) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const existingMultiplicity = thisValueMap.get(value) ?? 0;
|
|
63
|
-
const newMultiplicity = existingMultiplicity + multiplicity;
|
|
64
|
-
if (newMultiplicity === 0) {
|
|
65
|
-
thisValueMap.delete(value);
|
|
66
|
-
} else {
|
|
67
|
-
thisValueMap.set(value, newMultiplicity);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
92
|
+
var _a;
|
|
93
|
+
for (const [key, value] of __privateMethod(_a = other, _Index_instances, entries_fn).call(_a)) {
|
|
94
|
+
this.addValue(key, value);
|
|
70
95
|
}
|
|
71
96
|
}
|
|
72
97
|
join(other) {
|
|
98
|
+
var _a;
|
|
73
99
|
const result = [];
|
|
74
100
|
if (this.size <= other.size) {
|
|
75
|
-
for (const [key,
|
|
101
|
+
for (const [key, valueIt] of __privateMethod(this, _Index_instances, entriesIterators_fn).call(this)) {
|
|
76
102
|
if (!other.has(key)) continue;
|
|
77
103
|
const otherValues = other.get(key);
|
|
78
|
-
for (const [val1, mul1] of
|
|
104
|
+
for (const [val1, mul1] of valueIt) {
|
|
79
105
|
for (const [val2, mul2] of otherValues) {
|
|
80
106
|
if (mul1 !== 0 && mul2 !== 0) {
|
|
81
107
|
result.push([[key, [val1, val2]], mul1 * mul2]);
|
|
@@ -84,10 +110,10 @@ class Index {
|
|
|
84
110
|
}
|
|
85
111
|
}
|
|
86
112
|
} else {
|
|
87
|
-
for (const [key,
|
|
113
|
+
for (const [key, otherValueIt] of __privateMethod(_a = other, _Index_instances, entriesIterators_fn).call(_a)) {
|
|
88
114
|
if (!this.has(key)) continue;
|
|
89
115
|
const values = this.get(key);
|
|
90
|
-
for (const [val2, mul2] of
|
|
116
|
+
for (const [val2, mul2] of otherValueIt) {
|
|
91
117
|
for (const [val1, mul1] of values) {
|
|
92
118
|
if (mul1 !== 0 && mul2 !== 0) {
|
|
93
119
|
result.push([[key, [val1, val2]], mul1 * mul2]);
|
|
@@ -99,6 +125,32 @@ class Index {
|
|
|
99
125
|
return new multiset.MultiSet(result);
|
|
100
126
|
}
|
|
101
127
|
}
|
|
102
|
-
|
|
128
|
+
_valueIndex = new WeakMap();
|
|
129
|
+
_hashIndex = new WeakMap();
|
|
130
|
+
_Index_instances = new WeakSet();
|
|
131
|
+
/**
|
|
132
|
+
* This returns an iterator that iterates over all key-value pairs.
|
|
133
|
+
* @returns An iterable of all key-value pairs (and their multiplicities) in the index.
|
|
134
|
+
*/
|
|
135
|
+
entries_fn = function() {
|
|
136
|
+
return utils.concatIterable(
|
|
137
|
+
__privateGet(this, _valueIndex).entries(),
|
|
138
|
+
__privateGet(this, _hashIndex).entriesIterator()
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
entriesIterators_fn = function* () {
|
|
142
|
+
for (const [key, [value, multiplicity]] of __privateGet(this, _valueIndex).entries()) {
|
|
143
|
+
yield [key, /* @__PURE__ */ new Map([[value, multiplicity]])];
|
|
144
|
+
}
|
|
145
|
+
for (const [key, valueMap] of __privateGet(this, _hashIndex).entries()) {
|
|
146
|
+
yield [
|
|
147
|
+
key,
|
|
148
|
+
utils.mapIterable(valueMap, ([_hash, [value, multiplicity]]) => [
|
|
149
|
+
value,
|
|
150
|
+
multiplicity
|
|
151
|
+
])
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
};
|
|
103
155
|
exports.Index = Index;
|
|
104
156
|
//# sourceMappingURL=indexes.cjs.map
|
package/dist/cjs/indexes.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexes.cjs","sources":["../../src/indexes.ts"],"sourcesContent":["import { MultiSet } from \"./multiset.js\"\nimport {
|
|
1
|
+
{"version":3,"file":"indexes.cjs","sources":["../../src/indexes.ts"],"sourcesContent":["import { MultiSet } from \"./multiset.js\"\nimport { HashIndex } from \"./hashIndex.js\"\nimport { ValueIndex } from \"./valueIndex.js\"\nimport { concatIterable, mapIterable } from \"./utils.js\"\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<K, V> {\n /*\n * This is a hybrid Index that composes a ValueIndex and a HashIndex.\n * Keys that have only one value are stored in the ValueIndex.\n * Keys that have multiple values are stored in the HashIndex, the hash distinguishes between the values.\n * This reduces the amount of hashes we need to compute since often times only a small portion of the keys are updated\n * so we don't have to hash the keys that are never updated.\n *\n * Note: The `valueIndex` and `hashIndex` have disjoint keys.\n * When a key that has only one value gets a new distinct value,\n * it is added to the `hashIndex` and removed from the `valueIndex` and vice versa.\n */\n #valueIndex: ValueIndex<K, V>\n #hashIndex: HashIndex<K, V>\n\n constructor() {\n this.#valueIndex = new ValueIndex<K, V>()\n this.#hashIndex = new HashIndex<K, V>()\n }\n\n toString(indent = false): string {\n return `Index(\\n ${this.#valueIndex.toString(indent)},\\n ${this.#hashIndex.toString(indent)}\\n)`\n }\n\n get(key: K): Array<[V, number]> {\n if (this.#valueIndex.has(key)) {\n return [this.#valueIndex.get(key)!]\n }\n return this.#hashIndex.get(key)\n }\n\n getMultiplicity(key: K, value: V): number {\n if (this.#valueIndex.has(key)) {\n return this.#valueIndex.getMultiplicity(key)\n }\n return this.#hashIndex.getMultiplicity(key, value)\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<[K, [V, number]]> {\n return concatIterable(\n this.#valueIndex.entries(),\n this.#hashIndex.entriesIterator()\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<[K, Iterable<[V, number]>]> {\n for (const [key, [value, multiplicity]] of this.#valueIndex.entries()) {\n yield [key, new Map<V, number>([[value, multiplicity]])]\n }\n for (const [key, valueMap] of this.#hashIndex.entries()) {\n yield [\n key,\n mapIterable(valueMap, ([_hash, [value, multiplicity]]) => [\n value,\n multiplicity,\n ]),\n ]\n }\n }\n\n has(key: K): boolean {\n return this.#valueIndex.has(key) || this.#hashIndex.has(key)\n }\n\n get size(): number {\n return this.#valueIndex.size + this.#hashIndex.size\n }\n\n addValue(key: K, value: [V, number]): void {\n const containedInValueIndex = this.#valueIndex.has(key)\n const containedInHashIndex = this.#hashIndex.has(key)\n\n if (containedInHashIndex && containedInValueIndex) {\n throw new Error(\n `Key ${key} is contained in both the value index and the hash index. This should never happen because they should have disjoint keysets.`\n )\n }\n\n if (!containedInValueIndex && !containedInHashIndex) {\n // This is the first time we see the key\n // Add it to the value index\n this.#valueIndex.addValue(key, value)\n return\n }\n\n if (containedInValueIndex) {\n // This key is already in the value index\n // It could be that it's the same value or a different one\n // If it's a different value we will need to remove the key from the value index\n // and add the key and its two values to the hash index\n try {\n this.#valueIndex.addValue(key, value)\n } catch {\n // This is a different value, need to move the key to the hash index\n const existingValue = this.#valueIndex.get(key)!\n this.#valueIndex.delete(key)\n this.#hashIndex.addValue(key, existingValue)\n this.#hashIndex.addValue(key, value)\n }\n return\n }\n\n if (containedInHashIndex) {\n // This key is already in the hash index so it already has two or more values.\n // However, this new value and multiplicity could cause an existing value to be removed\n // and lead to the key having only a single value in which case we need to move it back to the value index\n const singleRemainingValue = this.#hashIndex.addValue(key, value)\n if (singleRemainingValue) {\n // The key only has a single remaining value so we need to move it back to the value index\n this.#hashIndex.delete(key)\n this.#valueIndex.addValue(key, singleRemainingValue)\n }\n return\n }\n }\n\n append(other: Index<K, V>): void {\n for (const [key, value] of other.#entries()) {\n this.addValue(key, value)\n }\n }\n\n join<V2>(other: Index<K, V2>): MultiSet<[K, [V, V2]]> {\n const result: Array<[[K, [V, V2]], number]> = []\n\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"],"names":["ValueIndex","HashInd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