diff options
Diffstat (limited to 'jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js')
-rw-r--r-- | jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js | 107 |
1 files changed, 53 insertions, 54 deletions
diff --git a/jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js b/jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js index 84da15b1b8f..55bc6f36f06 100644 --- a/jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js +++ b/jstests/libs/override_methods/implicitly_wrap_pipelines_in_facets.js @@ -4,73 +4,72 @@ * yield the same results, but stress the logic of the $facet stage. */ (function() { - 'use strict'; +'use strict'; - // Set the batch size of the $facet stage's buffer to be lower. This will further stress the - // batching logic, since most pipelines will fall below the default size of 100MB. - assert.commandWorked( - db.adminCommand({setParameter: 1, internalQueryFacetBufferSizeBytes: 1000})); +// Set the batch size of the $facet stage's buffer to be lower. This will further stress the +// batching logic, since most pipelines will fall below the default size of 100MB. +assert.commandWorked(db.adminCommand({setParameter: 1, internalQueryFacetBufferSizeBytes: 1000})); - // Save a reference to the original runCommand method in the IIFE's scope. - // This scoping allows the original method to be called by the override below. - var originalRunCommand = Mongo.prototype.runCommand; +// Save a reference to the original runCommand method in the IIFE's scope. +// This scoping allows the original method to be called by the override below. +var originalRunCommand = Mongo.prototype.runCommand; - Mongo.prototype.runCommand = function(dbName, cmdObj, options) { - // Skip wrapping the pipeline in a $facet stage if it's not an aggregation, or if it's - // possibly an invalid one without a pipeline. - if (typeof cmdObj !== 'object' || cmdObj === null || !cmdObj.hasOwnProperty('aggregate') || - !cmdObj.hasOwnProperty('pipeline') || !Array.isArray(cmdObj.pipeline)) { - return originalRunCommand.apply(this, arguments); - } +Mongo.prototype.runCommand = function(dbName, cmdObj, options) { + // Skip wrapping the pipeline in a $facet stage if it's not an aggregation, or if it's + // possibly an invalid one without a pipeline. + if (typeof cmdObj !== 'object' || cmdObj === null || !cmdObj.hasOwnProperty('aggregate') || + !cmdObj.hasOwnProperty('pipeline') || !Array.isArray(cmdObj.pipeline)) { + return originalRunCommand.apply(this, arguments); + } - var originalPipeline = cmdObj.pipeline; + var originalPipeline = cmdObj.pipeline; + + if (originalPipeline.length === 0) { + // Empty pipelines are disallowed within a $facet stage. + print('Not wrapping empty pipeline in a $facet stage'); + return originalRunCommand.apply(this, arguments); + } - if (originalPipeline.length === 0) { - // Empty pipelines are disallowed within a $facet stage. - print('Not wrapping empty pipeline in a $facet stage'); + const stagesDisallowedInsideFacet = + ['$changeStream', '$collStats', '$facet', '$geoNear', '$indexStats', '$merge', '$out']; + for (let stageSpec of originalPipeline) { + // Skip wrapping the pipeline in a $facet stage if it has an invalid stage + // specification. + if (typeof stageSpec !== 'object' || stageSpec === null) { + print('Not wrapping invalid pipeline in a $facet stage'); return originalRunCommand.apply(this, arguments); } - const stagesDisallowedInsideFacet = - ['$changeStream', '$collStats', '$facet', '$geoNear', '$indexStats', '$merge', '$out']; - for (let stageSpec of originalPipeline) { - // Skip wrapping the pipeline in a $facet stage if it has an invalid stage - // specification. - if (typeof stageSpec !== 'object' || stageSpec === null) { - print('Not wrapping invalid pipeline in a $facet stage'); + if (stageSpec.hasOwnProperty('$match') && typeof stageSpec.$match === 'object' && + stageSpec.$match !== null) { + if (stageSpec.$match.hasOwnProperty('$text')) { + // A $text search is disallowed within a $facet stage. + print('Not wrapping $text in a $facet stage'); return originalRunCommand.apply(this, arguments); } - - if (stageSpec.hasOwnProperty('$match') && typeof stageSpec.$match === 'object' && - stageSpec.$match !== null) { - if (stageSpec.$match.hasOwnProperty('$text')) { - // A $text search is disallowed within a $facet stage. - print('Not wrapping $text in a $facet stage'); - return originalRunCommand.apply(this, arguments); - } - if (Object.keys(stageSpec.$match).length === 0) { - // Skip wrapping an empty $match stage, since it can be optimized out, resulting - // in an empty pipeline which is disallowed within a $facet stage. - print('Not wrapping empty $match in a $facet stage'); - return originalRunCommand.apply(this, arguments); - } + if (Object.keys(stageSpec.$match).length === 0) { + // Skip wrapping an empty $match stage, since it can be optimized out, resulting + // in an empty pipeline which is disallowed within a $facet stage. + print('Not wrapping empty $match in a $facet stage'); + return originalRunCommand.apply(this, arguments); } + } - // Skip wrapping the pipeline in a $facet stage if it contains a stage disallowed inside - // a $facet. - for (let disallowedStage of stagesDisallowedInsideFacet) { - if (stageSpec.hasOwnProperty(disallowedStage)) { - print('Not wrapping ' + disallowedStage + ' in a $facet stage'); - return originalRunCommand.apply(this, arguments); - } + // Skip wrapping the pipeline in a $facet stage if it contains a stage disallowed inside + // a $facet. + for (let disallowedStage of stagesDisallowedInsideFacet) { + if (stageSpec.hasOwnProperty(disallowedStage)) { + print('Not wrapping ' + disallowedStage + ' in a $facet stage'); + return originalRunCommand.apply(this, arguments); } } + } - cmdObj.pipeline = [ - {$facet: {originalPipeline: originalPipeline}}, - {$unwind: '$originalPipeline'}, - {$replaceRoot: {newRoot: '$originalPipeline'}}, - ]; - return originalRunCommand.apply(this, arguments); - }; + cmdObj.pipeline = [ + {$facet: {originalPipeline: originalPipeline}}, + {$unwind: '$originalPipeline'}, + {$replaceRoot: {newRoot: '$originalPipeline'}}, + ]; + return originalRunCommand.apply(this, arguments); +}; }()); |