Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { asError } from "../../common/helpers-pure";

/**
* A cached mapping from strings to value of type U.
* A cached mapping from strings to a value of type U.
*/
export class CachedOperation<U> {
private readonly operation: (t: string, ...args: any[]) => Promise<U>;
export class CachedOperation<S extends unknown[], U> {
private readonly operation: (t: string, ...args: S) => Promise<U>;
private readonly cached: Map<string, U>;
private readonly lru: string[];
private readonly inProgressCallbacks: Map<
string,
Array<[(u: U) => void, (reason?: any) => void]>
Array<[(u: U) => void, (reason?: Error) => void]>
>;

constructor(
operation: (t: string, ...args: any[]) => Promise<U>,
operation: (t: string, ...args: S) => Promise<U>,
private cacheSize = 100,
) {
this.operation = operation;
this.lru = [];
this.inProgressCallbacks = new Map<
string,
Array<[(u: U) => void, (reason?: any) => void]>
Array<[(u: U) => void, (reason?: Error) => void]>
>();
this.cached = new Map<string, U>();
}

async get(t: string, ...args: any[]): Promise<U> {
async get(t: string, ...args: S): Promise<U> {
// Try and retrieve from the cache
const fromCache = this.cached.get(t);
if (fromCache !== undefined) {
Expand All @@ -46,7 +48,7 @@ export class CachedOperation<U> {
}

// Otherwise compute the new value, but leave a callback to allow sharing work
const callbacks: Array<[(u: U) => void, (reason?: any) => void]> = [];
const callbacks: Array<[(u: U) => void, (reason?: Error) => void]> = [];
this.inProgressCallbacks.set(t, callbacks);
try {
const result = await this.operation(t, ...args);
Expand All @@ -61,7 +63,7 @@ export class CachedOperation<U> {
return result;
} catch (e) {
// Rethrow error on all callbacks
callbacks.forEach((f) => f[1](e));
callbacks.forEach((f) => f[1](asError(e)));
throw e;
} finally {
this.inProgressCallbacks.delete(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ import { MultiCancellationToken } from "../../common/vscode/multi-cancellation-t
*/

export class TemplateQueryDefinitionProvider implements DefinitionProvider {
private cache: CachedOperation<LocationLink[]>;
private cache: CachedOperation<[CancellationToken], LocationLink[]>;

constructor(
private cli: CodeQLCliServer,
private qs: QueryRunner,
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<LocationLink[]>(
this.getDefinitions.bind(this),
);
this.cache = new CachedOperation(this.getDefinitions.bind(this));
}

async provideDefinition(
Expand Down Expand Up @@ -112,17 +110,15 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
* or from a selected identifier.
*/
export class TemplateQueryReferenceProvider implements ReferenceProvider {
private cache: CachedOperation<FullLocationLink[]>;
private cache: CachedOperation<[CancellationToken], FullLocationLink[]>;

constructor(
private cli: CodeQLCliServer,
private qs: QueryRunner,
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<FullLocationLink[]>(
this.getReferences.bind(this),
);
this.cache = new CachedOperation(this.getReferences.bind(this));
}

async provideReferences(
Expand Down Expand Up @@ -185,17 +181,18 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
* source-language files.
*/
export class TemplatePrintAstProvider {
private cache: CachedOperation<CoreCompletedQuery>;
private cache: CachedOperation<
[ProgressCallback, CancellationToken],
CoreCompletedQuery
>;

constructor(
private cli: CodeQLCliServer,
private qs: QueryRunner,
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<CoreCompletedQuery>(
this.getAst.bind(this),
);
this.cache = new CachedOperation(this.getAst.bind(this));
}

async provideAst(
Expand Down Expand Up @@ -283,15 +280,16 @@ export class TemplatePrintAstProvider {
* source-language files.
*/
export class TemplatePrintCfgProvider {
private cache: CachedOperation<[Uri, Record<string, string>] | undefined>;
private cache: CachedOperation<
[number, number],
[Uri, Record<string, string>]
>;

constructor(
private cli: CodeQLCliServer,
private dbm: DatabaseManager,
) {
this.cache = new CachedOperation<[Uri, Record<string, string>] | undefined>(
this.getCfgUri.bind(this),
);
this.cache = new CachedOperation(this.getCfgUri.bind(this));
}

async provideCfgUri(
Expand Down