LimitDocumentSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. var LimitDocumentSource = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A document source limiter
  6. * @class LimitDocumentSource
  7. * @namespace mungedb-aggregate.pipeline.documentSources
  8. * @module mungedb-aggregate
  9. * @constructor
  10. * @param [ctx] {ExpressionContext}
  11. **/
  12. var klass = module.exports = LimitDocumentSource = function LimitDocumentSource(ctx){
  13. if(arguments.length > 1) throw new Error("up to one arg expected");
  14. base.call(this, ctx);
  15. this.limit = 0;
  16. this.count = 0;
  17. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. klass.limitName = "$limit";
  19. proto.getSourceName = function getSourceName(){
  20. return klass.limitName;
  21. };
  22. proto.getFactory = function getFactory(){
  23. return klass; // using the ctor rather than a separate .create() method
  24. };
  25. /**
  26. * Coalesce limits together
  27. *
  28. * @param {Object} nextSource the next source
  29. * @return {bool} return whether we can coalese together
  30. **/
  31. proto.coalesce = function coalesce(nextSource) {
  32. var nextLimit = nextSource.constructor === LimitDocumentSource?nextSource:null;
  33. /* if it's not another $limit, we can't coalesce */
  34. if (!nextLimit)
  35. return false;
  36. /* we need to limit by the minimum of the two limits */
  37. if (nextLimit.limit < this.limit)
  38. this.limit = nextLimit.limit;
  39. return true;
  40. };
  41. /**
  42. * Is the source at EOF?
  43. *
  44. * @method eof
  45. **/
  46. proto.eof = function eof() {
  47. return this.source.eof() || this.count >= this.limit;
  48. };
  49. /**
  50. * some implementations do the equivalent of verify(!eof()) so check eof() first
  51. *
  52. * @method getCurrent
  53. * @returns {Document} the current Document without advancing
  54. **/
  55. proto.getCurrent = function getCurrent() {
  56. return this.source.getCurrent();
  57. };
  58. /**
  59. * Advance the state of the DocumentSource so that it will return the next Document.
  60. * The default implementation returns false, after checking for interrupts.
  61. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  62. *
  63. * @method advance
  64. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  65. **/
  66. proto.advance = function advance() {
  67. base.prototype.advance.call(this); // check for interrupts
  68. ++this.count;
  69. if (this.count >= this.limit) {
  70. return false;
  71. }
  72. this.current = this.source.getCurrent();
  73. return this.source.advance();
  74. };
  75. /**
  76. * Create an object that represents the document source. The object
  77. * will have a single field whose name is the source's name. This
  78. * will be used by the default implementation of addToJsonArray()
  79. * to add this object to a pipeline being represented in JSON.
  80. *
  81. * @method sourceToJson
  82. * @param {Object} builder JSONObjBuilder: a blank object builder to write to
  83. * @param {Boolean} explain create explain output
  84. **/
  85. proto.sourceToJson = function sourceToJson(builder, explain) {
  86. builder.$limit = this.limit;
  87. };
  88. /**
  89. * Creates a new LimitDocumentSource with the input number as the limit
  90. *
  91. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  92. **/
  93. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  94. if (typeof jsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  95. var Limit = proto.getFactory(),
  96. nextLimit = new Limit(ctx);
  97. nextLimit.limit = jsonElement;
  98. if ((nextLimit.limit <= 0) || isNaN(nextLimit.limit)) throw new Error("code 15958; the limit must be positive");
  99. return nextLimit;
  100. };
  101. return klass;
  102. })();