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() {
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`);
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) {