From 779b2a8d73541bacc492b1226cd34355875f2145 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 6 Sep 2014 00:58:03 -0400 Subject: [PATCH] optimize queries --- src/groups.js | 4 +--- src/posts.js | 14 ++++++++++++-- src/topics.js | 11 ++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/groups.js b/src/groups.js index 219fae8e45..06c03bcffa 100644 --- a/src/groups.js +++ b/src/groups.js @@ -439,15 +439,13 @@ }; Groups.getUserGroups = function(uids, callback) { - var ignoredGroups = ['registered-users']; - db.getSetMembers('groups', function(err, groupNames) { if (err) { return callback(err); } var groupKeys = groupNames.filter(function(groupName) { - return ignoredGroups.indexOf(groupName) === -1; + return groupName !== 'registered-users' && groupName.indexOf(':privileges:') === -1; }).map(function(groupName) { return 'group:' + groupName; }); diff --git a/src/posts.js b/src/posts.js index c70002f4d0..d09ad0d555 100644 --- a/src/posts.js +++ b/src/posts.js @@ -404,15 +404,25 @@ var async = require('async'), var tids = posts.map(function(post) { return post.tid; + }).filter(function(tid, index, array) { + return array.indexOf(tid) === index; }); topics.getTopicsFields(tids, ['cid'], function(err, topics) { if (err) { return callback(err); } - var cids = topics.map(function(topic) { - return topic.cid; + var map = {}; + topics.forEach(function(topic) { + if (topic) { + map[topic.tid] = topic.cid; + } }); + + var cids = posts.map(function(post) { + return map[post.tid]; + }) + callback(null, cids); }); }); diff --git a/src/topics.js b/src/topics.js index 545945af89..2e53e2672c 100644 --- a/src/topics.js +++ b/src/topics.js @@ -348,7 +348,7 @@ var async = require('async'), return callback(err); } - var postKeys = pids.map(function(pid) { + var postKeys = pids.filter(Boolean).map(function(pid) { return 'post:' + pid; }); @@ -379,14 +379,19 @@ var async = require('async'), results.users.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.timestamp = utils.toISOString(post.timestamp); + tidToPost[post.tid] = post; }); - callback(null, postData); + var teasers = tids.map(function(tid) { + return tidToPost[tid]; + }); + + callback(null, teasers); }); }); });