SkipDocumentSource.js 3.5 KB

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