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/db/field_ref.h" 38 #include "mongo/db/update/update_node.h" 39 40 namespace mongo { 41 42 /** 43 * An internal node in the prefix tree of update modifier expressions. See comment in class 44 * definition of UpdateNode for more details. 45 */ 46 class UpdateInternalNode : public UpdateNode { 47 public: 48 /** 49 * Helper class for appending to a FieldRef for the duration of the current scope and then 50 * restoring the FieldRef at the end of the scope. 51 */ 52 class FieldRefTempAppend { 53 public: FieldRefTempAppend(FieldRef & fieldRef,StringData part)54 FieldRefTempAppend(FieldRef& fieldRef, StringData part) : _fieldRef(fieldRef) { 55 _fieldRef.appendPart(part); 56 } 57 ~FieldRefTempAppend()58 ~FieldRefTempAppend() { 59 _fieldRef.removeLastPart(); 60 } 61 62 private: 63 FieldRef& _fieldRef; 64 }; 65 UpdateInternalNode(UpdateNode::Type type)66 UpdateInternalNode(UpdateNode::Type type) : UpdateNode(type) {} 67 68 /** 69 * Returns the child with field name 'field' or nullptr if there is no such child. 70 */ 71 virtual UpdateNode* getChild(const std::string& field) const = 0; 72 73 /** 74 * Adds a child with field name 'field'. The node must not already have a child with field 75 * name 'field'. 76 */ 77 virtual void setChild(std::string field, std::unique_ptr<UpdateNode> child) = 0; 78 79 protected: 80 /** 81 * Helper for subclass implementations of createUpdateNodeByMerging. Any UpdateNode value whose 82 * key is only in 'leftMap' or only in 'rightMap' is cloned and added to the output map. If the 83 * key is in both maps, the two UpdateNodes are merged and added to the output map. If 84 * wrapFieldNameAsArrayFilterIdentifier is true, field names are wrapped as $[<field name>] for 85 * error reporting. 86 */ 87 static std::map<std::string, clonable_ptr<UpdateNode>> createUpdateNodeMapByMerging( 88 const std::map<std::string, clonable_ptr<UpdateNode>>& leftMap, 89 const std::map<std::string, clonable_ptr<UpdateNode>>& rightMap, 90 FieldRef* pathTaken, 91 bool wrapFieldNameAsArrayFilterIdentifier = false); 92 93 /** 94 * Helper for subclass implementations of createUpdateNodeByMerging. If one of 'leftNode' or 95 * 'rightNode' is non-null, we clone it. If 'leftNode' and 'rightNode' are both non-null, we 96 * merge them recursively. If 'leftNode' and 'rightNode' are both null, we return nullptr. If 97 * wrapFieldNameAsArrayFilterIdentifier is true, field names are wrapped as $[<field name>] for 98 * error reporting. 99 */ 100 static std::unique_ptr<UpdateNode> copyOrMergeAsNecessary( 101 UpdateNode* leftNode, 102 UpdateNode* rightNode, 103 FieldRef* pathTaken, 104 const std::string& nextField, 105 bool wrapFieldNameAsArrayFilterIdentifier = false); 106 }; 107 108 } // namespace mongo 109