diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-11 22:25:03 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-11 22:25:03 +0000 |
commit | 3aa01ec8ce4b509569a4e6c8f64858038b2ca5d4 (patch) | |
tree | 6fbf6080266e5d6774317ba0f453c4a4fb1fc917 /document/src | |
parent | 6d5b7bb9cff88d5030a606082b703a2c706e0747 (diff) |
Move the feed reject helper to document module.
Diffstat (limited to 'document/src')
-rw-r--r-- | document/src/tests/documenttestcase.cpp | 52 | ||||
-rw-r--r-- | document/src/vespa/document/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | document/src/vespa/document/util/feed_reject_helper.cpp | 52 | ||||
-rw-r--r-- | document/src/vespa/document/util/feed_reject_helper.h | 23 |
4 files changed, 128 insertions, 0 deletions
diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp index e917e33f9ef..64f0029aa56 100644 --- a/document/src/tests/documenttestcase.cpp +++ b/document/src/tests/documenttestcase.cpp @@ -10,10 +10,24 @@ #include <vespa/vespalib/objects/nbostream.h> #include <vespa/vespalib/io/fileutil.h> #include <vespa/vespalib/util/growablebytebuffer.h> +#include <vespa/document/fieldvalue/referencefieldvalue.h> +#include <vespa/document/update/removefieldpathupdate.h> +#include <vespa/document/update/clearvalueupdate.h> +#include <vespa/document/update/removevalueupdate.h> +#include <vespa/document/update/arithmeticvalueupdate.h> +#include <vespa/document/update/addvalueupdate.h> +#include <vespa/document/update/mapvalueupdate.h> +#include <vespa/document/update/assignvalueupdate.h> + +#include <vespa/document/update/tensor_remove_update.h> +#include <vespa/document/update/tensor_modify_update.h> +#include <vespa/document/update/tensor_add_update.h> +#include <vespa/document/update/tensor_partial_update.h> #include <vespa/vespalib/testkit/test_kit.h> #include <vespa/document/util/serializableexceptions.h> #include <vespa/document/util/bytebuffer.h> +#include <vespa/document/util/feed_reject_helper.h> #include <fcntl.h> #include <gtest/gtest.h> #include <gmock/gmock.h> @@ -1251,4 +1265,42 @@ TEST(DocumentTest, testDeserializeMultiple) EXPECT_EQ(correct, sv3); } +TEST(DocumentRejectTest, requireThatFixedSizeFieldValuesAreDetected) { + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::BoolFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::ByteFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::ShortFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::IntFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::LongFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::FloatFieldValue())); + EXPECT_TRUE(FeedRejectHelper::isFixedSizeSingleValue(document::DoubleFieldValue())); + + EXPECT_FALSE(FeedRejectHelper::isFixedSizeSingleValue(document::StringFieldValue())); + EXPECT_FALSE(FeedRejectHelper::isFixedSizeSingleValue(document::RawFieldValue())); + EXPECT_FALSE(FeedRejectHelper::isFixedSizeSingleValue(document::PredicateFieldValue())); + EXPECT_FALSE(FeedRejectHelper::isFixedSizeSingleValue(document::ReferenceFieldValue())); + + document::ArrayDataType intArrayType(*document::DataType::INT); + EXPECT_FALSE(FeedRejectHelper::isFixedSizeSingleValue(document::ArrayFieldValue(intArrayType))); +} + +TEST(DocumentRejectTest, requireThatClearRemoveTensorRemoveAndArtithmeticUpdatesIgnoreFeedRejection) { + EXPECT_FALSE(FeedRejectHelper::mustReject(ClearValueUpdate())); + EXPECT_FALSE(FeedRejectHelper::mustReject(RemoveValueUpdate(StringFieldValue()))); + EXPECT_FALSE(FeedRejectHelper::mustReject(ArithmeticValueUpdate(ArithmeticValueUpdate::Add, 5.0))); + EXPECT_FALSE(FeedRejectHelper::mustReject(TensorRemoveUpdate(std::make_unique<TensorFieldValue>()))); +} + +TEST(DocumentRejectTest, requireThatAddMapTensorModifyAndTensorAddUpdatesWillBeRejected) { + EXPECT_TRUE(FeedRejectHelper::mustReject(AddValueUpdate(IntFieldValue()))); + EXPECT_TRUE(FeedRejectHelper::mustReject(MapValueUpdate(IntFieldValue(), ClearValueUpdate()))); + EXPECT_TRUE(FeedRejectHelper::mustReject(TensorModifyUpdate(TensorModifyUpdate::Operation::REPLACE, + std::make_unique<TensorFieldValue>()))); + EXPECT_TRUE(FeedRejectHelper::mustReject(TensorAddUpdate(std::make_unique<TensorFieldValue>()))); +} + +TEST(DocumentRejectTest, requireThatAssignUpdatesWillBeRejectedBasedOnTheirContent) { + EXPECT_FALSE(FeedRejectHelper::mustReject(AssignValueUpdate(IntFieldValue()))); + EXPECT_TRUE(FeedRejectHelper::mustReject(AssignValueUpdate(StringFieldValue()))); +} + } diff --git a/document/src/vespa/document/util/CMakeLists.txt b/document/src/vespa/document/util/CMakeLists.txt index 9ad7d4cff06..7b6423a5f34 100644 --- a/document/src/vespa/document/util/CMakeLists.txt +++ b/document/src/vespa/document/util/CMakeLists.txt @@ -2,6 +2,7 @@ vespa_add_library(document_util OBJECT SOURCES bytebuffer.cpp + feed_reject_helper.cpp printable.cpp serializableexceptions.cpp stringutil.cpp diff --git a/document/src/vespa/document/util/feed_reject_helper.cpp b/document/src/vespa/document/util/feed_reject_helper.cpp new file mode 100644 index 00000000000..e55b408219c --- /dev/null +++ b/document/src/vespa/document/util/feed_reject_helper.cpp @@ -0,0 +1,52 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "feed_reject_helper.h" +#include <vespa/document/update/documentupdate.h> +#include <vespa/document/update/assignvalueupdate.h> +#include <vespa/document/fieldvalue/boolfieldvalue.h> +#include <vespa/document/fieldvalue/numericfieldvalue.h> + + +namespace document { + +bool +FeedRejectHelper::isFixedSizeSingleValue(const document::FieldValue & fv) { + return fv.inherits(BoolFieldValue::classId) || fv.inherits(NumericFieldValueBase::classId); +} + +bool +FeedRejectHelper::mustReject(const document::ValueUpdate & valueUpdate) { + using namespace document; + switch (valueUpdate.getType()) { + case ValueUpdate::Add: + case ValueUpdate::TensorAddUpdate: + case ValueUpdate::TensorModifyUpdate: + case ValueUpdate::Map: + return true; + case ValueUpdate::Assign: { + const auto & assign = dynamic_cast<const AssignValueUpdate &>(valueUpdate); + if (assign.hasValue()) { + if ( ! isFixedSizeSingleValue(assign.getValue())) { + return true; + } + } + } + default: + break; + } + return false; +} + +bool +FeedRejectHelper::mustReject(const DocumentUpdate & documentUpdate) { + for (const auto & update : documentUpdate.getUpdates()) { + for (const auto & valueUpdate : update.getUpdates()) { + if (mustReject(*valueUpdate)) { + return true; + } + } + } + return ! documentUpdate.getFieldPathUpdates().empty(); +} + +} diff --git a/document/src/vespa/document/util/feed_reject_helper.h b/document/src/vespa/document/util/feed_reject_helper.h new file mode 100644 index 00000000000..a8ba37eb775 --- /dev/null +++ b/document/src/vespa/document/util/feed_reject_helper.h @@ -0,0 +1,23 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +namespace document { + +class FieldValue; +class DocumentUpdate; +class ValueUpdate; + + +/** + * Tells wether an operation should be blocked when resourcelimits have been reached. + * It looks at the operation type and also the content if it is an 'update' operation. + */ +class FeedRejectHelper { +public: + static bool isFixedSizeSingleValue(const FieldValue & fv); + static bool mustReject(const ValueUpdate & valueUpdate); + static bool mustReject(const DocumentUpdate & documentUpdate); +}; + +} |