SkipDocumentSource.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. var SkipDocumentSource = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A document source skipper
  6. * @class SkipDocumentSource
  7. * @namespace mungedb-aggregate.pipeline.documentSources
  8. * @module mungedb-aggregate
  9. * @constructor
  10. * @param [ctx] {ExpressionContext}
  11. **/
  12. var klass = module.exports = SkipDocumentSource = function SkipDocumentSource(ctx){
  13. if(arguments.length > 1) throw new Error("up to one arg expected");
  14. base.call(this, ctx);
  15. this.skip = 0;
  16. this.count = 0;
  17. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. klass.skipName = "$skip";
  19. proto.getSourceName = function getSourceName(){
  20. return klass.skipName;
  21. };
  22. /**
  23. * Coalesce skips together
  24. *
  25. * @param {Object} nextSource the next source
  26. * @return {bool} return whether we can coalese together
  27. **/
  28. proto.coalesce = function coalesce(nextSource) {
  29. var nextSkip = nextSource.constructor === SkipDocumentSource?nextSource:null;
  30. /* if it's not another $skip, we can't coalesce */
  31. if (!nextSkip)
  32. return false;
  33. /* we need to skip over the sum of the two consecutive $skips */
  34. this.skip += nextSkip.skip;
  35. return true;
  36. };
  37. proto.skipper = function skipper() {
  38. if (this.count === 0) {
  39. while (!this.source.eof() && this.count++ < this.skip) {
  40. this.source.advance();
  41. }
  42. }
  43. if (this.source.eof()) {
  44. this.current = null;
  45. return;
  46. }
  47. this.current = this.source.getCurrent();
  48. };
  49. /**
  50. * Is the source at EOF?
  51. *
  52. * @method eof
  53. **/
  54. proto.eof = function eof() {
  55. this.skipper();
  56. return this.source.eof();
  57. };
  58. /**
  59. * some implementations do the equivalent of verify(!eof()) so check eof() first
  60. *
  61. * @method getCurrent
  62. * @returns {Document} the current Document without advancing
  63. **/
  64. proto.getCurrent = function getCurrent() {
  65. this.skipper();
  66. return this.source.getCurrent();
  67. };
  68. /**
  69. * Advance the state of the DocumentSource so that it will return the next Document.
  70. * The default implementation returns false, after checking for interrupts.
  71. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  72. *
  73. * @method advance
  74. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  75. **/
  76. proto.advance = function advance() {
  77. base.prototype.advance.call(this); // check for interrupts
  78. if (this.eof()) {
  79. this.current = null;
  80. return false;
  81. }
  82. this.current = this.source.getCurrent();
  83. return this.source.advance();
  84. };
  85. /**
  86. * Create an object that represents the document source. The object
  87. * will have a single field whose name is the source's name. This
  88. * will be used by the default implementation of addToJsonArray()
  89. * to add this object to a pipeline being represented in JSON.
  90. *
  91. * @method sourceToJson
  92. * @param {Object} builder JSONObjBuilder: a blank object builder to write to
  93. * @param {Boolean} explain create explain output
  94. **/
  95. proto.sourceToJson = function sourceToJson(builder, explain) {
  96. builder.$skip = this.skip;
  97. };
  98. /**
  99. * Creates a new SkipDocumentSource with the input number as the skip
  100. *
  101. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  102. **/
  103. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  104. if (typeof jsonElement !== "number") throw new Error("code 15972; the value to skip must be a number");
  105. var nextSkip = new SkipDocumentSource(ctx);
  106. nextSkip.skip = jsonElement;
  107. if ((nextSkip.skip < 0) || isNaN(nextSkip.skip)) throw new Error("code 15956; the number to skip cannot be negative");
  108. return nextSkip;
  109. };
  110. return klass;
  111. })();