Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async runOperation(pattern: Algebra.OrderBy, context: ActionContext)
: Promise {
const outputRaw = await this.mediatorQueryOperation.mediate({ operation: pattern.input, context });
const output = ActorQueryOperation.getSafeBindings(outputRaw);
const options = { window: this.window };
const sparqleeConfig = { ...ActorQueryOperation.getExpressionContext(context) };
let bindingsStream = output.bindingsStream;
for (let expr of pattern.expressions) {
const isAscending = this.isAscending(expr);
expr = this.extractSortExpression(expr);
// Transform the stream by annotating it with the expr result
const evaluator = new AsyncEvaluator(expr, sparqleeConfig);
interface IAnnotatedBinding { bindings: Bindings; result: Term; }
const transform = async (bindings: Bindings, next: any) => {
try {
const result = await evaluator.evaluate(bindings);
transformedStream._push({ bindings, result });
} catch (err) {
if (!isExpressionError(err)) {
bindingsStream.emit('error', err);
}
transformedStream._push({ bindings, result: undefined });
}
next();
};
const transformedStream = bindingsStream.transform({ transform });
// Sort the annoted stream
const transform = async (bindings: Bindings, next: any) => {
try {
const result = await evaluator.evaluate(bindings);
// Extend operation is undefined when the key already exists
// We just override it here.
const extended = bindings.set(extendKey, result);
bindingsStream._push(extended);
} catch (err) {
if (isExpressionError(err)) {
// Errors silently don't actually extend according to the spec
bindingsStream._push(bindings);
// But let's warn anyway
this.logWarn(context, `Expression error for extend operation with bindings '${JSON.stringify(bindings)}'`);
} else {
bindingsStream.emit('error', err);
}
}
next();
};
public async testOperation(pattern: Algebra.Filter, context: ActionContext): Promise {
// Will throw error for unsupported operators
const config = { exists: this.createExistenceResolver(context) };
const _ = new AsyncEvaluator(pattern.expression, config);
return true;
}
public async runOperation(pattern: Algebra.Extend, context: ActionContext)
: Promise {
const { expression, input, variable } = pattern;
const output: IActorQueryOperationOutputBindings = ActorQueryOperation.getSafeBindings(
await this.mediatorQueryOperation.mediate({ operation: input, context }));
const extendKey = termToString(variable);
const config = { ...ActorQueryOperation.getExpressionContext(context) };
const evaluator = new AsyncEvaluator(expression, config);
// Transform the stream by extending each Bindings with the expression result
const transform = async (bindings: Bindings, next: any) => {
try {
const result = await evaluator.evaluate(bindings);
// Extend operation is undefined when the key already exists
// We just override it here.
const extended = bindings.set(extendKey, result);
bindingsStream._push(extended);
} catch (err) {
if (isExpressionError(err)) {
// Errors silently don't actually extend according to the spec
bindingsStream._push(bindings);
// But let's warn anyway
this.logWarn(context, `Expression error for extend operation with bindings '${JSON.stringify(bindings)}'`);
} else {
public async testOperation(pattern: Algebra.Extend, context: ActionContext): Promise {
// Will throw error for unsupported opperations
const _ = !!new AsyncEvaluator(pattern.expression);
return true;
}
public async testOperation(pattern: Algebra.OrderBy, context: ActionContext): Promise {
// Will throw error for unsupported operators
for (let expr of pattern.expressions) {
expr = this.extractSortExpression(expr);
const _ = new AsyncEvaluator(expr);
}
return true;
}
public consumeBindings(bindings: Bindings) {
// Select the bindings on which we group
const grouper = bindings
.filter((_, variable) => this.groupVariables.has(variable))
.toMap();
const groupHash = this.hashBindings(grouper);
// First member of group -> create new group
if (!this.groups.has(groupHash)) {
// Initialize state for all aggregators for new group
const aggregators: { [key: string]: AggregateEvaluator } = {};
for (const i in this.pattern.aggregates) {
const aggregate = this.pattern.aggregates[i];
const key = termToString(aggregate.variable);
aggregators[key] = new AggregateEvaluator(aggregate, this.sparqleeConfig);
aggregators[key].put(bindings);
}
const group: IGroup = { aggregators, bindings: grouper };
this.groups.set(groupHash, group);
if (this.distinctHashes) {
const bindingsHash = this.hashBindings(bindings);
this.distinctHashes.set(groupHash, new Set([bindingsHash]));
}
} else {
// Group already exists
// Update all the aggregators with the input binding
const group = this.groups.get(groupHash);
for (const i in this.pattern.aggregates) {
}
}
// Merge grouping bindings and aggregator bindings
return groupBindings.merge(aggBindings);
});
// Case: No Input
// Some aggregators still define an output on the empty input
// Result is a single Bindings
if (rows.length === 0) {
const single: { [key: string]: Term } = {};
for (const i in this.pattern.aggregates) {
const aggregate = this.pattern.aggregates[i];
const key = termToString(aggregate.variable);
const value = AggregateEvaluator.emptyValue(aggregate);
if (value !== undefined) {
single[key] = value;
}
}
rows = [Bindings(single)];
}
return rows;
}
public async testOperation(pattern: Algebra.Group, context: ActionContext): Promise {
for (const i in pattern.aggregates) {
// Will throw for unsupported expressions
const _ = new SyncEvaluator(pattern.aggregates[i].expression);
}
return true;
}
public async runOperation(pattern: Algebra.Filter, context: ActionContext)
: Promise {
const outputRaw = await this.mediatorQueryOperation.mediate({ operation: pattern.input, context });
const output = ActorQueryOperation.getSafeBindings(outputRaw);
ActorQueryOperation.validateQueryOutput(output, 'bindings');
const { variables, metadata } = output;
const expressionContext = ActorQueryOperation.getExpressionContext(context);
const config = {
...expressionContext,
exists: this.createExistenceResolver(context),
};
const evaluator = new AsyncEvaluator(pattern.expression, config);
const transform = async (item: Bindings, next: any) => {
try {
const result = await evaluator.evaluateAsEBV(item);
if (result) {
bindingsStream._push(item);
}
} catch (err) {
if (!isExpressionError(err)) {
bindingsStream.emit('error', err);
}
}
next();
};
const bindingsStream = output.bindingsStream.transform({ transform });