SplitDocumentSource.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var SplitDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A document source sorter
  5. *
  6. * Since we don't have shards, this inherits from DocumentSource, instead of SplittableDocumentSource
  7. *
  8. * @class SortDocumentSource
  9. * @namespace munge.pipeline.documentsource
  10. * @module munge
  11. * @constructor
  12. **/
  13. var klass = module.exports = SplitDocumentSource = function SplitDocumentSource(/* pCtx*/){
  14. if(arguments.length !== 0) throw new Error("zero args expected");
  15. base.call(this);
  16. /*
  17. * Before returning anything, this source must fetch everything from
  18. * the underlying source and group it. populate() is used to do that
  19. * on the first call to any method on this source. The populated
  20. * boolean indicates that this has been done
  21. **/
  22. this.populated = false;
  23. this.current = null;
  24. this.docIterator = null; // a number tracking our position in the documents array
  25. this.documents = []; // an array of documents
  26. this.pipelines = {};
  27. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  28. klass.splitName = "$split";
  29. proto.getSourceName = function getSourceName(){
  30. return klass.splitName;
  31. };
  32. proto.getFactory = function getFactory(){
  33. return klass; // using the ctor rather than a separate .create() method
  34. };
  35. /**
  36. * Is the source at EOF?
  37. *
  38. * @method eof
  39. * @return {bool} return if we have hit the end of input
  40. **/
  41. proto.eof = function eof() {
  42. if (!this.populated)
  43. this.populate();
  44. return (this.docIterator == this.documents.length);
  45. };
  46. /**
  47. * some implementations do the equivalent of verify(!eof()) so check eof() first
  48. *
  49. * @method getCurrent
  50. * @returns {Document} the current Document without advancing
  51. **/
  52. proto.getCurrent = function getCurrent() {
  53. if (!this.populated)
  54. this.populate();
  55. return this.current;
  56. };
  57. /**
  58. * Advance the state of the DocumentSource so that it will return the next Document.
  59. * The default implementation returns false, after checking for interrupts.
  60. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  61. *
  62. * @method advance
  63. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  64. **/
  65. proto.advance = function advance() {
  66. base.prototype.advance.call(this); // check for interrupts
  67. if (!this.populated)
  68. this.populate();
  69. if (this.docIterator == this.documents.length) throw new Error("This should never happen");
  70. ++this.docIterator;
  71. if (this.docIterator == this.documents.length) {
  72. this.current = null;
  73. return false;
  74. }
  75. this.current = this.documents[this.docIterator];
  76. return true;
  77. };
  78. /**
  79. * Create an object that represents the document source. The object
  80. * will have a single field whose name is the source's name. This
  81. * will be used by the default implementation of addToJsonArray()
  82. * to add this object to a pipeline being represented in JSON.
  83. *
  84. * @method sourceToJson
  85. * @param {Object} builder JSONObjBuilder: a blank object builder to write to
  86. * @param {Boolean} explain create explain output
  87. **/
  88. proto.sourceToJson = function sourceToJson(builder, explain) {
  89. builder.$split = {}; // TODO: this is the default for split but it may need to have a key?
  90. };
  91. proto.populate = function populate() {
  92. /* pull everything from the underlying source */
  93. for(var hasNext = !this.pSource.eof(); hasNext; hasNext = this.pSource.advance()) {
  94. var doc = this.pSource.getCurrent();
  95. this.documents.push(doc);
  96. }
  97. var splitDocument = {};
  98. for(var pipelineKey in this.pipelines){
  99. var pipeline = this.pipelines[pipelineKey],
  100. result = {};
  101. result.ok = pipeline.run(this.documents, result);
  102. splitDocument[pipelineKey] = result.result;
  103. }
  104. //"Join" all documents by placing the various pipeline results as the only doc in this.documents
  105. this.documents = [splitDocument];
  106. this.docIterator = 0;
  107. if (this.docIterator < this.documents.length)
  108. this.current = this.documents[this.docIterator];
  109. this.populated = true;
  110. };
  111. /**
  112. * Creates a new SortDocumentSource
  113. *
  114. * @param {Object} JsonElement
  115. **/
  116. klass.createFromJson = function createFromJson(jsonElement) {
  117. if (typeof jsonElement !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
  118. var split = new SplitDocumentSource(),
  119. splitKeys = 0,
  120. PipelineCommand = require('../../commands/PipelineCommand');
  121. for(var key in jsonElement) {
  122. split.pipelines[key] = new PipelineCommand(jsonElement[key]);
  123. ++splitKeys;
  124. }
  125. if ( splitKeys <= 0) throw new Error("code 15977; " + klass.splitName + " must have at least one split key");
  126. return split;
  127. };
  128. /**
  129. * Reset the document source so that it is ready for a new stream of data.
  130. * Note that this is a deviation from the mongo implementation.
  131. *
  132. * @method reset
  133. **/
  134. proto.reset = function reset(){
  135. this.populated = false;
  136. this.current = null;
  137. this.docIterator = null; // a number tracking our position in the documents array
  138. this.documents = []; // an array of documents
  139. };
  140. return klass;
  141. })();