summaryrefslogtreecommitdiff
path: root/src/mongo/db/matcher/expression_tree.h
blob: b2c9f2c252fc234bf618c03d5b8977ed30080fa3 (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
// expression_tree.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 "mongo/db/matcher/expression.h"

#include <boost/scoped_ptr.hpp>

/**
 * this contains all Expessions that define the structure of the tree
 * they do not look at the structure of the documents themselves, just combine other things
 */
namespace mongo {

    class ListOfMatchExpression : public MatchExpression {
    public:
        ListOfMatchExpression( MatchType type ) : MatchExpression( TREE, type ){}
        virtual ~ListOfMatchExpression();

        /**
         * @param e - I take ownership
         */
        void add( MatchExpression* e );

        /**
         * clears all the thingsd we own, and does NOT delete
         * someone else has taken ownership
         */
        void clearAndRelease() { _expressions.clear(); }

        virtual size_t numChildren() const { return _expressions.size(); }
        virtual const MatchExpression* getChild( size_t i ) const { return _expressions[i]; }

        bool equivalent( const MatchExpression* other ) const;

    protected:
        void _debugList( StringBuilder& debug, int level ) const;

    private:
        std::vector< MatchExpression* > _expressions;
    };

    class AndMatchExpression : public ListOfMatchExpression {
    public:
        AndMatchExpression() : ListOfMatchExpression( AND ){}
        virtual ~AndMatchExpression(){}

        virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const;
        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level = 0 ) const;
    };

    class OrMatchExpression : public ListOfMatchExpression {
    public:
        OrMatchExpression() : ListOfMatchExpression( OR ){}
        virtual ~OrMatchExpression(){}

        virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const;
        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level = 0 ) const;
    };

    class NorMatchExpression : public ListOfMatchExpression {
    public:
        NorMatchExpression() : ListOfMatchExpression( NOR ){}
        virtual ~NorMatchExpression(){}

        virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const;
        virtual bool matchesSingleElement( const BSONElement& e ) const;

        virtual void debugString( StringBuilder& debug, int level = 0 ) const;
    };

    class NotMatchExpression : public MatchExpression {
    public:
        NotMatchExpression() : MatchExpression( TREE, NOT ){}
        /**
         * @param exp - I own it, and will delete
         */
        virtual Status init( MatchExpression* exp ) {
            _exp.reset( exp );
            return Status::OK();
        }

        virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const {
            return !_exp->matches( doc, NULL );
        }

        virtual bool matchesSingleElement( const BSONElement& e ) const {
            return !_exp->matchesSingleElement( e );
        }

        virtual void debugString( StringBuilder& debug, int level = 0 ) const;

        bool equivalent( const MatchExpression* other ) const;

        virtual size_t numChildren() const { return 1; }
        virtual MatchExpression* getChild( size_t i ) const { return _exp.get(); }


    private:
        boost::scoped_ptr<MatchExpression> _exp;
    };

}