|
|
@@ -1,9 +1,5 @@
|
|
|
"use strict";
|
|
|
|
|
|
-// Mimicking max memory size from mongo/db/query/new_find.cpp
|
|
|
-// Need to actually decide some size for this?
|
|
|
-var MAX_BATCH_DOCS = 150;
|
|
|
-
|
|
|
/**
|
|
|
* Constructs and returns Documents from the objects produced by a supplied Cursor.
|
|
|
* An object of this type may only be used by one thread, see SERVER-6123.
|
|
|
@@ -34,10 +30,6 @@ var CursorDocumentSource = module.exports = CursorDocumentSource = function Curs
|
|
|
this._projection = null;
|
|
|
|
|
|
this._cursorWithContext = cursorWithContext;
|
|
|
- this._curIdx = 0;
|
|
|
- this._currentBatch = [];
|
|
|
- this._limit = undefined;
|
|
|
- this._docsAddedToBatches = 0;
|
|
|
|
|
|
if (!this._cursorWithContext || !this._cursorWithContext._cursor) throw new Error("CursorDocumentSource requires a valid cursorWithContext");
|
|
|
|
|
|
@@ -67,41 +59,6 @@ klass.CursorWithContext = (function (){
|
|
|
**/
|
|
|
proto.dispose = function dispose() {
|
|
|
this._cursorWithContext = null;
|
|
|
- this._currentBatch = [];
|
|
|
- this._curIdx = 0;
|
|
|
-};
|
|
|
-
|
|
|
-proto.getSourceName = function getSourceName() {
|
|
|
- return "$cursor";
|
|
|
-};
|
|
|
-
|
|
|
-proto.getNext = function getNext(callback) {
|
|
|
- if (this._currentBatch.length <= this._curIdx) {
|
|
|
- this.loadBatch();
|
|
|
-
|
|
|
- if (!this._currentBatch) {
|
|
|
- if (callback)
|
|
|
- return callback(null);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Don't unshift. It's expensiver.
|
|
|
- var out = this._currentBatch[this._curIdx];
|
|
|
- this._curIdx++;
|
|
|
-
|
|
|
- if (callback)
|
|
|
- return callback(out);
|
|
|
- return out;
|
|
|
-};
|
|
|
-
|
|
|
-proto.coalesce = function coalesce(nextSource) {
|
|
|
- if (!this._limit) {
|
|
|
- this._limit = nextSource;
|
|
|
- return this._limit;
|
|
|
- } else {
|
|
|
- return this._limit.coalesce(nextSource);
|
|
|
- }
|
|
|
};
|
|
|
|
|
|
///**
|
|
|
@@ -121,10 +78,9 @@ proto.coalesce = function coalesce(nextSource) {
|
|
|
// * @method setQuery
|
|
|
// * @param {Object} pBsonObj the query to record
|
|
|
// **/
|
|
|
-proto.setQuery = function setQuery(query) {
|
|
|
- this._query = query;
|
|
|
-};
|
|
|
-
|
|
|
+//proto.setQuery = function setQuery(pBsonObj) {};
|
|
|
+//
|
|
|
+//
|
|
|
///**
|
|
|
// * Record the sort that was specified for the cursor this wraps, if any.
|
|
|
// * This should be captured after any optimizations are applied to
|
|
|
@@ -142,7 +98,7 @@ proto.setQuery = function setQuery(query) {
|
|
|
* @method setProjection
|
|
|
* @param {Object} projection
|
|
|
**/
|
|
|
-proto.setProjection = function setProjection(projection, deps) {
|
|
|
+proto.setProjection = function setProjection(projection) {
|
|
|
|
|
|
if (this._projection){
|
|
|
throw new Error("projection is already set");
|
|
|
@@ -157,10 +113,42 @@ proto.setProjection = function setProjection(projection, deps) {
|
|
|
// this.cursor().fields = this._projection;
|
|
|
|
|
|
this._projection = projection; //just for testing
|
|
|
- this._dependencies = deps;
|
|
|
};
|
|
|
|
|
|
//----------------virtuals from DocumentSource--------------
|
|
|
+/**
|
|
|
+ * Is the source at EOF?
|
|
|
+ * @method eof
|
|
|
+ **/
|
|
|
+proto.eof = function eof() {
|
|
|
+ if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
|
|
|
+ return (this.current === null);
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 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
|
|
|
+ if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
|
|
|
+ this.findNext();
|
|
|
+ return (this.current !== null);
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 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() {
|
|
|
+ if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
|
|
|
+ return this.current;
|
|
|
+};
|
|
|
|
|
|
/**
|
|
|
* Set the underlying source this source should use to get Documents
|
|
|
@@ -177,73 +165,74 @@ proto.setProjection = function setProjection(projection, deps) {
|
|
|
* @param source {DocumentSource} the underlying source to use
|
|
|
* @param callback {Function} a `mungedb-aggregate`-specific extension to the API to half-way support reading from async sources
|
|
|
**/
|
|
|
-proto.setSource = function setSource(theSource) {
|
|
|
+proto.setSource = function setSource(theSource, callback) {
|
|
|
if (theSource) throw new Error("CursorDocumentSource doesn't take a source"); //TODO: This needs to put back without the if once async is fully and properly supported
|
|
|
+ if (callback) return setTimeout(callback, 0);
|
|
|
};
|
|
|
|
|
|
-proto.serialize = function serialize(explain) {
|
|
|
- if (!explain)
|
|
|
- return null;
|
|
|
-
|
|
|
- if (!this._cursorWithContext)
|
|
|
- throw new Error("code 17135; Cursor deleted.");
|
|
|
-
|
|
|
- // A stab at what mongo wants
|
|
|
- return {
|
|
|
- query: this._query,
|
|
|
- sort: this._sort ? this._sort : null,
|
|
|
- limit: this._limit ? this._limit : null,
|
|
|
- fields: this._projection ? this._projection : null,
|
|
|
- indexonly: false,
|
|
|
- cursorType: this._cursorWithContext ? "cursor" : null
|
|
|
- };
|
|
|
-};
|
|
|
-
|
|
|
-// LimitDocumentSource has the setLimit function which trickles down to any documentsource
|
|
|
-proto.getLimit = function getLimit() {
|
|
|
- return this._limit;
|
|
|
+/**
|
|
|
+ * Create an object that represents the document source. The object
|
|
|
+ * will have a single field whose name is the source's name. This
|
|
|
+ * will be used by the default implementation of addToBsonArray()
|
|
|
+ * to add this object to a pipeline being represented in BSON.
|
|
|
+ *
|
|
|
+ * @method sourceToJson
|
|
|
+ * @param {Object} pBuilder BSONObjBuilder: a blank object builder to write to
|
|
|
+ * @param {Boolean} explain create explain output
|
|
|
+ **/
|
|
|
+proto.sourceToJson = function sourceToJson(pBuilder, explain) {
|
|
|
+ /* this has no analog in the BSON world, so only allow it for explain */
|
|
|
+ //if (explain){
|
|
|
+ ////we are not currently supporting explain in mungedb-aggregate
|
|
|
+ //}
|
|
|
};
|
|
|
|
|
|
//----------------private--------------
|
|
|
|
|
|
-//proto.chunkMgr = function chunkMgr(){};
|
|
|
-
|
|
|
-//proto.canUseCoveredIndex = function canUseCoveredIndex(){};
|
|
|
+proto.findNext = function findNext(){
|
|
|
|
|
|
-//proto.yieldSometimes = function yieldSometimes(){};
|
|
|
+ if ( !this._cursorWithContext ) {
|
|
|
+ this.current = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
-proto.loadBatch = function loadBatch() {
|
|
|
- var nDocs = 0,
|
|
|
- cursor = this._cursorWithContext ? this._cursorWithContext._cursor : null;
|
|
|
+ for( ; this.cursor().ok(); this.cursor().advance() ) {
|
|
|
|
|
|
- if (!cursor)
|
|
|
- return this.dispose();
|
|
|
+ //yieldSometimes();
|
|
|
+// if ( !this.cursor().ok() ) {
|
|
|
+// // The cursor was exhausted during the yield.
|
|
|
+// break;
|
|
|
+// }
|
|
|
|
|
|
- for(;cursor.ok(); cursor.advance()) {
|
|
|
- if (!cursor.ok())
|
|
|
- break;
|
|
|
+// if ( !this.cursor().currentMatches() || this.cursor().currentIsDup() )
|
|
|
+// continue;
|
|
|
|
|
|
- // these methods do not exist
|
|
|
- // if (!cursor.currentMatches() || cursor.currentIsDup())
|
|
|
- // continue;
|
|
|
|
|
|
- var next = cursor.current();
|
|
|
- this._currentBatch.push(this._projection ? this.documentFromBsonDeps(next, this._dependencies) : next);
|
|
|
+ // grab the matching document
|
|
|
+ var documentObj;
|
|
|
+// if (this.canUseCoveredIndex()) { ... Dont need any of this, I think
|
|
|
|
|
|
- if (this._limit) {
|
|
|
- if (++this._docsAddedToBatches == this._limit.getLimit())
|
|
|
- break;
|
|
|
+ documentObj = this.cursor().current();
|
|
|
+ this.current = documentObj;
|
|
|
+ this.cursor().advance();
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (this._docsAddedToBatches >= this._limit.getLimit()) {
|
|
|
- throw new Error("added documents to the batch over limit size");
|
|
|
- }
|
|
|
- }
|
|
|
+ // If we got here, there aren't any more documents.
|
|
|
+ // The CursorWithContext (and its read lock) must be released, see SERVER-6123.
|
|
|
+ this.dispose();
|
|
|
+ this.current = null;
|
|
|
+};
|
|
|
|
|
|
- // Mongo uses number of bytes, but that doesn't make sense here. Yield when nDocs is over a threshold
|
|
|
- if (nDocs > MAX_BATCH_DOCS) {
|
|
|
- this._curIdx++; // advance the deque
|
|
|
- nDocs++;
|
|
|
- return;
|
|
|
- }
|
|
|
+proto.cursor = function cursor(){
|
|
|
+ if( this._cursorWithContext && this._cursorWithContext._cursor){
|
|
|
+ return this._cursorWithContext._cursor;
|
|
|
}
|
|
|
+ throw new Error("cursor not defined");
|
|
|
};
|
|
|
+
|
|
|
+//proto.chunkMgr = function chunkMgr(){};
|
|
|
+
|
|
|
+//proto.canUseCoveredIndex = function canUseCoveredIndex(){};
|
|
|
+
|
|
|
+//proto.yieldSometimes = function yieldSometimes(){};
|