import type { Algebra } from '@comunica/utils-algebra'; import type * as RDF from '@rdfjs/types'; import type { AsyncIterator } from 'asynciterator'; import type { BindingsStream } from './Bindings'; import type { IActionContext } from './IActionContext'; import type { QueryAlgebraContext, QueryStringContext } from './IQueryContext'; import type { IQueryExplained, QueryEnhanced, QueryExplainMode } from './IQueryOperationResult'; import type { QuerySourceUnidentified } from './IQuerySource'; export type QueryFormatType = string | Algebra.Operation; export type SourceType = QuerySourceUnidentified; export type QueryType = QueryEnhanced & { context?: IActionContext; }; /** * Quads to insert into and delete from a destination, and graphs to delete and create in it. */ export interface IQuadUpdate { /** * An optional stream of quads to insert. */ quadStreamInsert?: AsyncIterator; /** * An optional stream of quads to delete. */ quadStreamDelete?: AsyncIterator; /** * An optional deletion of graphs. */ deleteGraphs?: { /** * The graph(s) in which all triples must be removed. */ graphs: RDF.DefaultGraph | 'NAMED' | 'ALL' | RDF.NamedNode[]; /** * If true, and the graph does not exist, an error must be emitted. * * Should only be considered on destinations that record empty graphs. */ requireExistence: boolean; /** * If the graph itself should also be dropped. * Should not happen on the 'DEFAULT' graph. * * Should only be considered on destinations that record empty graphs. */ dropGraphs: boolean; }; /** * An optional creation of (empty) graphs. */ createGraphs?: { /** * The graph names to create. */ graphs: RDF.NamedNode[]; /** * If true, an error MUST be thrown when the graph already exists. * * For destinations that do not record empty graphs, * this should only throw if at least one quad with the given quad already exists. */ requireNonExistence: boolean; }; } /** * Options for parsing a stream of RDF. */ export interface IRdfParseOptions { /** * The content type of the stream. */ contentType: string; /** * An optional base IRI of the stream's document. */ baseIRI?: string; /** * The version to consider as media type parameter. */ version?: string; } /** * Options for warming up a query engine. */ export interface IWarmupOptions { /** * The maximum number of query executions. */ executions?: number; /** * The maximum warmup duration in milliseconds. */ budget?: number; /** * A signal to stop early, after which no new query executions start. */ signal?: AbortSignal; } /** * Options for warming up a query engine, with at least one of the options that stop the warm-up. * About 2000 executions suit sources in memory or in files. * Over remote sources, every execution accesses the network, * so fewer executions or a budget suit them, depending on the type of source. */ export type IWarmupOptionsBounded = IWarmupOptions & ({ executions: number; } | { budget: number; } | { signal: AbortSignal; }); /** * The outcome of warming up a query engine. */ export interface IWarmupResult { /** * The number of query executions. */ executions: number; } /** * Base interface for a Comunica query engine. */ export interface IQueryEngine extends RDF.StringQueryable, RDF.AlgebraQueryable, RDF.StringSparqlQueryable, RDF.AlgebraSparqlQueryable { /** * Query the bindings results of a SELECT query. * @param query A query string or algebra object. * Algebra objects must not contain blank nodes. They should be converted to variables. * @param context A context. */ queryBindings: (query: QueryFormatTypeInner, context?: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner) => Promise; /** * Query the quad results of a CONSTRUCT or DESCRIBE query. * @param query A query string or algebra object. * Algebra objects must not contain blank nodes. They should be converted to variables. * @param context A context. */ queryQuads: (query: QueryFormatTypeInner, context?: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner) => Promise & RDF.ResultStream>; /** * Query the boolean result of an ASK query. * @param query A query string or algebra object. * Algebra objects must not contain blank nodes. They should be converted to variables. * @param context A context. */ queryBoolean: (query: QueryFormatTypeInner, context?: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner) => Promise; /** * Execute an UPDATE query. * @param query A query string or algebra object. * Algebra objects must not contain blank nodes. They should be converted to variables. * @param context A context. */ queryVoid: (query: QueryFormatTypeInner, context?: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner) => Promise; /** * Initiate a given query. * This will produce a future to a query result, which has to be executed to obtain the query results. * This can reject given an unsupported or invalid query. * * This method is prefered in case you don't know beforehand what type of query will be executed, * or if you require access to the metadata of the results. * * @param query A query string or algebra object. * Algebra objects must not contain blank nodes. They should be converted to variables. * @param context A context. */ query: (query: QueryFormatTypeInner, context?: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner) => Promise; /** * Explain the given query * @param {string | Algebra.Operation} query A query string or algebra. * Algebra objects must not contain blank nodes. * They should be converted to variables. * @param context A query context. * @param explainMode The explain mode. * @return {Promise} * A promise that resolves to the query output. */ explain: (query: QueryFormatTypeInner, context: QueryFormatTypeInner extends string ? QueryStringContextInner : QueryAlgebraContextInner, explainMode: QueryExplainMode) => Promise; /** * Update the destination with the given quads and graphs, without serializing them into an update query. * @param update The quads to insert and delete, and the graphs to delete and create. * @param context A context, which determines the destination in the same way as for update queries. * @return {Promise} A promise that resolves when the destination has been updated. */ updateQuads?: (update: IQuadUpdate, context?: Partial) => Promise; /** * Parse a stream of RDF with the parsers of this engine. * @param stream A text stream in an RDF serialization. * @param options The content type of the stream, and optional parsing and context options. * @return A promise resolving to a stream of the parsed quads. */ parseRdf?: (stream: NodeJS.ReadableStream, options: IRdfParseOptions & Partial) => Promise; /** * Warm up this engine by executing queries over the sources in the given context, * so that the just-in-time compiler has optimized the engine before it handles actual queries. * The warm-up does not modify the sources. * @param context A context, which determines the sources in the same way as for queries. * @param options The maximum number of query executions and duration, and a signal to stop early. * At least one of them is required. * @return A promise resolving to the number of query executions. */ warmup?: (context: QueryStringContextInner, options: IWarmupOptionsBounded) => Promise; /** * @param context An optional context. * @return {Promise<{[p: string]: number}>} All available SPARQL (weighted) result media types. */ getResultMediaTypes: (context?: IActionContext) => Promise>; /** * @param context An optional context. * @return {Promise<{[p: string]: number}>} All available SPARQL result media type formats. */ getResultMediaTypeFormats: (context?: IActionContext) => Promise>; /** * Convert a query result to a string stream based on a certain media type. * @param {QueryType} queryResult A query result. * @param {string} mediaType A media type. * @param {IActionContext} context An optional context. * @return {Promise} A text stream. */ resultToString: (queryResult: QueryType, mediaType?: string, context?: any) => any; /** * Invalidate all internal caches related to the given page URL. * If no page URL is given, then all pages will be invalidated. * @param {string} url The page URL to invalidate. * @return {Promise} A promise resolving when the caches have been invalidated. */ invalidateHttpCache: (url?: string) => Promise; }