diff options
Diffstat (limited to 'src/mongo/db/fts/fts_element_iterator.h')
-rw-r--r-- | src/mongo/db/fts/fts_element_iterator.h | 246 |
1 files changed, 117 insertions, 129 deletions
diff --git a/src/mongo/db/fts/fts_element_iterator.h b/src/mongo/db/fts/fts_element_iterator.h index 17f72fff7f9..21e1e446627 100644 --- a/src/mongo/db/fts/fts_element_iterator.h +++ b/src/mongo/db/fts/fts_element_iterator.h @@ -40,133 +40,121 @@ namespace mongo { - namespace fts { - - /** - * Encapsulates data fields returned by FTSElementIterator - */ - struct FTSIteratorValue { - - FTSIteratorValue( const char* text, - const FTSLanguage* language, - double weight ) - : _text(text), - _language(language), - _weight(weight), - _valid(true) - {} - - FTSIteratorValue() - : _text(NULL), - _language(), - _weight(0.0), - _valid(false) - {} - - bool valid() const { return _valid; } - - const char* _text; - const FTSLanguage* _language; - double _weight; - bool _valid; - }; - - /** - * Iterator pattern for walking through text-indexed fields of a - * BSON document. - * - * Example usage: - * FTSSpec spec( FTSSpec::fixSpec( indexSpec ) ); - * FTSElementIterator it( spec, obj ); - * while ( it.more() ) { - * FTSIteratorValue val = it.next(); - * std::cout << val._text << '[' << val._language.str() - * << ',' << val._weight << ']' << std::endl; - * } - * - */ - class FTSElementIterator { - public: - /** - * Iterator constructor - * - * Note: Caller must ensure that the constructed FTSElementIterator - * does >not< outlive either spec or obj. - * - * @arg spec text index specifier - * @arg obj document that the iterator will traverse - */ - FTSElementIterator( const FTSSpec& spec, const BSONObj& obj); - - /** - * Iterator interface: returns false iff there are no further text-indexable fields. - */ - bool more(); - - /** - * Iterator interface: advances to the next text-indexable field. - */ - FTSIteratorValue next(); - - /** - * Iterator frame needed for iterative implementation of - * recursive sub-documents. - */ - struct FTSIteratorFrame { - FTSIteratorFrame( const BSONObj& obj, - const FTSSpec& spec, - const FTSLanguage* parentLanguage, - const std::string& parentPath, - bool isArray ) - : _it( obj ), - _language( spec._getLanguageToUseV2( obj, parentLanguage ) ), - _parentPath( parentPath ), - _isArray( isArray ) - {} - - friend std::ostream& operator<<(std::ostream&, FTSIteratorFrame&); - - BSONObjIterator _it; - const FTSLanguage* _language; - std::string _parentPath; - bool _isArray; - }; - - private: - /** - * Helper method: - * returns false iff all FTSIteratorFrames on _frameStack are exhausted. - */ - bool moreFrames(); - - /** - * Helper method: - * advances to the next text-indexable field, possibly pushing frames as - * needed for recursive sub-documents. - */ - FTSIteratorValue advance(); - - /** - * Stack used by iterative implementation of recursive sub-document traversal. - */ - std::stack<FTSIteratorFrame> _frameStack; - - /** - * Current frame, not yet pushed to stack. - */ - FTSIteratorFrame _frame; - - /** - * Constructor input parameter: text index specification. - */ - const FTSSpec& _spec; - - /** - * Current iterator return value, computed by 'more()', returned by 'next()'. - */ - FTSIteratorValue _currentValue; - }; - - } // namespace fts -} // namespace mongo +namespace fts { +/** + * Encapsulates data fields returned by FTSElementIterator + */ +struct FTSIteratorValue { + FTSIteratorValue(const char* text, const FTSLanguage* language, double weight) + : _text(text), _language(language), _weight(weight), _valid(true) {} + + FTSIteratorValue() : _text(NULL), _language(), _weight(0.0), _valid(false) {} + + bool valid() const { + return _valid; + } + + const char* _text; + const FTSLanguage* _language; + double _weight; + bool _valid; +}; + +/** + * Iterator pattern for walking through text-indexed fields of a + * BSON document. + * + * Example usage: + * FTSSpec spec( FTSSpec::fixSpec( indexSpec ) ); + * FTSElementIterator it( spec, obj ); + * while ( it.more() ) { + * FTSIteratorValue val = it.next(); + * std::cout << val._text << '[' << val._language.str() + * << ',' << val._weight << ']' << std::endl; + * } + * + */ +class FTSElementIterator { +public: + /** + * Iterator constructor + * + * Note: Caller must ensure that the constructed FTSElementIterator + * does >not< outlive either spec or obj. + * + * @arg spec text index specifier + * @arg obj document that the iterator will traverse + */ + FTSElementIterator(const FTSSpec& spec, const BSONObj& obj); + + /** + * Iterator interface: returns false iff there are no further text-indexable fields. + */ + bool more(); + + /** + * Iterator interface: advances to the next text-indexable field. + */ + FTSIteratorValue next(); + + /** + * Iterator frame needed for iterative implementation of + * recursive sub-documents. + */ + struct FTSIteratorFrame { + FTSIteratorFrame(const BSONObj& obj, + const FTSSpec& spec, + const FTSLanguage* parentLanguage, + const std::string& parentPath, + bool isArray) + : _it(obj), + _language(spec._getLanguageToUseV2(obj, parentLanguage)), + _parentPath(parentPath), + _isArray(isArray) {} + + friend std::ostream& operator<<(std::ostream&, FTSIteratorFrame&); + + BSONObjIterator _it; + const FTSLanguage* _language; + std::string _parentPath; + bool _isArray; + }; + +private: + /** + * Helper method: + * returns false iff all FTSIteratorFrames on _frameStack are exhausted. + */ + bool moreFrames(); + + /** + * Helper method: + * advances to the next text-indexable field, possibly pushing frames as + * needed for recursive sub-documents. + */ + FTSIteratorValue advance(); + + /** + * Stack used by iterative implementation of recursive sub-document traversal. + */ + std::stack<FTSIteratorFrame> _frameStack; + + /** + * Current frame, not yet pushed to stack. + */ + FTSIteratorFrame _frame; + + /** + * Constructor input parameter: text index specification. + */ + const FTSSpec& _spec; + + /** + * Current iterator return value, computed by 'more()', returned by 'next()'. + */ + FTSIteratorValue _currentValue; +}; + +} // namespace fts +} // namespace mongo |