Просмотр исходного кода

Refs #2150: allow 'pipelineArgs' to be passed down to the Pipeline ctor.

Charles Ezell 13 лет назад
Родитель
Сommit
b52b4e5983
4 измененных файлов с 18 добавлено и 13 удалено
  1. 3 0
      README.md
  2. 5 5
      lib/Aggregator.js
  3. 2 2
      lib/commands/PipelineCommand.js
  4. 8 6
      lib/pipeline/Pipeline.js

+ 3 - 0
README.md

@@ -21,6 +21,9 @@ Here is a list of the major items where we have deviated from the MongoDB code a
       * DESIGN: Basically all of the `BSON`-specific code has become equivalent `JSON`-specific code since that's what we're working with (no need for needless conversions)
       * DESIGN: A lot of these have a `addToBson...` and other `BSONObjBuilder`-related methods that take in an instance to be modified but it's owned by the caller; in `munge` we build a new `Object` and return it because it's simpler and that's how they're generally used anyhow
     * TESTING: Many of the tests have been written without the use of the testing base classes as they are in the MongoDB code to try and simplify and make things more clear (but never less complete)
+  * **`Aggregator` class**
+    * This is a helper class that runs a pipeline.  If you provide a dictionary `pipelineArgs` after the document sources, `pipelineArgs` will be
+	  pass as the `theCtx` to your Pipeline class (the current implementation does not use it for anything).
   * **Pipeline components**
     * `Document` class
       * DESIGN: `Document` now provides static helpers rather than instance helpers to avoid unecessary boxing/unboxing since that seems to make more sense here (we treat any `Object` like a `Document`)

+ 5 - 5
lib/Aggregator.js

@@ -3,14 +3,14 @@ var PipelineCommand = require('./commands/PipelineCommand');
 
 var Aggregator = module.exports = (function(){
 	// CONSTRUCTOR
-	var base = Object, proto, klass = function Aggregator(ops){
-		if (!ops){
+	var base = Object, proto, klass = function Aggregator(docSrcs, pipelineArgs){
+		if (!docSrcs){
 			throw new Error("mungedb Aggregator requires a pipeline!");
 		}
-		if (typeof ops.length !== "number"){
-			ops = [ops];
+		if (typeof docSrcs.length !== "number"){
+			docSrcs = [docSrcs];
 		}
-		this.pipeline = new PipelineCommand(ops);
+		this.pipeline = new PipelineCommand(docSrcs, pipelineArgs);
 	};
 	proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 

+ 2 - 2
lib/commands/PipelineCommand.js

@@ -9,11 +9,11 @@ var PipelineCommand = module.exports = (function(){
 	 * @module mungedb-aggregate
 	 * @constructor
 	 **/
-	var klass = function PipelineCommand(cmdObj){
+	var klass = function PipelineCommand(docSrcs, pipelineArgs){
         /* try to parse the command; if this fails, then we didn't run */
         //NOTE: this is different from the mongo implementation.  It used to be in the 'run' method that we would parse the pipeline, 
         //but we decided it would be better to be able to save the parsed command
-        this.pPipeline = klass.Pipeline.parseCommand(cmdObj);
+        this.pPipeline = klass.Pipeline.parseCommand(docSrcs, pipelineArgs);
 	}, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
     //Put these on the class so they can be overriden.

+ 8 - 6
lib/pipeline/Pipeline.js

@@ -38,15 +38,17 @@ var Pipeline = module.exports = (function(){
 	 * @param	{Object} cmdObj the command object sent from the client
 	 * @returns	{Array}	the pipeline, if created, otherwise a NULL reference
 	 **/
-	klass.parseCommand = function parseCommand(cmdObj){
-		var pipelineInstance = new (require("../Aggregator")).Pipeline(),
-			pipeline = cmdObj;//munge: skipping the command parsing since all we care about is the pipeline
-		
+	klass.parseCommand = function parseCommand(docSrcs, pipelineArgs){
+		//We are require()'ing this here in case the Pipeline class was overriden by someone else.
+		var pipelineInstance = new (require("../Aggregator")).Pipeline(pipelineArgs);
+
+		//Note: munge: skipping the command parsing since all we care about is the pipeline
+
 		var sourceVector = pipelineInstance.sourceVector,
-			nSteps = pipeline.length;
+			nSteps = docSrcs.length;
 		for( var iStep = 0; iStep<nSteps; ++iStep){
 			/* pull out the pipeline element as an object */
-			var pipeElement = pipeline[iStep];
+			var pipeElement = docSrcs[iStep];
 			if (!(pipeElement instanceof Object)){
 				throw new Error("pipeline element " + iStep + " is not an object; code 15942" );
 			}