Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: improve the counters

Adorian Dorane3ac6e94defa
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 1405818ff5..f3fb1b1867 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2683,9 +2683,11 @@
"center_subtree_size": "Subtree: <Size/>",
"center_subtree_size_hint": "Space this note and all of its descendants actually occupy, revisions included, with duplicated content counted once",
"back": "Back",
- "status_content": "Database content: {{size}}",
- "status_content_hint": "Space the content actually occupies: all notes (hidden ones included), attachments and revisions, with duplicated content counted once",
- "status_deleted": "Deleted notes: {{size}}",
+ "status_content_one": "Total content size: {{size}} in {{count}} note ({{revisionsSize}} revisions, {{attachmentsSize}} note attachments)",
+ "status_content_other": "Total content size: {{size}} in {{count}} notes ({{revisionsSize}} revisions, {{attachmentsSize}} note attachments)",
+ "status_content_hint": "Space the content actually occupies: all notes (hidden ones included), attachments and revisions, with duplicated content counted once. The bracketed figures are the space only revisions, respectively only attachments, hold.",
+ "status_deleted_one": "Deleted notes: {{size}} in {{count}} note",
+ "status_deleted_other": "Deleted notes: {{size}} in {{count}} notes",
"status_deleted_hint": "Space still held by deleted notes; erasing them reclaims it"
},
"note_language": {
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.css b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.css
index 7e98ed04e7..a7697d2849 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.css
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.css
@@ -33,12 +33,17 @@
.space-usage-status {
display: flex;
flex: 0 0 auto;
- gap: 2em;
+ flex-wrap: wrap;
+ gap: 0.6em;
border-top: 1px solid var(--main-border-color);
padding-block: 6px;
font-size: 0.85em;
}
+ .space-usage-status-separator {
+ opacity: 0.6;
+ }
+
.space-usage-loading {
opacity: 0.7;
}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
index 4d30c66188..5cedef8a6e 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
@@ -36,10 +36,19 @@ export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionPro
{overview && (
<footer className="space-usage-status">
<span title={t("space_usage.status_content_hint")}>
- {t("space_usage.status_content", { size: formatSize(overview.contentSize) })}
+ {t("space_usage.status_content", {
+ count: overview.content.noteCount,
+ size: formatSize(overview.content.size),
+ revisionsSize: formatSize(overview.content.revisionsSize),
+ attachmentsSize: formatSize(overview.content.attachmentsSize)
+ })}
</span>
+ <span className="space-usage-status-separator" aria-hidden="true">–</span>
<span title={t("space_usage.status_deleted_hint")}>
- {t("space_usage.status_deleted", { size: formatSize(overview.deletedNotes.size) })}
+ {t("space_usage.status_deleted", {
+ count: overview.deletedNotes.noteCount,
+ size: formatSize(overview.deletedNotes.size)
+ })}
</span>
</footer>
)}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
index 34eaf0b4a2..6146a7d83e 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
@@ -10,7 +10,7 @@ function entry(noteId: string, notePath: string[], sizes: Partial<SpaceUsageOver
function response(notes: SpaceUsageOverviewNote[], overrides: Partial<SpaceUsageOverviewResponse> = {}): SpaceUsageOverviewResponse {
return {
- contentSize: 0,
+ content: { size: 0, noteCount: 0, attachmentsSize: 0, revisionsSize: 0 },
notes,
otherNotes: { size: 0, revisionsSize: 0, noteCount: 0 },
hiddenNotes: { size: 0, revisionsSize: 0, noteCount: 0 },
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 87f4ac7b67..890b080d69 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -269,14 +269,28 @@ export interface SpaceUsageDeletedNotes {
noteCount: number;
}
+/**
+ * What the live content actually occupies — every blob referenced by a live note, attachment or
+ * revision, hidden subtree included, counted once however many entities share it through
+ * deduplication. The breakdown attributes each blob to exactly one tier — bodies first, then
+ * attachments, then revisions — so the parts sum to {@link size} and each reads as "the space only
+ * this category holds". The per-note numbers elsewhere intentionally count per entity instead, so
+ * a note's reported size never depends on duplicates elsewhere.
+ */
+export interface SpaceUsageContent {
+ /** The whole live content; the "database size" figure. */
+ size: number;
+ /** Live notes, hidden ones included. */
+ noteCount: number;
+ /** Space only note-owned attachments hold: their blobs minus those shared with note bodies. */
+ attachmentsSize: number;
+ /** Space only revisions hold (snapshot attachments included): their blobs minus those shared
+ * with live bodies or live attachments. */
+ revisionsSize: number;
+}
+
export interface SpaceUsageOverviewResponse {
- /**
- * What the live content actually occupies: every blob referenced by a live note, attachment or
- * revision — hidden subtree included — counted once however many entities share it through
- * deduplication. This is the "database size" figure; the per-note numbers below intentionally
- * count per entity instead, so a note's reported size never depends on duplicates elsewhere.
- */
- contentSize: number;
+ content: SpaceUsageContent;
/** The largest notes first, ranked by body + attachments (+ revisions when requested). */
notes: SpaceUsageOverviewNote[];
/** The remaining notes of the visible tree, below the ranking cutoff. */
diff --git a/packages/trilium-core/src/routes/api/space_usage.spec.ts b/packages/trilium-core/src/routes/api/space_usage.spec.ts
index 4d7eee4df2..f70ea27e1c 100644
--- a/packages/trilium-core/src/routes/api/space_usage.spec.ts
+++ b/packages/trilium-core/src/routes/api/space_usage.spec.ts
@@ -88,7 +88,7 @@ describe("Space usage API (core)", () => {
expect(limited.otherNotes.noteCount).toBe(limited.total.noteCount - 1);
});
- it("counts deduplicated content once in contentSize, but per entity in the note sizes", async () => {
+ it("counts deduplicated content once in the content figure, but per entity in the note sizes", async () => {
const duplicated = `<p>${"space-usage-dedup-".repeat(300)}</p>`;
const before = await getOverview({ limit: 1 });
@@ -99,7 +99,20 @@ describe("Space usage API (core)", () => {
// Two notes, one shared blob: the visible-tree total grows twice, the deduplicated
// database-content figure only once.
expect(after.total.size - before.total.size).toBe(2 * duplicated.length);
- expect(after.contentSize - before.contentSize).toBe(duplicated.length);
+ expect(after.content.size - before.content.size).toBe(duplicated.length);
+ expect(after.content.noteCount - before.content.noteCount).toBe(2);
+ });
+
+ it("attributes each blob to exactly one content tier: bodies, then attachments, then revisions", async () => {
+ const { content } = await getOverview({ limit: 1 });
+
+ // The fixture attachment shares no blob with any body, so its tier holds at least it; the
+ // revision-heavy note's snapshot no longer matches its shrunken body, so the revisions tier
+ // holds at least that. The child's unchanged revision shares its body's blob and thus
+ // belongs to the bodies tier — invisible here.
+ expect(content.attachmentsSize).toBeGreaterThanOrEqual(ATTACHMENT_CONTENT.length);
+ expect(content.revisionsSize).toBeGreaterThanOrEqual(REVISION_HEAVY_CONTENT.length);
+ expect(content.size).toBeGreaterThanOrEqual(content.attachmentsSize + content.revisionsSize);
});
it("keeps zero-sized notes out of the individual listing", async () => {
@@ -152,7 +165,7 @@ describe("Space usage API (core)", () => {
const overview = await getOverview({ limit: 1 });
const rootUsage = await getNoteUsage("root");
expect(rootUsage.subtreeContentSize).toBeGreaterThan(0);
- expect(rootUsage.subtreeContentSize).toBeLessThanOrEqual(overview.contentSize);
+ expect(rootUsage.subtreeContentSize).toBeLessThanOrEqual(overview.content.size);
});
it("counts a cloned note only under its canonical parent", async () => {
diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.ts
index 38befb426c..9ce47a74b8 100644
--- a/packages/trilium-core/src/services/space_usage.ts
+++ b/packages/trilium-core/src/services/space_usage.ts
@@ -1,5 +1,6 @@
import {
SpaceUsageBucket,
+ SpaceUsageContent,
SpaceUsageDeletedNotes,
SpaceUsageNoteResponse,
SpaceUsageOverviewResponse,
@@ -53,7 +54,7 @@ export function getOverview({ includeRevisions, limit }: { includeRevisions: boo
}
return {
- contentSize: collectContentSize(),
+ content: collectContent(),
notes: top.map(({ noteId, ...entrySizes }) => ({
noteId,
notePath: buildNotePath(forest.parentByNoteId, noteId),
@@ -335,23 +336,54 @@ function bucketOf(entries: SpaceUsageSizes[]): SpaceUsageBucket {
return bucket;
}
+const LIVE_BODY_BLOBS = `SELECT blobId FROM notes WHERE isDeleted = 0 AND blobId IS NOT NULL`;
+
+const LIVE_NOTE_ATTACHMENT_BLOBS = `
+ SELECT attachments.blobId FROM attachments
+ JOIN notes ON notes.noteId = attachments.ownerId
+ WHERE attachments.isDeleted = 0 AND notes.isDeleted = 0 AND attachments.blobId IS NOT NULL`;
+
+const LIVE_REVISION_BLOBS = `
+ SELECT revisions.blobId FROM revisions
+ JOIN notes ON notes.noteId = revisions.noteId
+ WHERE notes.isDeleted = 0 AND revisions.blobId IS NOT NULL
+ UNION
+ SELECT attachments.blobId FROM attachments
+ JOIN revisions ON revisions.revisionId = attachments.ownerId
+ JOIN notes ON notes.noteId = revisions.noteId
+ WHERE attachments.isDeleted = 0 AND notes.isDeleted = 0 AND attachments.blobId IS NOT NULL`;
+
/**
- * What the live content actually occupies on disk: every blob referenced by a live note, a live
- * attachment or a revision of a live note — the hidden subtree included — counted once however
- * many entities share it through deduplication. The per-entity numbers elsewhere deliberately
- * count differently, so this is the one figure comparable to the database's real size.
+ * What the live content actually occupies on disk, deduplicated, broken into mutually exclusive
+ * tiers: bodies claim their blobs first, then note-owned attachments, then revisions (snapshot
+ * attachments included). The tiers sum to the total by construction, so each parenthetical figure
+ * reads as "the space that would go away if only this category vanished". The per-entity numbers
+ * elsewhere deliberately count differently; this is the one figure comparable to the database's
+ * real size.
*/
-function collectContentSize(): number {
- return getSql().getValue<number | null>(`
- SELECT SUM(COALESCE(LENGTH(blobs.content), 0))
- FROM blobs
- WHERE blobs.blobId IN (
- SELECT blobId FROM notes WHERE isDeleted = 0 AND blobId IS NOT NULL
- UNION SELECT blobId FROM attachments WHERE isDeleted = 0 AND blobId IS NOT NULL
- UNION SELECT revisions.blobId FROM revisions
- JOIN notes ON notes.noteId = revisions.noteId
- WHERE notes.isDeleted = 0 AND revisions.blobId IS NOT NULL
- )`) ?? 0;
+function collectContent(): SpaceUsageContent {
+ const sql = getSql();
+ const sumBlobs = (tier: string) =>
+ sql.getValue<number | null>(`
+ SELECT SUM(COALESCE(LENGTH(blobs.content), 0))
+ FROM blobs
+ WHERE ${tier}`) ?? 0;
+
+ const bodiesSize = sumBlobs(`blobs.blobId IN (${LIVE_BODY_BLOBS})`);
+ const attachmentsSize = sumBlobs(`
+ blobs.blobId IN (${LIVE_NOTE_ATTACHMENT_BLOBS})
+ AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})`);
+ const revisionsSize = sumBlobs(`
+ blobs.blobId IN (${LIVE_REVISION_BLOBS})
+ AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})
+ AND blobs.blobId NOT IN (${LIVE_NOTE_ATTACHMENT_BLOBS})`);
+
+ return {
+ size: bodiesSize + attachmentsSize + revisionsSize,
+ noteCount: sql.getValue<number>(`SELECT COUNT(*) FROM notes WHERE isDeleted = 0`),
+ attachmentsSize,
+ revisionsSize
+ };
}
/**