SkipDocumentSource.js 4.0 KB

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