Skip to content

Build your personal knowledge base with Trilium Notes

fix note size calculation with blobs

zadam78d83b882615
diff --git a/package-lock.json b/package-lock.json
index e7e2bd700b..084afc6374 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5,7 +5,6 @@
"requires": true,
"packages": {
"": {
- "name": "trilium",
"version": "0.59.4",
"hasInstallScript": true,
"license": "AGPL-3.0-only",
diff --git a/src/routes/api/stats.js b/src/routes/api/stats.js
index b342c577cb..22431886e0 100644
--- a/src/routes/api/stats.js
+++ b/src/routes/api/stats.js
@@ -4,19 +4,18 @@ const NotFoundError = require("../../errors/not_found_error");
function getNoteSize(req) {
const {noteId} = req.params;
- const note = becca.getNote(noteId);
- const noteSize = sql.getValue(`
- SELECT
- COALESCE((SELECT LENGTH(content) FROM blobs WHERE blobId = ?), 0)
- +
- COALESCE(
- (SELECT SUM(LENGTH(content))
- FROM note_revisions
- JOIN blobs USING (blobId)
- WHERE note_revisions.noteId = ?),
- 0
- )`, [note.blobId, noteId]);
+ const blobSizes = sql.getMap(`
+ SELECT blobs.blobId, LENGTH(content)
+ FROM blobs
+ LEFT JOIN notes ON notes.blobId = blobs.blobId AND notes.noteId = ? AND notes.isDeleted = 0
+ LEFT JOIN attachments ON attachments.blobId = blobs.blobId AND attachments.parentId = ? AND attachments.isDeleted = 0
+ LEFT JOIN note_revisions ON note_revisions.blobId = blobs.blobId AND note_revisions.noteId = ?
+ WHERE notes.noteId IS NOT NULL
+ OR attachments.attachmentId IS NOT NULL
+ OR note_revisions.noteRevisionId IS NOT NULL`, [noteId, noteId, noteId]);
+
+ const noteSize = Object.values(blobSizes).reduce((acc, blobSize) => acc + blobSize, 0);
return {
noteSize
@@ -35,22 +34,15 @@ function getSubtreeSize(req) {
sql.fillParamList(subTreeNoteIds);
- const subTreeSize = sql.getValue(`
- SELECT
- COALESCE((
- SELECT SUM(LENGTH(content))
- FROM notes
- JOIN blobs USING (blobId)
- JOIN param_list ON param_list.paramId = notes.noteId
- ), 0)
- +
- COALESCE(
- (SELECT SUM(LENGTH(content))
- FROM note_revisions
- JOIN blobs USING (blobId)
- JOIN param_list ON param_list.paramId = note_revisions.noteId),
- 0
- )`);
+ const blobSizes = sql.getMap(`
+ SELECT blobs.blobId, LENGTH(content)
+ FROM param_list
+ JOIN notes ON notes.noteId = param_list.paramId AND notes.isDeleted = 0
+ LEFT JOIN attachments ON attachments.parentId = param_list.paramId AND attachments.isDeleted = 0
+ LEFT JOIN note_revisions ON note_revisions.noteId = param_list.paramId
+ JOIN blobs ON blobs.blobId = notes.blobId OR blobs.blobId = attachments.blobId OR blobs.blobId = note_revisions.blobId`);
+
+ const subTreeSize = Object.values(blobSizes).reduce((acc, blobSize) => acc + blobSize, 0);
return {
subTreeSize,