fix calculating revision attachment size

This commit is contained in:
zadam 2023-10-03 09:05:30 +02:00
parent 055bb39e4d
commit 9db0a062ed

View File

@ -92,6 +92,13 @@ function searchFromRelation(note, relationName) {
function loadNeededInfoFromDatabase() { function loadNeededInfoFromDatabase() {
const sql = require('../../sql'); 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 noteBlobs = {};
const noteContentLengths = sql.getRows(` const noteContentLengths = sql.getRows(`
@ -148,13 +155,25 @@ function loadNeededInfoFromDatabase() {
SELECT SELECT
noteId, noteId,
revisions.blobId, revisions.blobId,
LENGTH(content) AS length LENGTH(content) AS length,
1 AS isNoteRevision
FROM notes FROM notes
JOIN revisions USING(noteId) JOIN revisions USING(noteId)
JOIN blobs ON revisions.blobId = blobs.blobId 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`); WHERE notes.isDeleted = 0`);
for (const {noteId, blobId, length} of revisionContentLengths) { for (const {noteId, blobId, length, isNoteRevision} of revisionContentLengths) {
if (!(noteId in becca.notes)) { if (!(noteId in becca.notes)) {
log.error(`Note '${noteId}' not found in becca.`); log.error(`Note '${noteId}' not found in becca.`);
continue; continue;
@ -167,8 +186,10 @@ function loadNeededInfoFromDatabase() {
noteBlobs[noteId][blobId] = length; noteBlobs[noteId][blobId] = length;
if (isNoteRevision) {
becca.notes[noteId].revisionCount++; becca.notes[noteId].revisionCount++;
} }
}
for (const noteId in noteBlobs) { for (const noteId in noteBlobs) {
becca.notes[noteId].contentAndAttachmentsAndRevisionsSize = Object.values(noteBlobs[noteId]).reduce((acc, size) => acc + size, 0); becca.notes[noteId].contentAndAttachmentsAndRevisionsSize = Object.values(noteBlobs[noteId]).reduce((acc, size) => acc + size, 0);