From 3d26c74b6262aa387ba72032ed0c5ffc867ce4a3 Mon Sep 17 00:00:00 2001 From: Theodor Diaconu Date: Sat, 7 Jul 2018 09:10:31 +0300 Subject: [PATCH] Performance fixes for assembler --- lib/query/hypernova/assembler.js | 56 +++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/lib/query/hypernova/assembler.js b/lib/query/hypernova/assembler.js index 33679eb..a02d1d5 100644 --- a/lib/query/hypernova/assembler.js +++ b/lib/query/hypernova/assembler.js @@ -20,13 +20,51 @@ export default (childCollectionNode, {limit, skip, metaFilters}) => { }) } - _.each(parent.results, result => { - let data = assembleData(childCollectionNode, result, { - fieldStorage, strategy, isSingle - }); + const resultsByKeyId = _.groupBy(childCollectionNode.results, '_id'); - result[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip}) - }); + if (strategy === 'one') { + parent.results.forEach(parentResult => { + parentResult[childCollectionNode.linkName] = + filterAssembledData( + resultsByKeyId[parentResult[fieldStorage]], + {limit, skip} + ) + }) + } + + if (strategy === 'many') { + parent.results.forEach(parentResult => { + let data = []; + parentResult[fieldStorage].forEach(_id => { + data.push(resultsByKeyId[_id]); + }) + + parentResult[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip}); + }) + } + + if (strategy === 'one-meta') { + parent.results.forEach(parentResult => { + parentResult[childCollectionNode.linkName] = + filterAssembledData( + resultsByKeyId[parentResult[fieldStorage]._id], + {limit, skip} + ) + }) + } + + + if (strategy === 'many-meta') { + parent.results.forEach(parentResult => { + const _ids = _.pluck(parentResult[fieldStorage], '_id'); + let data = []; + _ids.forEach(_id => { + data.push(resultsByKeyId[_id]); + }) + + parentResult[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip}); + }) + } } function filterAssembledData(data, {limit, skip}) { @@ -36,9 +74,3 @@ function filterAssembledData(data, {limit, skip}) { return data; } - -function assembleData(childCollectionNode, result, {fieldStorage, strategy}) { - const filters = createSearchFilters(result, fieldStorage, strategy, false); - - return sift(filters, childCollectionNode.results); -}