LimitDocumentSource.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var DocumentSource = require("./DocumentSource");
  3. /**
  4. * A document source limiter
  5. * @class LimitDocumentSource
  6. * @namespace mungedb-aggregate.pipeline.documentSources
  7. * @module mungedb-aggregate
  8. * @constructor
  9. * @param [ctx] {ExpressionContext}
  10. **/
  11. var LimitDocumentSource = module.exports = function LimitDocumentSource(ctx, limit){
  12. if (arguments.length > 2) throw new Error("up to two args expected");
  13. base.call(this, ctx);
  14. this.limit = limit;
  15. this.count = 0;
  16. }, klass = LimitDocumentSource, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  17. klass.limitName = "$limit";
  18. proto.getSourceName = function getSourceName(){
  19. return klass.limitName;
  20. };
  21. proto.getFactory = function getFactory(){
  22. return klass; // using the ctor rather than a separate .create() method
  23. };
  24. /**
  25. * Coalesce limits together
  26. * @param {Object} nextSource the next source
  27. * @return {bool} return whether we can coalese together
  28. **/
  29. proto.coalesce = function coalesce(nextSource) {
  30. var nextLimit = nextSource.constructor === LimitDocumentSource?nextSource:null;
  31. // if it's not another $limit, we can't coalesce
  32. if (!nextLimit) return false;
  33. // we need to limit by the minimum of the two limits
  34. if (nextLimit.limit < this.limit) this.limit = nextLimit.limit;
  35. return true;
  36. };
  37. /**
  38. * Returns the next Documnet if there is one or null if at EOF
  39. * @method getNext
  40. */
  41. proto.getNext = function getNext() {
  42. if (this.expCtx && this.expCtx.checkForInterrupt) this.expCtx.checkForInterrupt();
  43. if (++this.count > this.limit) {
  44. this.source.dispose();
  45. return null;
  46. }
  47. return this.source.getNext();
  48. };
  49. /**
  50. Create a limiting DocumentSource from JSON.
  51. This is a convenience method that uses the above, and operates on
  52. a JSONElement that has been deteremined to be an Object with an
  53. element named $limit.
  54. @param jsonElement the JSONELement that defines the limit
  55. @param ctx the expression context
  56. @returns the grouping DocumentSource
  57. */
  58. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  59. if (typeof jsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  60. var limit = jsonElement;
  61. return klass.create(ctx, limit);
  62. };
  63. klass.create = function create(ctx, limit){
  64. if ((limit <= 0) || isNaN(limit)) throw new Error("code 15958; the limit must be positive");
  65. return new LimitDocumentSource(ctx, limit);
  66. };
  67. proto.getLimit = function getLimit(newLimit) {
  68. return this.limit;
  69. };
  70. proto.setLimit = function setLimit(newLimit) {
  71. this.limit = newLimit;
  72. };
  73. proto.getDependencies = function(deps) {
  74. return DocumentSource.GetDepsReturn.SEE_NEXT;
  75. };
  76. proto.serialize = function(explain) {
  77. var out = {};
  78. out[this.getSourceName()] = this.limit;
  79. return out;
  80. };