@tanstack/db-ivm
Version:
Incremental View Maintenance for TanStack DB based on Differential Dataflow
84 lines (83 loc) • 2.9 kB
JavaScript
var __typeError = (msg) => {
throw TypeError(msg);
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
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);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var _inner;
import { DefaultMap } from "./utils.js";
import { hash } from "./hashing/hash.js";
class HashIndex {
constructor() {
__privateAdd(this, _inner);
__privateSet(this, _inner, new DefaultMap(
() => new DefaultMap(() => [void 0, 0])
));
}
toString(indent = false) {
return `HashIndex(${JSON.stringify(
[...__privateGet(this, _inner)].map(([k, valueMap]) => [k, [...valueMap]]),
void 0,
indent ? 2 : void 0
)})`;
}
get(key) {
const valueMap = __privateGet(this, _inner).get(key);
return [...valueMap.values()];
}
getMultiplicity(key, value) {
const valueMap = __privateGet(this, _inner).get(key);
const valueHash = hash(value);
const [, multiplicity] = valueMap.get(valueHash);
return multiplicity;
}
entries() {
return __privateGet(this, _inner).entries();
}
*entriesIterator() {
for (const [key, valueMap] of __privateGet(this, _inner).entries()) {
for (const [_valueHash, [value, multiplicity]] of valueMap.entries()) {
yield [key, [value, multiplicity]];
}
}
}
has(key) {
return __privateGet(this, _inner).has(key);
}
delete(key) {
__privateGet(this, _inner).delete(key);
}
get size() {
return __privateGet(this, _inner).size;
}
/**
* Adds a value to the index and does not return anything
* except if the addition caused the value to be removed
* and the key to be left with only a single value.
* In that case, we return the single remaining value.
*/
addValue(key, value) {
const [val, multiplicity] = value;
const valueMap = __privateGet(this, _inner).get(key);
const valueHash = hash(val);
const [, existingMultiplicity] = valueMap.get(valueHash);
const newMultiplicity = existingMultiplicity + multiplicity;
if (multiplicity !== 0) {
if (newMultiplicity === 0) {
valueMap.delete(valueHash);
if (valueMap.size === 1) {
return valueMap.entries().next().value[1];
}
} else {
valueMap.set(valueHash, [val, newMultiplicity]);
}
}
__privateGet(this, _inner).set(key, valueMap);
}
}
_inner = new WeakMap();
export {
HashIndex
};
//# sourceMappingURL=hashIndex.js.map