CursorDocumentSource.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. var CursorDocumentSource = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Constructs and returns Documents from the objects produced by a supplied Cursor.
  6. * An object of this type may only be used by one thread, see SERVER-6123.
  7. *
  8. * This is usually put at the beginning of a chain of document sources
  9. * in order to fetch data from the database.
  10. *
  11. * @class CursorDocumentSource
  12. * @namespace mungedb-aggregate.pipeline.documentSources
  13. * @module mungedb-aggregate
  14. * @constructor
  15. * @param {CursorDocumentSource.CursorWithContext} cursorWithContext the cursor to use to fetch data
  16. **/
  17. var klass = module.exports = CursorDocumentSource = function CursorDocumentSource(cursorWithContext, expCtx){
  18. base.call(this, expCtx);
  19. this.current = null;
  20. // this.ns = null;
  21. // /*
  22. // The bson dependencies must outlive the Cursor wrapped by this
  23. // source. Therefore, bson dependencies must appear before pCursor
  24. // in order cause its destructor to be called *after* pCursor's.
  25. // */
  26. // this.query = null;
  27. // this.sort = null;
  28. this._projection = null;
  29. this._cursorWithContext = cursorWithContext;
  30. if (!this._cursorWithContext || !this._cursorWithContext._cursor) throw new Error("CursorDocumentSource requires a valid cursorWithContext");
  31. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  32. // DEPENDENCIES
  33. //var Document = require("../Document");
  34. klass.CursorWithContext = (function (){
  35. /**
  36. * Holds a Cursor and all associated state required to access the cursor.
  37. * @class CursorWithContext
  38. * @namespace mungedb-aggregate.pipeline.documentSources.CursorDocumentSource
  39. * @module mungedb-aggregate
  40. * @constructor
  41. **/
  42. var klass = function CursorWithContext(ns){
  43. this._cursor = null;
  44. };
  45. return klass;
  46. })();
  47. /**
  48. * Release the Cursor and the read lock it requires, but without changing the other data.
  49. * Releasing the lock is required for proper concurrency, see SERVER-6123. This
  50. * functionality is also used by the explain version of pipeline execution.
  51. *
  52. * @method dispose
  53. **/
  54. proto.dispose = function dispose() {
  55. this._cursorWithContext = null;
  56. };
  57. // /**
  58. // * Record the namespace. Required for explain.
  59. // *
  60. // * @method setNamespace
  61. // * @param {String} ns the namespace
  62. // **/
  63. // proto.setNamespace = function setNamespace(ns) {}
  64. //
  65. // /**
  66. // * Record the query that was specified for the cursor this wraps, if any.
  67. // * This should be captured after any optimizations are applied to
  68. // * the pipeline so that it reflects what is really used.
  69. // * This gets used for explain output.
  70. // *
  71. // * @method setQuery
  72. // * @param {Object} pBsonObj the query to record
  73. // **/
  74. // proto.setQuery = function setQuery(pBsonObj) {};
  75. //
  76. //
  77. // /**
  78. // * Record the sort that was specified for the cursor this wraps, if any.
  79. // * This should be captured after any optimizations are applied to
  80. // * the pipeline so that it reflects what is really used.
  81. // * This gets used for explain output.
  82. // *
  83. // * @method setSort
  84. // * @param {Object} pBsonObj the query to record
  85. // **/
  86. // proto.setSort = function setSort(pBsonObj) {};
  87. /**
  88. * setProjection method
  89. *
  90. * @method setProjection
  91. * @param {Object} projection
  92. **/
  93. proto.setProjection = function setProjection(projection) {
  94. if (this._projection){
  95. throw new Error("projection is already set");
  96. }
  97. //dont think we need this yet
  98. // this._projection = new Projection();
  99. // this._projection.init(projection);
  100. //
  101. // this.cursor().fields = this._projection;
  102. this._projection = projection; //just for testing
  103. };
  104. //----------------virtuals from DocumentSource--------------
  105. /**
  106. * Is the source at EOF?
  107. * @method eof
  108. **/
  109. proto.eof = function eof() {
  110. if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
  111. return (this.current === null);
  112. };
  113. /**
  114. * Advance the state of the DocumentSource so that it will return the next Document.
  115. * The default implementation returns false, after checking for interrupts.
  116. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  117. *
  118. * @method advance
  119. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  120. **/
  121. proto.advance = function advance() {
  122. base.prototype.advance.call(this); // check for interrupts
  123. if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
  124. this.findNext();
  125. return (this.current !== null);
  126. };
  127. /**
  128. * some implementations do the equivalent of verify(!eof()) so check eof() first
  129. * @method getCurrent
  130. * @returns {Document} the current Document without advancing
  131. **/
  132. proto.getCurrent = function getCurrent() {
  133. if (!this.current) this.findNext(); // if we haven't gotten the first one yet, do so now
  134. return this.current;
  135. };
  136. /**
  137. * Set the underlying source this source should use to get Documents
  138. * from.
  139. * It is an error to set the source more than once. This is to
  140. * prevent changing sources once the original source has been started;
  141. * this could break the state maintained by the DocumentSource.
  142. * This pointer is not reference counted because that has led to
  143. * some circular references. As a result, this doesn't keep
  144. * sources alive, and is only intended to be used temporarily for
  145. * the lifetime of a Pipeline::run().
  146. *
  147. * @method setSource
  148. * @param source {DocumentSource} the underlying source to use
  149. * @param callback {Function} a `mungedb-aggregate`-specific extension to the API to half-way support reading from async sources
  150. **/
  151. proto.setSource = function setSource(theSource, callback) {
  152. 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
  153. if (callback) return process.nextTick(callback);
  154. };
  155. /**
  156. * Create an object that represents the document source. The object
  157. * will have a single field whose name is the source's name. This
  158. * will be used by the default implementation of addToBsonArray()
  159. * to add this object to a pipeline being represented in BSON.
  160. *
  161. * @method sourceToJson
  162. * @param {Object} pBuilder BSONObjBuilder: a blank object builder to write to
  163. * @param {Boolean} explain create explain output
  164. **/
  165. proto.sourceToJson = function sourceToJson(pBuilder, explain) {
  166. /* this has no analog in the BSON world, so only allow it for explain */
  167. //if (explain){
  168. ////we are not currently supporting explain in mungedb-aggregate
  169. //}
  170. };
  171. //----------------private--------------
  172. proto.findNext = function findNext(){
  173. if ( !this._cursorWithContext ) {
  174. this.current = null;
  175. return;
  176. }
  177. for( ; this.cursor().ok(); this.cursor().advance() ) {
  178. //yieldSometimes();
  179. // if ( !this.cursor().ok() ) {
  180. // // The cursor was exhausted during the yield.
  181. // break;
  182. // }
  183. // if ( !this.cursor().currentMatches() || this.cursor().currentIsDup() )
  184. // continue;
  185. // grab the matching document
  186. var documentObj;
  187. // if (this.canUseCoveredIndex()) { ... Dont need any of this, I think
  188. documentObj = this.cursor().current();
  189. this.current = documentObj;
  190. this.cursor().advance();
  191. return;
  192. }
  193. // If we got here, there aren't any more documents.
  194. // The CursorWithContext (and its read lock) must be released, see SERVER-6123.
  195. this.dispose();
  196. this.current = null;
  197. };
  198. proto.cursor = function cursor(){
  199. if( this._cursorWithContext && this._cursorWithContext._cursor){
  200. return this._cursorWithContext._cursor;
  201. }
  202. throw new Error("cursor not defined");
  203. };
  204. // proto.chunkMgr = function chunkMgr(){};
  205. // proto.canUseCoveredIndex = function canUseCoveredIndex(){};
  206. // proto.yieldSometimes = function yieldSometimes(){};
  207. return klass;
  208. })();