UNPKG

@thi.ng/rstream-query

Version:

@thi.ng/rstream based triple store & reactive query engine

80 lines (79 loc) 2 kB
import { intersection } from "@thi.ng/associative/intersection"; import { join } from "@thi.ng/associative/join"; import { equiv } from "@thi.ng/equiv"; import { LOGGER } from "@thi.ng/rstream/logger"; import { comp } from "@thi.ng/transducers/comp"; import { compR } from "@thi.ng/transducers/compr"; import { dedupe } from "@thi.ng/transducers/dedupe"; import { keySelector } from "@thi.ng/transducers/key-selector"; import { map } from "@thi.ng/transducers/map"; const intersect2 = comp( map(({ a, b }) => intersection(a, b)), dedupe(equiv) ); const intersect3 = comp( map(({ s, p, o }) => intersection(intersection(s, p), o)), dedupe(equiv) ); const indexSel = (key) => (rfn) => { const r = rfn[2]; return compR(rfn, (acc, e) => { LOGGER.fine("index sel", e.key, key); return equiv(e.key, key) ? r(acc, e.index) : acc; }); }; const resultTriples = (graph) => map((ids) => { const res = /* @__PURE__ */ new Set(); for (let id of ids) res.add(graph.triples[id]); return res; }); const joinSolutions = (n) => map((src) => { let res = src[0]; for (let i = 1; i < n && res.size; i++) { res = join(res, src[i]); } return res; }); const filterSolutions = (qvars) => { const filterVars = keySelector([...qvars]); return map((sol) => { const res = /* @__PURE__ */ new Set(); for (let s of sol) { res.add(filterVars(s)); } return res; }); }; const limitSolutions = (n) => map((sol) => { if (sol.size <= n) { return sol; } const res = /* @__PURE__ */ new Set(); let m = n; for (let s of sol) { res.add(s); if (--m <= 0) break; } return res; }); const bindVars = (bindings) => map((sol) => { const res = /* @__PURE__ */ new Set(); for (let s of sol) { s = { ...s }; res.add(s); for (let b in bindings) { s[b] = bindings[b](s); } } return res; }); export { bindVars, filterSolutions, indexSel, intersect2, intersect3, joinSolutions, limitSolutions, resultTriples };