Skip to content
Closed
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
4 changes: 4 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@
"command": "codeQL.clearCache",
"title": "CodeQL: Clear Cache"
},
{
"command": "codeQL.trimCache",
"title": "CodeQL: Trim Cache"
},
{
"command": "codeQL.installPackDependencies",
"title": "CodeQL: Install Pack Dependencies"
Expand Down
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export type LocalDatabasesCommands = {
"codeQL.chooseDatabaseGithub": () => Promise<void>;
"codeQL.upgradeCurrentDatabase": () => Promise<void>;
"codeQL.clearCache": () => Promise<void>;
"codeQL.trimCache": () => Promise<void>;

// Explorer context menu
"codeQL.setCurrentDatabase": (uri: Uri) => Promise<void>;
Expand Down
6 changes: 4 additions & 2 deletions extensions/ql-vscode/src/databases/local-databases-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ export class DatabaseUI extends DisposableObject {
this.handleSetDefaultTourDatabase.bind(this),
"codeQL.upgradeCurrentDatabase":
this.handleUpgradeCurrentDatabase.bind(this),
"codeQL.clearCache": this.handleClearCache.bind(this),
"codeQL.clearCache": this.handleClearCache.bind(this, "clear"),
"codeQL.trimCache": this.handleClearCache.bind(this, "trim"),
"codeQLDatabases.chooseDatabaseFolder":
this.handleChooseDatabaseFolder.bind(this),
"codeQLDatabases.chooseDatabaseArchive":
Expand Down Expand Up @@ -684,7 +685,7 @@ export class DatabaseUI extends DisposableObject {
);
}

private async handleClearCache(): Promise<void> {
private async handleClearCache(mode: "trim" | "clear"): Promise<void> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to extract "trim" | "clear" into a type so it can be reused below.

return withProgress(
async (_progress, token) => {
if (
Expand All @@ -694,6 +695,7 @@ export class DatabaseUI extends DisposableObject {
await this.queryServer.clearCacheInDatabase(
this.databaseManager.currentDatabaseItem,
token,
mode,
);
}
},
Expand Down
29 changes: 29 additions & 0 deletions extensions/ql-vscode/src/query-server/new-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ export interface ClearCacheParams {
* Whether the cache should actually be cleared.
*/
dryRun: boolean;
/**
* The mode to use when trimming the disk cache.
*/
mode?: CacheTrimmingMode;
}

export type CacheTrimmingMode = number;
/**
* The mode to use when trimming the disk cache. This namespace is intentionally not an enum, see
* "for the sake of extensibility" comment above.
*/
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CacheTrimmingMode {
/** The entire cache is deleted unconditionally. */
export const BRUTAL = 0;
/** Only `cached` predicates are kept. */
export const NORMAL = 1;
/**
* Trim the cache down to the configured size, but *within* this limit keep anything that
* appears to be even possibly potentially useful in the future.
*/
export const LIGHT = 2;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there plans to support LIGHT and GENTLE in the future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, as they're pretty obscure and there's little practical reason to support them in VSCode. Also, we're renaming "brutal" → "clear" and "normal" → "trim" to be more in line with the VSCode commands, so if you have suggestions on new names for "light" and "gentle", I'm all ears :)

/**
* As {@link LIGHT}, but only if the *currently active* backend has written anything to
* the cache in its lifetime. (Thus it doesn't make much sense to specify this in a stand-alone
* CLI invocation, but we do it as a separate operation before shutting down a query server,
* because it can then have its own timeout).
*/
export const GENTLE = 3;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion extensions/ql-vscode/src/query-server/new-query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DatabaseItem } from "../databases/local-databases";
import {
clearCache,
ClearCacheParams,
CacheTrimmingMode,
clearPackCache,
deregisterDatabases,
registerDatabases,
Expand Down Expand Up @@ -57,15 +58,20 @@ export class NewQueryRunner extends QueryRunner {
async clearCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
mode: "trim" | "clear",
): Promise<void> {
if (dbItem.contents === undefined) {
throw new Error("Can't clear the cache in an invalid database.");
throw new Error(`Can't ${mode} the cache in an invalid database.`);
}

const db = dbItem.databaseUri.fsPath;
const params: ClearCacheParams = {
dryRun: false,
db,
mode: {
trim: CacheTrimmingMode.NORMAL,
clear: CacheTrimmingMode.BRUTAL,
}[mode],
Comment on lines +71 to +74
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: could you extract this to a function? I find this hard to read.

};
await this.qs.sendRequest(clearCache, params, token);
}
Expand Down
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/query-server/query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export abstract class QueryRunner {
abstract clearCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
mode: "trim" | "clear",
): Promise<void>;

/**
Expand Down