SortDocumentSource.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use strict";
  2. var SortDocumentSource = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A document source sorter
  6. *
  7. * Since we don't have shards, this inherits from DocumentSource, instead of SplittableDocumentSource
  8. *
  9. * @class SortDocumentSource
  10. * @namespace munge.pipeline.documentsource
  11. * @module munge
  12. * @constructor
  13. **/
  14. var klass = module.exports = SortDocumentSource = function SortDocumentSource(/* pCtx*/){
  15. if(arguments.length !== 0) throw new Error("zero args expected");
  16. base.call(this);
  17. /*
  18. * Before returning anything, this source must fetch everything from
  19. * the underlying source and group it. populate() is used to do that
  20. * on the first call to any method on this source. The populated
  21. * boolean indicates that this has been done
  22. **/
  23. this.populated = false;
  24. this.current = null;
  25. this.docIterator = null; // a number tracking our position in the documents array
  26. this.documents = []; // an array of documents
  27. this.vSortKey = [];
  28. this.vAscending = [];
  29. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  30. // DEPENDENCIES
  31. var FieldPathExpression = require("../expressions/FieldPathExpression"),
  32. Value = require("../Value");
  33. klass.sortName = "$sort";
  34. proto.getSourceName = function getSourceName(){
  35. return klass.sortName;
  36. };
  37. proto.getFactory = function getFactory(){
  38. return klass; // using the ctor rather than a separate .create() method
  39. };
  40. klass.GetDepsReturn = {
  41. SEE_NEXT:"SEE_NEXT" // Add the next Source's deps to the set
  42. };
  43. proto.getDependencies = function getDependencies(deps) {
  44. for(var i = 0; i < this.vSortKey.length; ++i) {
  45. this.vSortKey[i].addDependencies(deps);
  46. }
  47. return klass.GetDepsReturn.SEE_NEXT;
  48. };
  49. /**
  50. * Is the source at EOF?
  51. *
  52. * @method eof
  53. * @return {bool} return if we have hit the end of input
  54. **/
  55. proto.eof = function eof() {
  56. if (!this.populated)
  57. this.populate();
  58. return (this.docIterator == this.documents.length);
  59. };
  60. /**
  61. * some implementations do the equivalent of verify(!eof()) so check eof() first
  62. *
  63. * @method getCurrent
  64. * @returns {Document} the current Document without advancing
  65. **/
  66. proto.getCurrent = function getCurrent() {
  67. if (!this.populated)
  68. this.populate();
  69. return this.current;
  70. };
  71. /**
  72. * Advance the state of the DocumentSource so that it will return the next Document.
  73. * The default implementation returns false, after checking for interrupts.
  74. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  75. *
  76. * @method advance
  77. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  78. **/
  79. proto.advance = function advance() {
  80. base.prototype.advance.call(this); // check for interrupts
  81. if (!this.populated)
  82. this.populate();
  83. if (this.docIterator == this.documents.length) throw new Error("This should never happen");
  84. ++this.docIterator;
  85. if (this.docIterator == this.documents.length) {
  86. this.current = null;
  87. return false;
  88. }
  89. this.current = this.documents[this.docIterator];
  90. return true;
  91. };
  92. /**
  93. * Create an object that represents the document source. The object
  94. * will have a single field whose name is the source's name. This
  95. * will be used by the default implementation of addToJsonArray()
  96. * to add this object to a pipeline being represented in JSON.
  97. *
  98. * @method sourceToJson
  99. * @param {Object} builder JSONObjBuilder: a blank object builder to write to
  100. * @param {Boolean} explain create explain output
  101. **/
  102. proto.sourceToJson = function sourceToJson(builder, explain) {
  103. var insides = {};
  104. this.sortKeyToJson(insides, false);
  105. builder[this.getSourceName()] = insides;
  106. };
  107. /**
  108. * Add sort key field.
  109. *
  110. * Adds a sort key field to the key being built up. A concatenated
  111. * key is built up by calling this repeatedly.
  112. *
  113. * @param {String} fieldPath the field path to the key component
  114. * @param {bool} ascending if true, use the key for an ascending sort, otherwise, use it for descending
  115. **/
  116. proto.addKey = function addKey(fieldPath, ascending) {
  117. var pathExpr = new FieldPathExpression(fieldPath);
  118. this.vSortKey.push(pathExpr);
  119. if (ascending === true || ascending === false)
  120. this.vAscending.push(ascending);
  121. else
  122. throw new Error("ascending must be true or false");
  123. };
  124. proto.populate = function populate() {
  125. /* make sure we've got a sort key */
  126. if (this.vSortKey.length === null) throw new Error("This should never happen");
  127. /* pull everything from the underlying source */
  128. for(var hasNext = !this.pSource.eof(); hasNext; hasNext = this.pSource.advance()) {
  129. var doc = this.pSource.getCurrent();
  130. this.documents.push(doc);
  131. }
  132. /* sort the list */
  133. this.documents.sort(SortDocumentSource.prototype.compare.bind(this));
  134. /* start the sort iterator */
  135. this.docIterator = 0;
  136. if (this.docIterator < this.documents.length)
  137. this.current = this.documents[this.docIterator];
  138. this.populated = true;
  139. };
  140. /**
  141. * Compare two documents according to the specified sort key.
  142. *
  143. * @param {Object} pL the left side doc
  144. * @param {Object} pR the right side doc
  145. * @returns {Number} a number less than, equal to, or greater than zero, indicating pL < pR, pL == pR, or pL > pR, respectively
  146. **/
  147. proto.compare = function compare(pL,pR) {
  148. /**
  149. * populate() already checked that there is a non-empty sort key,
  150. * so we shouldn't have to worry about that here.
  151. *
  152. * However, the tricky part is what to do is none of the sort keys are
  153. * present. In this case, consider the document less.
  154. **/
  155. var n = this.vSortKey.length;
  156. for(var i = 0; i < n; ++i) {
  157. /* evaluate the sort keys */
  158. var pathExpr = new FieldPathExpression(this.vSortKey[i].getFieldPath(false));
  159. var left = pathExpr.evaluate(pL), right = pathExpr.evaluate(pR);
  160. /*
  161. Compare the two values; if they differ, return. If they are
  162. the same, move on to the next key.
  163. */
  164. var cmp = Value.compare(left, right);
  165. if (cmp) {
  166. /* if necessary, adjust the return value by the key ordering */
  167. if (!this.vAscending[i])
  168. cmp = -cmp;
  169. return cmp;
  170. }
  171. }
  172. /**
  173. * If we got here, everything matched (or didn't exist), so we'll
  174. * consider the documents equal for purposes of this sort
  175. **/
  176. return 0;
  177. };
  178. /**
  179. * Write out an object whose contents are the sort key.
  180. *
  181. * @param {Object} builder initialized object builder.
  182. * @param {bool} fieldPrefix specify whether or not to include the field
  183. **/
  184. proto.sortKeyToJson = function sortKeyToJson(builder, usePrefix) {
  185. /* add the key fields */
  186. var n = this.vSortKey.length;
  187. for(var i = 0; i < n; ++i) {
  188. /* create the "field name" */
  189. var ss = this.vSortKey[i].getFieldPath(usePrefix); // renamed write to get
  190. /* push a named integer based on the sort order */
  191. builder[ss] = ((this.vAscending[i] > 0) ? 1 : -1);
  192. }
  193. };
  194. /**
  195. * Creates a new SortDocumentSource
  196. *
  197. * @param {Object} JsonElement
  198. **/
  199. klass.createFromJson = function createFromJson(JsonElement) {
  200. if (typeof JsonElement !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
  201. var Sort = proto.getFactory(),
  202. nextSort = new Sort();
  203. /* check for then iterate over the sort object */
  204. var sortKeys = 0;
  205. for(var key in JsonElement) {
  206. var sortOrder = 0;
  207. if (typeof JsonElement[key] !== "number") throw new Error("code 15974; " + klass.sortName + " key ordering must be specified using a number");
  208. sortOrder = JsonElement[key];
  209. if ((sortOrder != 1) && (sortOrder !== -1)) throw new Error("code 15975; " + klass.sortName + " key ordering must be 1 (for ascending) or 0 (for descending)");
  210. nextSort.addKey(key, (sortOrder > 0));
  211. ++sortKeys;
  212. }
  213. if (sortKeys <= 0) throw new Error("code 15976; " + klass.sortName + " must have at least one sort key");
  214. return nextSort;
  215. };
  216. /**
  217. * Reset the document source so that it is ready for a new stream of data.
  218. * Note that this is a deviation from the mongo implementation.
  219. *
  220. * @method reset
  221. **/
  222. proto.reset = function reset(){
  223. this.populated = false;
  224. this.current = null;
  225. this.docIterator = null; // a number tracking our position in the documents array
  226. this.documents = []; // an array of documents
  227. };
  228. return klass;
  229. })();