SkipDocumentSource.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var SkipDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A document source skipper
  5. *
  6. * @class SkipDocumentSource
  7. * @namespace munge.pipeline.documentsource
  8. * @module munge
  9. * @constructor
  10. * @param {Object} query the match query to use
  11. **/
  12. var klass = module.exports = SkipDocumentSource = function SkipDocumentSource(/* pCtx*/){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  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.pSource.eof() && this.count++ < this.skip) {
  40. this.pSource.advance();
  41. }
  42. }
  43. if (this.pSource.eof()) {
  44. this.pCurrent = null;
  45. return;
  46. }
  47. this.pCurrent = this.pSource.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.pSource.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.pSource.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.pCurrent = null;
  80. return false;
  81. }
  82. this.pCurrent = this.pSource.getCurrent();
  83. return this.pSource.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) {
  104. if (typeof JsonElement !== "number") throw new Error("code 15972; the value to skip must be a number");
  105. var nextSkip = new SkipDocumentSource();
  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. /**
  111. * Reset the document source so that it is ready for a new stream of data.
  112. * Note that this is a deviation from the mongo implementation.
  113. *
  114. * @method reset
  115. **/
  116. proto.reset = function reset(){
  117. this.count = 0;
  118. };
  119. return klass;
  120. })();