mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
perf: move attachments to post hash
This commit is contained in:
@@ -1,17 +1,21 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
|
|
||||||
const Attachments = module.exports;
|
const Attachments = module.exports;
|
||||||
|
const posts = require('./index');
|
||||||
|
|
||||||
Attachments.get = async (pid) => {
|
Attachments.get = async (pid) => {
|
||||||
const hashes = await db.getSortedSetMembers(`post:${pid}:attachments`);
|
const hashes = await posts.getPostField(pid, `attachments`);
|
||||||
const keys = hashes.map(hash => `attachment:${hash}`);
|
return Attachments.getAttachments(hashes);
|
||||||
const attachments = (await db.getObjects(keys)).filter(Boolean);
|
};
|
||||||
|
|
||||||
return attachments;
|
Attachments.getAttachments = async (hashes) => {
|
||||||
|
const keys = hashes.map(hash => `attachment:${hash}`);
|
||||||
|
return (await db.getObjects(keys)).filter(Boolean);
|
||||||
};
|
};
|
||||||
|
|
||||||
Attachments.update = async (pid, attachments) => {
|
Attachments.update = async (pid, attachments) => {
|
||||||
@@ -21,12 +25,8 @@ Attachments.update = async (pid, attachments) => {
|
|||||||
|
|
||||||
const bulkOps = {
|
const bulkOps = {
|
||||||
hash: [],
|
hash: [],
|
||||||
zset: {
|
|
||||||
score: [],
|
|
||||||
value: [],
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
const hashes = [];
|
||||||
attachments.filter(Boolean).forEach(({ _type, mediaType, url, name, width, height }, idx) => {
|
attachments.filter(Boolean).forEach(({ _type, mediaType, url, name, width, height }, idx) => {
|
||||||
if (!url) { // only required property
|
if (!url) { // only required property
|
||||||
return;
|
return;
|
||||||
@@ -40,21 +40,21 @@ Attachments.update = async (pid, attachments) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bulkOps.hash.push([key, { _type, mediaType, url, name, width, height }]);
|
bulkOps.hash.push([key, { _type, mediaType, url, name, width, height }]);
|
||||||
bulkOps.zset.score.push(idx);
|
hashes.push(hash);
|
||||||
bulkOps.zset.value.push(hash);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
db.setObjectBulk(bulkOps.hash),
|
db.setObjectBulk(bulkOps.hash),
|
||||||
db.sortedSetAdd(`post:${pid}:attachments`, bulkOps.zset.score, bulkOps.zset.value),
|
db.setObjectField(`post:${pid}`, 'attachments', hashes.join(',')),
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Attachments.empty = async (pids) => {
|
Attachments.empty = async (pids) => {
|
||||||
const zsets = pids.map(pid => `post:${pid}:attachments`);
|
const postKeys = pids.map(pid => `post:${pid}`);
|
||||||
const hashes = await db.getSortedSetsMembers(zsets);
|
const hashes = await posts.getPostsFields(postKeys, ['attachments']);
|
||||||
let keys = hashes.reduce((memo, hashes) => new Set([...memo, ...hashes]), new Set());
|
const keys = _.uniq(_.flatten(hashes));
|
||||||
keys = Array.from(keys).map(hash => `attachment:${hash}`);
|
await Promise.all([
|
||||||
|
db.deleteAll(keys.map(hash => `attachment:${hash}`)),
|
||||||
await db.deleteAll(keys.concat(zsets));
|
db.deleteObjectFields(postKeys, ['attachments']),
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,5 +67,8 @@ function modifyPost(post, fields) {
|
|||||||
if (post.hasOwnProperty('edited')) {
|
if (post.hasOwnProperty('edited')) {
|
||||||
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
|
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
|
||||||
}
|
}
|
||||||
|
if (!fields.length || fields.includes('attachments')) {
|
||||||
|
post.attachments = (post.attachments || '').split(',').filter(Boolean);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user