Skip to content

Commit d0f4188

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 8ecc31f commit d0f4188

6 files changed

Lines changed: 57 additions & 0 deletions

File tree

extensions/ql-vscode/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@
712712
"command": "codeQL.clearCache",
713713
"title": "CodeQL: Clear Cache"
714714
},
715+
{
716+
"command": "codeQL.trimCache",
717+
"title": "CodeQL: Trim Cache"
718+
},
715719
{
716720
"command": "codeQL.installPackDependencies",
717721
"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
@@ -207,6 +207,7 @@ export type LocalDatabasesCommands = {
207207
"codeQL.chooseDatabaseGithub": () => Promise<void>;
208208
"codeQL.upgradeCurrentDatabase": () => Promise<void>;
209209
"codeQL.clearCache": () => Promise<void>;
210+
"codeQL.trimCache": () => Promise<void>;
210211

211212
// Explorer context menu
212213
"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
@@ -53,13 +53,23 @@ export class LegacyQueryRunner extends QueryRunner {
5353
) {
5454
this.qs.onDidStartQueryServer(callBack);
5555
}
56+
5657
async clearCacheInDatabase(
5758
dbItem: DatabaseItem,
5859
token: CancellationToken,
5960
): Promise<void> {
6061
await clearCacheInDatabase(this.qs, dbItem, token);
6162
}
6263

64+
async trimCacheInDatabase(
65+
dbItem: DatabaseItem,
66+
token: CancellationToken,
67+
): Promise<void> {
68+
// For the sake of conforming to the QueryRunner interface, delegate to clearCacheInDatabase
69+
// just for the effect of getting a legacy query server deprecation error response.
70+
await clearCacheInDatabase(this.qs, dbItem, token);
71+
}
72+
6373
public async compileAndRunQueryAgainstDatabaseCore(
6474
dbPath: string,
6575
query: CoreQueryTarget,

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)