diff options
| author | Justin Seyster <justin.seyster@mongodb.com> | 2017-06-02 11:30:52 -0400 |
|---|---|---|
| committer | Justin Seyster <justin.seyster@mongodb.com> | 2017-06-02 11:30:52 -0400 |
| commit | 1036e4ad6cf2465612faaa7cc57693f2c80e41af (patch) | |
| tree | 78a75f11832990f2e81baf75579a55a67e8db53b /src | |
| parent | 19a48549ee1eba1a00fd3477839859fbc0f27e22 (diff) | |
| download | mongo-1036e4ad6cf2465612faaa7cc57693f2c80e41af.tar.gz | |
SERVER-28767 Create IncNode/MulNode
A lot of the update modifiers have behavior that is very similar to
$set in how paths that don't yet exist get created (including $inc and
$mul, which are added in this commit). The new PathCreatingNode will
allow us to use the same path creation code path in all those
modifiers.
Diffstat (limited to 'src')
| -rw-r--r-- | src/mongo/db/update/SConscript | 10 | ||||
| -rw-r--r-- | src/mongo/db/update/arithmetic_node.cpp | 127 | ||||
| -rw-r--r-- | src/mongo/db/update/arithmetic_node.h | 62 | ||||
| -rw-r--r-- | src/mongo/db/update/arithmetic_node_test.cpp | 1741 | ||||
| -rw-r--r-- | src/mongo/db/update/modifier_table.cpp | 5 | ||||
| -rw-r--r-- | src/mongo/db/update/path_creating_node.cpp | 120 | ||||
| -rw-r--r-- | src/mongo/db/update/path_creating_node.h | 70 | ||||
| -rw-r--r-- | src/mongo/db/update/set_node.cpp | 71 | ||||
| -rw-r--r-- | src/mongo/db/update/set_node.h | 16 | ||||
| -rw-r--r-- | src/mongo/db/update/set_node_test.cpp | 6 | ||||
| -rw-r--r-- | src/mongo/db/update/update_object_node_test.cpp | 24 |
11 files changed, 2173 insertions, 79 deletions
diff --git a/src/mongo/db/update/SConscript b/src/mongo/db/update/SConscript index e985acbbca1..0f451aa8555 100644 --- a/src/mongo/db/update/SConscript +++ b/src/mongo/db/update/SConscript @@ -55,7 +55,9 @@ env.CppUnitTest( env.Library( target='update', source=[ + 'arithmetic_node.cpp', 'modifier_table.cpp', + 'path_creating_node.cpp', 'set_node.cpp', 'unset_node.cpp', 'update_node.cpp', @@ -69,6 +71,14 @@ env.Library( ) env.CppUnitTest( + target='arithmetic_node_test', + source='arithmetic_node_test.cpp', + LIBDEPS=[ + '$BUILD_DIR/mongo/bson/mutable/mutable_bson_test_utils', + 'update', + ], +) +env.CppUnitTest( target='modifier_table_test', source='modifier_table_test.cpp', LIBDEPS=[ diff --git a/src/mongo/db/update/arithmetic_node.cpp b/src/mongo/db/update/arithmetic_node.cpp new file mode 100644 index 00000000000..1485e7c05c8 --- /dev/null +++ b/src/mongo/db/update/arithmetic_node.cpp @@ -0,0 +1,127 @@ +/** + * Copyright (C) 2017 MongoDB 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/>. + * + * 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 GNU Affero General 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/arithmetic_node.h" + +#include "mongo/bson/mutable/algorithm.h" + +namespace mongo { + +namespace { +const char* getNameForOp(ArithmeticNode::ArithmeticOp op) { + switch (op) { + case ArithmeticNode::ArithmeticOp::kAdd: + return "increment"; + case ArithmeticNode::ArithmeticOp::kMultiply: + return "multiply"; + default: + MONGO_UNREACHABLE; + } +} + +const char* getModifierNameForOp(ArithmeticNode::ArithmeticOp op) { + switch (op) { + case ArithmeticNode::ArithmeticOp::kAdd: + return "$inc"; + case ArithmeticNode::ArithmeticOp::kMultiply: + return "$mul"; + default: + MONGO_UNREACHABLE; + } +} +} // namespace + +Status ArithmeticNode::init(BSONElement modExpr, const CollatorInterface* collator) { + invariant(modExpr.ok()); + + if (!modExpr.isNumber()) { + return Status(ErrorCodes::TypeMismatch, + str::stream() << "Cannot " << getNameForOp(_op) + << " with non-numeric argument: {" + << modExpr + << "}"); + } + + _val = modExpr; + return Status::OK(); +} + +Status ArithmeticNode::updateExistingElement(mutablebson::Element* element, bool* noop) const { + if (!element->isNumeric()) { + mutablebson::Element idElem = + mutablebson::findFirstChildNamed(element->getDocument().root(), "_id"); + return Status(ErrorCodes::TypeMismatch, + str::stream() << "Cannot apply " << getModifierNameForOp(_op) + << " to a value of non-numeric type. {" + << idElem.toString() + << "} has the field '" + << element->getFieldName() + << "' of non-numeric type " + << typeName(element->getType())); + } + + SafeNum originalValue = element->getValueSafeNum(); + SafeNum valueToSet = _val; + switch (_op) { + case ArithmeticOp::kAdd: + valueToSet += originalValue; + break; + case ArithmeticOp::kMultiply: + valueToSet *= originalValue; + break; + } + + // If the updated value is identical to the original value, treat this as a no-op. Caveat: + // if the found element is in a deserialized state, we can't do that. + if (element->getValue().ok() && valueToSet.isIdentical(originalValue)) { + *noop = true; + } else { + element->setValueSafeNum(valueToSet); + } + + return Status::OK(); +} + +Status ArithmeticNode::setValueForNewElement(mutablebson::Element* element) const { + SafeNum valueToSet = _val; + switch (_op) { + case ArithmeticOp::kAdd: + // valueToSet += 0 + break; + case ArithmeticOp::kMultiply: + // This results in a createdValue that has the same type as the original "element" + // but has a 0 value. + valueToSet *= SafeNum(static_cast<int32_t>(0)); + break; + } + return element->setValueSafeNum(valueToSet); +} + +} // namespace mongo diff --git a/src/mongo/db/update/arithmetic_node.h b/src/mongo/db/update/arithmetic_node.h new file mode 100644 index 00000000000..96977319a53 --- /dev/null +++ b/src/mongo/db/update/arithmetic_node.h @@ -0,0 +1,62 @@ +/** + * Copyright (C) 2017 MongoDB 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/>. + * + * 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 GNU Affero General 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. + */ + +#pragma once + +#include "mongo/db/update/path_creating_node.h" +#include "mongo/stdx/memory.h" + +namespace mongo { + +/** + * Represents the application of $inc or $mul to the value at the end of a path. + */ +class ArithmeticNode : public PathCreatingNode { +public: + enum class ArithmeticOp { kAdd, kMultiply }; + + ArithmeticNode(ArithmeticOp op) : _op(op) {} + + Status init(BSONElement modExpr, const CollatorInterface* collator) final; + + std::unique_ptr<UpdateNode> clone() const final { + return stdx::make_unique<ArithmeticNode>(*this); + } + + void setCollator(const CollatorInterface* collator) final {} + +protected: + Status updateExistingElement(mutablebson::Element* element, bool* noop) const final; + Status setValueForNewElement(mutablebson::Element* element) const final; + +private: + ArithmeticOp _op; + BSONElement _val; +}; + +} // namespace mongo diff --git a/src/mongo/db/update/arithmetic_node_test.cpp b/src/mongo/db/update/arithmetic_node_test.cpp new file mode 100644 index 00000000000..ef6037ada47 --- /dev/null +++ b/src/mongo/db/update/arithmetic_node_test.cpp @@ -0,0 +1,1741 @@ +/** + * Copyright (C) 2017 MongoDB 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/>. + * + * 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 GNU Affero General 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/arithmetic_node.h" + +#include "mongo/bson/mutable/algorithm.h" +#include "mongo/bson/mutable/mutable_bson_test_utils.h" +#include "mongo/db/json.h" +#include "mongo/unittest/death_test.h" +#include "mongo/unittest/unittest.h" + +namespace mongo { +namespace { + +using mongo::mutablebson::Document; +using mongo::mutablebson::Element; +using mongo::mutablebson::countChildren; + +DEATH_TEST(ArithmeticNodeTest, InitFailsForEmptyElement, "Invariant failure modExpr.ok()") { + auto update = fromjson("{$inc: {}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + node.init(update["$inc"].embeddedObject().firstElement(), collator); +} + +TEST(ArithmeticNodeTest, InitSucceedsForNumberIntElement) { + auto update = fromjson("{$inc: {a: NumberInt(5)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); +} + +TEST(ArithmeticNodeTest, InitSucceedsForNumberLongElement) { + auto update = fromjson("{$inc: {a: NumberLong(5)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); +} + +TEST(ArithmeticNodeTest, InitSucceedsForDoubleElement) { + auto update = fromjson("{$inc: {a: 5.1}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); +} + +TEST(ArithmeticNodeTest, InitSucceedsForDecimalElement) { + auto update = fromjson("{$inc: {a: NumberDecimal(\"5.1\")}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); +} + +TEST(ArithmeticNodeTest, InitFailsForNonNumericElement) { + auto update = fromjson("{$inc: {a: 'foo'}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + Status result = node.init(update["$inc"]["a"], collator); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), "Cannot increment with non-numeric argument: {a: \"foo\"}"); +} + +TEST(ArithmeticNodeTest, InitFailsForObjectElement) { + auto update = fromjson("{$mul: {a: {b: 6}}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kMultiply); + Status result = node.init(update["$mul"]["a"], collator); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), "Cannot multiply with non-numeric argument: {a: { b: 6 }}"); +} + +TEST(ArithmeticNodeTest, InitFailsForArrayElement) { + auto update = fromjson("{$mul: {a: []}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kMultiply); + Status result = node.init(update["$mul"]["a"], collator); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), "Cannot multiply with non-numeric argument: {a: []}"); +} + +TEST(ArithmeticNodeTest, ApplyIncNoOp) { + auto update = fromjson("{$inc: {a: 0}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 5}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyMulNoOp) { + auto update = fromjson("{$mul: {a: 1}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kMultiply); + ASSERT_OK(node.init(update["$mul"]["a"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 5}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyRoundingNoOp) { + auto update = fromjson("{$inc: {a: 1.0}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 6.022e23}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 6.022e23}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyEmptyPathToCreate) { + auto update = fromjson("{$inc: {a: 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 11}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {a: 11}}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyCreatePath) { + auto update = fromjson("{$inc: {'a.b.c': 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b.c"], collator)); + + Document doc(fromjson("{a: {d: 5}}")); + FieldRef pathToCreate("b.c"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {d: 5, b: {c: 6}}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {'a.b.c': 6}}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyCreatePathFromRoot) { + auto update = fromjson("{$inc: {'a.b': 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{c: 5}")); + FieldRef pathToCreate("a.b"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{c: 5, a: {b: 6}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {'a.b': 6}}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyPositional) { + auto update = fromjson("{$inc: {'a.$': 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.$"], collator)); + + Document doc(fromjson("{a: [0, 1, 2]}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.1"); + StringData matchedField("1"); + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["1"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [0, 7, 2]}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {'a.1': 7}}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyNonViablePathToInc) { + auto update = fromjson("{$inc: {'a.b': 5}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate("b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + auto result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::PathNotViable); + ASSERT_EQ(result.reason(), "Cannot create field 'b' in element {a: 5}"); +} + +TEST(ArithmeticNodeTest, ApplyNonViablePathToCreateFromReplicationIsNoOp) { + auto update = fromjson("{$inc: {'a.b': 5}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate("b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = true; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 5}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyNoIndexDataNoLogBuilder) { + auto update = fromjson("{$inc: {a: 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 11}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyDoesNotAffectIndexes) { + auto update = fromjson("{$inc: {a: 6}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 5}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 11}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, IncTypePromotionIsNotANoOp) { + auto update = fromjson("{$inc: {a: NumberLong(0)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: NumberInt(2)}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberLong(2)}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, MulTypePromotionIsNotANoOp) { + auto update = fromjson("{$mul: {a: NumberLong(1)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kMultiply); + ASSERT_OK(node.init(update["$mul"]["a"], collator)); + + Document doc(fromjson("{a: NumberInt(2)}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberLong(2)}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, TypePromotionFromIntToDecimalIsNotANoOp) { + auto update = fromjson("{$inc: {a: NumberDecimal(\"0.0\")}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: NumberInt(5)}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberDecimal(\"5.0\")}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {a: NumberDecimal(\"5.0\")}}"), logDoc); +} + +TEST(ArithmeticNodeTest, TypePromotionFromLongToDecimalIsNotANoOp) { + auto update = fromjson("{$inc: {a: NumberDecimal(\"0.0\")}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: NumberLong(5)}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberDecimal(\"5.0\")}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {a: NumberDecimal(\"5.0\")}}"), logDoc); +} + +TEST(ArithmeticNodeTest, TypePromotionFromDoubleToDecimalIsNotANoOp) { + auto update = fromjson("{$inc: {a: NumberDecimal(\"0.0\")}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 5.25}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberDecimal(\"5.25\")}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {a: NumberDecimal(\"5.25\")}}"), logDoc); +} + +TEST(ArithmeticNodeTest, IncrementedDecimalStaysDecimal) { + auto update = fromjson("{$inc: {a: 5.25}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: NumberDecimal(\"6.25\")}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: NumberDecimal(\"11.5\")}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(fromjson("{$set: {a: NumberDecimal(\"11.5\")}}"), logDoc); +} + +TEST(ArithmeticNodeTest, OverflowIntToLong) { + auto update = fromjson("{$inc: {a: 1}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + const int initialValue = std::numeric_limits<int>::max(); + Document doc(BSON("a" << initialValue)); + ASSERT_EQUALS(mongo::NumberInt, doc.root()["a"].getType()); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(mongo::NumberLong, doc.root()["a"].getType()); + ASSERT_EQUALS(BSON("a" << static_cast<long long>(initialValue) + 1), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, UnderflowIntToLong) { + auto update = fromjson("{$inc: {a: -1}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + const int initialValue = std::numeric_limits<int>::min(); + Document doc(BSON("a" << initialValue)); + ASSERT_EQUALS(mongo::NumberInt, doc.root()["a"].getType()); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(mongo::NumberLong, doc.root()["a"].getType()); + ASSERT_EQUALS(BSON("a" << static_cast<long long>(initialValue) - 1), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, IncModeCanBeReused) { + auto update = fromjson("{$inc: {a: 1}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc1(fromjson("{a: 1}")); + Document doc2(fromjson("{a: 2}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + Document logDoc; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc1.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 2}"), doc1); + ASSERT_TRUE(doc1.isInPlaceModeEnabled()); + + indexesAffected = false; + noop = false; + ASSERT_OK(node.apply(doc2.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 3}"), doc2); + ASSERT_TRUE(doc1.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, CreatedNumberHasSameTypeAsInc) { + auto update = fromjson("{$inc: {a: NumberLong(5)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{b: 6}")); + FieldRef pathToCreate("a"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{b: 6, a: NumberLong(5)}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, CreatedNumberHasSameTypeAsMul) { + auto update = fromjson("{$mul: {a: NumberLong(5)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kMultiply); + ASSERT_OK(node.init(update["$mul"]["a"], collator)); + + Document doc(fromjson("{b: 6}")); + FieldRef pathToCreate("a"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{b: 6, a: NumberLong(0)}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyEmptyDocument) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{}")); + FieldRef pathToCreate("a"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 2}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyIncToObjectFails) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{_id: 'test_object', a: {b: 1}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + Status result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), + "Cannot apply $inc to a value of non-numeric type. {_id: \"test_object\"} has the " + "field 'a' of non-numeric type object"); +} + +TEST(ArithmeticNodeTest, ApplyIncToArrayFails) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{_id: 'test_object', a: []}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + Status result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), + "Cannot apply $inc to a value of non-numeric type. {_id: \"test_object\"} has the " + "field 'a' of non-numeric type array"); +} + +TEST(ArithmeticNodeTest, ApplyIncToStringFails) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{_id: 'test_object', a: \"foo\"}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + Status result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::TypeMismatch); + ASSERT_EQ(result.reason(), + "Cannot apply $inc to a value of non-numeric type. {_id: \"test_object\"} has the " + "field 'a' of non-numeric type string"); +} + +TEST(ArithmeticNodeTest, ApplyPromoteToFP) { + auto update = fromjson("{$inc: {a: 0.2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: NumberLong(1)}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: 1.2}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNewPath) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{b: 1}")); + FieldRef pathToCreate("a"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{b: 1, a: 2}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyEmptyIndexData) { + auto update = fromjson("{$inc: {a: 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a"], collator)); + + Document doc(fromjson("{a: 1}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_EQUALS(fromjson("{a: 3}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(countChildren(logDoc.root()), 1u); + ASSERT_EQUALS(fromjson("{$set: {a: 3}}"), logDoc); +} + +TEST(ArithmeticNodeTest, ApplyNoOpDottedPath) { + auto update = fromjson("{$inc: {'a.b': 0}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: {b: 2}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b : 2}}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, TypePromotionOnDottedPathIsNotANoOp) { + auto update = fromjson("{$inc: {'a.b': NumberLong(0)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: {b: NumberInt(2)}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b : NumberLong(2)}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyPathNotViableArray) { + auto update = fromjson("{$inc: {'a.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a:[{b:1}]}")); + FieldRef pathToCreate("b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + Status result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::PathNotViable); + ASSERT_EQ(result.reason(), "Cannot create field 'b' in element {a: [ { b: 1 } ]}"); +} + +TEST(ArithmeticNodeTest, ApplyInPlaceDottedPath) { + auto update = fromjson("{$inc: {'a.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: {b: 1}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b: 3}}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyPromotionDottedPath) { + auto update = fromjson("{$inc: {'a.b': NumberLong(2)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: {b: NumberInt(3)}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b: NumberLong(5)}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyExtendPath) { + auto update = fromjson("{$inc: {'a.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{a: {c: 1}}")); + FieldRef pathToCreate("b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {c: 1, b: 2}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyDottedPathEmptyDoc) { + auto update = fromjson("{$inc: {'a.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{}")); + FieldRef pathToCreate("a.b"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b: 2}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyFieldWithDot) { + auto update = fromjson("{$inc: {'a.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.b"], collator)); + + Document doc(fromjson("{'a.b':4}")); + FieldRef pathToCreate("a.b"); + FieldRef pathTaken(""); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a.b"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root(), + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{'a.b':4, a: {b: 2}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNoOpArrayIndex) { + auto update = fromjson("{$inc: {'a.2.b': 0}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b: 0},{b: 1},{b: 2}]}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.2.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["2"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_TRUE(noop); + ASSERT_FALSE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [{b: 0},{b: 1},{b: 2}]}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, TypePromotionInArrayIsNotANoOp) { + auto update = fromjson("{$set: {'a.2.b': NumberLong(0)}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$set"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b: NumberInt(0)},{b: NumberInt(1)},{b: NumberInt(2)}]}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.2.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["2"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [{b: 0},{b: 1},{b: NumberLong(2)}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNonViablePathThroughArray) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: 0}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + Status result = node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + logBuilder, + &indexesAffected, + &noop); + ASSERT_NOT_OK(result); + ASSERT_EQ(result.code(), ErrorCodes::PathNotViable); + ASSERT_EQ(result.reason(), "Cannot create field '2' in element {a: 0}"); +} + +TEST(ArithmeticNodeTest, ApplyInPlaceArrayIndex) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b: 0},{b: 1},{b: 1}]}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.2.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["2"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [{b: 0},{b: 1},{b: 3}]}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyAppendArray) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b: 0},{b: 1}]}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [{b: 0},{b: 1},{b: 2}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyPaddingArray) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b: 0}]}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [{b: 0},null,{b: 2}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNumericObject) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: {b: 0}}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {b: 0, '2': {b: 2}}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNumericField) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: {'2': {b: 1}}}")); + FieldRef pathToCreate(""); + FieldRef pathTaken("a.2.b"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["2"]["b"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {'2': {b: 3}}}"), doc); + ASSERT_TRUE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyExtendNumericField) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: {'2': {c: 1}}}")); + FieldRef pathToCreate("b"); + FieldRef pathTaken("a.2"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"]["2"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {'2': {c: 1, b: 2}}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyNumericFieldToEmptyObject) { + auto update = fromjson("{$set: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$set"]["a.2.b"], collator)); + + Document doc(fromjson("{a: {}}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: {'2': {b: 2}}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyEmptyArray) { + auto update = fromjson("{$set: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$set"]["a.2.b"], collator)); + + Document doc(fromjson("{a: []}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + UpdateIndexData indexData; + indexData.addPath("a"); + LogBuilder* logBuilder = nullptr; + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + &indexData, + logBuilder, + &indexesAffected, + &noop)); + ASSERT_FALSE(noop); + ASSERT_TRUE(indexesAffected); + ASSERT_EQUALS(fromjson("{a: [null, null, {b: 2}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); +} + +TEST(ArithmeticNodeTest, ApplyLogDottedPath) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: [{b:0}, {b:1}]}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_EQUALS(fromjson("{a: [{b:0}, {b:1}, {b:2}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(countChildren(logDoc.root()), 1u); + ASSERT_EQUALS(fromjson("{$set: {'a.2.b': 2}}"), logDoc); +} + +TEST(ArithmeticNodeTest, LogEmptyArray) { + auto update = fromjson("{$inc: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$inc"]["a.2.b"], collator)); + + Document doc(fromjson("{a: []}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_EQUALS(fromjson("{a: [null, null, {b:2}]}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(countChildren(logDoc.root()), 1u); + ASSERT_EQUALS(fromjson("{$set: {'a.2.b': 2}}"), logDoc); +} + +TEST(ArithmeticNodeTest, LogEmptyObject) { + auto update = fromjson("{$set: {'a.2.b': 2}}"); + const CollatorInterface* collator = nullptr; + ArithmeticNode node(ArithmeticNode::ArithmeticOp::kAdd); + ASSERT_OK(node.init(update["$set"]["a.2.b"], collator)); + + Document doc(fromjson("{a: {}}")); + FieldRef pathToCreate("2.b"); + FieldRef pathTaken("a"); + StringData matchedField; + auto fromReplication = false; + const UpdateIndexData* indexData = nullptr; + Document logDoc; + LogBuilder logBuilder(logDoc.root()); + auto indexesAffected = false; + auto noop = false; + ASSERT_OK(node.apply(doc.root()["a"], + &pathToCreate, + &pathTaken, + matchedField, + fromReplication, + indexData, + &logBuilder, + &indexesAffected, + &noop)); + ASSERT_EQUALS(fromjson("{a: {'2': {b: 2}}}"), doc); + ASSERT_FALSE(doc.isInPlaceModeEnabled()); + ASSERT_EQUALS(countChildren(logDoc.root()), 1u); + ASSERT_EQUALS(fromjson("{$set: {'a.2.b': 2}}"), logDoc); +} + +} // namespace +} // namespace mongo diff --git a/src/mongo/db/update/modifier_table.cpp b/src/mongo/db/update/modifier_table.cpp index d98e6f40e95..99bedd67009 100644 --- a/src/mongo/db/update/modifier_table.cpp +++ b/src/mongo/db/update/modifier_table.cpp @@ -46,6 +46,7 @@ #include "mongo/db/ops/modifier_rename.h" #include "mongo/db/ops/modifier_set.h" #include "mongo/db/ops/modifier_unset.h" +#include "mongo/db/update/arithmetic_node.h" #include "mongo/db/update/set_node.h" #include "mongo/db/update/unset_node.h" #include "mongo/platform/unordered_map.h" @@ -180,6 +181,10 @@ ModifierInterface* makeUpdateMod(ModifierType modType) { std::unique_ptr<UpdateLeafNode> makeUpdateLeafNode(ModifierType modType) { switch (modType) { + case MOD_INC: + return stdx::make_unique<ArithmeticNode>(ArithmeticNode::ArithmeticOp::kAdd); + case MOD_MUL: + return stdx::make_unique<ArithmeticNode>(ArithmeticNode::ArithmeticOp::kMultiply); case MOD_SET: return stdx::make_unique<SetNode>(); case MOD_UNSET: diff --git a/src/mongo/db/update/path_creating_node.cpp b/src/mongo/db/update/path_creating_node.cpp new file mode 100644 index 00000000000..4e6683dabee --- /dev/null +++ b/src/mongo/db/update/path_creating_node.cpp @@ -0,0 +1,120 @@ +/** + * Copyright (C) 2017 MongoDB 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/>. + * + * 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 GNU Affero General 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/path_creating_node.h" + +#include "mongo/db/update/path_support.h" + +namespace mongo { + +Status PathCreatingNode::apply(mutablebson::Element element, + FieldRef* pathToCreate, + FieldRef* pathTaken, + StringData matchedField, + bool fromReplication, + const UpdateIndexData* indexData, + LogBuilder* logBuilder, + bool* indexesAffected, + bool* noop) const { + *indexesAffected = false; + *noop = false; + + // The value in this Element gets used to create a logging entry (if we have a LogBuilder). + mutablebson::Element valueToLog = element.getDocument().end(); + + if (pathToCreate->empty()) { + // We found an existing element at the update path. + auto status = updateExistingElement(&element, noop); + if (!status.isOK()) { + return status; + } + if (*noop) { + return Status::OK(); + } + + valueToLog = element; + } else { + // We did not find an element at the update path. Create one. + auto newElementFieldName = pathToCreate->getPart(pathToCreate->numParts() - 1); + auto newElement = element.getDocument().makeElementNull(newElementFieldName); + auto status = setValueForNewElement(&newElement); + if (!status.isOK()) { + return status; + } + + invariant(newElement.ok()); + status = pathsupport::createPathAt(*pathToCreate, 0, element, newElement); + if (!status.isOK()) { + // $sets 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 "fromReplication" is + // true.) + if (status.code() == ErrorCodes::PathNotViable && fromReplication) { + *noop = true; + return Status::OK(); + } + return status; + } + + valueToLog = newElement; + } + + // Create full field path of set element. + StringBuilder builder; + builder << pathTaken->dottedField(); + if (!pathTaken->empty() && !pathToCreate->empty()) { + builder << "."; + } + builder << pathToCreate->dottedField(); + auto fullPath = builder.str(); + + // Determine if indexes are affected. + if (indexData && indexData->mightBeIndexed(fullPath)) { + *indexesAffected = true; + } + + // Log the operation. + if (logBuilder) { + auto logElement = + logBuilder->getDocument().makeElementWithNewFieldName(fullPath, valueToLog); + invariant(logElement.ok()); + return logBuilder->addToSets(logElement); + } + + return Status::OK(); +} +} // namespace mongo diff --git a/src/mongo/db/update/path_creating_node.h b/src/mongo/db/update/path_creating_node.h new file mode 100644 index 00000000000..0e17e763ce3 --- /dev/null +++ b/src/mongo/db/update/path_creating_node.h @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2017 MongoDB 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/>. + * + * 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 GNU Affero General 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. + */ + +#pragma once + +#include "mongo/db/update/update_leaf_node.h" +#include "mongo/stdx/memory.h" + +namespace mongo { + +/** + * A PathCreatingNode represents an update modifier that materializes the field it wants to update + * when that field does not already exist. This category of modifiers includes $set, $inc, and $mul. + * These nodes are very similar in operation, so most of their logic is in the apply() method of + * this abstract base class, which calls into a virtual methods for modifier-specific functionality. + */ +class PathCreatingNode : public UpdateLeafNode { +public: + Status apply(mutablebson::Element element, + FieldRef* pathToCreate, + FieldRef* pathTaken, + StringData matchedField, + bool fromReplication, + const UpdateIndexData* indexData, + LogBuilder* logBuilder, + bool* indexesAffected, + bool* noop) const final; + +protected: + /** + * PathCreatingNode::apply() calls the updateExistingElement() method when applying its update + * to an existing path. The child's implementation of this method is responsible for either + * updating the given Element or setting *noop to true to indicate that no update is necessary. + */ + virtual Status updateExistingElement(mutablebson::Element* element, bool* noop) const = 0; + + /** + * PathCreatingNode::apply() calls the setValueForNewElement() method when it must materialize a + * new field in order to apply its update. The child's implemenation of this method is + * responsible for assigning a value to the new element (which will initially be null). + */ + virtual Status setValueForNewElement(mutablebson::Element* element) const = 0; +}; + +} // namespace mongo diff --git a/src/mongo/db/update/set_node.cpp b/src/mongo/db/update/set_node.cpp index 21c40ec5a69..f580ffa5841 100644 --- a/src/mongo/db/update/set_node.cpp +++ b/src/mongo/db/update/set_node.cpp @@ -42,71 +42,20 @@ Status SetNode::init(BSONElement modExpr, const CollatorInterface* collator) { return Status::OK(); } -Status SetNode::apply(mutablebson::Element element, - FieldRef* pathToCreate, - FieldRef* pathTaken, - StringData matchedField, - bool fromReplication, - const UpdateIndexData* indexData, - LogBuilder* logBuilder, - bool* indexesAffected, - bool* noop) const { - *indexesAffected = false; - *noop = false; - - // Set the element with the new value. - if (pathToCreate->empty()) { - // If 'element' is deserialized, then element.getValue() will be EOO, which will never equal - // _val. - if (element.getValue().binaryEqualValues(_val)) { - *noop = true; - return Status::OK(); - } - element.setValueBSONElement(_val); +Status SetNode::updateExistingElement(mutablebson::Element* element, bool* noop) const { + // If 'element' is deserialized, then element.getValue() will be EOO, which will never equal + // _val. + if (element->getValue().binaryEqualValues(_val)) { + *noop = true; } else { - auto elemToSet = element.getDocument().makeElementWithNewFieldName( - pathToCreate->getPart(pathToCreate->numParts() - 1), _val); - invariant(elemToSet.ok()); - auto status = pathsupport::createPathAt(*pathToCreate, 0, element, elemToSet); - if (!status.isOK()) { - // $sets 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'. - if (status.code() == ErrorCodes::PathNotViable && fromReplication) { - *noop = true; - return Status::OK(); - } - return status; - } - } - - // Create full field path of set element. - StringBuilder builder; - builder << pathTaken->dottedField(); - if (!pathTaken->empty() && !pathToCreate->empty()) { - builder << "."; - } - builder << pathToCreate->dottedField(); - auto fullPath = builder.str(); - - // Determine if indexes are affected. - if (indexData && indexData->mightBeIndexed(fullPath)) { - *indexesAffected = true; - } - - // Log the set. - if (logBuilder) { - auto logElement = logBuilder->getDocument().makeElementWithNewFieldName(fullPath, _val); - invariant(logElement.ok()); - return logBuilder->addToSets(logElement); + element->setValueBSONElement(_val); } return Status::OK(); } +Status SetNode::setValueForNewElement(mutablebson::Element* element) const { + return element->setValueBSONElement(_val); +} + } // namespace mongo diff --git a/src/mongo/db/update/set_node.h b/src/mongo/db/update/set_node.h index 5446ca05a2c..07f27115846 100644 --- a/src/mongo/db/update/set_node.h +++ b/src/mongo/db/update/set_node.h @@ -28,7 +28,7 @@ #pragma once -#include "mongo/db/update/update_leaf_node.h" +#include "mongo/db/update/path_creating_node.h" #include "mongo/stdx/memory.h" namespace mongo { @@ -36,7 +36,7 @@ namespace mongo { /** * Represents the application of a $set to the value at the end of a path. */ -class SetNode : public UpdateLeafNode { +class SetNode : public PathCreatingNode { public: Status init(BSONElement modExpr, const CollatorInterface* collator) final; @@ -46,15 +46,9 @@ public: void setCollator(const CollatorInterface* collator) final {} - Status apply(mutablebson::Element element, - FieldRef* pathToCreate, - FieldRef* pathTaken, - StringData matchedField, - bool fromReplication, - const UpdateIndexData* indexData, - LogBuilder* logBuilder, - bool* indexesAffected, - bool* noop) const final; +protected: + Status updateExistingElement(mutablebson::Element* element, bool* noop) const final; + Status setValueForNewElement(mutablebson::Element* element) const final; private: BSONElement _val; diff --git a/src/mongo/db/update/set_node_test.cpp b/src/mongo/db/update/set_node_test.cpp index e2fe5fe14fa..b72a02dbb10 100644 --- a/src/mongo/db/update/set_node_test.cpp +++ b/src/mongo/db/update/set_node_test.cpp @@ -198,7 +198,7 @@ TEST(SetNodeTest, ApplyPositional) { Document doc(fromjson("{a: [0, 1, 2]}")); FieldRef pathToCreate(""); FieldRef pathTaken("a.1"); - StringData matchedField; + StringData matchedField("1"); auto fromReplication = false; UpdateIndexData indexData; indexData.addPath("a"); @@ -206,7 +206,7 @@ TEST(SetNodeTest, ApplyPositional) { LogBuilder logBuilder(logDoc.root()); auto indexesAffected = false; auto noop = false; - ASSERT_OK(node.apply(doc.root(), + ASSERT_OK(node.apply(doc.root()["a"]["1"], &pathToCreate, &pathTaken, matchedField, @@ -217,7 +217,7 @@ TEST(SetNodeTest, ApplyPositional) { &noop)); ASSERT_FALSE(noop); ASSERT_TRUE(indexesAffected); - ASSERT_EQUALS(fromjson("{a: [0, 1, 2]}"), doc); + ASSERT_EQUALS(fromjson("{a: [0, 6, 2]}"), doc); ASSERT_TRUE(doc.isInPlaceModeEnabled()); ASSERT_EQUALS(fromjson("{$set: {'a.1': 6}}"), logDoc); } diff --git a/src/mongo/db/update/update_object_node_test.cpp b/src/mongo/db/update/update_object_node_test.cpp index be71ef001fe..a2fc156bb71 100644 --- a/src/mongo/db/update/update_object_node_test.cpp +++ b/src/mongo/db/update/update_object_node_test.cpp @@ -53,6 +53,22 @@ TEST(UpdateObjectNodeTest, InvalidPathFailsToParse) { ASSERT_EQ(result.getStatus().reason(), "An empty update path is not valid."); } +TEST(UpdateObjectNodeTest, ValidIncPathParsesSuccessfully) { + auto update = fromjson("{$inc: {'a.b': 5}}"); + const CollatorInterface* collator = nullptr; + UpdateObjectNode root; + ASSERT_OK(UpdateObjectNode::parseAndMerge( + &root, modifiertable::ModifierType::MOD_INC, update["$inc"]["a.b"], collator)); +} + +TEST(UpdateObjectNodeTest, ValidMulPathParsesSuccessfully) { + auto update = fromjson("{$mul: {'a.b': 5}}"); + const CollatorInterface* collator = nullptr; + UpdateObjectNode root; + ASSERT_OK(UpdateObjectNode::parseAndMerge( + &root, modifiertable::ModifierType::MOD_MUL, update["$mul"]["a.b"], collator)); +} + TEST(UpdateObjectNodeTest, ValidSetPathParsesSuccessfully) { auto update = fromjson("{$set: {'a.b': 5}}"); const CollatorInterface* collator = nullptr; @@ -114,15 +130,15 @@ TEST(UpdateObjectNodeTest, PositionalElementFirstPositionFailsToParse) { } // TODO SERVER-28777: All modifier types should succeed. -TEST(UpdateObjectNodeTest, IncFailsToParse) { - auto update = fromjson("{$inc: {a: 5}}"); +TEST(UpdateObjectNodeTest, PushFailsToParse) { + auto update = fromjson("{$push: {a: 5}}"); const CollatorInterface* collator = nullptr; UpdateObjectNode root; auto result = UpdateObjectNode::parseAndMerge( - &root, modifiertable::ModifierType::MOD_INC, update["$inc"]["a"], collator); + &root, modifiertable::ModifierType::MOD_PUSH, update["$push"]["a"], collator); ASSERT_NOT_OK(result); ASSERT_EQ(result.getStatus().code(), ErrorCodes::FailedToParse); - ASSERT_EQ(result.getStatus().reason(), "Cannot construct modifier of type 3"); + ASSERT_EQ(result.getStatus().reason(), "Cannot construct modifier of type 10"); } TEST(UpdateObjectNodeTest, TwoModifiersOnSameFieldFailToParse) { |
