CursorDocumentSource.js 8.0 KB

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