@jahed/sparql-engine
Version:
SPARQL query engine for servers and web browsers.
37 lines • 1.76 kB
JavaScript
// SPDX-License-Identifier: MIT
import ExecutionContext from "../../engine/context/execution-context.js";
import { Pipeline } from "../../engine/pipeline/pipeline.js";
import { BindingBase, Bindings } from "../../rdf/bindings.js";
import Graph from "../../rdf/graph.js";
import { isVariable } from "../../utils/rdf.js";
/**
* Perform a join between a source of solution bindings (left relation)
* and a triple pattern (right relation) using the Index Nested Loop Join algorithm.
* This algorithm is more efficient if the cardinality of the left relation is smaller
* than the cardinality of the right one.
* @param source - Left input (a {@link PipelineStage})
* @param pattern - Triple pattern to join with (right relation)
* @param graph - RDF Graph on which the join is performed
* @param context - Execution context
* @return A {@link PipelineStage} which evaluate the join
*/
export default function indexJoin(source, pattern, graph, context) {
const engine = Pipeline.getInstance();
return engine.mergeMap(source, (bindings) => {
const boundedPattern = bindings.bound(pattern);
return engine.map(engine.from(graph.find(boundedPattern, context)), (item) => {
const obj = {};
if (isVariable(boundedPattern.subject)) {
obj[boundedPattern.subject.value] = item.subject;
}
if (isVariable(boundedPattern.predicate)) {
obj[boundedPattern.predicate.value] = item.predicate;
}
if (isVariable(boundedPattern.object)) {
obj[boundedPattern.object.value] = item.object;
}
return BindingBase.fromObject(obj).union(bindings);
});
});
}
//# sourceMappingURL=index-join.js.map