Skip to content

Commit

Permalink
refactor(rstream-query): simplify .findTriple(), move xforms, update …
Browse files Browse the repository at this point in the history
…deps
  • Loading branch information
postspectacular committed Aug 18, 2019
1 parent 071c346 commit c6a40df
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 96 deletions.
3 changes: 2 additions & 1 deletion packages/rstream-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@thi.ng/checks": "^2.3.0",
"@thi.ng/equiv": "^1.0.9",
"@thi.ng/errors": "^1.1.2",
"@thi.ng/math": "^1.4.2",
"@thi.ng/rstream": "^2.5.3",
"@thi.ng/rstream-dot": "^1.1.3",
"@thi.ng/transducers": "^5.4.3"
Expand All @@ -60,4 +61,4 @@
"access": "public"
},
"sideEffects": false
}
}
108 changes: 13 additions & 95 deletions packages/rstream-query/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { assert, IObjectOf } from "@thi.ng/api";
import { intersection, join } from "@thi.ng/associative";
import { join } from "@thi.ng/associative";
import { equiv } from "@thi.ng/equiv";
import { illegalArgs } from "@thi.ng/errors";
import { min3id } from "@thi.ng/math";
import {
ISubscribable,
nextID,
Expand All @@ -18,19 +19,14 @@ import {
import {
assocObj,
comp,
compR,
dedupe,
keySelector,
map,
mapIndexed,
Reducer,
transduce,
Transducer
} from "@thi.ng/transducers";
import {
BindFn,
Edit,
LOGGER,
PathPattern,
PathQuerySpec,
Pattern,
Expand All @@ -45,6 +41,16 @@ import {
} from "./api";
import { patternVars, resolvePathPattern } from "./pattern";
import { isQVar, qvarResolver } from "./qvar";
import {
bindVars,
filterSolutions,
indexSel,
intersect2,
intersect3,
joinSolutions,
limitSolutions,
resultTriples
} from "./xforms";

export class TripleStore implements Iterable<Triple>, IToDot {
NEXT_ID: number;
Expand Down Expand Up @@ -424,18 +430,7 @@ export class TripleStore implements Iterable<Triple>, IToDot {
) {
if (s && p && o) {
const triples = this.triples;
const index =
s.size < p.size
? s.size < o.size
? s
: p.size < o.size
? p
: o
: p.size < o.size
? p
: s.size < o.size
? s
: o;
const index = [s, p, o][min3id(s.size, p.size, o.size)];
for (let id of index) {
if (equiv(triples[id], f)) {
return id;
Expand Down Expand Up @@ -482,83 +477,6 @@ const submit = (
}
};

const intersect2: Transducer<IObjectOf<TripleIds>, TripleIds> = comp(
map(({ a, b }) => intersection(a, b)),
dedupe(equiv)
);

const intersect3: Transducer<IObjectOf<TripleIds>, TripleIds> = comp(
map(({ s, p, o }) => intersection(intersection(s, p), o)),
dedupe(equiv)
);

const indexSel = (key: any): Transducer<Edit, TripleIds> => (
rfn: Reducer<any, TripleIds>
) => {
const r = rfn[2];
return compR(rfn, (acc, e) => {
LOGGER.fine("index sel", e.key, key);
if (equiv(e.key, key)) {
return r(acc, e.index);
}
return acc;
});
};

const resultTriples = (graph: TripleStore) =>
map<TripleIds, Set<Triple>>((ids) => {
const res = new Set<Triple>();
for (let id of ids) res.add(graph.triples[id]);
return res;
});

const joinSolutions = (n: number) =>
map<IObjectOf<Solutions>, Solutions>((src) => {
let res: Solutions = src[0];
for (let i = 1; i < n && res.size; i++) {
res = join(res, src[i]);
}
return res;
});

const filterSolutions = (qvars: Iterable<string>) => {
const filterVars = keySelector([...qvars]);
return map((sol: Solutions) => {
const res: Solutions = new Set();
for (let s of sol) {
res.add(filterVars(s));
}
return res;
});
};

const limitSolutions = (n: number) =>
map((sol: Solutions) => {
if (sol.size <= n) {
return sol;
}
const res: Solutions = new Set();
let m = n;
for (let s of sol) {
res.add(s);
if (--m <= 0) break;
}
return res;
});

const bindVars = (bindings: IObjectOf<BindFn>) =>
map((sol: Solutions) => {
const res: Solutions = new Set();
for (let s of sol) {
s = { ...s };
res.add(s);
for (let b in bindings) {
s[b] = bindings[b](s);
}
}
return res;
});

const isWhereQuery = (q: SubQuerySpec): q is WhereQuerySpec => !!(<any>q).where;

const isPathQuery = (q: SubQuerySpec): q is PathQuerySpec => !!(<any>q).path;
98 changes: 98 additions & 0 deletions packages/rstream-query/src/xforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { IObjectOf } from "@thi.ng/api";
import { intersection, join } from "@thi.ng/associative";
import { equiv } from "@thi.ng/equiv";
import { LOGGER } from "@thi.ng/rstream";
import {
comp,
compR,
dedupe,
keySelector,
map,
Reducer,
Transducer
} from "@thi.ng/transducers";
import {
BindFn,
Edit,
Solutions,
Triple,
TripleIds
} from "./api";
import { TripleStore } from "./store";

export const intersect2: Transducer<IObjectOf<TripleIds>, TripleIds> = comp(
map(({ a, b }) => intersection(a, b)),
dedupe(equiv)
);

export const intersect3: Transducer<IObjectOf<TripleIds>, TripleIds> = comp(
map(({ s, p, o }) => intersection(intersection(s, p), o)),
dedupe(equiv)
);

export const indexSel = (key: any): Transducer<Edit, TripleIds> => (
rfn: Reducer<any, TripleIds>
) => {
const r = rfn[2];
return compR(rfn, (acc, e) => {
LOGGER.fine("index sel", e.key, key);
if (equiv(e.key, key)) {
return r(acc, e.index);
}
return acc;
});
};

export const resultTriples = (graph: TripleStore) =>
map<TripleIds, Set<Triple>>((ids) => {
const res = new Set<Triple>();
for (let id of ids) res.add(graph.triples[id]);
return res;
});

export const joinSolutions = (n: number) =>
map<IObjectOf<Solutions>, Solutions>((src) => {
let res: Solutions = src[0];
for (let i = 1; i < n && res.size; i++) {
res = join(res, src[i]);
}
return res;
});

export const filterSolutions = (qvars: Iterable<string>) => {
const filterVars = keySelector([...qvars]);
return map((sol: Solutions) => {
const res: Solutions = new Set();
for (let s of sol) {
res.add(filterVars(s));
}
return res;
});
};

export const limitSolutions = (n: number) =>
map((sol: Solutions) => {
if (sol.size <= n) {
return sol;
}
const res: Solutions = new Set();
let m = n;
for (let s of sol) {
res.add(s);
if (--m <= 0) break;
}
return res;
});

export const bindVars = (bindings: IObjectOf<BindFn>) =>
map((sol: Solutions) => {
const res: Solutions = new Set();
for (let s of sol) {
s = { ...s };
res.add(s);
for (let b in bindings) {
s[b] = bindings[b](s);
}
}
return res;
});

0 comments on commit c6a40df

Please sign in to comment.