summaryrefslogtreecommitdiff
path: root/src/mongo/db/matcher/expression_leaf.h
blob: a0aebe7a345919f2330d58be46bea30241761d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// expression_leaf.h

/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <pcrecpp.h>

#include <boost/scoped_ptr.hpp>

#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonmisc.h"
#include "mongo/db/matcher/expression.h"

namespace mongo {

    class LeafMatchExpression : public MatchExpression {
    public:
        LeafMatchExpression() {
            _allHaveToMatch = false;
        }

        virtual ~LeafMatchExpression(){}

        virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const;

        virtual bool matchesSingleElement( const BSONElement& e ) const = 0;
    protected:
        StringData _path;
        bool _allHaveToMatch;
    };

    // -----

    class ComparisonMatchExpression : public LeafMatchExpression {
    public:
        enum Type { LTE, LT, EQ, GT, GTE, NE };

        Status init( const StringData& path, Type type, const BSONElement& rhs );

        virtual ~ComparisonMatchExpression(){}

        virtual Type getType() const { return _type; }

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level = 0 ) const;
    protected:
        bool _invertForNE( bool normal ) const;

    private:
        Type _type;
        BSONElement _rhs;
    };

    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;

        Status init( const StringData& path, const StringData& regex, const StringData& options );
        Status init( const StringData& path, const BSONElement& e );

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        std::string _regex;
        std::string _flags;
        boost::scoped_ptr<pcrecpp::RE> _re;
    };

    class ModMatchExpression : public LeafMatchExpression {
    public:
        Status init( const StringData& path, int divisor, int remainder );

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        int _divisor;
        int _remainder;
    };

    class ExistsMatchExpression : public LeafMatchExpression {
    public:
        Status init( const StringData& path, bool exists );

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        bool _exists;
    };

    class TypeMatchExpression : public MatchExpression {
    public:
        Status init( const StringData& path, int type );

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        bool _matches( const StringData& path, const BSONObj& doc, MatchDetails* details = 0 ) const;

        StringData _path;
        int _type;
    };

    /**
     * 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; }
        int size() const { return _equalities.size() + _regexes.size(); }

    private:
        bool _hasNull; // if _equalities has a jstNULL element in it
        BSONElementSet _equalities;
        std::vector<RegexMatchExpression*> _regexes;
    };


    /**
     * query operator: $in
     */
    class InMatchExpression : public LeafMatchExpression {
    public:
        void init( const StringData& path );
        ArrayFilterEntries* getArrayFilterEntries() { return &_arrayEntries; }

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        ArrayFilterEntries _arrayEntries;
    };

    class NinMatchExpression : public LeafMatchExpression {
    public:
        void init( const StringData& path );
        ArrayFilterEntries* getArrayFilterEntries() { return _in.getArrayFilterEntries(); }

        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level ) const;
    private:
        InMatchExpression _in;
    };



}