mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	optimize getPostSummaryByPids
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| "use strict"; | ||||
|  | ||||
| var async = require('async'); | ||||
|  | ||||
| module.exports = function(db, module) { | ||||
| 	var helpers = module.helpers.mongo; | ||||
|  | ||||
| @@ -119,6 +121,17 @@ module.exports = function(db, module) { | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	module.sortedSetsRanks = function(keys, values, callback) { | ||||
| 		var data = new Array(values.length); | ||||
| 		for (var i=0; i<values.length; ++i) { | ||||
| 			data[i] = {key: keys[i], value: values[i]}; | ||||
| 		} | ||||
|  | ||||
| 		async.map(data, function(item, next) { | ||||
| 			getSortedSetRank(module.getSortedSetRange, item.key, item.value, next); | ||||
| 		}, callback); | ||||
| 	}; | ||||
|  | ||||
| 	module.sortedSetScore = function(key, value, callback) { | ||||
| 		value = helpers.valueToString(value); | ||||
| 		db.collection('objects').findOne({_key:key, value: value}, {fields:{score:1}}, function(err, result) { | ||||
|   | ||||
| @@ -54,6 +54,14 @@ module.exports = function(redisClient, module) { | ||||
| 		redisClient.zrank(key, value, callback); | ||||
| 	}; | ||||
|  | ||||
| 	module.sortedSetsRanks = function(keys, values, callback) { | ||||
| 		var multi = redisClient.multi(); | ||||
| 		for(var i=0; i<values.length; ++i) { | ||||
| 			multi.zrank(keys[i], values[i]); | ||||
| 		} | ||||
| 		multi.exec(callback); | ||||
| 	}; | ||||
|  | ||||
| 	module.sortedSetRevRank = function(key, value, callback) { | ||||
| 		redisClient.zrevrank(key, value, callback); | ||||
| 	}; | ||||
|   | ||||
							
								
								
									
										158
									
								
								src/posts.js
									
									
									
									
									
								
							
							
						
						
									
										158
									
								
								src/posts.js
									
									
									
									
									
								
							| @@ -265,64 +265,6 @@ var async = require('async'), | ||||
| 		options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false; | ||||
| 		options.parse = options.hasOwnProperty('parse') ? options.parse : true; | ||||
|  | ||||
| 		function getPostSummary(post, callback) { | ||||
|  | ||||
| 			post.relativeTime = utils.toISOString(post.timestamp); | ||||
|  | ||||
| 			async.parallel({ | ||||
| 				user: function(next) { | ||||
| 					user.getUserFields(post.uid, ['username', 'userslug', 'picture'], next); | ||||
| 				}, | ||||
| 				topicCategory: function(next) { | ||||
| 					topics.getTopicFields(post.tid, ['title', 'cid', 'slug', 'deleted'], function(err, topicData) { | ||||
| 						if (err) { | ||||
| 							return next(err); | ||||
| 						} else if (parseInt(topicData.deleted, 10) === 1) { | ||||
| 							return callback(); | ||||
| 						} | ||||
|  | ||||
| 						categories.getCategoryFields(topicData.cid, ['name', 'icon', 'slug'], function(err, categoryData) { | ||||
| 							if (err) { | ||||
| 								return next(err); | ||||
| 							} | ||||
|  | ||||
| 							topicData.title = validator.escape(topicData.title); | ||||
|  | ||||
| 							next(null, {topic: topicData, category: categoryData}); | ||||
| 						}); | ||||
| 					}); | ||||
| 				}, | ||||
| 				content: function(next) { | ||||
| 					if (!post.content || !options.parse) { | ||||
| 						return next(null, post.content); | ||||
| 					} | ||||
|  | ||||
| 					postTools.parse(post.content, next); | ||||
| 				}, | ||||
| 				index: function(next) { | ||||
| 					Posts.getPidIndex(post.pid, next); | ||||
| 				} | ||||
| 			}, function(err, results) { | ||||
| 				if (err) { | ||||
| 					return callback(err); | ||||
| 				} | ||||
|  | ||||
| 				post.user = results.user; | ||||
| 				post.topic = results.topicCategory.topic; | ||||
| 				post.category = results.topicCategory.category; | ||||
| 				post.index = results.index; | ||||
|  | ||||
| 				if (options.stripTags && results.content) { | ||||
| 					var s = S(results.content); | ||||
| 					post.content = s.stripTags.apply(s, utils.stripTags).s; | ||||
| 				} else { | ||||
| 					post.content = results.content; | ||||
| 				} | ||||
|  | ||||
| 				callback(null, post); | ||||
| 			}); | ||||
| 		} | ||||
|  | ||||
| 		var keys = pids.map(function(pid) { | ||||
| 			return 'post:' + pid; | ||||
| 		}); | ||||
| @@ -336,7 +278,102 @@ var async = require('async'), | ||||
| 				return !!p && parseInt(p.deleted, 10) !== 1; | ||||
| 			}); | ||||
|  | ||||
| 			async.map(posts, getPostSummary, function(err, posts) { | ||||
| 			var uids = [], tids = []; | ||||
| 			for(var i=0; i<posts.length; ++i) { | ||||
| 				if (uids.indexOf(posts[i].uid) === -1) { | ||||
| 					uids.push(posts[i].uid); | ||||
| 				} | ||||
| 				if (tids.indexOf('topic:' + posts[i].tid) === -1) { | ||||
| 					tids.push('topic:' + posts[i].tid); | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			async.parallel({ | ||||
| 				users: function(next) { | ||||
| 					user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next); | ||||
| 				}, | ||||
| 				topicsAndCategories: function(next) { | ||||
| 					db.getObjectsFields(tids, ['tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) { | ||||
| 						if (err) { | ||||
| 							return next(err); | ||||
| 						} | ||||
|  | ||||
| 						var cidKeys = topics.map(function(topic) { | ||||
| 							return 'category:' + topic.cid; | ||||
| 						}).filter(function(value, index, array) { | ||||
| 							return array.indexOf(value) === index; | ||||
| 						}); | ||||
|  | ||||
| 						db.getObjectsFields(cidKeys, ['cid', 'name', 'icon', 'slug'], function(err, categories) { | ||||
| 							next(err, {topics: topics, categories: categories}); | ||||
| 						}); | ||||
| 					}); | ||||
| 				}, | ||||
| 				indices: function(next) { | ||||
| 					var pids = [], keys = []; | ||||
| 					for (var i=0; i<posts.length; ++i) { | ||||
| 						pids.push(posts[i].pid); | ||||
| 						keys.push('tid:' + posts[i].tid + ':posts'); | ||||
| 					} | ||||
|  | ||||
| 					db.sortedSetsRanks(keys, pids, next); | ||||
| 				} | ||||
| 			}, function(err, results) { | ||||
| 				function toObject(key, data) { | ||||
| 					var obj = {}; | ||||
| 					for(var i=0; i<data.length; ++i) { | ||||
| 						obj[data[i][key]] = data[i]; | ||||
| 					} | ||||
| 					return obj; | ||||
| 				} | ||||
|  | ||||
| 				if (err) { | ||||
| 					return callback(err); | ||||
| 				} | ||||
|  | ||||
| 				results.users = toObject('uid', results.users); | ||||
| 				results.topics = toObject('tid', results.topicsAndCategories.topics); | ||||
| 				results.categories = toObject('cid', results.topicsAndCategories.categories); | ||||
|  | ||||
| 				for(var i=0; i<posts.length; ++i) { | ||||
| 					if (!utils.isNumber(results.indices[i])) { | ||||
| 						posts[i].index = 1; | ||||
| 					} else { | ||||
| 						posts[i].index = parseInt(results.indices[i], 10) + 2; | ||||
| 					} | ||||
| 				} | ||||
|  | ||||
| 				async.map(posts, function(post, next) { | ||||
| 					if (parseInt(results.topics[post.tid].deleted, 10) === 1) { | ||||
| 						return next(); | ||||
| 					} | ||||
|  | ||||
| 					post.user = results.users[post.uid]; | ||||
| 					post.topic = results.topics[post.tid]; | ||||
| 					post.category = results.categories[post.topic.cid]; | ||||
|  | ||||
| 					post.topic.title = validator.escape(post.topic.title); | ||||
| 					post.relativeTime = utils.toISOString(post.timestamp); | ||||
|  | ||||
| 					if (!post.content || !options.parse) { | ||||
| 						return next(null, post); | ||||
| 					} | ||||
|  | ||||
| 					postTools.parse(post.content, function(err, content) { | ||||
| 						if (err) { | ||||
| 							return next(err); | ||||
| 						} | ||||
|  | ||||
| 						if (options.stripTags && content) { | ||||
| 							var s = S(content); | ||||
| 							post.content = s.stripTags.apply(s, utils.stripTags).s; | ||||
| 						} else { | ||||
| 							post.content = content; | ||||
| 						} | ||||
|  | ||||
| 						next(null, post); | ||||
| 					}); | ||||
| 				}, function(err, posts) { | ||||
| 					if (err) { | ||||
| 						return callback(err); | ||||
| 					} | ||||
| @@ -345,7 +382,8 @@ var async = require('async'), | ||||
| 						return !!post; | ||||
| 					}); | ||||
|  | ||||
| 				return callback(null, posts); | ||||
| 					callback(null, posts); | ||||
| 				}); | ||||
| 			}); | ||||
| 		}); | ||||
| 	}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user