From 86a8b8ab9304e89da6c69e1dfec73d95cebf7d0c Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 27 Feb 2017 15:27:07 -0500 Subject: [PATCH 1/4] posts:votes sorted set --- src/posts.js | 3 +++ src/upgrade.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/posts.js b/src/posts.js index 2f9132a25b..83b8e39a1f 100644 --- a/src/posts.js +++ b/src/posts.js @@ -248,6 +248,9 @@ var plugins = require('./plugins'); }, ], next); }, + function (next) { + db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next); + }, function (next) { Posts.setPostFields(postData.pid, { upvotes: postData.upvotes, downvotes: postData.downvotes }, next); }, diff --git a/src/upgrade.js b/src/upgrade.js index 46075ff5e9..14b394efb1 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -12,7 +12,7 @@ var schemaDate; var thisSchemaDate; // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema -var latestSchema = Date.UTC(2017, 1, 25); +var latestSchema = Date.UTC(2017, 1, 27); Upgrade.check = function (callback) { db.get('schemaDate', function (err, value) { @@ -479,6 +479,50 @@ Upgrade.upgrade = function (callback) { next(); } }, + function (next) { + thisSchemaDate = Date.UTC(2017, 1, 27); + var schemaName = '[2017/2/27] New sorted set posts:votes'; + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.verbose(schemaName); + + db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { + if (err) { + return next(err); + } + + async.eachSeries(cids, function (cid, next) { + db.getSortedSetRevRange('cid:' + cid + ':pids', 0, -1, function (err, pids) { + if (err || !pids) { + return next(err); + } + + async.each(pids, function(pid, next) { + db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { + if (err || !postData) { + return next(err); + } + + var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); + db.sortedSetAdd('posts:votes', votes, pid, next); + }); + }, next); + }); + }, function (err) { + if (err) { + return next(err); + } + + winston.info(schemaName + ' - done'); + Upgrade.update(thisSchemaDate, next); + }); + }); + } else { + winston.info(schemaName + ' - skipped!'); + next(); + } + }, // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!! ], function (err) { From 5d9b6062d6f72cb8d9d40e0ce9555dc619cde4cd Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 27 Feb 2017 17:03:40 -0500 Subject: [PATCH 2/4] simpler method for getting pids --- src/upgrade.js | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/upgrade.js b/src/upgrade.js index 14b394efb1..8976a63290 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -480,43 +480,33 @@ Upgrade.upgrade = function (callback) { } }, function (next) { - thisSchemaDate = Date.UTC(2017, 1, 27); + thisSchemaDate = Date.UTC(2017, 1, 28); var schemaName = '[2017/2/27] New sorted set posts:votes'; if (schemaDate < thisSchemaDate) { updatesMade = true; winston.verbose(schemaName); - db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { + require('./batch').processSortedSet('posts:pid', function (pids, next) { + async.each(pids, function (pid, next) { + async.each(pids, function (pid, next) { + db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { + if (err || !postData) { + return next(err); + } + + var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); + db.sortedSetAdd('posts:votes', votes, pid, next); + }); + }, next); + }, next); + }, {}, function (err) { if (err) { return next(err); } - async.eachSeries(cids, function (cid, next) { - db.getSortedSetRevRange('cid:' + cid + ':pids', 0, -1, function (err, pids) { - if (err || !pids) { - return next(err); - } - - async.each(pids, function(pid, next) { - db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { - if (err || !postData) { - return next(err); - } - - var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); - db.sortedSetAdd('posts:votes', votes, pid, next); - }); - }, next); - }); - }, function (err) { - if (err) { - return next(err); - } - - winston.info(schemaName + ' - done'); - Upgrade.update(thisSchemaDate, next); - }); + winston.info(schemaName + ' - done'); + Upgrade.update(thisSchemaDate, next); }); } else { winston.info(schemaName + ' - skipped!'); From 7bdbe7d9db6536c4e22e7b89d5ad91d3936ef3ce Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 27 Feb 2017 17:04:36 -0500 Subject: [PATCH 3/4] wrong date --- src/upgrade.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/upgrade.js b/src/upgrade.js index 8976a63290..038f7b8389 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -480,7 +480,7 @@ Upgrade.upgrade = function (callback) { } }, function (next) { - thisSchemaDate = Date.UTC(2017, 1, 28); + thisSchemaDate = Date.UTC(2017, 1, 27); var schemaName = '[2017/2/27] New sorted set posts:votes'; if (schemaDate < thisSchemaDate) { From 06eabbc5076a093740aaad1358785eafae944a7a Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 27 Feb 2017 17:07:17 -0500 Subject: [PATCH 4/4] typo --- src/upgrade.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/upgrade.js b/src/upgrade.js index 038f7b8389..2b4510b099 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -489,16 +489,14 @@ Upgrade.upgrade = function (callback) { require('./batch').processSortedSet('posts:pid', function (pids, next) { async.each(pids, function (pid, next) { - async.each(pids, function (pid, next) { - db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { - if (err || !postData) { - return next(err); - } + db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { + if (err || !postData) { + return next(err); + } - var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); - db.sortedSetAdd('posts:votes', votes, pid, next); - }); - }, next); + var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); + db.sortedSetAdd('posts:votes', votes, pid, next); + }); }, next); }, {}, function (err) { if (err) {