summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/parsed_exclusion_projection.cpp
blob: 51809826dfee684fc82adc5fa61cd99188670088 (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
192
193
194
195
196
197
198
199
200
201
/**
 *    Copyright (C) 2016 MongoDB, 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/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/pipeline/parsed_exclusion_projection.h"

#include "mongo/db/pipeline/document.h"
#include "mongo/db/pipeline/field_path.h"
#include "mongo/db/pipeline/value.h"
#include "mongo/stdx/memory.h"

namespace mongo {

namespace parsed_aggregation_projection {

//
// ExclusionNode.
//

ExclusionNode::ExclusionNode(std::string pathToNode) : _pathToNode(std::move(pathToNode)) {}

Document ExclusionNode::serialize() const {
    MutableDocument output;
    for (auto&& excludedField : _excludedFields) {
        output.addField(excludedField, Value(false));
    }

    for (auto&& childPair : _children) {
        output.addField(childPair.first, Value(childPair.second->serialize()));
    }
    return output.freeze();
}

void ExclusionNode::excludePath(FieldPath path) {
    if (path.getPathLength() == 1) {
        _excludedFields.insert(path.fullPath());
        return;
    }
    addOrGetChild(path.getFieldName(0))->excludePath(path.tail());
}

Document ExclusionNode::applyProjection(Document input) const {
    MutableDocument output(input);
    for (auto&& field : _excludedFields) {
        output.remove(field);
    }
    for (auto&& childPair : _children) {
        output[childPair.first] = childPair.second->applyProjectionToValue(input[childPair.first]);
    }
    return output.freeze();
}

ExclusionNode* ExclusionNode::addOrGetChild(FieldPath fieldPath) {
    invariant(fieldPath.getPathLength() == 1);
    auto child = getChild(fieldPath.fullPath());
    return child ? child : addChild(fieldPath.fullPath());
}

ExclusionNode* ExclusionNode::getChild(std::string field) const {
    auto it = _children.find(field);
    return it == _children.end() ? nullptr : it->second.get();
}

ExclusionNode* ExclusionNode::addChild(std::string field) {
    auto pathToChild = _pathToNode.empty() ? field : _pathToNode + "." + field;

    auto emplacedPair = _children.emplace(
        std::make_pair(std::move(field), stdx::make_unique<ExclusionNode>(pathToChild)));

    // emplacedPair is a pair<iterator position, bool inserted>.
    invariant(emplacedPair.second);

    return emplacedPair.first->second.get();
}

Value ExclusionNode::applyProjectionToValue(Value val) const {
    switch (val.getType()) {
        case BSONType::Object:
            return Value(applyProjection(val.getDocument()));
        case BSONType::Array: {
            // Apply exclusion to each element of the array. Note that numeric paths aren't treated
            // specially, and we will always apply the projection to each element in the array.
            //
            // For example, applying the projection {"a.1": 0} to the document
            // {a: [{b: 0, "1": 0}, {b: 1, "1": 1}]} will not result in {a: [{b: 0, "1": 0}]}, but
            // instead will result in {a: [{b: 0}, {b: 1}]}.
            std::vector<Value> values = val.getArray();
            for (auto it = values.begin(); it != values.end(); it++) {
                *it = applyProjectionToValue(*it);
            }
            return Value(std::move(values));
        }
        default:
            return val;
    }
}

void ExclusionNode::addModifiedPaths(std::set<std::string>* modifiedPaths) const {
    for (auto&& excludedField : _excludedFields) {
        modifiedPaths->insert(FieldPath::getFullyQualifiedPath(_pathToNode, excludedField));
    }

    for (auto&& childPair : _children) {
        childPair.second->addModifiedPaths(modifiedPaths);
    }
}

//
// ParsedExclusionProjection.
//

Document ParsedExclusionProjection::serialize(
    boost::optional<ExplainOptions::Verbosity> explain) const {
    return _root->serialize();
}

Document ParsedExclusionProjection::applyProjection(Document inputDoc) const {
    return _root->applyProjection(inputDoc);
}

void ParsedExclusionProjection::parse(const BSONObj& spec, ExclusionNode* node, size_t depth) {
    for (auto elem : spec) {
        const auto fieldName = elem.fieldNameStringData().toString();

        // A $ should have been detected in ParsedAggregationProjection's parsing before we get
        // here.
        invariant(fieldName[0] != '$');

        switch (elem.type()) {
            case BSONType::Bool:
            case BSONType::NumberInt:
            case BSONType::NumberLong:
            case BSONType::NumberDouble:
            case BSONType::NumberDecimal: {
                // We have already verified this is an exclusion projection.
                invariant(!elem.trueValue());

                node->excludePath(FieldPath(fieldName));
                break;
            }
            case BSONType::Object: {
                // This object represents a nested projection specification, like the sub-object in
                // {a: {b: 0, c: 0}} or {"a.b": {c: 0}}.
                ExclusionNode* child;

                if (elem.fieldNameStringData().find('.') == std::string::npos) {
                    child = node->addOrGetChild(fieldName);
                } else {
                    // A dotted field is not allowed in a sub-object, and should have been detected
                    // in ParsedAggregationProjection's parsing before we get here.
                    invariant(depth == 0);

                    // We need to keep adding children to our tree until we create a child that
                    // represents this dotted path.
                    child = node;
                    auto fullPath = FieldPath(fieldName);
                    while (fullPath.getPathLength() > 1) {
                        child = child->addOrGetChild(fullPath.getFieldName(0));
                        fullPath = fullPath.tail();
                    }
                    // It is illegal to construct an empty FieldPath, so the above loop ends one
                    // iteration too soon. Add the last path here.
                    child = child->addOrGetChild(fullPath.fullPath());
                }

                parse(elem.Obj(), child, depth + 1);
                break;
            }
            default: { MONGO_UNREACHABLE; }
        }
    }
}

}  // namespace parsed_aggregation_projection
}  // namespace mongo