LimitDocumentSource.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  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. /* Returns the execution of the callback against
  38. * the next documentSource
  39. * @param {function} callback
  40. * @return {bool} indicating end of document reached
  41. */
  42. proto.getNext = function getNext(callback) {
  43. if (!callback) throw new Error(this.getSourceName() + ' #getNext() requires callback');
  44. if (this.expCtx instanceof Object && this.expCtx.checkForInterrupt && this.expCtx.checkForInterrupt() === false)
  45. return callback(new Error("Interrupted"));
  46. if (++this.count > this.limit) {
  47. this.source.dispose();
  48. callback(null, DocumentSource.EOF);
  49. return DocumentSource.EOF;
  50. }
  51. return this.source.getNext(callback);
  52. };
  53. /**
  54. Create a limiting DocumentSource from JSON.
  55. This is a convenience method that uses the above, and operates on
  56. a JSONElement that has been deteremined to be an Object with an
  57. element named $limit.
  58. @param jsonElement the JSONELement that defines the limit
  59. @param ctx the expression context
  60. @returns the grouping DocumentSource
  61. */
  62. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  63. if (typeof jsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  64. var limit = jsonElement;
  65. return this.create(ctx, limit);
  66. };
  67. klass.create = function create(ctx, limit){
  68. if ((limit <= 0) || isNaN(limit)) throw new Error("code 15958; the limit must be positive");
  69. return new LimitDocumentSource(ctx, limit);
  70. };
  71. proto.getLimit = function getLimit(newLimit) {
  72. return this.limit;
  73. };
  74. proto.setLimit = function setLimit(newLimit) {
  75. this.limit = newLimit;
  76. };
  77. proto.getDependencies = function(deps) {
  78. return DocumentSource.GetDepsReturn.SEE_NEXT;
  79. };
  80. proto.serialize = function(explain) {
  81. var out = {};
  82. out[this.getSourceName()] = this.limit;
  83. return out;
  84. };