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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/update/modifier_node.h"
#include "mongo/db/bson/dotted_path_support.h"
#include "mongo/db/update/path_support.h"
#include "mongo/db/update/storage_validation.h"
namespace mongo {
namespace {
/**
* Checks that no immutable paths were modified in the case where we are modifying an existing path
* in the document.
*
* This check does not make assumptions about how 'element' was modified; it explicitly checks
* immutable fields in 'element' to see if they differ from the 'original' value. Consider an
* updating the document {a: {b: 1, c: 1}} with {$set: {a: {b: 1, d: 1}}} where 'a.b' is an
* immutable path. Even though we've overwritten the immutable field, it has the same value, and the
* update is allowed.
*
* 'element' should be the modified element. 'pathTaken' is the path to the modified element.
* 'original' should be provided as the preimage of the whole document. We _do_ assume that we have
* already checked the update is not a noop.
*/
void checkImmutablePathsNotModifiedFromOriginal(mutablebson::Element element,
const FieldRef& pathTaken,
const FieldRefSet& immutablePaths,
BSONObj original) {
for (auto immutablePath = immutablePaths.begin(); immutablePath != immutablePaths.end();
++immutablePath) {
auto prefixSize = pathTaken.commonPrefixSize(**immutablePath);
// If 'immutablePath' is a (strict or non-strict) prefix of 'pathTaken', and the update is
// not a noop, then we have modified 'immutablePath', which is immutable.
if (prefixSize == (*immutablePath)->numParts()) {
uasserted(ErrorCodes::ImmutableField,
str::stream() << "Updating the path '" << pathTaken.dottedField() << "' to "
<< element.toString() << " would modify the immutable field '"
<< (*immutablePath)->dottedField() << "'");
}
// If 'pathTaken' is a strict prefix of 'immutablePath', then we may have modified
// 'immutablePath'. We already know that 'pathTaken' is not equal to 'immutablePath', or we
// would have uasserted.
if (prefixSize == pathTaken.numParts()) {
auto oldElem = dotted_path_support::extractElementAtPath(
original, (*immutablePath)->dottedField());
// We are allowed to modify immutable paths that do not yet exist.
if (!oldElem.ok()) {
continue;
}
auto newElem = element;
for (size_t i = pathTaken.numParts(); i < (*immutablePath)->numParts(); ++i) {
uassert(ErrorCodes::NotSingleValueField,
str::stream()
<< "After applying the update to the document, the immutable field '"
<< (*immutablePath)->dottedField()
<< "' was found to be an array or array descendant.",
newElem.getType() != BSONType::Array);
newElem = newElem[(*immutablePath)->getPart(i)];
if (!newElem.ok()) {
break;
}
}
uassert(ErrorCodes::ImmutableField,
str::stream() << "After applying the update, the immutable field '"
<< (*immutablePath)->dottedField()
<< "' was found to have been removed.",
newElem.ok());
uassert(ErrorCodes::ImmutableField,
str::stream() << "After applying the update, the immutable field '"
<< (*immutablePath)->dottedField()
<< "' was found to have been altered to " << newElem.toString(),
newElem.compareWithBSONElement(oldElem, nullptr, false) == 0);
}
}
}
/**
* Checks that no immutable paths were modified in the case where we are modifying an existing path
* in the document.
*
* Unlike checkImmutablePathsNotModifiedFromOriginal(), this function does not check the original
* document. It always assumes that an update where 'pathTaken' is a prefix of any immutable path or
* vice versa is modifying an immutable path. This assumption is valid when we already know that
* 'pathTaken' is not a prefix of any immutable path or when the update is to a primitive value or
* array. (Immutable paths cannot include array elements.)
*
* See the comment above checkImmutablePathNotModifiedFromOriginal() for an example where that
* assumption does not apply.
*
* 'element' should be the modified element. 'pathTaken' is the path to the modified element. We
* assume that we have already checked the update is not a noop.
*/
void checkImmutablePathsNotModified(mutablebson::Element element,
const FieldRef& pathTaken,
const FieldRefSet& immutablePaths) {
for (auto immutablePath = immutablePaths.begin(); immutablePath != immutablePaths.end();
++immutablePath) {
uassert(ErrorCodes::ImmutableField,
str::stream() << "Performing an update on the path '" << pathTaken.dottedField()
<< "' would modify the immutable field '"
<< (*immutablePath)->dottedField() << "'",
pathTaken.commonPrefixSize(**immutablePath) <
std::min(pathTaken.numParts(), (*immutablePath)->numParts()));
}
}
} // namespace
UpdateExecutor::ApplyResult ModifierNode::applyToExistingElement(
ApplyParams applyParams, UpdateNodeApplyParams updateNodeApplyParams) const {
invariant(!updateNodeApplyParams.pathTaken->empty());
invariant(updateNodeApplyParams.pathToCreate->empty());
invariant(applyParams.element.ok());
mutablebson::ConstElement leftSibling = applyParams.element.leftSibling();
mutablebson::ConstElement rightSibling = applyParams.element.rightSibling();
bool compareWithOriginal = false;
if (canSetObjectValue()) {
for (auto immutablePath = applyParams.immutablePaths.begin();
immutablePath != applyParams.immutablePaths.end();
++immutablePath) {
if (updateNodeApplyParams.pathTaken->fieldRef().isPrefixOf(**immutablePath)) {
compareWithOriginal = true;
break;
}
}
}
// We have two different ways of checking for changes to immutable paths, depending on the style
// of update. See the comments above checkImmutablePathsNotModifiedFromOriginal() and
// checkImmutablePathsNotModified().
ModifyResult updateResult;
if (compareWithOriginal) {
BSONObj original = applyParams.element.getDocument().getObject();
updateResult = updateExistingElement(&applyParams.element,
updateNodeApplyParams.pathTaken->fieldRef());
if (updateResult == ModifyResult::kNoOp) {
return ApplyResult::noopResult();
}
checkImmutablePathsNotModifiedFromOriginal(applyParams.element,
updateNodeApplyParams.pathTaken->fieldRef(),
applyParams.immutablePaths,
original);
} else {
updateResult = updateExistingElement(&applyParams.element,
updateNodeApplyParams.pathTaken->fieldRef());
if (updateResult == ModifyResult::kNoOp) {
return ApplyResult::noopResult();
}
checkImmutablePathsNotModified(applyParams.element,
updateNodeApplyParams.pathTaken->fieldRef(),
applyParams.immutablePaths);
}
invariant(updateResult != ModifyResult::kCreated);
ApplyResult applyResult;
if (!applyParams.indexData ||
!applyParams.indexData->mightBeIndexed(updateNodeApplyParams.pathTaken->fieldRef())) {
applyResult.indexesAffected = false;
}
const uint32_t recursionLevel = updateNodeApplyParams.pathTaken->size();
validateUpdate(applyParams.element,
leftSibling,
rightSibling,
recursionLevel,
updateResult,
applyParams.validateForStorage,
&applyResult.containsDotsAndDollarsField);
if (auto logBuilder = updateNodeApplyParams.logBuilder) {
logUpdate(logBuilder,
*updateNodeApplyParams.pathTaken,
applyParams.element,
updateResult,
boost::none // No path was created.
);
}
return applyResult;
}
UpdateExecutor::ApplyResult ModifierNode::applyToNonexistentElement(
ApplyParams applyParams, UpdateNodeApplyParams updateNodeApplyParams) const {
invariant(!updateNodeApplyParams.pathToCreate->empty());
if (allowCreation()) {
auto newElementFieldName = updateNodeApplyParams.pathToCreate->getPart(
updateNodeApplyParams.pathToCreate->numParts() - 1);
auto newElement = applyParams.element.getDocument().makeElementNull(newElementFieldName);
setValueForNewElement(&newElement);
invariant(newElement.ok());
auto statusWithFirstCreatedElem = pathsupport::createPathAt(
*(updateNodeApplyParams.pathToCreate), 0, applyParams.element, newElement);
if (!statusWithFirstCreatedElem.isOK()) {
// $set operations on non-viable paths are ignored when the update came from
// replication. We do not error because idempotency requires that any other update
// modifiers must still be applied. For example, consider applying the following updates
// twice to an initially empty document:
// {$set: {c: 0}}
// {$set: {'a.b': 0, c: 1}}
// {$set: {a: 0}}
// Setting 'a.b' will fail the second time, but we must still set 'c'.
// (There are modifiers besides $set that use this code path, but they are not used for
// replication, so we are not concerned with their behavior when "fromOplogApplication"
// is true.)
if (statusWithFirstCreatedElem.getStatus().code() == ErrorCodes::PathNotViable &&
applyParams.fromOplogApplication) {
return ApplyResult::noopResult();
}
uassertStatusOK(statusWithFirstCreatedElem);
MONGO_UNREACHABLE; // The previous uassertStatusOK should always throw.
}
ApplyResult applyResult;
const uint32_t recursionLevel = updateNodeApplyParams.pathTaken->size() + 1;
mutablebson::ConstElement elementForValidation = statusWithFirstCreatedElem.getValue();
validateUpdate(elementForValidation,
elementForValidation.leftSibling(),
elementForValidation.rightSibling(),
recursionLevel,
ModifyResult::kCreated,
applyParams.validateForStorage,
&applyResult.containsDotsAndDollarsField);
for (auto immutablePath = applyParams.immutablePaths.begin();
immutablePath != applyParams.immutablePaths.end();
++immutablePath) {
// If 'immutablePath' is a (strict or non-strict) prefix of 'pathTaken', then we are
// modifying 'immutablePath'. For example, adding '_id.x' will illegally modify '_id'.
// (Note that this behavior is subtly different from checkImmutablePathsNotModified(),
// because we just created this element.)
uassert(ErrorCodes::ImmutableField,
str::stream() << "Updating the path '"
<< updateNodeApplyParams.pathTaken->fieldRef().dottedField()
<< "' to " << applyParams.element.toString()
<< " would modify the immutable field '"
<< (*immutablePath)->dottedField() << "'",
updateNodeApplyParams.pathTaken->fieldRef().commonPrefixSize(**immutablePath) !=
(*immutablePath)->numParts());
}
FieldRef fullPathFr;
std::vector<RuntimeUpdatePath::ComponentType> fullPathTypes;
if (updateNodeApplyParams.pathTaken->empty()) {
fullPathFr = *updateNodeApplyParams.pathToCreate;
// Newly created paths consist only of objects.
fullPathTypes.assign(fullPathFr.numParts(),
RuntimeUpdatePath::ComponentType::kFieldName);
} else {
fullPathFr =
updateNodeApplyParams.pathTaken->fieldRef() + *updateNodeApplyParams.pathToCreate;
fullPathTypes = updateNodeApplyParams.pathTaken->types();
const bool isCreatingArrayElem = applyParams.element.getType() == BSONType::Array;
fullPathTypes.push_back(isCreatingArrayElem
? RuntimeUpdatePath::ComponentType::kArrayIndex
: RuntimeUpdatePath::ComponentType::kFieldName);
for (auto i = 0; i < updateNodeApplyParams.pathToCreate->numParts() - 1; ++i) {
fullPathTypes.push_back(RuntimeUpdatePath::ComponentType::kFieldName);
}
// If adding an element to an array, only mark the path to the array itself as modified.
if (applyParams.modifiedPaths && isCreatingArrayElem) {
applyParams.modifiedPaths->keepShortest(
updateNodeApplyParams.pathTaken->fieldRef());
}
}
invariant(fullPathTypes.size() == fullPathFr.numParts());
// Determine if indexes are affected. If we did not create a new element in an array, check
// whether the full path affects indexes. If we did create a new element in an array, check
// whether the array itself might affect any indexes. This is necessary because if there is
// an index {"a.b": 1}, and we set "a.1.c" and implicitly create an array element in "a",
// then we may need to add a null key to the index, even though "a.1.c" does not appear to
// affect the index.
if (!applyParams.indexData ||
!applyParams.indexData->mightBeIndexed(
applyParams.element.getType() != BSONType::Array
? fullPathFr
: updateNodeApplyParams.pathTaken->fieldRef())) {
applyResult.indexesAffected = false;
}
if (auto logBuilder = updateNodeApplyParams.logBuilder) {
logUpdate(logBuilder,
RuntimeUpdatePath(std::move(fullPathFr), std::move(fullPathTypes)),
newElement,
ModifyResult::kCreated,
updateNodeApplyParams.pathTaken->size());
}
return applyResult;
} else {
// This path is for modifiers like $pop or $pull that generally have no effect when applied
// to a path that does not exist.
if (!allowNonViablePath()) {
// One exception: some of these modifiers still fail when the nonexistent path is
// "non-viable," meaning it couldn't be created even if we intended to.
UpdateLeafNode::checkViability(applyParams.element,
*(updateNodeApplyParams.pathToCreate),
(updateNodeApplyParams.pathTaken->fieldRef()));
}
return ApplyResult::noopResult();
}
}
UpdateExecutor::ApplyResult ModifierNode::apply(ApplyParams applyParams,
UpdateNodeApplyParams updateNodeApplyParams) const {
ApplyResult result;
if (context == Context::kInsertOnly && !applyParams.insert) {
result = ApplyResult::noopResult();
} else if (!updateNodeApplyParams.pathToCreate->empty()) {
result = applyToNonexistentElement(applyParams, updateNodeApplyParams);
} else {
result = applyToExistingElement(applyParams, updateNodeApplyParams);
}
if (applyParams.modifiedPaths) {
applyParams.modifiedPaths->keepShortest(updateNodeApplyParams.pathTaken->fieldRef() +
*updateNodeApplyParams.pathToCreate);
}
return result;
}
void ModifierNode::validateUpdate(mutablebson::ConstElement updatedElement,
mutablebson::ConstElement leftSibling,
mutablebson::ConstElement rightSibling,
std::uint32_t recursionLevel,
ModifyResult modifyResult,
const bool validateForStorage,
bool* containsDotsAndDollarsField) const {
const bool doRecursiveCheck = true;
storage_validation::scanDocument(updatedElement,
doRecursiveCheck,
recursionLevel,
false, /* allowTopLevelDollarPrefixedFields */
validateForStorage,
containsDotsAndDollarsField);
}
void ModifierNode::logUpdate(LogBuilderInterface* logBuilder,
const RuntimeUpdatePath& pathTaken,
mutablebson::Element element,
ModifyResult modifyResult,
boost::optional<int> createdFieldIdx) const {
invariant(logBuilder);
invariant(modifyResult == ModifyResult::kNormalUpdate ||
modifyResult == ModifyResult::kCreated);
if (modifyResult == ModifyResult::kCreated) {
invariant(createdFieldIdx);
uassertStatusOK(logBuilder->logCreatedField(pathTaken, *createdFieldIdx, element));
} else {
uassertStatusOK(logBuilder->logUpdatedField(pathTaken, element));
}
}
} // namespace mongo
|