optimize getTopicsByTids and getTeasers

v1.18.x
barisusakli 11 years ago
parent c6ced111b5
commit 528ee335d5

@ -296,6 +296,13 @@ var db = require('./database'),
db.getObjectField('category:' + cid, field, callback); db.getObjectField('category:' + cid, field, callback);
}; };
Categories.getMultipleCategoryFields = function(cids, fields, callback) {
var keys = cids.map(function(cid) {
return 'category:' + cid;
});
db.getObjectsFields(keys, fields, callback);
};
Categories.getCategoryFields = function(cid, fields, callback) { Categories.getCategoryFields = function(cid, fields, callback) {
db.getObjectFields('category:' + cid, fields, callback); db.getObjectFields('category:' + cid, fields, callback);
}; };

@ -74,22 +74,16 @@ categoriesController.get = function(req, res, next) {
}, },
function(disabled, next) { function(disabled, next) {
if (parseInt(disabled, 10) === 1) { if (parseInt(disabled, 10) === 1) {
return next(new Error('category-disabled')); return next(new Error('[[error:category-disabled]]'));
} }
privileges.categories.get(cid, uid, function(err, categoryPrivileges) { privileges.categories.get(cid, uid, next);
if (err) {
return next(err);
}
if (!categoryPrivileges.read) {
return next(new Error('[[error:no-privileges]]'));
}
next(null, categoryPrivileges);
});
}, },
function (privileges, next) { function (privileges, next) {
if (!privileges.read) {
return next(new Error('[[error:no-privileges]]'));
}
user.getSettings(uid, function(err, settings) { user.getSettings(uid, function(err, settings) {
if (err) { if (err) {
return next(err); return next(err);
@ -111,12 +105,6 @@ categoriesController.get = function(req, res, next) {
return next(err); return next(err);
} }
if (categoryData) {
if (parseInt(categoryData.disabled, 10) === 1) {
return next(new Error('[[error:category-disabled]]'));
}
}
categoryData.privileges = privileges; categoryData.privileges = privileges;
next(err, categoryData); next(err, categoryData);
}); });
@ -189,7 +177,6 @@ categoriesController.get = function(req, res, next) {
active: x === parseInt(page, 10) active: x === parseInt(page, 10)
}); });
} }
res.render('category', data); res.render('category', data);
}); });
}; };

@ -51,6 +51,10 @@ module.exports = function(db, module) {
}); });
}; };
module.getSetsMembers = function(keys, callback) {
throw new Error('not-implemented');
};
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
module.getListRange(key, 0, -1, function(err, set) { module.getListRange(key, 0, -1, function(err, set) {
callback(err, set.length); callback(err, set.length);

@ -91,6 +91,24 @@ module.exports = function(db, module) {
}); });
}; };
module.getSetsMembers = function(keys, callback) {
db.collection('objects').find({_key: {$in: keys}}, {_key: 1, members: 1}).toArray(function(err, data) {
if (err) {
return callback(err);
}
var sets = {};
data.forEach(function(set) {
sets[set._key] = set.members || [];
});
var returnData = new Array(keys.length);
for(var i=0; i<keys.length; ++i) {
returnData[i] = sets[keys[i]] || [];
}
callback(null, returnData);
});
};
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
return callback(err, data ? data.members.length : 0); return callback(err, data ? data.members.length : 0);

@ -54,6 +54,14 @@ module.exports = function(redisClient, module) {
redisClient.smembers(key, callback); redisClient.smembers(key, callback);
}; };
module.getSetsMembers = function(keys, callback) {
var multi = redisClient.multi();
for (var i=0; i<keys.length; ++i) {
multi.smembers(keys[i]);
}
multi.exec(callback);
};
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
redisClient.scard(key, callback); redisClient.scard(key, callback);
}; };

@ -104,6 +104,28 @@ module.exports = function(privileges) {
}); });
}; };
privileges.categories.isAdminOrMod = function(cids, uid, callback) {
async.parallel({
isModerators: function(next) {
user.isModerator(uid, cids, next);
},
isAdmin: function(next) {
user.isAdministrator(uid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var returnData = new Array(cids.length);
for (var i=0; i<cids.length; ++i) {
returnData[i] = results.isAdmin || results.isModerators[i];
}
callback(null, returnData);
});
};
privileges.categories.canMoveAllTopics = function(currentCid, targetCid, uid, callback) { privileges.categories.canMoveAllTopics = function(currentCid, targetCid, uid, callback) {
async.parallel({ async.parallel({
isAdministrator: function(next) { isAdministrator: function(next) {

@ -170,96 +170,83 @@ var async = require('async'),
}; };
Topics.getTopicsByTids = function(tids, uid, callback) { Topics.getTopicsByTids = function(tids, uid, callback) {
if (!Array.isArray(tids) || tids.length === 0) { if (!Array.isArray(tids) || !tids.length) {
return callback(null, []); return callback(null, []);
} }
var categoryCache = {}, Topics.getTopicsData(tids, function(err, topics) {
privilegeCache = {}, function mapFilter(array, field) {
userCache = {}; return array.map(function(topic) {
return topic[field];
}).filter(function(value, index, array) {
function loadTopicInfo(topicData, next) { return array.indexOf(value) === index;
if (!topicData) { });
return next(null, null);
} }
function isTopicVisible(topicData, topicInfo) { if (err) {
if (parseInt(topicInfo.categoryData.disabled, 10) === 1) { return callback(err);
return false;
}
var deleted = parseInt(topicData.deleted, 10) !== 0;
return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(uid, 10);
} }
var uids = mapFilter(topics, 'uid');
var cids = mapFilter(topics, 'cid');
async.parallel({ async.parallel({
hasread: function(next) { users: function(next) {
Topics.hasReadTopic(topicData.tid, uid, next); user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
}, },
teaser: function(next) { categories: function(next) {
Topics.getTeaser(topicData.tid, next); categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
}, },
privileges: function(next) { hasRead: function(next) {
if (privilegeCache[topicData.cid]) { Topics.hasReadTopics(tids, uid, next);
return next(null, privilegeCache[topicData.cid]);
}
privileges.categories.get(topicData.cid, uid, next);
}, },
categoryData: function(next) { isAdminOrMod: function(next) {
if (categoryCache[topicData.cid]) { privileges.categories.isAdminOrMod(cids, uid, next);
return next(null, categoryCache[topicData.cid]);
}
categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
}, },
user: function(next) { teasers: function(next) {
if (userCache[topicData.uid]) { Topics.getTeasers(tids, next);
return next(null, userCache[topicData.uid]);
}
user.getUserFields(topicData.uid, ['username', 'userslug', 'picture'], next);
}, },
tags: function(next) { tags: function(next) {
Topics.getTopicTagsObjects(topicData.tid, next); Topics.getTopicsTagsObjects(tids, next);
} }
}, function(err, topicInfo) { }, function(err, results) {
if(err) { function arrayToObject(array, field) {
return next(err); var obj = {};
for (var i=0; i<array.length; ++i) {
obj[array[i][field]] = array[i];
}
return obj;
} }
privilegeCache[topicData.cid] = topicInfo.privileges; if (err) {
categoryCache[topicData.cid] = topicInfo.categoryData; return callback(err);
userCache[topicData.uid] = topicInfo.user;
if (!isTopicVisible(topicData, topicInfo)) {
return next(null, null);
} }
topicData.pinned = parseInt(topicData.pinned, 10) === 1; var users = arrayToObject(results.users, 'uid');
topicData.locked = parseInt(topicData.locked, 10) === 1; var categories = arrayToObject(results.categories, 'cid');
topicData.deleted = parseInt(topicData.deleted, 10) === 1; var isAdminOrMod = {};
topicData.unread = !(topicInfo.hasread && parseInt(uid, 10) !== 0); cids.forEach(function(cid, index) {
topicData.unreplied = parseInt(topicData.postcount, 10) <= 1; isAdminOrMod[cid] = results.isAdminOrMod[index];
});
topicData.category = topicInfo.categoryData;
topicData.teaser = topicInfo.teaser;
topicData.user = topicInfo.user;
topicData.tags = topicInfo.tags;
next(null, topicData);
});
}
Topics.getTopicsData(tids, function(err, topics) {
if (err) {
return callback(err);
}
async.mapSeries(topics, loadTopicInfo, function(err, topics) { for (var i=0; i<topics.length; ++i) {
if(err) { topics[i].category = categories[topics[i].cid];
return callback(err); topics[i].category.disabled = parseInt(topics[i].category.disabled, 10) === 1;
topics[i].user = users[topics[i].uid];
topics[i].teaser = results.teasers[i];
topics[i].tags = results.tags[i];
topics[i].pinned = parseInt(topics[i].pinned, 10) === 1;
topics[i].locked = parseInt(topics[i].locked, 10) === 1;
topics[i].deleted = parseInt(topics[i].deleted, 10) === 1;
topics[i].unread = !(results.hasRead[i] && parseInt(uid, 10) !== 0);
topics[i].unreplied = parseInt(topics[i].postcount, 10) <= 1;
} }
topics = topics.filter(function(topic) { topics = topics.filter(function(topic) {
return !!topic; return !topic.category.disabled ||
!topic.deleted || (topic.deleted && isAdminOrMod[topic.cid]) ||
parseInt(topic.uid, 10) === parseInt(uid, 10);
}); });
plugins.fireHook('filter:topics.get', topics, callback); plugins.fireHook('filter:topics.get', topics, callback);
@ -336,12 +323,71 @@ var async = require('async'),
}; };
Topics.getTeasers = function(tids, callback) { Topics.getTeasers = function(tids, callback) {
if(!Array.isArray(tids)) { if(!Array.isArray(tids)) {
return callback(null, []); return callback(null, []);
} }
async.map(tids, Topics.getTeaser, callback); 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) {
if (err) {
return callback(err);
}
var postKeys = pids.map(function(pid) {
return 'post:' + pid;
});
async.parallel({
indices: function(next) {
var sets = tids.map(function(tid) {
return 'tid:' + tid + ':posts';
});
db.sortedSetsRanks(sets, pids, next);
},
posts: function(next) {
db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp'], next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var indices = results.indices.map(function(index) {
if (!utils.isNumber(index)) {
return 1;
}
return parseInt(index, 10) + 2;
});
var uids = results.posts.map(function(post) {
return post.uid;
}).filter(function(uid, index, array) {
return array.indexOf(uid) === index;
});
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
if (err) {
return callback(err);
}
var users = {};
userData.forEach(function(user) {
users[user.uid] = user;
});
results.posts.forEach(function(post, index) {
post.user = users[post.uid];
post.index = indices[index];
post.timestamp = utils.toISOString(post.timestamp);
});
callback(err, results.posts);
});
});
});
}; };
Topics.getTeaser = function(tid, callback) { Topics.getTeaser = function(tid, callback) {

@ -92,6 +92,25 @@ module.exports = function(Topics) {
}); });
}; };
Topics.getTopicsTagsObjects = function(tids, callback) {
var sets = tids.map(function(tid) {
return 'topic:' + tid + ':tags';
});
db.getSetsMembers(sets, function(err, members) {
if (err) {
return callback(err);
}
members.forEach(function(tags, index) {
if (Array.isArray(tags)) {
members[index] = mapToObject(tags);
}
})
callback(null, members);
});
};
function mapToObject(tags) { function mapToObject(tags) {
if (!tags) { if (!tags) {
return tags; return tags;

Loading…
Cancel
Save