LimitDocumentSource.js 2.5 KB

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