diff options
Diffstat (limited to 'src/mongo/db/matcher/expression_leaf.h')
-rw-r--r-- | src/mongo/db/matcher/expression_leaf.h | 593 |
1 files changed, 312 insertions, 281 deletions
diff --git a/src/mongo/db/matcher/expression_leaf.h b/src/mongo/db/matcher/expression_leaf.h index 44f889d707b..e0e61ac11ea 100644 --- a/src/mongo/db/matcher/expression_leaf.h +++ b/src/mongo/db/matcher/expression_leaf.h @@ -36,366 +36,397 @@ #include "mongo/db/matcher/expression.h" namespace pcrecpp { - class RE; -} // namespace pcrecpp; +class RE; +} // namespace pcrecpp; namespace mongo { - /** - * This file contains leaves in the parse tree that are not array-based. - * - * LeafMatchExpression: REGEX MOD EXISTS MATCH_IN - * ComparisonMatchExpression: EQ LTE LT GT GTE - * MatchExpression: TYPE_OPERATOR - */ +/** + * This file contains leaves in the parse tree that are not array-based. + * + * LeafMatchExpression: REGEX MOD EXISTS MATCH_IN + * ComparisonMatchExpression: EQ LTE LT GT GTE + * MatchExpression: TYPE_OPERATOR + */ - /** - * Many operators subclass from this: - * REGEX, MOD, EXISTS, IN - * Everything that inherits from ComparisonMatchExpression. - */ - class LeafMatchExpression : public MatchExpression { - public: - LeafMatchExpression( MatchType matchType ) - : MatchExpression( matchType ) { - } +/** + * Many operators subclass from this: + * REGEX, MOD, EXISTS, IN + * Everything that inherits from ComparisonMatchExpression. + */ +class LeafMatchExpression : public MatchExpression { +public: + LeafMatchExpression(MatchType matchType) : MatchExpression(matchType) {} - virtual ~LeafMatchExpression(){} + virtual ~LeafMatchExpression() {} - virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; + virtual bool matches(const MatchableDocument* doc, MatchDetails* details = 0) const; - virtual bool matchesSingleElement( const BSONElement& e ) const = 0; + virtual bool matchesSingleElement(const BSONElement& e) const = 0; - virtual const StringData path() const { return _path; } + virtual const StringData path() const { + return _path; + } - protected: - Status initPath( StringData path ); +protected: + Status initPath(StringData path); - private: - StringData _path; - ElementPath _elementPath; - }; +private: + StringData _path; + ElementPath _elementPath; +}; - /** - * EQ, LTE, LT, GT, GTE subclass from ComparisonMatchExpression. - */ - class ComparisonMatchExpression : public LeafMatchExpression { - public: - ComparisonMatchExpression( MatchType type ) : LeafMatchExpression( type ){} +/** + * EQ, LTE, LT, GT, GTE subclass from ComparisonMatchExpression. + */ +class ComparisonMatchExpression : public LeafMatchExpression { +public: + ComparisonMatchExpression(MatchType type) : LeafMatchExpression(type) {} - Status init( StringData path, const BSONElement& rhs ); + Status init(StringData path, const BSONElement& rhs); - virtual ~ComparisonMatchExpression(){} + virtual ~ComparisonMatchExpression() {} - virtual bool matchesSingleElement( const BSONElement& e ) const; + virtual bool matchesSingleElement(const BSONElement& e) const; - virtual const BSONElement& getRHS() const { return _rhs; } + virtual const BSONElement& getRHS() const { + return _rhs; + } - virtual void debugString( StringBuilder& debug, int level = 0 ) const; + virtual void debugString(StringBuilder& debug, int level = 0) const; - virtual void toBSON(BSONObjBuilder* out) const; + virtual void toBSON(BSONObjBuilder* out) const; - virtual bool equivalent( const MatchExpression* other ) const; + virtual bool equivalent(const MatchExpression* other) const; - const BSONElement& getData() const { return _rhs; } + const BSONElement& getData() const { + return _rhs; + } - protected: - BSONElement _rhs; - }; +protected: + BSONElement _rhs; +}; - // - // ComparisonMatchExpression inheritors - // +// +// ComparisonMatchExpression inheritors +// - class EqualityMatchExpression : public ComparisonMatchExpression { - public: - EqualityMatchExpression() : ComparisonMatchExpression( EQ ){} - virtual LeafMatchExpression* shallowClone() const { - ComparisonMatchExpression* e = new EqualityMatchExpression(); - e->init( path(), _rhs ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; +class EqualityMatchExpression : public ComparisonMatchExpression { +public: + EqualityMatchExpression() : ComparisonMatchExpression(EQ) {} + virtual LeafMatchExpression* shallowClone() const { + ComparisonMatchExpression* e = new EqualityMatchExpression(); + e->init(path(), _rhs); + if (getTag()) { + e->setTag(getTag()->clone()); } - }; - - class LTEMatchExpression : public ComparisonMatchExpression { - public: - LTEMatchExpression() : ComparisonMatchExpression( LTE ){} - virtual LeafMatchExpression* shallowClone() const { - ComparisonMatchExpression* e = new LTEMatchExpression(); - e->init( path(), _rhs ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; - } - - }; - - class LTMatchExpression : public ComparisonMatchExpression { - public: - LTMatchExpression() : ComparisonMatchExpression( LT ){} - virtual LeafMatchExpression* shallowClone() const { - ComparisonMatchExpression* e = new LTMatchExpression(); - e->init( path(), _rhs ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; + return e; + } +}; + +class LTEMatchExpression : public ComparisonMatchExpression { +public: + LTEMatchExpression() : ComparisonMatchExpression(LTE) {} + virtual LeafMatchExpression* shallowClone() const { + ComparisonMatchExpression* e = new LTEMatchExpression(); + e->init(path(), _rhs); + if (getTag()) { + e->setTag(getTag()->clone()); } - - }; - - class GTMatchExpression : public ComparisonMatchExpression { - public: - GTMatchExpression() : ComparisonMatchExpression( GT ){} - virtual LeafMatchExpression* shallowClone() const { - ComparisonMatchExpression* e = new GTMatchExpression(); - e->init( path(), _rhs ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; + return e; + } +}; + +class LTMatchExpression : public ComparisonMatchExpression { +public: + LTMatchExpression() : ComparisonMatchExpression(LT) {} + virtual LeafMatchExpression* shallowClone() const { + ComparisonMatchExpression* e = new LTMatchExpression(); + e->init(path(), _rhs); + if (getTag()) { + e->setTag(getTag()->clone()); } - - }; - - class GTEMatchExpression : public ComparisonMatchExpression { - public: - GTEMatchExpression() : ComparisonMatchExpression( GTE ){} - virtual LeafMatchExpression* shallowClone() const { - ComparisonMatchExpression* e = new GTEMatchExpression(); - e->init( path(), _rhs ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; + return e; + } +}; + +class GTMatchExpression : public ComparisonMatchExpression { +public: + GTMatchExpression() : ComparisonMatchExpression(GT) {} + virtual LeafMatchExpression* shallowClone() const { + ComparisonMatchExpression* e = new GTMatchExpression(); + e->init(path(), _rhs); + if (getTag()) { + e->setTag(getTag()->clone()); } - - }; - - // - // LeafMatchExpression inheritors - // - - class RegexMatchExpression : public LeafMatchExpression { - public: - /** - * Maximum pattern size which pcre v8.3 can do matches correctly with - * LINK_SIZE define macro set to 2 @ pcre's config.h (based on - * experiments) - */ - static const size_t MaxPatternSize = 32764; - - RegexMatchExpression(); - ~RegexMatchExpression(); - - Status init( StringData path, StringData regex, StringData options ); - Status init( StringData path, const BSONElement& e ); - - virtual LeafMatchExpression* shallowClone() const { - RegexMatchExpression* e = new RegexMatchExpression(); - e->init( path(), _regex, _flags ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; + return e; + } +}; + +class GTEMatchExpression : public ComparisonMatchExpression { +public: + GTEMatchExpression() : ComparisonMatchExpression(GTE) {} + virtual LeafMatchExpression* shallowClone() const { + ComparisonMatchExpression* e = new GTEMatchExpression(); + e->init(path(), _rhs); + if (getTag()) { + e->setTag(getTag()->clone()); } + return e; + } +}; - virtual bool matchesSingleElement( const BSONElement& e ) const; - - virtual void debugString( StringBuilder& debug, int level ) const; - - virtual void toBSON(BSONObjBuilder* out) const; +// +// LeafMatchExpression inheritors +// - void shortDebugString( StringBuilder& debug ) const; +class RegexMatchExpression : public LeafMatchExpression { +public: + /** + * Maximum pattern size which pcre v8.3 can do matches correctly with + * LINK_SIZE define macro set to 2 @ pcre's config.h (based on + * experiments) + */ + static const size_t MaxPatternSize = 32764; - virtual bool equivalent( const MatchExpression* other ) const; + RegexMatchExpression(); + ~RegexMatchExpression(); - const std::string& getString() const { return _regex; } - const std::string& getFlags() const { return _flags; } + Status init(StringData path, StringData regex, StringData options); + Status init(StringData path, const BSONElement& e); - private: - std::string _regex; - std::string _flags; - std::unique_ptr<pcrecpp::RE> _re; - }; + virtual LeafMatchExpression* shallowClone() const { + RegexMatchExpression* e = new RegexMatchExpression(); + e->init(path(), _regex, _flags); + if (getTag()) { + e->setTag(getTag()->clone()); + } + return e; + } - class ModMatchExpression : public LeafMatchExpression { - public: - ModMatchExpression() : LeafMatchExpression( MOD ){} + virtual bool matchesSingleElement(const BSONElement& e) const; - Status init( StringData path, int divisor, int remainder ); + virtual void debugString(StringBuilder& debug, int level) const; - virtual LeafMatchExpression* shallowClone() const { - ModMatchExpression* m = new ModMatchExpression(); - m->init( path(), _divisor, _remainder ); - if ( getTag() ) { - m->setTag(getTag()->clone()); - } - return m; - } + virtual void toBSON(BSONObjBuilder* out) const; - virtual bool matchesSingleElement( const BSONElement& e ) const; + void shortDebugString(StringBuilder& debug) const; - virtual void debugString( StringBuilder& debug, int level ) const; + virtual bool equivalent(const MatchExpression* other) const; - virtual void toBSON(BSONObjBuilder* out) const; + const std::string& getString() const { + return _regex; + } + const std::string& getFlags() const { + return _flags; + } - virtual bool equivalent( const MatchExpression* other ) const; +private: + std::string _regex; + std::string _flags; + std::unique_ptr<pcrecpp::RE> _re; +}; - int getDivisor() const { return _divisor; } - int getRemainder() const { return _remainder; } +class ModMatchExpression : public LeafMatchExpression { +public: + ModMatchExpression() : LeafMatchExpression(MOD) {} - private: - int _divisor; - int _remainder; - }; + Status init(StringData path, int divisor, int remainder); - class ExistsMatchExpression : public LeafMatchExpression { - public: - ExistsMatchExpression() : LeafMatchExpression( EXISTS ){} + virtual LeafMatchExpression* shallowClone() const { + ModMatchExpression* m = new ModMatchExpression(); + m->init(path(), _divisor, _remainder); + if (getTag()) { + m->setTag(getTag()->clone()); + } + return m; + } - Status init( StringData path ); + virtual bool matchesSingleElement(const BSONElement& e) const; - virtual LeafMatchExpression* shallowClone() const { - ExistsMatchExpression* e = new ExistsMatchExpression(); - e->init( path() ); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; - } + virtual void debugString(StringBuilder& debug, int level) const; - virtual bool matchesSingleElement( const BSONElement& e ) const; + virtual void toBSON(BSONObjBuilder* out) const; - virtual void debugString( StringBuilder& debug, int level ) const; + virtual bool equivalent(const MatchExpression* other) const; - virtual void toBSON(BSONObjBuilder* out) const; + int getDivisor() const { + return _divisor; + } + int getRemainder() const { + return _remainder; + } - virtual bool equivalent( const MatchExpression* other ) const; - }; +private: + int _divisor; + int _remainder; +}; - /** - * INTERNAL - * terrible name - * holds the entries of an $in or $all - * either scalars or regex - */ - class ArrayFilterEntries { - MONGO_DISALLOW_COPYING( ArrayFilterEntries ); - public: - ArrayFilterEntries(); - ~ArrayFilterEntries(); +class ExistsMatchExpression : public LeafMatchExpression { +public: + ExistsMatchExpression() : LeafMatchExpression(EXISTS) {} - Status addEquality( const BSONElement& e ); - Status addRegex( RegexMatchExpression* expr ); + Status init(StringData path); - const BSONElementSet& equalities() const { return _equalities; } - bool contains( const BSONElement& elem ) const { return _equalities.count(elem) > 0; } + virtual LeafMatchExpression* shallowClone() const { + ExistsMatchExpression* e = new ExistsMatchExpression(); + e->init(path()); + if (getTag()) { + e->setTag(getTag()->clone()); + } + return e; + } - size_t numRegexes() const { return _regexes.size(); } - RegexMatchExpression* regex( int idx ) const { return _regexes[idx]; } + virtual bool matchesSingleElement(const BSONElement& e) const; - bool hasNull() const { return _hasNull; } - bool singleNull() const { return size() == 1 && _hasNull; } - bool hasEmptyArray() const { return _hasEmptyArray; } - int size() const { return _equalities.size() + _regexes.size(); } + virtual void debugString(StringBuilder& debug, int level) const; - bool equivalent( const ArrayFilterEntries& other ) const; + virtual void toBSON(BSONObjBuilder* out) const; - void copyTo( ArrayFilterEntries& toFillIn ) const; + virtual bool equivalent(const MatchExpression* other) const; +}; - void debugString( StringBuilder& debug ) const; +/** + * INTERNAL + * terrible name + * holds the entries of an $in or $all + * either scalars or regex + */ +class ArrayFilterEntries { + MONGO_DISALLOW_COPYING(ArrayFilterEntries); + +public: + ArrayFilterEntries(); + ~ArrayFilterEntries(); + + Status addEquality(const BSONElement& e); + Status addRegex(RegexMatchExpression* expr); + + const BSONElementSet& equalities() const { + return _equalities; + } + bool contains(const BSONElement& elem) const { + return _equalities.count(elem) > 0; + } + + size_t numRegexes() const { + return _regexes.size(); + } + RegexMatchExpression* regex(int idx) const { + return _regexes[idx]; + } + + bool hasNull() const { + return _hasNull; + } + bool singleNull() const { + return size() == 1 && _hasNull; + } + bool hasEmptyArray() const { + return _hasEmptyArray; + } + int size() const { + return _equalities.size() + _regexes.size(); + } + + bool equivalent(const ArrayFilterEntries& other) const; + + void copyTo(ArrayFilterEntries& toFillIn) const; + + void debugString(StringBuilder& debug) const; + + void toBSON(BSONArrayBuilder* out) const; + +private: + bool _hasNull; // if _equalities has a jstNULL element in it + bool _hasEmptyArray; + BSONElementSet _equalities; + std::vector<RegexMatchExpression*> _regexes; +}; - void toBSON(BSONArrayBuilder* out) const; +/** + * query operator: $in + */ +class InMatchExpression : public LeafMatchExpression { +public: + InMatchExpression() : LeafMatchExpression(MATCH_IN) {} + Status init(StringData path); - private: - bool _hasNull; // if _equalities has a jstNULL element in it - bool _hasEmptyArray; - BSONElementSet _equalities; - std::vector<RegexMatchExpression*> _regexes; - }; + virtual LeafMatchExpression* shallowClone() const; - /** - * query operator: $in - */ - class InMatchExpression : public LeafMatchExpression { - public: - InMatchExpression() : LeafMatchExpression( MATCH_IN ){} - Status init( StringData path ); + ArrayFilterEntries* getArrayFilterEntries() { + return &_arrayEntries; + } - virtual LeafMatchExpression* shallowClone() const; + virtual bool matchesSingleElement(const BSONElement& e) const; - ArrayFilterEntries* getArrayFilterEntries() { return &_arrayEntries; } + virtual void debugString(StringBuilder& debug, int level) const; - virtual bool matchesSingleElement( const BSONElement& e ) const; + virtual void toBSON(BSONObjBuilder* out) const; - virtual void debugString( StringBuilder& debug, int level ) const; + virtual bool equivalent(const MatchExpression* other) const; - virtual void toBSON(BSONObjBuilder* out) const; + void copyTo(InMatchExpression* toFillIn) const; - virtual bool equivalent( const MatchExpression* other ) const; + const ArrayFilterEntries& getData() const { + return _arrayEntries; + } - void copyTo( InMatchExpression* toFillIn ) const; +private: + bool _matchesRealElement(const BSONElement& e) const; + ArrayFilterEntries _arrayEntries; +}; - const ArrayFilterEntries& getData() const { return _arrayEntries; } +// +// The odd duck out, TYPE_OPERATOR. +// - private: - bool _matchesRealElement( const BSONElement& e ) const; - ArrayFilterEntries _arrayEntries; - }; +/** + * Type has some odd semantics with arrays and as such it can't inherit from + * LeafMatchExpression. + */ +class TypeMatchExpression : public MatchExpression { +public: + TypeMatchExpression() : MatchExpression(TYPE_OPERATOR) {} - // - // The odd duck out, TYPE_OPERATOR. - // + Status init(StringData path, int type); - /** - * Type has some odd semantics with arrays and as such it can't inherit from - * LeafMatchExpression. - */ - class TypeMatchExpression : public MatchExpression { - public: - TypeMatchExpression() : MatchExpression( TYPE_OPERATOR ){} - - Status init( StringData path, int type ); - - virtual MatchExpression* shallowClone() const { - TypeMatchExpression* e = new TypeMatchExpression(); - e->init(_path, _type); - if ( getTag() ) { - e->setTag(getTag()->clone()); - } - return e; + virtual MatchExpression* shallowClone() const { + TypeMatchExpression* e = new TypeMatchExpression(); + e->init(_path, _type); + if (getTag()) { + e->setTag(getTag()->clone()); } + return e; + } - virtual bool matchesSingleElement( const BSONElement& e ) const; + virtual bool matchesSingleElement(const BSONElement& e) const; - virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; + virtual bool matches(const MatchableDocument* doc, MatchDetails* details = 0) const; - virtual void debugString( StringBuilder& debug, int level ) const; + virtual void debugString(StringBuilder& debug, int level) const; - virtual void toBSON(BSONObjBuilder* out) const; + virtual void toBSON(BSONObjBuilder* out) const; - virtual bool equivalent( const MatchExpression* other ) const; + virtual bool equivalent(const MatchExpression* other) const; - /** - * What is the type we're matching against? - */ - int getData() const { return _type; } + /** + * What is the type we're matching against? + */ + int getData() const { + return _type; + } - virtual const StringData path() const { return _path; } + virtual const StringData path() const { + return _path; + } - private: - bool _matches( StringData path, - const MatchableDocument* doc, - MatchDetails* details = 0 ) const; +private: + bool _matches(StringData path, const MatchableDocument* doc, MatchDetails* details = 0) const; - StringData _path; - ElementPath _elementPath; - int _type; - }; + StringData _path; + ElementPath _elementPath; + int _type; +}; } // namespace mongo |