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 #pragma once 32 33 #include <map> 34 #include <string> 35 36 #include "mongo/base/clonable_ptr.h" 37 #include "mongo/bson/bsonelement.h" 38 #include "mongo/db/matcher/expression_with_placeholder.h" 39 #include "mongo/db/update/modifier_table.h" 40 #include "mongo/db/update/update_internal_node.h" 41 #include "mongo/stdx/memory.h" 42 #include "mongo/stdx/unordered_map.h" 43 44 namespace mongo { 45 46 /** 47 * An internal node in the prefix tree of update modifier expressions, representing updates to an 48 * object. See comment in class definition of UpdateNode for more details. 49 */ 50 class UpdateObjectNode : public UpdateInternalNode { 51 52 public: 53 /** 54 * Parses 'modExpr' as an update modifier expression and merges with it with 'root'. Returns a 55 * non-OK status if 'modExpr' is not a valid update modifier expression, if merging would 56 * cause a conflict, or if there is an array filter identifier in 'modExpr' without a 57 * corresponding filter in 'arrayFilters'. Returns true if the path of 'modExpr' contains a 58 * positional $ element, e.g. 'a.$.b'. Any array filter identifiers are added to 59 * 'foundIdentifiers'. 60 */ 61 static StatusWith<bool> parseAndMerge( 62 UpdateObjectNode* root, 63 modifiertable::ModifierType type, 64 BSONElement modExpr, 65 const boost::intrusive_ptr<ExpressionContext>& expCtx, 66 const std::map<StringData, std::unique_ptr<ExpressionWithPlaceholder>>& arrayFilters, 67 std::set<std::string>& foundIdentifiers); 68 69 /** 70 * Creates a new UpdateObjectNode by merging two input UpdateObjectNode objects and their 71 * children. Each field that lives on one side of the merge but not the other (according to 72 * field name) is cloned to the newly created UpdateObjectNode. Fields that exist on both sides 73 * of the merge get merged recursively before being added to the resulting UpdateObjectNode. 74 * This merge operation is a deep copy: the new UpdateObjectNode is a brand new tree that does 75 * not contain any references to the objects in the original input trees. 76 */ 77 static std::unique_ptr<UpdateNode> createUpdateNodeByMerging(const UpdateObjectNode& leftNode, 78 const UpdateObjectNode& rightNode, 79 FieldRef* pathTaken); 80 UpdateObjectNode()81 UpdateObjectNode() : UpdateInternalNode(Type::Object) {} 82 clone()83 std::unique_ptr<UpdateNode> clone() const final { 84 return stdx::make_unique<UpdateObjectNode>(*this); 85 } 86 setCollator(const CollatorInterface * collator)87 void setCollator(const CollatorInterface* collator) final { 88 for (auto&& child : _children) { 89 child.second->setCollator(collator); 90 } 91 if (_positionalChild) { 92 _positionalChild->setCollator(collator); 93 } 94 } 95 96 ApplyResult apply(ApplyParams applyParams) const final; 97 98 UpdateNode* getChild(const std::string& field) const final; 99 100 void setChild(std::string field, std::unique_ptr<UpdateNode> child) final; 101 102 private: 103 std::map<std::string, clonable_ptr<UpdateNode>> _children; 104 clonable_ptr<UpdateNode> _positionalChild; 105 106 // When calling apply() causes us to merge an element of '_children' with '_positionalChild', we 107 // store the result of the merge in case we need it in a future call to apply(). 108 mutable stdx::unordered_map<std::string, clonable_ptr<UpdateNode>> _mergedChildrenCache; 109 }; 110 111 } // namespace mongo 112