LimitDocumentSource.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var LimitDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A document source limiter
  5. *
  6. * @class LimitDocumentSource
  7. * @namespace munge.pipepline.documentsource
  8. * @module munge
  9. * @constructor
  10. * @param {Object} query the match query to use
  11. **/
  12. var klass = module.exports = LimitDocumentSource = function LimitDocumentSource(/* pCtx*/){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  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. klass.GetDepsReturn = {
  26. SEE_NEXT:"SEE_NEXT", // Add the next Source's deps to the set
  27. };
  28. /**
  29. * Coalesce limits together
  30. *
  31. * @param {Object} nextSource the next source
  32. * @return {bool} return whether we can coalese together
  33. **/
  34. proto.coalesce = function coalesce(nextSource) {
  35. var nextLimit = nextSource.constructor === LimitDocumentSource?nextSource:null;
  36. /* if it's not another $limit, we can't coalesce */
  37. if (!nextLimit)
  38. return false;
  39. /* we need to limit by the minimum of the two limits */
  40. if (nextLimit.limit < this.limit)
  41. this.limit = nextLimit.limit;
  42. return true;
  43. };
  44. /**
  45. * Is the source at EOF?
  46. *
  47. * @method eof
  48. **/
  49. proto.eof = function eof() {
  50. return this.pSource.eof() || this.count >= this.limit;
  51. };
  52. /**
  53. * some implementations do the equivalent of verify(!eof()) so check eof() first
  54. *
  55. * @method getCurrent
  56. * @returns {Document} the current Document without advancing
  57. **/
  58. proto.getCurrent = function getCurrent() {
  59. return this.pSource.getCurrent();
  60. };
  61. /**
  62. * Advance the state of the DocumentSource so that it will return the next Document.
  63. * The default implementation returns false, after checking for interrupts.
  64. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  65. *
  66. * @method advance
  67. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  68. **/
  69. proto.advance = function advance() {
  70. base.prototype.advance.call(this); // check for interrupts
  71. ++this.count;
  72. if (this.count >= this.limit) {
  73. return false;
  74. }
  75. this.pCurrent = this.pSource.getCurrent();
  76. return this.pSource.advance();
  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.$limit = this.limit;
  90. };
  91. /**
  92. * Creates a new LimitDocumentSource with the input number as the limit
  93. *
  94. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  95. **/
  96. klass.createFromJson = function createFromJson(JsonElement) {
  97. if (typeof JsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  98. var Limit = proto.getFactory(),
  99. nextLimit = new Limit();
  100. nextLimit.limit = JsonElement;
  101. if ((nextLimit.limit <= 0) || isNaN(nextLimit.limit)) throw new Error("code 15958; the limit must be positive");
  102. return nextLimit;
  103. };
  104. /**
  105. * Reset the document source so that it is ready for a new stream of data.
  106. * Note that this is a deviation from the mongo implementation.
  107. *
  108. * @method reset
  109. **/
  110. proto.reset = function reset(){
  111. this.count = 0;
  112. };
  113. return klass;
  114. })();