From da09ce3e06370e8171f2420803402ca1e4d3275c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 16 Sep 2015 18:13:03 -0400 Subject: [PATCH] optimize getParents when loading the root categories listing all categories have a parentCid of 0 so return early instead of calling the database with ['category:0','category:0','category:0''category:0' ...] --- src/categories.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/categories.js b/src/categories.js index a7cd08eebe..4695f73278 100644 --- a/src/categories.js +++ b/src/categories.js @@ -293,21 +293,34 @@ var async = require('async'), } Categories.getParents = function(cids, callback) { - Categories.getMultipleCategoryFields(cids, ['parentCid'], function(err, data) { - if (err) { - return callback(err); - } + var categoriesData; + var parentCids; + async.waterfall([ + function (next) { + Categories.getMultipleCategoryFields(cids, ['parentCid'], next); + }, + function (_categoriesData, next) { + categoriesData = _categoriesData; - var parentCids = data.map(function(category) { - if (category && category.hasOwnProperty('parentCid') && category.parentCid) { - return category.parentCid; - } else { - return 0; + parentCids = categoriesData.filter(function(category) { + return category && category.hasOwnProperty('parentCid') && category.parentCid; + }).map(function(category) { + return parseInt(category.parentCid, 10); + }); + + if (!parentCids.length) { + return callback(null, cids.map(function() {return null;})); } - }); - Categories.getCategoriesData(parentCids, callback); - }); + Categories.getCategoriesData(parentCids, next); + }, + function (parentData, next) { + parentData = categoriesData.map(function(category) { + return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))]; + }); + next(null, parentData); + } + ], callback); }; Categories.getChildren = function(cids, uid, callback) {