LimitDocumentSource.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var LimitDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A document source limiter
  5. *
  6. * @class LimitDocumentSource
  7. * @namespace munge.pipeline.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. /**
  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.pSource.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.pSource.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.pCurrent = this.pSource.getCurrent();
  73. return this.pSource.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) {
  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();
  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. /**
  102. * Reset the document source so that it is ready for a new stream of data.
  103. * Note that this is a deviation from the mongo implementation.
  104. *
  105. * @method reset
  106. **/
  107. proto.reset = function reset(){
  108. this.count = 0;
  109. };
  110. return klass;
  111. })();