Add an index for going from a post to its replies

v1.18.x
Ben Lubar 8 years ago
parent 2824ce5587
commit fae28ed3cc

@ -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));

@ -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);
}

@ -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);
},

@ -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!!!

Loading…
Cancel
Save