DocumentSource.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. "use strict";
  2. var DocumentSource = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A base class for all document sources
  6. *
  7. * @class DocumentSource
  8. * @namespace munge.pipeline.documentsource
  9. * @module munge
  10. * @constructor
  11. * @param {ExpressionContext}
  12. **/
  13. var klass = module.exports = DocumentSource = function DocumentSource(/*pCtx*/){
  14. if(arguments.length !== 0) throw new Error("zero args expected");
  15. /*
  16. Most DocumentSources have an underlying source they get their data
  17. from. This is a convenience for them.
  18. The default implementation of setSource() sets this; if you don't
  19. need a source, override that to verify(). The default is to
  20. verify() if this has already been set.
  21. */
  22. this.pSource = null;
  23. /*
  24. The zero-based user-specified pipeline step. Used for diagnostics.
  25. Will be set to -1 for artificial pipeline steps that were not part
  26. of the original user specification.
  27. */
  28. this.step = -1;
  29. //we dont need this because we are not sharding
  30. //intrusive_ptr<ExpressionContext> pExpCtx;
  31. //this.pExpCtx = pCtx;
  32. /*
  33. for explain: # of rows returned by this source
  34. This is *not* unsigned so it can be passed to JSONObjBuilder.append().
  35. */
  36. this.nRowsOut = 0;
  37. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  38. /*
  39. class DocumentSource :
  40. public IntrusiveCounterUnsigned,
  41. public StringWriter {
  42. public:
  43. virtual ~DocumentSource();
  44. // virtuals from StringWriter
  45. virtual void writeString(stringstream &ss) const;
  46. */
  47. /**
  48. * Set the step for a user-specified pipeline step.
  49. *
  50. * @method setPipelineStep
  51. * @param {Number} step number 0 to n.
  52. **/
  53. proto.setPipelineStep = function setPipelineStep(step) {
  54. this.step = step;
  55. };
  56. /**
  57. * Get the user-specified pipeline step.
  58. *
  59. * @method getPipelineStep
  60. * @returns {Number} step
  61. **/
  62. proto.getPipelineStep = function getPipelineStep() {
  63. return this.step;
  64. };
  65. /**
  66. * Is the source at EOF?
  67. *
  68. * @method eof
  69. **/
  70. proto.eof = function eof() {
  71. throw new Error("not implemented");
  72. };
  73. /**
  74. * Advance the state of the DocumentSource so that it will return the next Document.
  75. * The default implementation returns false, after checking for interrupts.
  76. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  77. *
  78. * @method advance
  79. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  80. **/
  81. proto.advance = function advance() {
  82. //pExpCtx->checkForInterrupt(); // might not return
  83. return false;
  84. };
  85. /**
  86. * some implementations do the equivalent of verify(!eof()) so check eof() first
  87. *
  88. * @method getCurrent
  89. * @returns {Document} the current Document without advancing
  90. **/
  91. proto.getCurrent = function getCurrent() {
  92. throw new Error("not implemented");
  93. };
  94. /**
  95. * Inform the source that it is no longer needed and may release its resources. After
  96. * dispose() is called the source must still be able to handle iteration requests, but may
  97. * become eof().
  98. * NOTE: For proper mutex yielding, dispose() must be called on any DocumentSource that will
  99. * not be advanced until eof(), see SERVER-6123.
  100. *
  101. * @method dispose
  102. **/
  103. proto.dispose = function dispose() {
  104. if ( this.pSource ) {
  105. // This is required for the DocumentSourceCursor to release its read lock, see
  106. // SERVER-6123.
  107. this.pSource.dispose();
  108. }
  109. };
  110. /**
  111. * Get the source's name.
  112. *
  113. * @method getSourceName
  114. * @returns {String} the string name of the source as a constant string; this is static, and there's no need to worry about adopting it
  115. **/
  116. proto.getSourceName = function getSourceName() {
  117. return "[UNKNOWN]";
  118. };
  119. /**
  120. * Set the underlying source this source should use to get Documents
  121. * from.
  122. * It is an error to set the source more than once. This is to
  123. * prevent changing sources once the original source has been started;
  124. * this could break the state maintained by the DocumentSource.
  125. * This pointer is not reference counted because that has led to
  126. * some circular references. As a result, this doesn't keep
  127. * sources alive, and is only intended to be used temporarily for
  128. * the lifetime of a Pipeline::run().
  129. *
  130. * @method setSource
  131. * @param {DocumentSource} pSource the underlying source to use
  132. **/
  133. proto.setSource = function setSource(pTheSource) {
  134. if(this.pSource){
  135. throw new Error("It is an error to set the source more than once");
  136. }
  137. this.pSource = pTheSource;
  138. };
  139. /**
  140. * Attempt to coalesce this DocumentSource with its successor in the
  141. * document processing pipeline. If successful, the successor
  142. * DocumentSource should be removed from the pipeline and discarded.
  143. * If successful, this operation can be applied repeatedly, in an
  144. * attempt to coalesce several sources together.
  145. * The default implementation is to do nothing, and return false.
  146. *
  147. * @method coalesce
  148. * @param {DocumentSource} pNextSource the next source in the document processing chain.
  149. * @returns {Boolean} whether or not the attempt to coalesce was successful or not; if the attempt was not successful, nothing has been changed
  150. **/
  151. proto.coalesce = function coalesce(pNextSource) {
  152. return false;
  153. };
  154. /**
  155. * Optimize the pipeline operation, if possible. This is a local
  156. * optimization that only looks within this DocumentSource. For best
  157. * results, first coalesce compatible sources using coalesce().
  158. * This is intended for any operations that include expressions, and
  159. * provides a hook for those to optimize those operations.
  160. * The default implementation is to do nothing.
  161. *
  162. * @method optimize
  163. **/
  164. proto.optimize = function optimize(pNextSource) {
  165. };
  166. klass.GetDepsReturn = {
  167. NOT_SUPPORTED:"NOT_SUPPORTED", // This means the set should be ignored
  168. EXHAUSTIVE:"EXHAUSTIVE", // This means that everything needed should be in the set
  169. SEE_NEXT:"SEE_NEXT", // Add the next Source's deps to the set
  170. };
  171. /**
  172. * Get the fields this operation needs to do its job.
  173. * Deps should be in "a.b.c" notation
  174. *
  175. * @method getDependencies
  176. * @param {Object} deps set (unique array) of strings
  177. * @returns DocumentSource.GetDepsReturn
  178. **/
  179. proto.getDependencies = function getDependencies(deps) {
  180. return klass.GetDepsReturn.NOT_SUPPORTED;
  181. };
  182. /**
  183. * This takes dependencies from getDependencies and
  184. * returns a projection that includes all of them
  185. *
  186. * @method depsToProjection
  187. * @param {Object} deps set (unique array) of strings
  188. * @returns {Object} JSONObj
  189. **/
  190. klass.depsToProjection = function depsToProjection(deps) {
  191. var bb = {};
  192. if (deps._id === undefined)
  193. bb._id = 0;
  194. var last = "";
  195. Object.keys(deps).forEach(function(it){
  196. if (last !== "" && it.slice(0, last.length) === last){
  197. // we are including a parent of *it so we don't need to
  198. // include this field explicitly. In fact, due to
  199. // SERVER-6527 if we included this field, the parent
  200. // wouldn't be fully included.
  201. return;
  202. }
  203. last = it + ".";
  204. bb[it] = 1;
  205. });
  206. return bb;
  207. };
  208. /**
  209. * Add the DocumentSource to the array builder.
  210. * The default implementation calls sourceToJson() in order to
  211. * convert the inner part of the object which will be added to the
  212. * array being built here.
  213. *
  214. * @method addToJsonArray
  215. * @param {Array} pBuilder JSONArrayBuilder: the array builder to add the operation to.
  216. * @param {Boolean} explain create explain output
  217. * @returns {Object}
  218. **/
  219. proto.addToJsonArray = function addToJsonArray(pBuilder, explain) {
  220. pBuilder.push(this.sourceToJson({}, explain));
  221. };
  222. /**
  223. * Create an object that represents the document source. The object
  224. * will have a single field whose name is the source's name. This
  225. * will be used by the default implementation of addToJsonArray()
  226. * to add this object to a pipeline being represented in JSON.
  227. *
  228. * @method sourceToJson
  229. * @param {Object} pBuilder JSONObjBuilder: a blank object builder to write to
  230. * @param {Boolean} explain create explain output
  231. **/
  232. proto.sourceToJson = function sourceToJson(pBuilder, explain) {
  233. throw new Error("not implemented");
  234. };
  235. /**
  236. * Reset the document source so that it is ready for a new stream of data.
  237. * Note that this is a deviation from the mongo implementation.
  238. *
  239. * @method reset
  240. **/
  241. proto.reset = function reset(){
  242. throw new Error("not implemented");
  243. };
  244. return klass;
  245. })();