UNPKG

@jahed/sparql-engine

Version:

SPARQL query engine for servers and web browsers.

42 lines 1.62 kB
// SPDX-License-Identifier: MIT import { termToString } from "rdf-string"; import ExecutionContext from "../engine/context/execution-context.js"; import { Pipeline } from "../engine/pipeline/pipeline.js"; import { RDF } from "../utils/rdf.js"; import Graph from "./graph.js"; /** * An UnionGraph represents the dynamic union of several graphs. * Addition only affects the left-most operand, deletion affects all graphs. * Searching for RDF triple smatching a triple pattern in such Graph is equivalent * as the Union of matching RDF triples in all graphs. * @extends Graph */ export default class UnionGraph extends Graph { _graphs; // Public for tests. /** * Constructor * @param graphs - Set of RDF graphs */ constructor(graphs) { super(RDF.namedNode(graphs.map((g) => termToString(g.iri)).join("+"))); this._graphs = graphs; } insert(triple) { return this._graphs[0].insert(triple); } delete(triple) { return this._graphs.reduce((prev, g) => prev.then(() => g.delete(triple)), Promise.resolve()); } find(triple, context) { return Pipeline.getInstance().merge(...this._graphs.map((g) => g.find(triple, context))); } clear() { return this._graphs.reduce((prev, g) => prev.then(() => g.clear()), Promise.resolve()); } estimateCardinality(triple) { return Promise.all(this._graphs.map((g) => g.estimateCardinality(triple))).then((cardinalities) => { return Promise.resolve(cardinalities.reduce((acc, x) => acc + x, 0)); }); } } //# sourceMappingURL=union-graph.js.map