From 0dad568cbe9258093324f954a21e2dbe0695b618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 25 Feb 2021 11:31:09 -0500 Subject: [PATCH] perf: faster category tags upgrade script --- src/upgrades/1.16.0/category_tags.js | 33 +++++++++++++++------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/upgrades/1.16.0/category_tags.js b/src/upgrades/1.16.0/category_tags.js index 4d770b3b94..199185ab14 100644 --- a/src/upgrades/1.16.0/category_tags.js +++ b/src/upgrades/1.16.0/category_tags.js @@ -12,23 +12,26 @@ module.exports = { const { progress } = this; await batch.processSortedSet('topics:tid', async (tids) => { - await async.eachSeries(tids, async (tid) => { - const [topicData, tags] = await Promise.all([ - topics.getTopicFields(tid, ['cid', 'timestamp']), - topics.getTopicTags(tid), - ]); + const [topicData, tags] = await Promise.all([ + topics.getTopicsFields(tids, ['tid', 'cid', 'timestamp']), + topics.getTopicsTags(tids), + ]); + const topicsWithTags = topicData.map((t, i) => { + t.tags = tags[i]; + return t; + }).filter(t => t && t.tags.length); - if (tags.length) { - const { cid } = topicData; - await async.eachSeries(tags, async (tag) => { - await db.sortedSetAdd(`cid:${cid}:tag:${tag}:topics`, topicData.timestamp, tid); - const count = await db.sortedSetCard(`cid:${cid}:tag:${tag}:topics`); - await db.sortedSetAdd(`cid:${cid}:tags`, count, tag); - }); - } - - progress.incr(); + await async.eachSeries(topicsWithTags, async (topicObj) => { + const { cid, tags } = topicObj; + await db.sortedSetsAdd( + tags.map(tag => `cid:${cid}:tag:${tag}:topics`), + topicObj.timestamp, + topicObj.tid + ); + const counts = await db.sortedSetsCard(tags.map(tag => `cid:${cid}:tag:${tag}:topics`)); + await db.sortedSetAdd(`cid:${cid}:tags`, counts, tags); }); + progress.incr(tids.length); }, { batch: 500, progress: progress,