Преглед на файлове

Refs #5125: Implement 2.5 version of LimitDocumentSource

Chris Sexton преди 11 години
родител
ревизия
8afc04dd3c
променени са 2 файла, в които са добавени 49 реда и са изтрити 99 реда
  1. 13 33
      lib/pipeline/documentSources/LimitDocumentSource.js
  2. 36 66
      test/lib/pipeline/documentSources/LimitDocumentSource.js

+ 13 - 33
lib/pipeline/documentSources/LimitDocumentSource.js

@@ -1,5 +1,7 @@
 "use strict";
 
+var DocumentSource = require('./DocumentSource');
+
 /**
  * A document source limiter
  * @class LimitDocumentSource
@@ -35,46 +37,24 @@ proto.coalesce = function coalesce(nextSource) {
 
 	// if it's not another $limit, we can't coalesce
 	if (!nextLimit) return false;
-	
+
 	// we need to limit by the minimum of the two limits
 	if (nextLimit.limit < this.limit) this.limit = nextLimit.limit;
 
 	return true;
 };
 
-/**
- * Is the source at EOF?
- * @method	eof
- **/
-proto.eof = function eof() {
-	return this.source.eof() || this.count >= this.limit;
-};
-
-/**
- * some implementations do the equivalent of verify(!eof()) so check eof() first
- * @method	getCurrent
- * @returns	{Document}	the current Document without advancing
- **/
-proto.getCurrent = function getCurrent() {
-	return this.source.getCurrent();
-};
-
-/**
- * Advance the state of the DocumentSource so that it will return the next Document.
- * The default implementation returns false, after checking for interrupts.
- * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
- *
- * @method	advance
- * @returns	{Boolean}	whether there is another document to fetch, i.e., whether or not getCurrent() will succeed.  This default implementation always returns false.
- **/
-proto.advance = function advance() {
-	base.prototype.advance.call(this); // check for interrupts
-	++this.count;
-	if (this.count >= this.limit) {
-		return false;
+proto.getNext = function getNext(callback) {
+	if (++this.count > this.limit) {
+		this.source.dispose();
+		if (callback)
+			return callback(DocumentSource.EOF);
+		return DocumentSource.EOF;
 	}
-	this.current = this.source.getCurrent();
-	return this.source.advance();
+
+	if (callback)
+		return this.source.getNext(callback);
+	return this.source.getNext();
 };
 
 /**

+ 36 - 66
test/lib/pipeline/documentSources/LimitDocumentSource.js

@@ -1,5 +1,6 @@
 "use strict";
 var assert = require("assert"),
+	DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
 	LimitDocumentSource = require("../../../../lib/pipeline/documentSources/LimitDocumentSource");
 
 
@@ -47,86 +48,55 @@ module.exports = {
 
 		},
 
-		"#eof()": {
+		"#getNext()": {
 
-			"should return true if there are no more sources": function noSources(){
-				var lds = new LimitDocumentSource();
-				lds.limit = 9;
-				lds.count = 0;
-				lds.source = {
-					eof: function(){
-						return true;
-					}
-				};
-				assert.equal(lds.eof(), true);
-			},
-			"should return true if limit is hit": function hitLimit(){
-				var lds = new LimitDocumentSource();
-				lds.limit = 9;
-				lds.count = 9;
-				lds.source = {
-					eof: function(){
-						return false;
-					}
-				};
-				assert.equal(lds.eof(), true);
-			},
-			"should return false if limit is not hit and there are more documents": function hitLimit(){
-				var lds = new LimitDocumentSource();
-				lds.limit = 10;
-				lds.count = 9;
-				lds.source = {
-					eof: function(){
-						return false;
-					}
-				};
-				assert.equal(lds.eof(), false);
-			}
-
-		},
-
-		"#getCurrent()": {
-
-			"should return the current document source": function currSource(){
+			"should return the current document source": function currSource(next){
 				var lds = new LimitDocumentSource();
 				lds.limit = 1;
-				lds.source = {getCurrent:function(){return { item:1 };}};
-				assert.deepEqual(lds.getCurrent(), { item:1 }); 
-			}
-
-		},
-
-		"#advance()": {
-
-			"should return true for moving to the next source": function nextSource(){
-				var lds = new LimitDocumentSource();
-				lds.count = 0;
-				lds.limit = 2;
-				lds.source = {
-					getCurrent:function(){return { item:1 };},
-					advance:function(){return true;}
-				};
-				assert.strictEqual(lds.advance(), true); 
+				lds.source = {getNext:function(cb){cb({ item:1 });}};
+				lds.getNext(function(val) {
+					assert.deepEqual(val, { item:1 });
+					next();
+				});
 			},
 
-			"should return false for no sources remaining": function noMoar(){
+			"should return EOF for no sources remaining": function noMoar(next){
 				var lds = new LimitDocumentSource();
-				lds.limit = 1;
+				lds.limit = 10;
 				lds.source = {
-					getCurrent:function(){return { item:1 };},
-					advance:function(){return false;}
+					calls: 0,
+					getNext:function(cb) {
+						if (lds.source.calls)
+							return cb(DocumentSource.EOF);
+						lds.source.calls++;
+						return cb({item:1});
+					},
+					dispose:function() { return true; }
 				};
-				assert.strictEqual(lds.advance(), false); 
+				lds.getNext(function(){});
+				lds.getNext(function(val) {
+					assert.strictEqual(val, DocumentSource.EOF);
+					next();
+				});
 			},
 
-			"should return false if we hit our limit": function noMoar(){
+			"should return false if we hit our limit": function noMoar(next){
 				var lds = new LimitDocumentSource();
 				lds.limit = 1;
 				lds.source = {
-					getCurrent:function(){return { item:1 };},
-					advance:function(){return true;}
+					calls: 0,
+					getNext:function(cb) {
+						if (lds.source.calls)
+							return cb(DocumentSource.EOF);
+						return cb({item:1});
+					},
+					dispose:function() { return true; }
 				};
-				assert.strictEqual(lds.advance(), false); 
+				lds.getNext(function(){});
+				lds.getNext(function (val) {
+					assert.strictEqual(val, DocumentSource.EOF);
+					next();
+				});
 			}
 
 		},