Skip to content

Commit 585aa36

Browse files
committed
refactor: Use single place for DB manipulation
1 parent 1a7a57c commit 585aa36

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

packages/project/lib/build/cache/BuildCacheStorage.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,32 @@ export default class BuildCacheStorage {
550550
return new Set(rows.map((row) => row.integrity));
551551
}
552552

553+
/**
554+
* Clears all records from all tables and runs VACUUM.
555+
* Returns the number of bytes freed.
556+
*
557+
* @returns {number} Number of bytes freed
558+
*/
559+
clearAllRecords() {
560+
const {page_count: pageCountBefore} = this.#db.prepare("PRAGMA page_count").get();
561+
const {page_size: pageSize} = this.#db.prepare("PRAGMA page_size").get();
562+
const bytesBefore = pageCountBefore * pageSize;
563+
564+
this.#db.exec("BEGIN");
565+
this.#db.exec("DELETE FROM content");
566+
this.#db.exec("DELETE FROM index_cache");
567+
this.#db.exec("DELETE FROM stage_metadata");
568+
this.#db.exec("DELETE FROM task_metadata");
569+
this.#db.exec("DELETE FROM result_metadata");
570+
this.#db.exec("COMMIT");
571+
this.#db.exec("VACUUM");
572+
573+
const {page_count: pageCountAfter} = this.#db.prepare("PRAGMA page_count").get();
574+
const bytesAfter = pageCountAfter * pageSize;
575+
576+
return bytesBefore - bytesAfter;
577+
}
578+
553579
/**
554580
* Closes the database connection
555581
*/

packages/project/lib/cache/CacheCleanup.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path";
22
import fs from "node:fs/promises";
3-
import {DatabaseSync} from "node:sqlite";
3+
import BuildCacheStorage from "../build/cache/BuildCacheStorage.js";
44

55
/**
66
* Get the size of a directory tree recursively.
@@ -79,34 +79,17 @@ async function cleanBuildCache(buildCacheDir) {
7979
return removed;
8080
}
8181

82-
const tables = ["content", "index_cache", "stage_metadata", "task_metadata", "result_metadata"];
8382

8483
for (const versionDir of versionDirs) {
8584
if (!versionDir.isDirectory()) {
8685
continue;
8786
}
8887

89-
const dbPath = path.join(buildCacheDir, versionDir.name, "cache.db");
90-
try {
91-
await fs.access(dbPath);
92-
} catch {
93-
continue;
94-
}
95-
96-
const statBefore = await fs.stat(dbPath);
97-
const sizeBefore = statBefore.size;
98-
99-
const db = new DatabaseSync(dbPath);
100-
db.exec("BEGIN");
101-
for (const table of tables) {
102-
db.exec(`DELETE FROM ${table}`);
103-
}
104-
db.exec("COMMIT");
105-
db.exec("VACUUM");
106-
db.close();
88+
const dbDir = path.join(buildCacheDir, versionDir.name);
10789

108-
const statAfter = await fs.stat(dbPath);
109-
const freedSize = sizeBefore - statAfter.size;
90+
const storage = new BuildCacheStorage(dbDir);
91+
const freedSize = storage.clearAllRecords();
92+
storage.close();
11093

11194
removed.push({
11295
path: `buildCache/${versionDir.name}`,

packages/project/test/lib/package-exports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test("export of package.json", (t) => {
1313
// Check number of definied exports
1414
test("check number of exports", (t) => {
1515
const packageJson = require("@ui5/project/package.json");
16-
t.is(Object.keys(packageJson.exports).length, 14);
16+
t.is(Object.keys(packageJson.exports).length, 15);
1717
});
1818

1919
// Public API contract (exported modules)

0 commit comments

Comments
 (0)