From fae28ed3ccb4753bfe0c50508cb7f0c6e21549b4 Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Sat, 24 Sep 2016 14:43:48 -0500 Subject: [PATCH] Add an index for going from a post to its replies --- src/posts.js | 25 +++++++++++++++++++++++++ src/posts/create.js | 6 ++++++ src/posts/delete.js | 11 +++++++++++ src/upgrade.js | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/posts.js b/src/posts.js index 975846859b..0abba7e5a7 100644 --- a/src/posts.js +++ b/src/posts.js @@ -260,4 +260,29 @@ var plugins = require('./plugins'); }); }; + Posts.countReplies = function(pid, callback) { + if (Array.isArray(pid)) { + db.sortedSetsCard(pid.map(function (id) { + return 'pid:' + id + ':replies'; + }), callback); + } else { + db.sortedSetCard('pid:' + pid + ':replies', callback); + } + }; + + Posts.getReplyPids = function(pid, callback) { + db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback); + }; + + Posts.getReplyPosts = function(pid, uid, callback) { + async.waterfall([ + function (next) { + Posts.getReplyPids(pid, next); + }, + function (pids, next) { + Posts.getPostsByPids(pids, uid, next); + } + ], callback); + }; + }(exports)); diff --git a/src/posts/create.js b/src/posts/create.js index 1054c6d694..1c24280f25 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -82,6 +82,12 @@ module.exports = function (Posts) { function (next) { db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); }, + function (next) { + if (!postData.toPid) { + return next(null); + } + db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid, next); + }, function (next) { db.incrObjectField('global', 'postCount', next); } diff --git a/src/posts/delete.js b/src/posts/delete.js index bdaae4a70c..a6b042e768 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -137,6 +137,17 @@ module.exports = function (Posts) { function (next) { deletePostFromUsersVotes(pid, next); }, + function(next) { + Posts.getPostField(pid, 'toPid', function (err, toPid) { + if (err) { + return next(err); + } + if (!parseInt(toPid, 10)) { + return next(null); + } + db.sortedSetRemove('pid:' + toPid + ':replies', pid, next); + }); + }, function (next) { db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next); }, diff --git a/src/upgrade.js b/src/upgrade.js index 03c776dce3..d3547aa280 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -10,7 +10,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2016, 9, 8); + latestSchema = Date.UTC(2016, 9, 14); Upgrade.check = function (callback) { db.get('schemaDate', function (err, value) { @@ -907,6 +907,41 @@ Upgrade.upgrade = function (callback) { winston.info('[2016/10/8] favourite -> bookmark refactor - skipped!'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2016, 9, 14); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.info('[2016/10/14] Creating sorted sets for post replies'); + + var posts = require('./posts'); + var batch = require('./batch'); + batch.processSortedSet('posts:pid', function(ids, next) { + posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function(err, data) { + if (err) { + return next(err); + } + + async.each(data, function(postData, next) { + if (!parseInt(post.toPid, 10)) { + return next(null); + } + db.sortedSetAdd('pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid, next); + }, next); + }); + }, function(err) { + if (err) { + return next(err); + } + + winston.info('[2016/10/14] Creating sorted sets for post replies - done'); + Upgrade.update(thisSchemaDate, next); + }); + } else { + winston.info('[2016/10/14] Creating sorted sets for post replies - skipped!'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!