Skip to content

Commit

Permalink
Add "VSCode: Trim Cache" command that calls evaluation/trimCache
Browse files Browse the repository at this point in the history
The purpose of this change is to add a command that clears the cache except for predicates marked `cached`.
In contrast, the existing "VSCode: Clear Cache" command clears everything (`--mode=brutal`).

This calls into the query server's `evaluation/trimCache` method;
however, its existing behaviour is to do a database cleanup with `--mode=gentle`.
This is not well documented, and `--mode=normal` would give the desired behaviour.

Accordingly, this approach is dependent on separately changing the backend behaviour to `--mode=normal`.

Other possible amendments to this commit would be to not touch the legacy client
(replacing required methods by failing promises, since the legacy server is fully deprecated already),
or to have less duplication (by introducing more arguments — however,
I'm applying the rule of thumb that >3 copy-pastes are required for the introduction of a deduplicating abstraction).
  • Loading branch information
d10c committed Oct 11, 2023
1 parent 2b47d3d commit 2ca2b0f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,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 @@ -207,6 +207,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
20 changes: 20 additions & 0 deletions extensions/ql-vscode/src/databases/local-databases-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export class DatabaseUI extends DisposableObject {
"codeQL.upgradeCurrentDatabase":
this.handleUpgradeCurrentDatabase.bind(this),
"codeQL.clearCache": this.handleClearCache.bind(this),
"codeQL.trimCache": this.handleTrimCache.bind(this),
"codeQLDatabases.chooseDatabaseFolder":
this.handleChooseDatabaseFolder.bind(this),
"codeQLDatabases.chooseDatabaseArchive":
Expand Down Expand Up @@ -703,6 +704,25 @@ export class DatabaseUI extends DisposableObject {
);
}

private async handleTrimCache(): Promise<void> {
return withProgress(
async (_progress, token) => {
if (
this.queryServer !== undefined &&
this.databaseManager.currentDatabaseItem !== undefined
) {
await this.queryServer.trimCacheInDatabase(
this.databaseManager.currentDatabaseItem,
token,
);
}
},
{
title: "Trimming cache",
},
);
}

private async handleGetCurrentDatabase(): Promise<string | undefined> {
const dbItem = await this.getDatabaseItemInternal(undefined);
return dbItem?.databaseUri.fsPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@ export class LegacyQueryRunner extends QueryRunner {
) {
this.qs.onDidStartQueryServer(callBack);
}

async clearCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
): Promise<void> {
await clearCacheInDatabase(this.qs, dbItem, token);
}

async trimCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
): Promise<void> {
// For the sake of conforming to the QueryRunner interface, delegate to clearCacheInDatabase
// just for the effect of getting a legacy query server deprecation error response.
await clearCacheInDatabase(this.qs, dbItem, token);
}

public async compileAndRunQueryAgainstDatabaseCore(
dbPath: string,
query: CoreQueryTarget,
Expand Down
17 changes: 17 additions & 0 deletions extensions/ql-vscode/src/query-server/new-query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
clearPackCache,
deregisterDatabases,
registerDatabases,
trimCache,
TrimCacheParams,
upgradeDatabase,
} from "./new-messages";
import { CoreQueryResults, CoreQueryTarget, QueryRunner } from "./query-runner";
Expand Down Expand Up @@ -70,6 +72,21 @@ export class NewQueryRunner extends QueryRunner {
await this.qs.sendRequest(clearCache, params, token);
}

async trimCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
): Promise<void> {
if (dbItem.contents === undefined) {
throw new Error("Can't trim the cache in an invalid database.");
}

const db = dbItem.databaseUri.fsPath;
const params: TrimCacheParams = {
db,
};
await this.qs.sendRequest(trimCache, params, token);
}

public async compileAndRunQueryAgainstDatabaseCore(
dbPath: string,
query: CoreQueryTarget,
Expand Down
5 changes: 5 additions & 0 deletions extensions/ql-vscode/src/query-server/query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export abstract class QueryRunner {
token: CancellationToken,
): Promise<void>;

abstract trimCacheInDatabase(
dbItem: DatabaseItem,
token: CancellationToken,
): Promise<void>;

/**
* Overridden in subclasses to evaluate the query via the query server and return the results.
*/
Expand Down

0 comments on commit 2ca2b0f

Please sign in to comment.