SortDocumentSource.js 8.2 KB

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