FilterBaseDocumentSource.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var FilterBaseDocumentSource = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A base class for filter document sources
  5. *
  6. * @class FilterBaseDocumentSource
  7. * @namespace munge.pipepline.documentsource
  8. * @module munge
  9. * @constructor
  10. * @param {ExpressionContext}
  11. **/
  12. var klass = module.exports = FilterBaseDocumentSource = function FilterBaseDocumentSource(/*pCtx*/){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  15. this.unstarted = true;
  16. this.hasNext = false;
  17. this.pCurrent = null;
  18. }, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  19. /**
  20. * Find the next acceptable source document, if there are any left.
  21. *
  22. * @method findNext
  23. **/
  24. proto.findNext = function findNext() {
  25. /* only do this the first time */
  26. if (this.unstarted) {
  27. this.hasNext = !this.pSource.eof();
  28. this.unstarted = false;
  29. }
  30. while(this.hasNext) {
  31. pDocument = this.pSource.getCurrent();
  32. this.hasNext = this.pSource.advance();
  33. if (this.accept(pDocument)) {
  34. this.pCurrent = pDocument;
  35. return;
  36. }
  37. }
  38. this.pCurrent.reset();
  39. };
  40. /**
  41. * Is the source at EOF?
  42. *
  43. * @method eof
  44. **/
  45. proto.eof = function eof() {
  46. if (this.unstarted)
  47. this.findNext();
  48. return (this.pCurrent.get() === null);
  49. };
  50. /**
  51. * Advance the state of the DocumentSource so that it will return the next Document.
  52. * The default implementation returns false, after checking for interrupts.
  53. * Derived classes can call the default implementation in their own implementations in order to check for interrupts.
  54. *
  55. * @method advance
  56. * @returns {Boolean} whether there is another document to fetch, i.e., whether or not getCurrent() will succeed. This default implementation always returns false.
  57. **/
  58. proto.advance = function advance() {
  59. base.prototype.advance.call(this); // check for interrupts
  60. if (this.unstarted)
  61. this.findNext();
  62. /**
  63. * This looks weird after the above, but is correct. Note that calling
  64. * getCurrent() when first starting already yields the first document
  65. * in the collection. Calling advance() without using getCurrent()
  66. * first will skip over the first item.
  67. **/
  68. this.findNext();
  69. return (this.pCurrent.get() !== null);
  70. };
  71. /**
  72. * some implementations do the equivalent of verify(!eof()) so check eof() first
  73. *
  74. * @method getCurrent
  75. * @returns {Document} the current Document without advancing
  76. **/
  77. proto.getCurrent = function getCurrent() {
  78. if (this.unstarted)
  79. this.findNext();
  80. if (this.pCurrent.get() === null) throw new Error("This should never happen");
  81. return pCurrent;
  82. };
  83. /**
  84. * Test the given document against the predicate and report if it
  85. * should be accepted or not.
  86. *
  87. * @param {object} pDocument the document to test
  88. * @returns {bool} true if the document matches the filter, false otherwise
  89. **/
  90. proto.accept = function accept(pDocument) {
  91. throw new Error("not implemented");
  92. };
  93. /**
  94. * Create a JSONObj suitable for Matcher construction.
  95. *
  96. * This is used after filter analysis has moved as many filters to
  97. * as early a point as possible in the document processing pipeline.
  98. * See db/Matcher.h and the associated wiki documentation for the
  99. * format. This conversion is used to move back to the low-level
  100. * find() Cursor mechanism.
  101. *
  102. * @param pBuilder the builder to write to
  103. **/
  104. proto.toMatcherJson = function toMatcherJson(pBuilder) {
  105. throw new Error("not implemented");
  106. };
  107. return klass;
  108. })();