1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/platform/basic.h"
32
33 #include "mongo/db/update/update_internal_node.h"
34
35 namespace mongo {
36 namespace {
37
toArrayFilterIdentifier(const std::string & fieldName)38 std::string toArrayFilterIdentifier(const std::string& fieldName) {
39 return "$[" + fieldName + "]";
40 }
41 }
42
43 // static
createUpdateNodeMapByMerging(const std::map<std::string,clonable_ptr<UpdateNode>> & leftMap,const std::map<std::string,clonable_ptr<UpdateNode>> & rightMap,FieldRef * pathTaken,bool wrapFieldNameAsArrayFilterIdentifier)44 std::map<std::string, clonable_ptr<UpdateNode>> UpdateInternalNode::createUpdateNodeMapByMerging(
45 const std::map<std::string, clonable_ptr<UpdateNode>>& leftMap,
46 const std::map<std::string, clonable_ptr<UpdateNode>>& rightMap,
47 FieldRef* pathTaken,
48 bool wrapFieldNameAsArrayFilterIdentifier) {
49 std::map<std::string, clonable_ptr<UpdateNode>> mergedMap;
50
51 // Get the union of the field names we know about among the leftMap and rightMap.
52 stdx::unordered_set<std::string> allFields;
53 for (const auto& child : leftMap) {
54 allFields.insert(child.first);
55 }
56 for (const auto& child : rightMap) {
57 allFields.insert(child.first);
58 }
59
60 // Create an entry in mergedMap for all the fields we found.
61 for (const std::string& fieldName : allFields) {
62 auto leftChildIt = leftMap.find(fieldName);
63 auto rightChildIt = rightMap.find(fieldName);
64 UpdateNode* leftChildPtr =
65 (leftChildIt != leftMap.end()) ? leftChildIt->second.get() : nullptr;
66 UpdateNode* rightChildPtr =
67 (rightChildIt != rightMap.end()) ? rightChildIt->second.get() : nullptr;
68 invariant(leftChildPtr || rightChildPtr);
69 mergedMap.insert(
70 std::make_pair(fieldName,
71 copyOrMergeAsNecessary(leftChildPtr,
72 rightChildPtr,
73 pathTaken,
74 fieldName,
75 wrapFieldNameAsArrayFilterIdentifier)));
76 }
77
78 return mergedMap;
79 }
80
81 // static
copyOrMergeAsNecessary(UpdateNode * leftNode,UpdateNode * rightNode,FieldRef * pathTaken,const std::string & nextField,bool wrapFieldNameAsArrayFilterIdentifier)82 std::unique_ptr<UpdateNode> UpdateInternalNode::copyOrMergeAsNecessary(
83 UpdateNode* leftNode,
84 UpdateNode* rightNode,
85 FieldRef* pathTaken,
86 const std::string& nextField,
87 bool wrapFieldNameAsArrayFilterIdentifier) {
88 if (!leftNode && !rightNode) {
89 return nullptr;
90 } else if (!leftNode) {
91 return rightNode->clone();
92 } else if (!rightNode) {
93 return leftNode->clone();
94 } else {
95 FieldRefTempAppend tempAppend(
96 *pathTaken,
97 wrapFieldNameAsArrayFilterIdentifier ? toArrayFilterIdentifier(nextField) : nextField);
98 return UpdateNode::createUpdateNodeByMerging(*leftNode, *rightNode, pathTaken);
99 }
100 }
101 } // namespace mongo
102