LimitDocumentSource.js 3.4 KB

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