perf: make upgrade script faster

use bulkAdd/remove
v1.18.x
Barış Soner Uşaklı 4 years ago
parent 14a6c349c2
commit 0959b1248b

@ -2,57 +2,32 @@
const async = require('async');
const db = require('../../database');
const posts = require('../../posts');
const topics = require('../../topics');
const batch = require('../../batch');
module.exports = {
name: 'Fix category post zsets',
timestamp: Date.UTC(2018, 9, 10),
method: function (callback) {
method: async function () {
const { progress } = this;
db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => {
if (err) {
return callback(err);
}
const keys = cids.map(cid => `cid:${cid}:pids`);
const posts = require('../../posts');
batch.processSortedSet('posts:pid', (postData, next) => {
async.eachSeries(postData, (postData, next) => {
progress.incr();
const pid = postData.value;
const timestamp = postData.score;
let cid;
async.waterfall([
function (next) {
posts.getCidByPid(pid, next);
},
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);
const cids = await db.getSortedSetRange('categories:cid', 0, -1);
const keys = cids.map(cid => `cid:${cid}:pids`);
await batch.processSortedSet('posts:pid', async (postData) => {
const pids = postData.map(p => p.value);
const topicData = await posts.getPostsFields(pids, ['tid']);
const categoryData = await topics.getTopicsFields(topicData.map(t => t.tid), ['cid']);
await db.sortedSetRemove(keys, pids);
const bulkAdd = postData.map((p, i) => ([`cid:${categoryData[i].cid}:pids`, p.score, p.value]));
await db.sortedSetAddBulk(bulkAdd);
progress.incr(postData.length);
}, {
batch: 500,
progress: progress,
withScores: true,
});
},
};

Loading…
Cancel
Save