1 // expression.cpp
2 
3 
4 /**
5  *    Copyright (C) 2018-present MongoDB, Inc.
6  *
7  *    This program is free software: you can redistribute it and/or modify
8  *    it under the terms of the Server Side Public License, version 1,
9  *    as published by MongoDB, Inc.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    Server Side Public License for more details.
15  *
16  *    You should have received a copy of the Server Side Public License
17  *    along with this program. If not, see
18  *    <http://www.mongodb.com/licensing/server-side-public-license>.
19  *
20  *    As a special exception, the copyright holders give permission to link the
21  *    code of portions of this program with the OpenSSL library under certain
22  *    conditions as described in each individual source file and distribute
23  *    linked combinations including the program with the OpenSSL library. You
24  *    must comply with the Server Side Public License in all respects for
25  *    all of the code used other than as permitted herein. If you modify file(s)
26  *    with this exception, you may extend this exception to your version of the
27  *    file(s), but you are not obligated to do so. If you do not wish to do so,
28  *    delete this exception statement from your version. If you delete this
29  *    exception statement from all source files in the program, then also delete
30  *    it in the license file.
31  */
32 
33 #include "mongo/db/matcher/expression.h"
34 
35 #include "mongo/bson/bsonmisc.h"
36 #include "mongo/bson/bsonobj.h"
37 
38 namespace mongo {
39 
40 using std::string;
41 
MatchExpression(MatchType type)42 MatchExpression::MatchExpression(MatchType type) : _matchType(type) {}
43 
toString() const44 string MatchExpression::toString() const {
45     StringBuilder buf;
46     debugString(buf, 0);
47     return buf.str();
48 }
49 
_debugAddSpace(StringBuilder & debug,int level) const50 void MatchExpression::_debugAddSpace(StringBuilder& debug, int level) const {
51     for (int i = 0; i < level; i++)
52         debug << "    ";
53 }
54 
matchesBSON(const BSONObj & doc,MatchDetails * details) const55 bool MatchExpression::matchesBSON(const BSONObj& doc, MatchDetails* details) const {
56     BSONMatchableDocument mydoc(doc);
57     return matches(&mydoc, details);
58 }
59 
matchesBSONElement(BSONElement elem,MatchDetails * details) const60 bool MatchExpression::matchesBSONElement(BSONElement elem, MatchDetails* details) const {
61     BSONElementViewMatchableDocument matchableDoc(elem);
62     return matches(&matchableDoc, details);
63 }
64 
setCollator(const CollatorInterface * collator)65 void MatchExpression::setCollator(const CollatorInterface* collator) {
66     for (size_t i = 0; i < numChildren(); ++i) {
67         getChild(i)->setCollator(collator);
68     }
69 
70     _doSetCollator(collator);
71 }
72 
addDependencies(DepsTracker * deps) const73 void MatchExpression::addDependencies(DepsTracker* deps) const {
74     for (size_t i = 0; i < numChildren(); ++i) {
75 
76         // Don't recurse through MatchExpression nodes which require an entire array or entire
77         // subobject for matching.
78         const auto type = matchType();
79         switch (type) {
80             case MatchExpression::ELEM_MATCH_VALUE:
81             case MatchExpression::ELEM_MATCH_OBJECT:
82             case MatchExpression::INTERNAL_SCHEMA_OBJECT_MATCH:
83                 continue;
84             default:
85                 getChild(i)->addDependencies(deps);
86         }
87     }
88 
89     _doAddDependencies(deps);
90 }
91 }
92