Skip to content

Commit 08f8eb4

Browse files
committed
Add "VSCode: Trim Cache" command that calls evaluation/trimCache
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).
1 parent 1d691c2 commit 08f8eb4

7 files changed

Lines changed: 78 additions & 0 deletions

File tree

extensions/ql-vscode/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@
721721
"command": "codeQL.clearCache",
722722
"title": "CodeQL: Clear Cache"
723723
},
724+
{
725+
"command": "codeQL.trimCache",
726+
"title": "CodeQL: Trim Cache"
727+
},
724728
{
725729
"command": "codeQL.installPackDependencies",
726730
"title": "CodeQL: Install Pack Dependencies"

extensions/ql-vscode/src/common/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export type LocalDatabasesCommands = {
208208
"codeQL.chooseDatabaseGithub": () => Promise<void>;
209209
"codeQL.upgradeCurrentDatabase": () => Promise<void>;
210210
"codeQL.clearCache": () => Promise<void>;
211+
"codeQL.trimCache": () => Promise<void>;
211212

212213
// Explorer context menu
213214
"codeQL.setCurrentDatabase": (uri: Uri) => Promise<void>;

extensions/ql-vscode/src/databases/local-databases-ui.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class DatabaseUI extends DisposableObject {
252252
"codeQL.upgradeCurrentDatabase":
253253
this.handleUpgradeCurrentDatabase.bind(this),
254254
"codeQL.clearCache": this.handleClearCache.bind(this),
255+
"codeQL.trimCache": this.handleTrimCache.bind(this),
255256
"codeQLDatabases.chooseDatabaseFolder":
256257
this.handleChooseDatabaseFolder.bind(this),
257258
"codeQLDatabases.chooseDatabaseArchive":
@@ -703,6 +704,25 @@ export class DatabaseUI extends DisposableObject {
703704
);
704705
}
705706

707+
private async handleTrimCache(): Promise<void> {
708+
return withProgress(
709+
async (_progress, token) => {
710+
if (
711+
this.queryServer !== undefined &&
712+
this.databaseManager.currentDatabaseItem !== undefined
713+
) {
714+
await this.queryServer.trimCacheInDatabase(
715+
this.databaseManager.currentDatabaseItem,
716+
token,
717+
);
718+
}
719+
},
720+
{
721+
title: "Trimming cache",
722+
},
723+
);
724+
}
725+
706726
private async handleGetCurrentDatabase(): Promise<string | undefined> {
707727
const dbItem = await this.getDatabaseItemInternal(undefined);
708728
return dbItem?.databaseUri.fsPath;

extensions/ql-vscode/src/query-server/legacy/legacy-query-runner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { trimCache } from "./../new-messages";
12
import { CancellationToken } from "vscode";
23
import { CodeQLCliServer } from "../../codeql-cli/cli";
34
import { ProgressCallback } from "../../common/vscode/progress";
@@ -17,6 +18,7 @@ import { QueryOutputDir } from "../../run-queries-shared";
1718
import { QueryServerClient } from "./query-server-client";
1819
import {
1920
clearCacheInDatabase,
21+
trimCacheInDatabase,
2022
compileAndRunQueryAgainstDatabaseCore,
2123
} from "./run-queries";
2224
import { upgradeDatabaseExplicit } from "./upgrades";
@@ -53,13 +55,21 @@ export class LegacyQueryRunner extends QueryRunner {
5355
) {
5456
this.qs.onDidStartQueryServer(callBack);
5557
}
58+
5659
async clearCacheInDatabase(
5760
dbItem: DatabaseItem,
5861
token: CancellationToken,
5962
): Promise<void> {
6063
await clearCacheInDatabase(this.qs, dbItem, token);
6164
}
6265

66+
async trimCacheInDatabase(
67+
dbItem: DatabaseItem,
68+
token: CancellationToken,
69+
): Promise<void> {
70+
await trimCacheInDatabase(this.qs, dbItem, token);
71+
}
72+
6373
public async compileAndRunQueryAgainstDatabaseCore(
6474
dbPath: string,
6575
query: CoreQueryTarget,

extensions/ql-vscode/src/query-server/legacy/run-queries.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,27 @@ export async function clearCacheInDatabase(
214214
return qs.sendRequest(messages.clearCache, params, token);
215215
}
216216

217+
export async function trimCacheInDatabase(
218+
qs: qsClient.QueryServerClient,
219+
dbItem: DatabaseItem,
220+
token: CancellationToken,
221+
): Promise<messages.ClearCacheResult> {
222+
if (dbItem.contents === undefined) {
223+
throw new Error("Can't clear the cache in an invalid database.");
224+
}
225+
226+
const db: messages.Dataset = {
227+
dbDir: dbItem.contents.datasetUri.fsPath,
228+
workingSet: "default",
229+
};
230+
231+
const params: messages.TrimCacheParams = {
232+
db,
233+
};
234+
235+
return qs.sendRequest(messages.trimCache, params, token);
236+
}
237+
217238
function reportNoUpgradePath(
218239
qlProgram: messages.QlProgram,
219240
queryDbscheme: string,

extensions/ql-vscode/src/query-server/new-query-runner.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
clearPackCache,
1111
deregisterDatabases,
1212
registerDatabases,
13+
trimCache,
14+
TrimCacheParams,
1315
upgradeDatabase,
1416
} from "./new-messages";
1517
import { CoreQueryResults, CoreQueryTarget, QueryRunner } from "./query-runner";
@@ -70,6 +72,21 @@ export class NewQueryRunner extends QueryRunner {
7072
await this.qs.sendRequest(clearCache, params, token);
7173
}
7274

75+
async trimCacheInDatabase(
76+
dbItem: DatabaseItem,
77+
token: CancellationToken,
78+
): Promise<void> {
79+
if (dbItem.contents === undefined) {
80+
throw new Error("Can't trim the cache in an invalid database.");
81+
}
82+
83+
const db = dbItem.databaseUri.fsPath;
84+
const params: TrimCacheParams = {
85+
db,
86+
};
87+
await this.qs.sendRequest(trimCache, params, token);
88+
}
89+
7390
public async compileAndRunQueryAgainstDatabaseCore(
7491
dbPath: string,
7592
query: CoreQueryTarget,

extensions/ql-vscode/src/query-server/query-runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export abstract class QueryRunner {
6767
token: CancellationToken,
6868
): Promise<void>;
6969

70+
abstract trimCacheInDatabase(
71+
dbItem: DatabaseItem,
72+
token: CancellationToken,
73+
): Promise<void>;
74+
7075
/**
7176
* Overridden in subclasses to evaluate the query via the query server and return the results.
7277
*/

0 commit comments

Comments
 (0)