@tanstack/db-ivm
Version:
Incremental View Maintenance for TanStack DB based on Differential Dataflow
116 lines (115 loc) • 3.09 kB
JavaScript
import { topK, topKWithIndex } from "./topK.js";
import { topKWithFractionalIndex } from "./topKWithFractionalIndex.js";
import { map } from "./map.js";
import { innerJoin } from "./join.js";
import { consolidate } from "./consolidate.js";
function orderBy(valueExtractor, options) {
const limit = (options == null ? void 0 : options.limit) ?? Infinity;
const offset = (options == null ? void 0 : options.offset) ?? 0;
const comparator = (options == null ? void 0 : options.comparator) ?? ((a, b) => {
if (a === b) return 0;
if (a < b) return -1;
return 1;
});
return (stream) => {
return stream.pipe(
map(
([key, value]) => [
null,
[
key,
valueExtractor(
value
)
]
]
),
topK((a, b) => comparator(a[1], b[1]), { limit, offset }),
map(([_, [key]]) => [key, null]),
innerJoin(stream),
map(([key, value]) => {
return [key, value[1]];
}),
consolidate()
);
};
}
function orderByWithIndex(valueExtractor, options) {
const limit = (options == null ? void 0 : options.limit) ?? Infinity;
const offset = (options == null ? void 0 : options.offset) ?? 0;
const comparator = (options == null ? void 0 : options.comparator) ?? ((a, b) => {
if (a === b) return 0;
if (a < b) return -1;
return 1;
});
return (stream) => {
return stream.pipe(
map(
([key, value]) => [
null,
[
key,
valueExtractor(
value
)
]
]
),
topKWithIndex((a, b) => comparator(a[1], b[1]), { limit, offset }),
map(([_, [[key], index]]) => [key, index]),
innerJoin(stream),
map(([key, [index, value]]) => {
return [key, [value, index]];
}),
consolidate()
);
};
}
function orderByWithFractionalIndexBase(topKFunction, valueExtractor, options) {
const limit = (options == null ? void 0 : options.limit) ?? Infinity;
const offset = (options == null ? void 0 : options.offset) ?? 0;
const comparator = (options == null ? void 0 : options.comparator) ?? ((a, b) => {
if (a === b) return 0;
if (a < b) return -1;
return 1;
});
return (stream) => {
return stream.pipe(
map(
([key, value]) => [
null,
[
key,
valueExtractor(
value
)
]
]
),
topKFunction((a, b) => comparator(a[1], b[1]), {
limit,
offset
}),
map(([_, [[key], index]]) => [key, index]),
innerJoin(stream),
map(([key, [index, value]]) => {
return [key, [value, index]];
}),
consolidate()
);
};
}
function orderByWithFractionalIndex(valueExtractor, options) {
return orderByWithFractionalIndexBase(
topKWithFractionalIndex,
valueExtractor,
options
);
}
export {
orderBy,
orderByWithFractionalIndex,
orderByWithFractionalIndexBase,
orderByWithIndex
};
//# sourceMappingURL=orderBy.js.map