Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: deduplicate revisions

Adorian Doranf4317b5da8cf
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 4e6fa3b441..04562b9dd1 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2687,7 +2687,7 @@
"center_note_size": "Note: <Size/>",
"center_note_size_hint": "This note's body and its attachments, matching the inner ring. It actually occupies {{size}} on disk with its revisions included, shared content counted once.",
"center_revisions_size": "Revisions: <Size/>",
- "center_revisions_size_hint": "Every revision in this subtree, this note's and its descendants', matching the gray segment. A snapshot still sharing content with the live note is counted again here.",
+ "center_revisions_size_hint": "Space erasing this subtree's history would reclaim, matching the gray segment; snapshots still sharing content with a live note free nothing and count for nothing here",
"center_subtree_size": "Subtree: <Size/>",
"center_subtree_size_hint": "This note and all of its descendants with their history left out, matching the rings together. The subtree actually occupies {{size}} on disk with revisions included, shared content counted once.",
"back": "Back",
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
index 4c93ae2176..3cf14153f5 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
@@ -63,13 +63,13 @@ function usageOf(noteId: string, children: string[]): SpaceUsageNoteResponse {
ownSize: 10,
attachmentsSize: 0,
revisionsSize: 0,
+ subtreeRevisionsContentSize: 0,
noteContentSize: 10,
subtreeContentSize: 100,
attachments: [],
children: children.map((childId) => ({
noteId: childId,
subtreeSize: 40,
- subtreeRevisionsSize: 0,
subtreeNoteCount: 2
}))
};
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts
index 773c26a5c5..b68e72ec8c 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.spec.ts
@@ -10,6 +10,7 @@ function usage(overrides: Partial<SpaceUsageNoteResponse> = {}): SpaceUsageNoteR
ownSize: 0,
attachmentsSize: 0,
revisionsSize: 0,
+ subtreeRevisionsContentSize: 0,
noteContentSize: 0,
subtreeContentSize: 0,
attachments: [],
@@ -18,8 +19,8 @@ function usage(overrides: Partial<SpaceUsageNoteResponse> = {}): SpaceUsageNoteR
};
}
-function child(noteId: string, subtreeSize: number, subtreeRevisionsSize = 0) {
- return { noteId, subtreeSize, subtreeRevisionsSize, subtreeNoteCount: 1 };
+function child(noteId: string, subtreeSize: number) {
+ return { noteId, subtreeSize, subtreeNoteCount: 1 };
}
const makeTooltip = (kind: UsageTooltipKind, title: string, size: number) => `${kind}:${title}/${size}`;
@@ -150,18 +151,19 @@ describe("buildChildrenSegments", () => {
expect(buildChildrenSegments(usage({ deletedNotes: { size: 0, noteCount: 0 } }), options)).toEqual([]);
});
- it("carries the whole subtree's history in one segment, ahead of the deleted bucket", () => {
+ it("carries the subtree's reclaimable history in one segment, ahead of the deleted bucket", () => {
const segments = buildChildrenSegments(usage({
+ // Per-entity history counts a shared snapshot at every entity holding it, so it is never
+ // what the segment draws — only the deduplicated subtree figure is.
+ revisionsSize: 900,
+ subtreeRevisionsContentSize: 24,
// Far below 0.5% of the ring, yet history must stay its own segment rather than fold in.
- revisionsSize: 7,
- children: [ child("big", 100000, 12), child("small", 50, 5) ],
+ children: [ child("big", 100000), child("small", 50) ],
deletedNotes: { size: 25, noteCount: 3 }
}), options);
expect(segments.map((segment) => segment.id))
.toEqual([ "child/big", "others", "revisions", "/deleted-notes" ]);
- // This note's own 7 plus both children's subtree totals — never the per-child figures drawn
- // separately, which would count a shared snapshot at each entity holding it.
expect(segments[2]).toMatchObject({
value: 24,
className: "space-usage-segment-revisions",
@@ -170,8 +172,12 @@ describe("buildChildrenSegments", () => {
expect(segments[2].hue).toBeUndefined();
});
- it("omits the revisions segment when the subtree has no history at all", () => {
- const segments = buildChildrenSegments(usage({ children: [ child("a", 10) ] }), options);
+ it("omits the revisions segment when erasing the history would reclaim nothing", () => {
+ const segments = buildChildrenSegments(usage({
+ // Every snapshot still shares its blobs with a live note: weighty per entity, worth zero.
+ revisionsSize: 900,
+ children: [ child("a", 10) ]
+ }), options);
expect(segments.map((segment) => segment.id)).toEqual([ "child/a" ]);
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.ts
index 5367c2f9fb..9792a8bf2a 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/donut_segments.ts
@@ -82,9 +82,9 @@ interface ChildrenOptions {
* "random" stable hue. Slivers consolidate into "Others"; the subtree's history then closes the
* ring, and on the root the deleted-notes bucket follows it — neither is ever folded away.
*
- * Child weights leave history out, so one revisions segment stands for all of it at once: this
- * note's own and every descendant's. Splitting it per child would mean drawing per-entity revision
- * figures, which count a snapshot's blob again at every entity sharing it.
+ * Child weights leave history out, so one revisions segment stands for all of it at once. Like the
+ * deleted bucket beside it, that segment is a deduplicated reclaimable figure rather than a
+ * comparable per-entity one — which is why neither is tinted, clickable, or foldable into "Others".
*/
export function buildChildrenSegments(
usage: SpaceUsageNoteResponse,
@@ -102,7 +102,7 @@ export function buildChildrenSegments(
}));
const segments = consolidateSmallSegments(children, MIN_CHILD_SEGMENT_FRACTION, makeOthersTooltip);
- const revisionsSize = subtreeRevisionsSize(usage);
+ const revisionsSize = usage.subtreeRevisionsContentSize;
if (revisionsSize > 0) {
segments.push({
@@ -126,18 +126,6 @@ export function buildChildrenSegments(
return segments;
}
-/**
- * The whole subtree's history: this note's own revisions plus every descendant's, the children
- * already carrying their own subtrees' totals. Exported so the view can quote the same figure the
- * ring draws rather than reproducing the sum.
- */
-export function subtreeRevisionsSize(usage: SpaceUsageNoteResponse) {
- return usage.children.reduce(
- (sum, child) => sum + child.subtreeRevisionsSize,
- usage.revisionsSize
- );
-}
-
/** What the composition ring sums to: the note's body and its attachments, history excluded. */
export function noteWeight(usage: SpaceUsageNoteResponse) {
return usage.ownSize + usage.attachmentsSize;
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.spec.tsx
index 5c7e54b66d..6d77fc027c 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.spec.tsx
@@ -33,6 +33,7 @@ const USAGE: SpaceUsageNoteResponse = {
ownSize: 10000,
attachmentsSize: 5010,
revisionsSize: 3000,
+ subtreeRevisionsContentSize: 2400,
noteContentSize: 150,
subtreeContentSize: 700,
attachments: [
@@ -41,7 +42,7 @@ const USAGE: SpaceUsageNoteResponse = {
{ attachmentId: "tiny", title: "Tiny attachment", role: "file", size: 10 }
],
// One child, so the subtree figures are tellable apart from the note's own.
- children: [ { noteId: "c1", subtreeSize: 4000, subtreeRevisionsSize: 500, subtreeNoteCount: 2 } ]
+ children: [ { noteId: "c1", subtreeSize: 4000, subtreeNoteCount: 2 } ]
};
let container: HTMLDivElement | undefined;
@@ -119,8 +120,9 @@ describe("NoteUsageDonut", () => {
expect(values).toEqual([
// Body + attachments, what the composition ring draws — not the deduplicated 150.
formatSize(15010),
- // This note's revisions plus the child subtree's, what the gray segment draws.
- formatSize(3500),
+ // What erasing the subtree's history would reclaim, what the gray segment draws — not
+ // the per-entity 3000 sitting beside it on the payload.
+ formatSize(2400),
// Both rings together, history excluded — not the deduplicated 700.
formatSize(19010)
]);
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
index 3edf174a4b..d461808ce2 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
@@ -15,7 +15,6 @@ import { openNoteInNewTab } from "./context_menu";
import {
buildCompositionSegments,
noteWeight,
- subtreeRevisionsSize,
subtreeWeight,
type UsageSegmentData,
type UsageTooltipKind
@@ -91,7 +90,7 @@ export default function NoteUsageDonut({ usage, title, notePath, outerRings = []
<SizeLine
i18nKey="space_usage.center_revisions_size"
hint={t("space_usage.center_revisions_size_hint")}
- size={subtreeRevisionsSize(usage)}
+ size={usage.subtreeRevisionsContentSize}
/>
<SizeLine
i18nKey="space_usage.center_subtree_size"
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 890b080d69..c456740d10 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -304,9 +304,8 @@ export interface SpaceUsageOverviewResponse {
export interface SpaceUsageChild {
noteId: string;
- /** Bodies plus attachments of the child's whole canonical subtree. */
+ /** Bodies plus attachments of the child's whole canonical subtree; revisions are left out. */
subtreeSize: number;
- subtreeRevisionsSize: number;
subtreeNoteCount: number;
}
@@ -327,6 +326,13 @@ export interface SpaceUsageNoteResponse extends SpaceUsageSizes {
noteContentSize: number;
/** Like {@link noteContentSize}, over the note's whole canonical subtree. */
subtreeContentSize: number;
+ /**
+ * What erasing the whole subtree's history would actually reclaim: blobs held only by the
+ * subtree's revisions, each counted once, and none that a live body or note attachment still
+ * shares. Tiered like the database-wide figure, so the two are comparable — unlike the
+ * per-entity {@link revisionsSize}, which counts a shared snapshot at every entity holding it.
+ */
+ subtreeRevisionsContentSize: number;
attachments: SpaceUsageAttachment[];
/** Canonical children only: a cloned child is listed under its canonical parent alone. */
children: SpaceUsageChild[];
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 1b5137fef3..a18ab7f128 100644
--- a/packages/trilium-core/src/routes/api/space_usage.spec.ts
+++ b/packages/trilium-core/src/routes/api/space_usage.spec.ts
@@ -143,10 +143,29 @@ describe("Space usage API (core)", () => {
const childEntry = usage.children.find((entry) => entry.noteId === child.noteId);
expect(childEntry?.subtreeSize).toBe(CHILD_CONTENT.length + ATTACHMENT_CONTENT.length);
- expect(childEntry?.subtreeRevisionsSize).toBeGreaterThanOrEqual(CHILD_CONTENT.length);
expect(childEntry?.subtreeNoteCount).toBe(1);
});
+ it("measures a subtree's history by what erasing it would reclaim, not by snapshot sizes", async () => {
+ // This note's revision holds the content its shrunken body no longer does, so erasing the
+ // history really would free it.
+ const heavyUsage = await getNoteUsage(revisionHeavy.noteId);
+ expect(heavyUsage.subtreeRevisionsContentSize).toBeGreaterThanOrEqual(REVISION_HEAVY_CONTENT.length);
+
+ // The child's revision snapshots an unchanged body and attachment, so it shares their blobs:
+ // it weighs plenty per entity, yet erasing it would reclaim nothing at all.
+ const childUsage = await getNoteUsage(child.noteId);
+ expect(childUsage.revisionsSize).toBeGreaterThan(0);
+ expect(childUsage.subtreeRevisionsContentSize).toBe(0);
+
+ // The root's subtree covers both, and cannot exceed the database-wide revisions tier — the
+ // figure the overview's own revisions bucket draws, which is what makes the two comparable.
+ const rootUsage = await getNoteUsage("root");
+ const overview = await getOverview({ limit: 1 });
+ expect(rootUsage.subtreeRevisionsContentSize).toBeGreaterThanOrEqual(heavyUsage.subtreeRevisionsContentSize);
+ expect(rootUsage.subtreeRevisionsContentSize).toBeLessThanOrEqual(overview.content.revisionsSize);
+ });
+
it("reports deduplicated note and subtree content sizes", async () => {
const childUsage = await getNoteUsage(child.noteId);
// The revision snapshots the unchanged body and attachment, sharing their blobs — so it
diff --git a/packages/trilium-core/src/services/space_usage.spec.ts b/packages/trilium-core/src/services/space_usage.spec.ts
index 4336a2245f..82b0bd7f07 100644
--- a/packages/trilium-core/src/services/space_usage.spec.ts
+++ b/packages/trilium-core/src/services/space_usage.spec.ts
@@ -91,7 +91,9 @@ describe("buildCanonicalForest", () => {
});
describe("computeSubtreeTotals", () => {
- it("sums bodies with attachments, keeps revisions apart and counts notes", () => {
+ // The per-entity revision sizes are fed in and deliberately ignored: a subtree's history is
+ // measured deduplicated instead, which no sum of per-note figures can produce.
+ it("sums bodies with attachments and counts notes, leaving revisions out", () => {
const forest = forestOf({ root: [ "a", "b" ], a: [ "c" ] });
const totals = computeSubtreeTotals(forest, sizeLookup({
a: { ownSize: 10, attachmentsSize: 5, revisionsSize: 2 },
@@ -99,9 +101,9 @@ describe("computeSubtreeTotals", () => {
c: { ownSize: 1, revisionsSize: 1 }
}));
- expect(totals.get("c")).toEqual({ size: 1, revisionsSize: 1, noteCount: 1 });
- expect(totals.get("a")).toEqual({ size: 16, revisionsSize: 3, noteCount: 2 });
- expect(totals.get("root")).toEqual({ size: 19, revisionsSize: 3, noteCount: 4 });
+ expect(totals.get("c")).toEqual({ size: 1, noteCount: 1 });
+ expect(totals.get("a")).toEqual({ size: 16, noteCount: 2 });
+ expect(totals.get("root")).toEqual({ size: 19, noteCount: 4 });
});
it("counts a note cloned into the hidden subtree in its user placement only", () => {
@@ -113,9 +115,9 @@ describe("computeSubtreeTotals", () => {
const totals = computeSubtreeTotals(forest, sizeLookup({ bm: { ownSize: 7 } }));
expect(totals.get("a")?.size).toBe(7);
- expect(totals.get("_hidden")).toEqual({ size: 0, revisionsSize: 0, noteCount: 1 });
+ expect(totals.get("_hidden")).toEqual({ size: 0, noteCount: 1 });
// The hidden subtree still folds into the root, so the root total covers everything.
- expect(totals.get("root")).toEqual({ size: 7, revisionsSize: 0, noteCount: 4 });
+ expect(totals.get("root")).toEqual({ size: 7, noteCount: 4 });
});
it("survives a pathologically deep chain without recursing", () => {
@@ -130,7 +132,7 @@ describe("computeSubtreeTotals", () => {
const forest = forestOf(edges);
const totals = computeSubtreeTotals(forest, () => ({ ownSize: 1, attachmentsSize: 0, revisionsSize: 0 }));
- expect(totals.get("root")).toEqual({ size: 2001, revisionsSize: 0, noteCount: 2001 });
+ expect(totals.get("root")).toEqual({ size: 2001, noteCount: 2001 });
});
});
diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.ts
index b967db3af5..b9bd49264e 100644
--- a/packages/trilium-core/src/services/space_usage.ts
+++ b/packages/trilium-core/src/services/space_usage.ts
@@ -81,7 +81,6 @@ export function getNoteUsage(noteId: string): SpaceUsageNoteResponse {
/* v8 ignore next 3 -- the totals cover every note of the forest, so the fallbacks
would only fire on an internally inconsistent forest */
subtreeSize: total?.size ?? 0,
- subtreeRevisionsSize: total?.revisionsSize ?? 0,
subtreeNoteCount: total?.noteCount ?? 0
};
});
@@ -109,11 +108,14 @@ export function getNoteUsage(noteId: string): SpaceUsageNoteResponse {
} ];
});
+ const subtreeNoteIds = collectSubtreeNoteIds(forest, noteId);
+
return {
noteId,
...sizesOf(noteId, sizes),
noteContentSize: collectContentSizeOf([ noteId ]),
- subtreeContentSize: collectContentSizeOf(collectSubtreeNoteIds(forest, noteId)),
+ subtreeContentSize: collectContentSizeOf(subtreeNoteIds),
+ subtreeRevisionsContentSize: collectRevisionsSizeOf(subtreeNoteIds),
attachments,
children,
// Deleted notes have no place in the tree, so they surface once, at its root.
@@ -210,9 +212,12 @@ function appendAll(target: string[], items: string[]) {
/** The aggregate of a note's whole canonical subtree, the note itself included. */
export interface SubtreeTotal {
- /** Bodies plus attachments; revisions kept apart so clients can toggle them. */
+ /**
+ * Bodies plus attachments. Revisions are absent on purpose: summing the per-entity figures would
+ * count a snapshot's blob again at every entity sharing it, so a subtree's history is measured
+ * deduplicated instead, by {@link collectRevisionsSizeOf}.
+ */
size: number;
- revisionsSize: number;
noteCount: number;
}
@@ -230,8 +235,8 @@ export function computeSubtreeTotals(
const totals = new Map<string, SubtreeTotal>();
for (const noteId of [ ...forest.userNoteIds, ...forest.hiddenNoteIds ]) {
- const { ownSize, attachmentsSize, revisionsSize } = getSizes(noteId);
- totals.set(noteId, { size: ownSize + attachmentsSize, revisionsSize, noteCount: 1 });
+ const { ownSize, attachmentsSize } = getSizes(noteId);
+ totals.set(noteId, { size: ownSize + attachmentsSize, noteCount: 1 });
}
for (const order of [ forest.hiddenNoteIds, forest.userNoteIds ]) {
@@ -245,7 +250,6 @@ export function computeSubtreeTotals(
}
parentTotal.size += total.size;
- parentTotal.revisionsSize += total.revisionsSize;
parentTotal.noteCount += total.noteCount;
}
}
@@ -419,6 +423,34 @@ function collectContentSizeOf(noteIds: string[]): number {
)`) ?? 0;
}
+/**
+ * The revision tier of {@link collectContentSizeOf}: what erasing the given notes' history would
+ * actually reclaim. Deduplicated, and subtracted against the *database-wide* live bodies and note
+ * attachments exactly as {@link collectContent} does — a snapshot still sharing its blob with any
+ * live note frees nothing, so it counts for zero here. That tiering is what makes a subtree's figure
+ * comparable to the database-wide one, and what separates this from the per-entity `revisionsSize`,
+ * which counts a shared snapshot again at every entity holding it.
+ */
+function collectRevisionsSizeOf(noteIds: string[]): number {
+ const sql = getSql();
+ sql.fillParamList(noteIds);
+
+ return sql.getValue<number | null>(`
+ SELECT SUM(COALESCE(LENGTH(blobs.content), 0))
+ FROM blobs
+ WHERE blobs.blobId IN (
+ SELECT revisions.blobId FROM revisions
+ JOIN param_list ON param_list.paramId = revisions.noteId
+ WHERE revisions.blobId IS NOT NULL
+ UNION SELECT attachments.blobId FROM attachments
+ JOIN revisions ON revisions.revisionId = attachments.ownerId
+ JOIN param_list ON param_list.paramId = revisions.noteId
+ WHERE attachments.isDeleted = 0 AND attachments.blobId IS NOT NULL
+ )
+ AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})
+ AND blobs.blobId NOT IN (${LIVE_NOTE_ATTACHMENT_BLOBS})`) ?? 0;
+}
+
/**
* What erasing would actually reclaim: blobs referenced by deleted entities and by no live one.
* Deduplicated by blobId — several deleted clones of the same content free that content once.