From 9db0a062eddfcc5be09b3a1790ff7a3951252c42 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 3 Oct 2023 09:05:30 +0200 Subject: [PATCH] fix calculating revision attachment size --- src/services/search/services/search.js | 41 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js index 25b8bc848..a174e37b5 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.>} - 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`); + 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} of revisionContentLengths) { + 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) {