/* file : vector-pipeline.ts MIT License Copyright (c) 2018-2020 Thomas Minier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 'use strict' import { PipelineInput, StreamPipelineInput, PipelineStage, PipelineEngine } from './pipeline-engine' import { chunk, flatMap, flatten, isUndefined, slice, uniq, uniqBy } from 'lodash' /** * A PipelineStage which materializes all intermediate results in main memory. * @author Thomas Minier */ export class VectorStage implements PipelineStage { // We need to use Promise to store the stage content, // as some computations can require asynchronous computations. // For example, the RDF graph can send HTTP requests to evaluate triple patterns. private readonly _content: Promise> constructor (content: Promise>) { this._content = content } getContent (): Promise> { return this._content } subscribe (onData: (value: T) => void, onError: (err: any) => void, onEnd: () => void): void { try { this._content .then(c => { c.forEach(onData) onEnd() }) .catch(onError) } catch (e) { onError(e) } } forEach (cb: (value: T) => void): void { this._content .then(c => { c.forEach(cb) }) .catch(err => { throw err }) } } export class VectorStreamInput implements StreamPipelineInput { private readonly _resolve: (value: T[]) => void private readonly _reject: (err: any) => void private _content: Array constructor (resolve: any, reject: any) { this._resolve = resolve this._reject = reject this._content = [] } next (value: T): void { this._content.push(value) } error (err: any): void { this._reject(err) } complete (): void { this._resolve(this._content) } } /** * A pipeline implemented using {@link VectorStage}, *i.e.*, all intermediate results are materialized in main memory. This approach is often called **vectorized approach**. * This pipeline is more efficient CPU-wise than {@link RxjsPipeline}, but it also consumes much more memory, as it materializes evey stage of the pipeline before moving to the next. * It should only be used when SPARQL queries generate few intermediate results. * @see P. A. Boncz, S. Manegold, and M. L. Kersten. "Database architecture evolution: Mammals flourished long before dinosaurs became extinct". PVLDB, (2009) * @author Thomas Minier */ export default class VectorPipeline extends PipelineEngine { empty (): VectorStage { return new VectorStage(Promise.resolve([])) } of (...values: T[]): VectorStage { return new VectorStage(Promise.resolve(values)) } from (x: PipelineInput): VectorStage { if ('getContent' in x) { return new VectorStage((x as VectorStage).getContent()) } else if (Array.isArray(x)) { return new VectorStage(Promise.resolve(x)) } else if ('then' in x) { return new VectorStage((x as Promise).then(v => [v])) } else if (Symbol.iterator in x) { return new VectorStage(Promise.resolve(Array.from(x as Iterable))) } throw new Error('Invalid argument for VectorPipeline.from: ' + x) } fromAsync (cb: (input: StreamPipelineInput) => void): VectorStage { return new VectorStage(new Promise((resolve, reject) => { cb(new VectorStreamInput(resolve, reject)) })) } clone (stage: VectorStage): VectorStage { return new VectorStage(stage.getContent().then(c => c.slice(0))) } catch (input: VectorStage, handler?: (err: Error) => VectorStage): VectorStage { return new VectorStage(new Promise((resolve, reject) => { input.getContent() .then(c => resolve(c.slice(0))) .catch(err => { if (handler === undefined) { reject(err) } else { handler(err).getContent() .then(c => resolve(c.slice(0))) .catch(err => { throw err }) } }) })) } merge (...inputs: Array>): VectorStage { return new VectorStage(Promise.all(inputs.map(i => i.getContent())).then((contents: T[][]) => { return flatten(contents) })) } map (input: VectorStage, mapper: (value: F) => T): VectorStage { return new VectorStage(input.getContent().then(c => c.map(mapper))) } flatMap (input: VectorStage, mapper: (value: F) => T[]): VectorStage { return new VectorStage(input.getContent().then(c => flatMap(c, mapper))) } mergeMap (input: VectorStage, mapper: (value: F) => VectorStage): VectorStage { return new VectorStage(input.getContent().then(content => { const stages: VectorStage[] = content.map(value => mapper(value)) return Promise.all(stages.map(s => s.getContent())).then((contents: T[][]) => { return flatten(contents) }) })) } filter (input: VectorStage, predicate: (value: T) => boolean): VectorStage { return new VectorStage(input.getContent().then(c => c.filter(predicate))) } finalize (input: VectorStage, callback: () => void): VectorStage { return new VectorStage(input.getContent().then(c => { callback() return c })) } reduce (input: VectorStage, reducer: (acc: T, value: F) => T, initial: T): VectorStage { return new VectorStage(input.getContent().then(c => [c.reduce(reducer, initial)])) } limit (input: VectorStage, stopAfter: number): VectorStage { return new VectorStage(input.getContent().then(c => slice(c, 0, stopAfter))) } skip (input: VectorStage, toSkip: number): VectorStage { return new VectorStage(input.getContent().then(c => slice(c, toSkip))) } defaultValues (input: VectorStage, ...values: T[]): VectorStage { return new VectorStage(input.getContent().then(content => { if (content.length > 0) { return content.slice(0) } return values })) } bufferCount (input: VectorStage, count: number): VectorStage { return new VectorStage(input.getContent().then(c => chunk(c, count))) } forEach (input: VectorStage, cb: (value: T) => void): void { input.forEach(cb) } first (input: VectorStage): VectorStage { return new VectorStage(input.getContent().then(content => { if (content.length < 1) { return [] } return [content[0]] })) } collect (input: VectorStage): VectorStage { return new VectorStage(input.getContent().then(c => [c])) } }