Skip to content

Node.js based forum software built for the modern web

perf: make upgrade script faster

Barış Soner Uşaklı0959b1248b25
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,
});
},
};