@tanstack/db-ivm
Version:
Incremental View Maintenance for TanStack DB based on Differential Dataflow
79 lines (78 loc) • 3.14 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 _by, _values;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const graph = require("../graph.cjs");
const d2 = require("../d2.cjs");
const utils = require("../utils.cjs");
const multiset = require("../multiset.cjs");
class DistinctOperator extends graph.UnaryOperator {
// keeps track of the number of times each value has been seen
constructor(id, input, output, by = (value) => value) {
super(id, input, output);
__privateAdd(this, _by);
__privateAdd(this, _values);
__privateSet(this, _by, by);
__privateSet(this, _values, /* @__PURE__ */ new Map());
}
run() {
var _a;
const updatedValues = /* @__PURE__ */ new Map();
for (const message of this.inputMessages()) {
for (const [value, diff] of message.getInner()) {
const hashedValue = utils.hash(__privateGet(this, _by).call(this, value));
const oldMultiplicity = ((_a = updatedValues.get(hashedValue)) == null ? void 0 : _a[0]) ?? __privateGet(this, _values).get(hashedValue) ?? 0;
const newMultiplicity = oldMultiplicity + diff;
updatedValues.set(hashedValue, [newMultiplicity, value]);
}
}
const result = [];
for (const [
hashedValue,
[newMultiplicity, value]
] of updatedValues.entries()) {
const oldMultiplicity = __privateGet(this, _values).get(hashedValue) ?? 0;
if (newMultiplicity === 0) {
__privateGet(this, _values).delete(hashedValue);
} else {
__privateGet(this, _values).set(hashedValue, newMultiplicity);
}
if (oldMultiplicity <= 0 && newMultiplicity > 0) {
result.push([value, 1]);
} else if (oldMultiplicity > 0 && newMultiplicity <= 0) {
result.push([value, -1]);
}
}
if (result.length > 0) {
this.output.sendData(new multiset.MultiSet(result));
}
}
}
_by = new WeakMap();
_values = new WeakMap();
function distinct(by = (value) => value) {
return (stream) => {
const output = new d2.StreamBuilder(
stream.graph,
new graph.DifferenceStreamWriter()
);
const operator = new DistinctOperator(
stream.graph.getNextOperatorId(),
stream.connectReader(),
output.writer,
by
);
stream.graph.addOperator(operator);
stream.graph.addStream(output.connectReader());
return output;
};
}
exports.DistinctOperator = DistinctOperator;
exports.distinct = distinct;
//# sourceMappingURL=distinct.cjs.map