|
| 1 | +import type { MediatorExpressionEvaluatorFactory } from '@comunica/bus-expression-evaluator-factory'; |
| 2 | +import type { IActorQueryOperationTypedMediatedArgs } from '@comunica/bus-query-operation'; |
| 3 | +import { ActorQueryOperationTypedMediated } from '@comunica/bus-query-operation'; |
| 4 | +import type { MediatorTermComparatorFactory } from '@comunica/bus-term-comparator-factory'; |
| 5 | +import type { IActorTest, TestResult } from '@comunica/core'; |
| 6 | +import { passTestVoid } from '@comunica/core'; |
| 7 | +import type { BindingsStream, IActionContext, IQueryOperationResult } from '@comunica/types'; |
| 8 | +import type { Bindings } from '@comunica/utils-bindings-factory'; |
| 9 | +import { bindingsToCompactString } from '@comunica/utils-bindings-factory'; |
| 10 | +import { isExpressionError } from '@comunica/utils-expression-evaluator'; |
| 11 | +import { getSafeBindings } from '@comunica/utils-query-operation'; |
| 12 | +import { KeysBindings } from '@incremunica/context-entries'; |
| 13 | +import type * as RDF from '@rdfjs/types'; |
| 14 | +import type { AsyncIterator } from 'asynciterator'; |
| 15 | +import { Algebra } from 'sparqlalgebrajs'; |
| 16 | +import { IndexedSortTree } from './IndexedSortTree'; |
| 17 | + |
| 18 | +export interface IAnnotatedBinding { |
| 19 | + bindings: Bindings; |
| 20 | + result: (RDF.Term | undefined)[]; |
| 21 | + hash: string; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * An incremunica OrderBy Query Operation Actor. |
| 26 | + */ |
| 27 | +export class ActorQueryOperationOrderBy extends ActorQueryOperationTypedMediated<Algebra.OrderBy> { |
| 28 | + private readonly mediatorExpressionEvaluatorFactory: MediatorExpressionEvaluatorFactory; |
| 29 | + private readonly mediatorTermComparatorFactory: MediatorTermComparatorFactory; |
| 30 | + |
| 31 | + public constructor(args: IActorQueryOperationOrderBySparqleeArgs) { |
| 32 | + super(args, 'orderby'); |
| 33 | + this.mediatorExpressionEvaluatorFactory = args.mediatorExpressionEvaluatorFactory; |
| 34 | + this.mediatorTermComparatorFactory = args.mediatorTermComparatorFactory; |
| 35 | + } |
| 36 | + |
| 37 | + public async testOperation(): Promise<TestResult<IActorTest>> { |
| 38 | + return passTestVoid(); |
| 39 | + } |
| 40 | + |
| 41 | + public async runOperation(operation: Algebra.OrderBy, context: IActionContext): |
| 42 | + Promise<IQueryOperationResult> { |
| 43 | + const outputRaw = await this.mediatorQueryOperation.mediate({ operation: operation.input, context }); |
| 44 | + const output = getSafeBindings(outputRaw); |
| 45 | + const variables = (await output.metadata()).variables.map(v => v.variable); |
| 46 | + |
| 47 | + let bindingsStream = <AsyncIterator<Bindings>><any>output.bindingsStream; |
| 48 | + |
| 49 | + // Sorting backwards since the first one is the most important therefore should be ordered last. |
| 50 | + const orderByEvaluator = await this.mediatorTermComparatorFactory.mediate({ context }); |
| 51 | + |
| 52 | + let annotatedBindingsStream = bindingsStream.map<IAnnotatedBinding>((bindings: Bindings) => |
| 53 | + ({ bindings, result: [], hash: bindingsToCompactString(bindings, variables) })); |
| 54 | + const isAscending = []; |
| 55 | + for (let expr of operation.expressions) { |
| 56 | + isAscending.push(this.isAscending(expr)); |
| 57 | + expr = this.extractSortExpression(expr); |
| 58 | + // Transform the stream by annotating it with the expr result |
| 59 | + const evaluator = await this.mediatorExpressionEvaluatorFactory |
| 60 | + .mediate({ algExpr: expr, context }); |
| 61 | + |
| 62 | + const transform = async( |
| 63 | + annotatedBinding: IAnnotatedBinding, |
| 64 | + next: () => void, |
| 65 | + push: (result: IAnnotatedBinding) => void, |
| 66 | + ): Promise<void> => { |
| 67 | + try { |
| 68 | + const result = await evaluator.evaluate(annotatedBinding.bindings); |
| 69 | + annotatedBinding.result.push(result); |
| 70 | + push(annotatedBinding); |
| 71 | + } catch (error: unknown) { |
| 72 | + // We ignore all Expression errors. |
| 73 | + // Other errors (likely programming mistakes) are still propagated. |
| 74 | + if (!isExpressionError(<Error> error)) { |
| 75 | + bindingsStream.destroy(<Error> error); |
| 76 | + next(); |
| 77 | + return; |
| 78 | + } |
| 79 | + annotatedBinding.result.push(undefined); |
| 80 | + push(annotatedBinding); |
| 81 | + } |
| 82 | + next(); |
| 83 | + }; |
| 84 | + // eslint-disable-next-line ts/no-misused-promises |
| 85 | + annotatedBindingsStream = annotatedBindingsStream.transform<IAnnotatedBinding>({ transform, autoStart: false }); |
| 86 | + } |
| 87 | + |
| 88 | + const index = new IndexedSortTree(orderByEvaluator, isAscending); |
| 89 | + bindingsStream = annotatedBindingsStream.map((annotatedBindings) => { |
| 90 | + try { |
| 91 | + const bindingsOrderData = |
| 92 | + (annotatedBindings.bindings.getContextEntry(KeysBindings.isAddition) ?? true) ? |
| 93 | + index.insert(annotatedBindings).data : |
| 94 | + index.remove(annotatedBindings).data; |
| 95 | + return annotatedBindings.bindings.setContextEntry(KeysBindings.order, bindingsOrderData); |
| 96 | + } catch (error) { |
| 97 | + bindingsStream.destroy(<any>error); |
| 98 | + return null; |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + return { |
| 103 | + type: 'bindings', |
| 104 | + bindingsStream: <BindingsStream><any>bindingsStream, |
| 105 | + metadata: output.metadata, |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + // Remove descending operator if necessary |
| 110 | + private extractSortExpression(expr: Algebra.Expression): Algebra.Expression { |
| 111 | + const { expressionType, operator } = expr; |
| 112 | + if (expressionType !== Algebra.expressionTypes.OPERATOR) { |
| 113 | + return expr; |
| 114 | + } |
| 115 | + return operator === 'desc' ? |
| 116 | + expr.args[0] : |
| 117 | + expr; |
| 118 | + } |
| 119 | + |
| 120 | + private isAscending(expr: Algebra.Expression): boolean { |
| 121 | + const { expressionType, operator } = expr; |
| 122 | + if (expressionType !== Algebra.expressionTypes.OPERATOR) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + return operator !== 'desc'; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export interface IActorQueryOperationOrderBySparqleeArgs extends IActorQueryOperationTypedMediatedArgs { |
| 130 | + mediatorExpressionEvaluatorFactory: MediatorExpressionEvaluatorFactory; |
| 131 | + mediatorTermComparatorFactory: MediatorTermComparatorFactory; |
| 132 | +} |
0 commit comments