Skip to content

Build your personal knowledge base with Trilium Notes

fix calculating revision attachment size

zadam9db0a062eddf
diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js
index 25b8bc8488..a174e37b54 100644
--- a/src/services/search/services/search.js
+++ b/src/services/search/services/search.js
@@ -92,6 +92,13 @@ function searchFromRelation(note, relationName) {
function loadNeededInfoFromDatabase() {
const sql = require('../../sql');
+ /**
+ * This complex structure is needed to calculate total occupied space by a note. Several object instances
+ * (note, revisions, attachments) can point to a single blobId, and thus the blob size should count towards the total
+ * only once.
+ *
+ * @var {Object.<string, Object.<string, int>>} - noteId => { blobId => blobSize }
+ */
const noteBlobs = {};
const noteContentLengths = sql.getRows(`
@@ -145,16 +152,28 @@ function loadNeededInfoFromDatabase() {
}
const revisionContentLengths = sql.getRows(`
- SELECT
- noteId,
- revisions.blobId,
- LENGTH(content) AS length
- FROM notes
- JOIN revisions USING(noteId)
- JOIN blobs ON revisions.blobId = blobs.blobId
- WHERE notes.isDeleted = 0`);
-
- for (const {noteId, blobId, length} of revisionContentLengths) {
+ SELECT
+ noteId,
+ revisions.blobId,
+ LENGTH(content) AS length,
+ 1 AS isNoteRevision
+ FROM notes
+ JOIN revisions USING(noteId)
+ JOIN blobs ON revisions.blobId = blobs.blobId
+ WHERE notes.isDeleted = 0
+ UNION ALL
+ SELECT
+ noteId,
+ revisions.blobId,
+ LENGTH(content) AS length,
+ 0 AS isNoteRevision -- it's attachment not counting towards revision count
+ FROM notes
+ JOIN revisions USING(noteId)
+ JOIN attachments ON attachments.ownerId = revisions.revisionId
+ JOIN blobs ON attachments.blobId = blobs.blobId
+ WHERE notes.isDeleted = 0`);
+
+ for (const {noteId, blobId, length, isNoteRevision} of revisionContentLengths) {
if (!(noteId in becca.notes)) {
log.error(`Note '${noteId}' not found in becca.`);
continue;
@@ -167,7 +186,9 @@ function loadNeededInfoFromDatabase() {
noteBlobs[noteId][blobId] = length;
- becca.notes[noteId].revisionCount++;
+ if (isNoteRevision) {
+ becca.notes[noteId].revisionCount++;
+ }
}
for (const noteId in noteBlobs) {