summaryrefslogtreecommitdiff
path: root/src/mongo/db/ops/modifier_pull_all.cpp
blob: 362a352dc1c8ee44d93ee75ed137a5740b2b45a6 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 *    Copyright (C) 2013 10gen 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/>.
 */

#include "mongo/db/ops/modifier_pull_all.h"

#include "mongo/base/error_codes.h"
#include "mongo/bson/mutable/algorithm.h"
#include "mongo/bson/mutable/document.h"
#include "mongo/db/ops/field_checker.h"
#include "mongo/db/ops/log_builder.h"
#include "mongo/db/ops/path_support.h"

namespace mongo {

    struct ModifierPullAll::PreparedState {

        PreparedState(mutablebson::Document* targetDoc)
            : doc(*targetDoc)
            , pathFoundIndex(0)
            , pathFoundElement(doc.end())
            , pathPositionalPart(std::string())
            , applyCalled(false)
            , elementsToRemove() {
        }

        // Document that is going to be changed.
        mutablebson::Document& doc;

        // Index in _fieldRef for which an Element exist in the document.
        size_t pathFoundIndex;

        // Element corresponding to _fieldRef[0.._idxFound].
        mutablebson::Element pathFoundElement;

        // Value to bind to a $-positional field, if one is provided.
        std::string pathPositionalPart;

        bool applyCalled;

        // Elements to be removed
       vector<mutablebson::Element> elementsToRemove;
    };

    namespace {

        struct mutableElementEqualsBSONElement : std::unary_function<BSONElement, bool>
        {
            mutableElementEqualsBSONElement(const mutablebson::Element& elem) : _what(elem) {}
            bool operator()(const BSONElement& elem) const {
                return _what.compareWithBSONElement(elem, false) == 0;
            }
            const mutablebson::Element& _what;
        };
    } // namespace

    ModifierPullAll::ModifierPullAll()
        : _fieldRef()
        , _positionalPathIndex(0)
        , _elementsToFind() {
    }

    ModifierPullAll::~ModifierPullAll() {
    }

    Status ModifierPullAll::init(const BSONElement& modExpr, const Options& opts) {

        //
        // field name analysis
        //

        // Break down the field name into its 'dotted' components (aka parts) and check that
        // there are no empty parts.
        _fieldRef.parse(modExpr.fieldName());
        Status status = fieldchecker::isUpdatable(_fieldRef);
        if (! status.isOK()) {
            return status;
        }

        // If a $-positional operator was used, get the index in which it occurred
        // and ensure only one occurrence.
        size_t foundCount;
        bool foundDollar = fieldchecker::isPositional(_fieldRef,
                                                      &_positionalPathIndex,
                                                      &foundCount);
        if (foundDollar && foundCount > 1) {
            return Status(ErrorCodes::BadValue, "too many positional($) elements found.");
        }

        //
        // value analysis
        //

        if (modExpr.type() != Array) {
            return Status(ErrorCodes::BadValue, "$pullAll requires an array argument");
        }

        // store the stuff to remove later
        _elementsToFind = modExpr.Array();

        return Status::OK();
    }

    Status ModifierPullAll::prepare(mutablebson::Element root,
                                    const StringData& matchedField,
                                    ExecInfo* execInfo) {

        _preparedState.reset(new PreparedState(&root.getDocument()));

        // If we have a $-positional field, it is time to bind it to an actual field part.
        if (_positionalPathIndex) {
            if (matchedField.empty()) {
                return Status(ErrorCodes::BadValue, "matched field not provided");
            }
            _preparedState->pathPositionalPart = matchedField.toString();
            _fieldRef.setPart(_positionalPathIndex, _preparedState->pathPositionalPart);
        }

        // Locate the field name in 'root'. Note that if we don't have the full path in the
        // doc, there isn't anything to unset, really.
        Status status = pathsupport::findLongestPrefix(_fieldRef,
                                                       root,
                                                       &_preparedState->pathFoundIndex,
                                                       &_preparedState->pathFoundElement);
        // Check if we didn't find the full path
        if (status.isOK()) {
            // If the path exists, we require the target field to be already an
            // array.
            if (_preparedState->pathFoundElement.getType() != Array) {
                return Status(ErrorCodes::BadValue, "can only $pull* from arrays");
            }

            // No children, nothing to do -- not an error state
            if (!_preparedState->pathFoundElement.hasChildren()) {
                execInfo->noOp = true;
            } else {
                mutablebson::Element elem = _preparedState->pathFoundElement.leftChild();
                while (elem.ok()) {
                    if (std::find_if(_elementsToFind.begin(),
                                     _elementsToFind.end(),
                                     mutableElementEqualsBSONElement(elem))
                                        != _elementsToFind.end()) {
                        _preparedState->elementsToRemove.push_back(elem);
                    }
                    elem = elem.rightSibling();
                }

                // Nothing to remove so it is a noOp.
                if (_preparedState->elementsToRemove.empty())
                    execInfo->noOp = true;
            }

        } else {
            // Let the caller know we can't do anything given the mod, _fieldRef, and doc.
            execInfo->noOp = true;


            //okay if path not found
            if (status.code() == ErrorCodes::NonExistentPath)
                status = Status::OK();
        }

        // Let the caller know what field we care about
        execInfo->fieldRef[0] = &_fieldRef;

        return status;
    }

    Status ModifierPullAll::apply() const {
        _preparedState->applyCalled = true;

        vector<mutablebson::Element>::const_iterator curr =
                                            _preparedState->elementsToRemove.begin();
        const vector<mutablebson::Element>::const_iterator end =
                                            _preparedState->elementsToRemove.end();
        for ( ; curr != end; ++curr) {
            const_cast<mutablebson::Element&>(*curr).remove();
        }
        return Status::OK();
    }

    Status ModifierPullAll::log(LogBuilder* logBuilder) const {
        // log document
        mutablebson::Document& doc = logBuilder->getDocument();

        const bool pathExists = _preparedState->pathFoundElement.ok() &&
            (_preparedState->pathFoundIndex == (_fieldRef.numParts() - 1));

        // value for the logElement ("field.path.name": <value>)
        mutablebson::Element logElement = pathExists ?
            doc.makeElementWithNewFieldName(
                _fieldRef.dottedField(),
                _preparedState->pathFoundElement):
            doc.makeElementBool(_fieldRef.dottedField(), true);

        if (!logElement.ok()) {
            return Status(ErrorCodes::InternalError, "cannot create details");
        }

        // Now, we attach the {<fieldname>: <value>} Element under the {$op: ...} one.
        return pathExists ?
            logBuilder->addToSets(logElement) :
            logBuilder->addToUnsets(logElement);
    }

} // namespace mongo