DocumentSource.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. /**
  3. * A base class for all document sources
  4. * @class DocumentSource
  5. * @namespace mungedb-aggregate.pipeline.documentSources
  6. * @module mungedb-aggregate
  7. * @constructor
  8. * @param expCtx {ExpressionContext}
  9. **/
  10. var DocumentSource = module.exports = function DocumentSource(expCtx){
  11. if(arguments.length !== 1) throw new Error("one arg expected");
  12. /*
  13. * Most DocumentSources have an underlying source they get their data
  14. * from. This is a convenience for them.
  15. * The default implementation of setSource() sets this; if you don't
  16. * need a source, override that to verify(). The default is to
  17. * verify() if this has already been set.
  18. */
  19. this.source = null;
  20. /*
  21. * The zero-based user-specified pipeline step. Used for diagnostics.
  22. * Will be set to -1 for artificial pipeline steps that were not part
  23. * of the original user specification.
  24. */
  25. this.step = -1;
  26. this.expCtx = expCtx || {};
  27. /*
  28. * for explain: # of rows returned by this source
  29. * This is *not* unsigned so it can be passed to JSONObjBuilder.append().
  30. */
  31. this.nRowsOut = 0;
  32. }, klass = DocumentSource, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  33. klass.EOF = (function() {
  34. /**
  35. * Holds a Cursor and all associated state required to access the cursor.
  36. * @class CursorWithContext
  37. * @namespace mungedb-aggregate.pipeline.documentSources.CursorDocumentSource
  38. * @module mungedb-aggregate
  39. * @constructor
  40. **/
  41. var klass = function EOF(){
  42. };
  43. return klass;
  44. })();
  45. /*
  46. class DocumentSource :
  47. public IntrusiveCounterUnsigned,
  48. public StringWriter {
  49. public:
  50. virtual ~DocumentSource();
  51. // virtuals from StringWriter
  52. virtual void writeString(stringstream &ss) const;
  53. */
  54. /**
  55. * Set the step for a user-specified pipeline step.
  56. * @method setPipelineStep
  57. * @param {Number} step number 0 to n.
  58. **/
  59. proto.setPipelineStep = function setPipelineStep(step) {
  60. this.step = step;
  61. };
  62. /**
  63. * Get the user-specified pipeline step.
  64. * @method getPipelineStep
  65. * @returns {Number} step
  66. **/
  67. proto.getPipelineStep = function getPipelineStep() {
  68. return this.step;
  69. };
  70. /**
  71. * Returns the next Document if there is one or DocumentSource.EOF if at EOF.
  72. *
  73. * some implementations do the equivalent of verify(!eof()) so check eof() first
  74. * @method getNext
  75. * @returns {Document} the current Document without advancing
  76. **/
  77. proto.getNext = function getNext(callback) {
  78. throw new Error("not implemented");
  79. };
  80. /**
  81. * Inform the source that it is no longer needed and may release its resources. After
  82. * dispose() is called the source must still be able to handle iteration requests, but may
  83. * become eof().
  84. * NOTE: For proper mutex yielding, dispose() must be called on any DocumentSource that will
  85. * not be advanced until eof(), see SERVER-6123.
  86. *
  87. * @method dispose
  88. **/
  89. proto.dispose = function dispose() {
  90. if ( this.source ) {
  91. // This is required for the DocumentSourceCursor to release its read lock, see
  92. // SERVER-6123.
  93. this.source.dispose();
  94. }
  95. };
  96. /**
  97. * Get the source's name.
  98. * @method getSourceName
  99. * @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
  100. **/
  101. proto.getSourceName = function getSourceName() {
  102. return "[UNKNOWN]";
  103. };
  104. /**
  105. * Set the underlying source this source should use to get Documents
  106. * from.
  107. * It is an error to set the source more than once. This is to
  108. * prevent changing sources once the original source has been started;
  109. * this could break the state maintained by the DocumentSource.
  110. * This pointer is not reference counted because that has led to
  111. * some circular references. As a result, this doesn't keep
  112. * sources alive, and is only intended to be used temporarily for
  113. * the lifetime of a Pipeline::run().
  114. *
  115. * @method setSource
  116. * @param {DocumentSource} source the underlying source to use
  117. **/
  118. proto.setSource = function setSource(theSource) {
  119. if (this.source) throw new Error("It is an error to set the source more than once");
  120. this.source = theSource;
  121. };
  122. /**
  123. * Attempt to coalesce this DocumentSource with its successor in the
  124. * document processing pipeline. If successful, the successor
  125. * DocumentSource should be removed from the pipeline and discarded.
  126. * If successful, this operation can be applied repeatedly, in an
  127. * attempt to coalesce several sources together.
  128. * The default implementation is to do nothing, and return false.
  129. *
  130. * @method coalesce
  131. * @param {DocumentSource} nextSource the next source in the document processing chain.
  132. * @returns {Boolean} whether or not the attempt to coalesce was successful or not; if the attempt was not successful, nothing has been changed
  133. **/
  134. proto.coalesce = function coalesce(nextSource) {
  135. return false;
  136. };
  137. /**
  138. * Optimize the pipeline operation, if possible. This is a local
  139. * optimization that only looks within this DocumentSource. For best
  140. * results, first coalesce compatible sources using coalesce().
  141. * This is intended for any operations that include expressions, and
  142. * provides a hook for those to optimize those operations.
  143. * The default implementation is to do nothing.
  144. *
  145. * @method optimize
  146. **/
  147. proto.optimize = function optimize() {
  148. };
  149. klass.GetDepsReturn = {
  150. NOT_SUPPORTED: "NOT_SUPPORTED", // This means the set should be ignored
  151. EXHAUSTIVE: "EXHAUSTIVE", // This means that everything needed should be in the set
  152. SEE_NEXT: "SEE_NEXT" // Add the next Source's deps to the set
  153. };
  154. /**
  155. * Get the fields this operation needs to do its job.
  156. * Deps should be in "a.b.c" notation
  157. *
  158. * @method getDependencies
  159. * @param {Object} deps set (unique array) of strings
  160. * @returns DocumentSource.GetDepsReturn
  161. **/
  162. proto.getDependencies = function getDependencies(deps) {
  163. return klass.GetDepsReturn.NOT_SUPPORTED;
  164. };
  165. /**
  166. * This takes dependencies from getDependencies and
  167. * returns a projection that includes all of them
  168. *
  169. * @method depsToProjection
  170. * @param {Object} deps set (unique array) of strings
  171. * @returns {Object} JSONObj
  172. **/
  173. klass.depsToProjection = function depsToProjection(deps) {
  174. var needId = false,
  175. bb = {};
  176. if (deps._id === undefined)
  177. bb._id = 0;
  178. var last = "";
  179. Object.keys(deps).sort().forEach(function(it){
  180. if (it.indexOf('_id') === 0 && (it.length === 3 || it[3] === '.')) {
  181. needId = true;
  182. return;
  183. } else {
  184. if (last !== "" && it.slice(0, last.length) === last){
  185. // we are including a parent of *it so we don't need to
  186. // include this field explicitly. In fact, due to
  187. // SERVER-6527 if we included this field, the parent
  188. // wouldn't be fully included.
  189. return;
  190. }
  191. }
  192. last = it + ".";
  193. bb[it] = 1;
  194. });
  195. if (needId) // we are explicit either way
  196. bb._id = 1;
  197. else
  198. bb._id = 0;
  199. return bb;
  200. };
  201. proto._serialize = function _serialize(explain) {
  202. throw new Error("not implemented");
  203. };
  204. proto.serializeToArray = function serializeToArray(array, explain) {
  205. var entry = this.serialize(explain);
  206. if (!entry) {
  207. array.push(entry);
  208. }
  209. };