Skip to content

Node.js based forum software built for the modern web

faster getTeasers

barisusakli73d358edc56b
diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js
index ab4943c48c..41e0cf0663 100644
--- a/src/database/mongo/sorted.js
+++ b/src/database/mongo/sorted.js
@@ -203,7 +203,7 @@ module.exports = function(db, module) {
module.sortedSetCard = function(key, callback) {
if (!key) {
- return callback();
+ return callback(null, 0);
}
db.collection('objects').count({_key: key}, function(err, count) {
count = parseInt(count, 10);
diff --git a/src/topics.js b/src/topics.js
index 00d107f0fa..df68ec8514 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -193,6 +193,9 @@ var async = require('async'),
var cids = mapFilter(topics, 'cid');
async.parallel({
+ teasers: function(next) {
+ Topics.getTeasers(tids, uid, next);
+ },
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
@@ -205,9 +208,6 @@ var async = require('async'),
isAdminOrMod: function(next) {
privileges.categories.isAdminOrMod(cids, uid, next);
},
- teasers: function(next) {
- Topics.getTeasers(tids, uid, next);
- },
tags: function(next) {
Topics.getTopicsTagsObjects(tids, next);
}
@@ -346,20 +346,29 @@ var async = require('async'),
};
Topics.getTeasers = function(tids, uid, callback) {
- if(!Array.isArray(tids)) {
+ if(!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
- async.map(tids, function(tid, next) {
- db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, data) {
- next(err, Array.isArray(data) && data.length ? data[0] : null);
- });
- }, function(err, pids) {
+ async.parallel({
+ counts: function(next) {
+ async.map(tids, function(tid, next) {
+ db.sortedSetCard('tid:' + tid + ':posts', next);
+ }, next);
+ },
+ pids: function(next) {
+ async.map(tids, function(tid, next) {
+ db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, data) {
+ next(err, Array.isArray(data) && data.length ? data[0] : null);
+ });
+ }, next);
+ }
+ }, function(err, results) {
if (err) {
return callback(err);
}
- var postKeys = pids.filter(Boolean).map(function(pid) {
+ var postKeys = results.pids.filter(Boolean).map(function(pid) {
return 'post:' + pid;
});
@@ -374,26 +383,20 @@ var async = require('async'),
return array.indexOf(uid) === index;
});
- async.parallel({
- users: function(next) {
- user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
- },
- indices: function(next) {
- posts.getPostIndices(postData, uid, next);
- }
- }, function(err, results) {
+
+ user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, usersData) {
if (err) {
return callback(err);
}
var users = {};
- results.users.forEach(function(user) {
+ usersData.forEach(function(user) {
users[user.uid] = user;
});
var tidToPost = {};
postData.forEach(function(post, index) {
post.user = users[post.uid];
- post.index = results.indices[index] + 1;
+ post.index = results.counts[index] + 1;
post.timestamp = utils.toISOString(post.timestamp);
tidToPost[post.tid] = post;
});