@tanstack/db-ivm
Version:
Incremental View Maintenance for TanStack DB based on Differential Dataflow
106 lines (105 loc) • 2.85 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
class DefaultMap extends Map {
constructor(defaultValue, entries) {
super(entries);
this.defaultValue = defaultValue;
}
get(key) {
if (!this.has(key)) {
return this.defaultValue();
}
return super.get(key);
}
/**
* Update the value for a key using a function.
*/
update(key, updater) {
const value = this.get(key);
const newValue = updater(value);
this.set(key, newValue);
return newValue;
}
}
const chunkSize = 3e4;
function chunkedArrayPush(array, other) {
if (other.length <= chunkSize) {
array.push(...other);
} else {
for (let i = 0; i < other.length; i += chunkSize) {
const chunk = other.slice(i, i + chunkSize);
array.push(...chunk);
}
}
}
function binarySearch(array, value, comparator) {
let low = 0;
let high = array.length;
while (low < high) {
const mid = Math.floor((low + high) / 2);
const comparison = comparator(array[mid], value);
if (comparison < 0) {
low = mid + 1;
} else if (comparison > 0) {
high = mid;
} else {
return mid;
}
}
return low;
}
class ObjectIdGenerator {
constructor() {
this.objectIds = /* @__PURE__ */ new WeakMap();
this.nextId = 0;
}
/**
* Get a unique identifier for any value.
* - Objects: Uses WeakMap for reference-based identity
* - Primitives: Uses consistent string-based hashing
*/
getId(value) {
if (typeof value !== `object` || value === null) {
const str = String(value);
let hashValue = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hashValue = (hashValue << 5) - hashValue + char;
hashValue = hashValue & hashValue;
}
return hashValue;
}
if (!this.objectIds.has(value)) {
this.objectIds.set(value, this.nextId++);
}
return this.objectIds.get(value);
}
/**
* Get a string representation of the ID for use in composite keys.
*/
getStringId(value) {
if (value === null) return `null`;
if (value === void 0) return `undefined`;
if (typeof value !== `object`) return `str_${String(value)}`;
return `obj_${this.getId(value)}`;
}
}
const globalObjectIdGenerator = new ObjectIdGenerator();
function* concatIterable(...iterables) {
for (const iterable of iterables) {
yield* iterable;
}
}
function* mapIterable(it, fn) {
for (const t of it) {
yield fn(t);
}
}
exports.DefaultMap = DefaultMap;
exports.ObjectIdGenerator = ObjectIdGenerator;
exports.binarySearch = binarySearch;
exports.chunkedArrayPush = chunkedArrayPush;
exports.concatIterable = concatIterable;
exports.globalObjectIdGenerator = globalObjectIdGenerator;
exports.mapIterable = mapIterable;
//# sourceMappingURL=utils.cjs.map
;