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 <boost/optional.hpp>
34 #include <limits>
35 #include <vector>
36 
37 #include "mongo/db/update/modifier_node.h"
38 #include "mongo/db/update/push_sorter.h"
39 #include "mongo/stdx/memory.h"
40 
41 namespace mongo {
42 
43 class PushNode final : public ModifierNode {
44 public:
PushNode()45     PushNode()
46         : _slice(std::numeric_limits<long long>::max()),
47           _position(std::numeric_limits<long long>::max()) {}
48     Status init(BSONElement modExpr, const boost::intrusive_ptr<ExpressionContext>& expCtx) final;
49 
clone()50     std::unique_ptr<UpdateNode> clone() const final {
51         return stdx::make_unique<PushNode>(*this);
52     }
53 
setCollator(const CollatorInterface * collator)54     void setCollator(const CollatorInterface* collator) final {
55         if (_sort) {
56             invariant(!_sort->collator);
57             _sort->collator = collator;
58         }
59     }
60 
61 protected:
62     ModifyResult updateExistingElement(mutablebson::Element* element,
63                                        std::shared_ptr<FieldRef> elementPath) const final;
64     void setValueForNewElement(mutablebson::Element* element) const final;
65     void logUpdate(LogBuilder* logBuilder,
66                    StringData pathTaken,
67                    mutablebson::Element element,
68                    ModifyResult modifyResult) const final;
69 
allowCreation()70     bool allowCreation() const final {
71         return true;
72     }
73 
74 
75 private:
76     // A helper for performPush().
77     static ModifyResult insertElementsWithPosition(mutablebson::Element* array,
78                                                    long long position,
79                                                    const std::vector<BSONElement>& valuesToPush);
80 
81     /**
82      * Inserts the elements from '_valuesToPush' in the 'element' array using '_position' to
83      * determine where to insert. This function also applies any '_slice' and or '_sort' that is
84      * specified. The return value of this function will indicate to logUpdate() what kind of oplog
85      * entries should be generated.
86      *
87      * Returns:
88      *   - ModifyResult::kNoOp if '_valuesToPush' is empty and no slice or sort gets performed;
89      *   - ModifyResult::kArrayAppendUpdate if the 'elements' array is initially non-empty, all
90      *     inserted values are appended to the end, and no slice or sort gets performed; or
91      *   - ModifyResult::kNormalUpdate if 'elements' is initially an empty array, values get
92      *     inserted at the beginning or in the middle of the array, or a slice or sort gets
93      *     performed.
94      */
95     ModifyResult performPush(mutablebson::Element* element, FieldRef* elementPath) const;
96 
97     static const StringData kEachClauseName;
98     static const StringData kSliceClauseName;
99     static const StringData kSortClauseName;
100     static const StringData kPositionClauseName;
101 
102     std::vector<BSONElement> _valuesToPush;
103     long long _slice;
104     long long _position;
105     boost::optional<PatternElementCmp> _sort;
106 };
107 
108 }  // namespace mongo
109