LimitDocumentSource.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Create an object that represents the document source. The object
  50. * will have a single field whose name is the source's name. This
  51. * will be used by the default implementation of addToJsonArray()
  52. * to add this object to a pipeline being represented in JSON.
  53. *
  54. * @method sourceToJson
  55. * @param {Object} builder JSONObjBuilder: a blank object builder to write to
  56. * @param {Boolean} explain create explain output
  57. **/
  58. proto.sourceToJson = function sourceToJson(builder, explain) {
  59. builder.$limit = this.limit;
  60. };
  61. /**
  62. * Creates a new LimitDocumentSource with the input number as the limit
  63. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  64. **/
  65. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  66. if (typeof jsonElement !== "number") throw new Error("code 15957; the limit must be specified as a number");
  67. var Limit = proto.getFactory(),
  68. nextLimit = new Limit(ctx);
  69. nextLimit.limit = jsonElement;
  70. if ((nextLimit.limit <= 0) || isNaN(nextLimit.limit)) throw new Error("code 15958; the limit must be positive");
  71. return nextLimit;
  72. };
  73. proto.getLimit = function getLimit(newLimit) {
  74. return this.limit;
  75. };
  76. proto.setLimit = function setLimit(newLimit) {
  77. this.limit = newLimit;
  78. };
  79. proto.getDependencies = function(deps) {
  80. return DocumentSource.GetDepsReturn.SEE_NEXT;
  81. };
  82. proto.serialize = function(explain) {
  83. var out = {};
  84. out[this.getSourceName()] = this.limit;
  85. return out;
  86. };