LimitDocumentSource.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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){
  12. if (arguments.length > 1) throw new Error("up to one arg expected");
  13. base.call(this, ctx);
  14. this.limit = 0;
  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. proto.getNext = function getNext(callback) {
  38. if (!callback) throw new Error(this.getSourceName() + ' #getNext() requires callback');
  39. if (++this.count > this.limit) {
  40. this.source.dispose();
  41. callback(null, DocumentSource.EOF);
  42. return DocumentSource.EOF;
  43. }
  44. return this.source.getNext(callback);
  45. };
  46. /**
  47. * Creates a new LimitDocumentSource with the input number as the limit
  48. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  49. **/
  50. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  51. if (typeof jsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  52. var Limit = proto.getFactory(),
  53. nextLimit = new Limit(ctx);
  54. nextLimit.limit = jsonElement;
  55. if ((nextLimit.limit <= 0) || isNaN(nextLimit.limit)) throw new Error("code 15958; the limit must be positive");
  56. return nextLimit;
  57. };
  58. proto.getLimit = function getLimit(newLimit) {
  59. return this.limit;
  60. };
  61. proto.setLimit = function setLimit(newLimit) {
  62. this.limit = newLimit;
  63. };
  64. proto.getDependencies = function(deps) {
  65. return DocumentSource.GetDepsReturn.SEE_NEXT;
  66. };
  67. proto.serialize = function(explain) {
  68. var out = {};
  69. out[this.getSourceName()] = this.limit;
  70. return out;
  71. };