BSONElementIterator.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. ElementPath = require('ElementPath');
  3. BSONElement = require('BSONElemtn');
  4. // Autogenerated by cport.py on 2013-09-17 14:37
  5. var BSONElementIterator = module.exports = function ( path, context ){
  6. // File: path.cpp lines: 77-80
  7. this._path = path;
  8. this._context = context;
  9. this._state = 'BEGIN';
  10. //console.debug( 'path: ' + path.fieldRef().dottedField() + ' context: ' + context + '\n');
  11. }, klass = BSONElementIterator, base = Object , proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  12. // File: path.h lines: 140-140
  13. // ArrayIterationState _arrayIterationState;
  14. proto._arrayIterationState = undefined;
  15. // File: path.h lines: 117-117
  16. // Context _next;
  17. proto._next = undefined;
  18. // File: path.h lines: 142-142
  19. // boost::scoped_ptr<ElementIterator> _subCursor;
  20. proto._subCursor = undefined;
  21. // File: path.h lines: 143-143
  22. // boost::scoped_ptr<ElementPath> _subCursorPath;
  23. proto._subCursorPath = undefined;
  24. /**
  25. *
  26. * This documentation was automatically generated. Please update when you touch this function.
  27. * @method more
  28. * @param
  29. *
  30. */
  31. proto.more = function more( /* */ ){
  32. // File: path.cpp lines: 120-230
  33. if( this._subCursor ){
  34. if( this._subCursor.more() ) {return true;}
  35. this._subCursor.reset();
  36. if ( this._arrayIterationState.isArrayOffsetMatch( this._arrayIterationState._current.fieldName() ) ) {
  37. if ( this._arrayIterationState.nextEntireRest() ) {
  38. this._next.reset( this._arrayIterationState._current, this._arrayIterationState._current, true );
  39. this._arrayIterationState._current = new BSONElement(); //BSON
  40. return true;
  41. }
  42. this._subCursorPath.reset( new ElementPath() );
  43. this._subCursorPath.init( this._arrayIterationState.restOfPath.substr( this._arrayIterationState.nextPieceOfPath.size() + 1 ) );
  44. this._subCursorPath.setTraverseLeafArray( this._path.shouldTraverseLeafArray() );
  45. this._subCursor.reset( new BSONElementIterator( this._subCursorPath, this._arrayIterationState._current.Obj() ) );
  46. this._arrayIterationState._current = BSONElement();
  47. return this.more();
  48. }
  49. }
  50. if ( !this._next.element().eoo() ) {return true;}
  51. if ( this._state == 'DONE' ){ return false; }
  52. if ( this._state == 'BEGIN' ) {
  53. var idxPath = {pathID:0};
  54. var e = ElementPath.getFieldDottedOrArray( this._context, this._path.fieldRef(), idxPath ); //BSONElement
  55. if ( e.type() != Array ) {
  56. this._next.reset( e, new BSONElement(), false );
  57. this._state = 'DONE';
  58. return true;
  59. }
  60. // its an array
  61. this._arrayIterationState.reset( this._path.fieldRef(), idxPath.pathID + 1 );
  62. if ( !this._arrayIterationState.hasMore && this._path.shouldTraverseLeafArray() ) {
  63. this._next.reset( e, new BSONElement(), true );
  64. this._state = 'DONE';
  65. return true;
  66. }
  67. this._arrayIterationState.startIterator( e );
  68. this._state = IN_ARRAY;
  69. return more();
  70. }
  71. if ( this._state == 'IN_ARRAY' ) {
  72. while ( this._arrayIterationState.more() ) {
  73. var x = this._arrayIterationState.next(); //BSONElement
  74. if ( !this._arrayIterationState.hasMore() ) {
  75. this._next.reset( x, x, false );
  76. return true;
  77. }
  78. // i have deeper to go
  79. if ( x.type() == Object ) {
  80. this._subCursorPath.reset( new ElementPath() );
  81. this._subCursorPath.init( this._arrayIterationState.restOfPath );
  82. this._subCursorPath.setTraverseLeafArray( this._path.shouldTraverseLeafArray() );
  83. this._subCursor.reset( new BSONElementIterator( this._subCursorPath, x.Obj() ) );
  84. return more();
  85. }
  86. if ( this._arrayIterationState.isArrayOffsetMatch( x.fieldName() ) ) {
  87. if ( this._arrayIterationState.nextEntireRest() ) {
  88. this._next.reset( x, x, false );
  89. return true;
  90. }
  91. if ( x.isABSONObj() ) {
  92. this._subCursorPath.reset( new ElementPath() );
  93. this._subCursorPath.init( this._arrayIterationState.restOfPath.substr( this._arrayIterationState.nextPieceOfPath.size() + 1 ) );
  94. this._subCursorPath.setTraverseLeafArray( this._path.shouldTraverseLeafArray() );
  95. var real = new BSONElementIterator( this._subCursorPath, this._arrayIterationState._current.Obj() );
  96. this._subCursor.reset( real );
  97. real._arrayIterationState.reset( this._subCursorPath.fieldRef(), 0 );
  98. real._arrayIterationState.startIterator( x );
  99. real._state = 'IN_ARRAY';
  100. this._arrayIterationState._current = new BSONElement();
  101. return more();
  102. }
  103. }
  104. }
  105. if ( this._arrayIterationState.hasMore() ) { return false; }
  106. this._next.reset( this._arrayIterationState._theArray, new BSONElement(), true );
  107. this._state = 'DONE';
  108. return true;
  109. }
  110. return false;
  111. };
  112. /**
  113. *
  114. * This documentation was automatically generated. Please update when you touch this function.
  115. * @method next
  116. * @param
  117. *
  118. */
  119. proto.next = function next(){
  120. // File: path.cpp lines: 233-241
  121. if ( this._subCursor ) {
  122. var e = this._subCursor.next(); //Context
  123. e.setArrayOffset( this._arrayIterationState._current );
  124. return e;
  125. }
  126. var x = _next; //Context
  127. this._next.reset();
  128. return x;
  129. };