From 0959b1248b25820469f2b83aab711db6c9c22604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 24 Feb 2021 20:38:57 -0500 Subject: [PATCH] perf: make upgrade script faster use bulkAdd/remove --- .../1.10.2/fix_category_post_zsets.js | 63 ++++++------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/src/upgrades/1.10.2/fix_category_post_zsets.js b/src/upgrades/1.10.2/fix_category_post_zsets.js index 1411f894c4..73edf60996 100644 --- a/src/upgrades/1.10.2/fix_category_post_zsets.js +++ b/src/upgrades/1.10.2/fix_category_post_zsets.js @@ -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, }); }, };