mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-11 01:52:55 +01:00
perf: make upgrade script faster
use bulkAdd/remove
This commit is contained in:
@@ -2,57 +2,32 @@
|
|||||||
|
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
const db = require('../../database');
|
const db = require('../../database');
|
||||||
|
const posts = require('../../posts');
|
||||||
|
const topics = require('../../topics');
|
||||||
const batch = require('../../batch');
|
const batch = require('../../batch');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'Fix category post zsets',
|
name: 'Fix category post zsets',
|
||||||
timestamp: Date.UTC(2018, 9, 10),
|
timestamp: Date.UTC(2018, 9, 10),
|
||||||
method: function (callback) {
|
method: async function () {
|
||||||
const { progress } = this;
|
const { progress } = this;
|
||||||
|
|
||||||
db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => {
|
const cids = await db.getSortedSetRange('categories:cid', 0, -1);
|
||||||
if (err) {
|
const keys = cids.map(cid => `cid:${cid}:pids`);
|
||||||
return callback(err);
|
|
||||||
}
|
await batch.processSortedSet('posts:pid', async (postData) => {
|
||||||
const keys = cids.map(cid => `cid:${cid}:pids`);
|
const pids = postData.map(p => p.value);
|
||||||
const posts = require('../../posts');
|
const topicData = await posts.getPostsFields(pids, ['tid']);
|
||||||
batch.processSortedSet('posts:pid', (postData, next) => {
|
const categoryData = await topics.getTopicsFields(topicData.map(t => t.tid), ['cid']);
|
||||||
async.eachSeries(postData, (postData, next) => {
|
|
||||||
progress.incr();
|
await db.sortedSetRemove(keys, pids);
|
||||||
const pid = postData.value;
|
const bulkAdd = postData.map((p, i) => ([`cid:${categoryData[i].cid}:pids`, p.score, p.value]));
|
||||||
const timestamp = postData.score;
|
await db.sortedSetAddBulk(bulkAdd);
|
||||||
let cid;
|
progress.incr(postData.length);
|
||||||
async.waterfall([
|
}, {
|
||||||
function (next) {
|
batch: 500,
|
||||||
posts.getCidByPid(pid, next);
|
progress: progress,
|
||||||
},
|
withScores: true,
|
||||||
function (_cid, next) {
|
|
||||||
cid = _cid;
|
|
||||||
db.isMemberOfSortedSets(keys, pid, next);
|
|
||||||
},
|
|
||||||
function (isMembers, next) {
|
|
||||||
const memberCids = [];
|
|
||||||
isMembers.forEach((isMember, index) => {
|
|
||||||
if (isMember) {
|
|
||||||
memberCids.push(cids[index]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (memberCids.length > 1) {
|
|
||||||
async.waterfall([
|
|
||||||
async.apply(db.sortedSetRemove, memberCids.map(cid => `cid:${cid}:pids`), pid),
|
|
||||||
async.apply(db.sortedSetAdd, `cid:${cid}:pids`, timestamp, pid),
|
|
||||||
], next);
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, next);
|
|
||||||
}, {
|
|
||||||
progress: progress,
|
|
||||||
withScores: true,
|
|
||||||
}, callback);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user