szaszm commented on code in PR #1872: URL: https://github.com/apache/nifi-minifi-cpp/pull/1872#discussion_r1786714238
########## extensions/standard-processors/processors/SplitContent.cpp: ########## @@ -0,0 +1,168 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SplitContent.h" + +#include <range/v3/view/split.hpp> + +#include "core/ProcessContext.h" +#include "core/ProcessSession.h" +#include "core/Resource.h" +#include "core/FlowFile.h" +#include "utils/gsl.h" +#include "utils/ProcessorConfigUtils.h" + +namespace org::apache::nifi::minifi::processors { + +constexpr size_t BUFFER_TARGET_SIZE = 1024; + +void SplitContent::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitContent::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + auto byte_sequence_str = utils::getRequiredPropertyOrThrow<std::string>(context, ByteSequence.name); + const auto byte_sequence_format = utils::parseEnumProperty<ByteSequenceFormat>(context, ByteSequenceFormatProperty); + if (byte_sequence_format == ByteSequenceFormat::Hexadecimal) { + byte_sequence_ = utils::string::from_hex(byte_sequence_str); + } else { + byte_sequence_.resize(byte_sequence_str.size()); + std::ranges::transform(byte_sequence_str, byte_sequence_.begin(), [] (char c) { return static_cast<std::byte>(c); }); + } + byte_sequence_location_ = utils::parseEnumProperty<ByteSequenceLocation>(context, ByteSequenceLocationProperty); + keep_byte_sequence = utils::getRequiredPropertyOrThrow<bool>(context, KeepByteSequence.name); +} + +std::shared_ptr<core::FlowFile> SplitContent::createNewSplit(core::ProcessSession& session) const { + auto next_split = session.create(); + if (!next_split) { + throw Exception(PROCESSOR_EXCEPTION, "Couldn't create FlowFile"); + } + if (keep_byte_sequence && byte_sequence_location_ == ByteSequenceLocation::Leading) { + session.appendBuffer(next_split, byte_sequence_); + } + return next_split; +} + +void SplitContent::finalizeLatestSplitContent(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& latest_split, const std::vector<std::byte>& buffer) const { + const std::span<const std::byte> data_without_byte_sequence{buffer.data(), buffer.size() - byte_sequence_.size()}; + session.appendBuffer(latest_split, data_without_byte_sequence); + if (keep_byte_sequence && byte_sequence_location_ == ByteSequenceLocation::Trailing) { + session.appendBuffer(latest_split, byte_sequence_); + } +} + +void SplitContent::finalizeLastSplitContent(core::ProcessSession& session, + std::vector<std::shared_ptr<core::FlowFile>>& splits, + const std::vector<std::byte>& buffer, + const bool ended_with_byte_sequence) const { + if (ended_with_byte_sequence && splits.back()->getSize() != 0) { + if (keep_byte_sequence && byte_sequence_location_ == ByteSequenceLocation::Leading) { + const auto last_split = session.create(); + if (!last_split) { + throw Exception(PROCESSOR_EXCEPTION, "Couldn't create FlowFile"); + } + splits.push_back(last_split); + session.appendBuffer(splits.back(), byte_sequence_); + } + } else { + session.appendBuffer(splits.back(), buffer); + } +} + +namespace { +std::shared_ptr<core::FlowFile> createFirstSplit(core::ProcessSession& session) { + auto first_split = session.create(); + if (!first_split) { + throw Exception(PROCESSOR_EXCEPTION, "Couldn't create FlowFile"); + } + return first_split; +} + +void updateSplitAttributesAndTransfer(core::ProcessSession& session, const std::vector<std::shared_ptr<core::FlowFile>>& splits, const core::FlowFile& original) { + const std::string fragment_identifier_ = utils::IdGenerator::getIdGenerator()->generate().to_string(); + for (size_t split_i = 0; split_i < splits.size(); ++split_i) { + const auto& split = splits[split_i]; + split->setAttribute(SplitContent::FragmentCountOutputAttribute.name, std::to_string(splits.size())); + split->setAttribute(SplitContent::FragmentIndexOutputAttribute.name, std::to_string(split_i + 1)); // One based indexing + split->setAttribute(SplitContent::FragmentIdentifierOutputAttribute.name, fragment_identifier_); + split->setAttribute(SplitContent::SegmentOriginalFilenameOutputAttribute.name, original.getAttribute(core::SpecialFlowAttribute::FILENAME).value_or("")); + session.transfer(split, SplitContent::Splits); + } +} + +bool lastSplitIsEmpty(const std::vector<std::shared_ptr<core::FlowFile>>& splits) { + return splits.back()->getSize() != 0; +} +} // namespace + +void SplitContent::onTrigger(core::ProcessContext& context, core::ProcessSession& session) { + gsl_Assert(!byte_sequence_.empty()); Review Comment: If it's asserted here, it should be ensured in onSchedule, so a misconfigured agent doesn't just crash. ########## extensions/standard-processors/tests/unit/SplitContentTests.cpp: ########## @@ -0,0 +1,379 @@ +/** +* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "FlowFileRecord.h" +#include "catch2/generators/catch_generators.hpp" +#include "processors/SplitContent.h" +#include "unit/Catch.h" +#include "unit/SingleProcessorTestController.h" +#include "unit/TestBase.h" + +namespace org::apache::nifi::minifi::processors::test { + +template<typename... Bytes> +std::vector<std::byte> createByteVector(Bytes... bytes) { + return {static_cast<std::byte>(bytes)...}; +} + +TEST_CASE("TextFormatLeadingPosition", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + split_content->setProperty(SplitContent::ByteSequenceFormatProperty, magic_enum::enum_name(SplitContent::ByteSequenceFormat::Text)); + split_content->setProperty(SplitContent::ByteSequence, "ub"); + split_content->setProperty(SplitContent::KeepByteSequence, "true"); + split_content->setProperty(SplitContent::ByteSequenceLocationProperty, magic_enum::enum_name(SplitContent::ByteSequenceLocation::Leading)); + + auto trigger_results = controller.trigger("rub-a-dub-dub"); + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 4); + + CHECK(controller.plan->getContent(original[0]) == "rub-a-dub-dub"); + + CHECK(controller.plan->getContent(splits[0]) == "r"); + CHECK(controller.plan->getContent(splits[1]) == "ub-a-d"); + CHECK(controller.plan->getContent(splits[2]) == "ub-d"); + CHECK(controller.plan->getContent(splits[3]) == "ub"); +} + +TEST_CASE("TextFormatTrailingPosition", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::ByteSequenceFormatProperty, magic_enum::enum_name(SplitContent::ByteSequenceFormat::Text)); + split_content->setProperty(SplitContent::ByteSequence, "ub"); + split_content->setProperty(SplitContent::KeepByteSequence, "true"); + split_content->setProperty(SplitContent::ByteSequenceLocationProperty, magic_enum::enum_name(SplitContent::ByteSequenceLocation::Trailing)); + + auto trigger_results = controller.trigger("rub-a-dub-dub"); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 3); + + CHECK(controller.plan->getContent(original[0]) == "rub-a-dub-dub"); + + CHECK(controller.plan->getContent(splits[0]) == "rub"); + CHECK(controller.plan->getContent(splits[1]) == "-a-dub"); + CHECK(controller.plan->getContent(splits[2]) == "-dub"); +} + +TEST_CASE("TextFormatSplits", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::ByteSequenceFormatProperty, magic_enum::enum_name(SplitContent::ByteSequenceFormat::Text)); + split_content->setProperty(SplitContent::ByteSequence, "test"); + + constexpr std::string_view input_1 = "This is a test. This is another test. And this is yet another test. Finally this is the last Test."; + constexpr std::string_view input_2 = "This is a test. This is another test. And this is yet another test. Finally this is the last test"; + + const auto [keep_byte_sequence, byte_sequence_location, input, expected_splits] = GENERATE_REF( + std::make_tuple("true", "Leading", input_1, std::vector<std::string_view>{"This is a ", "test. This is another ", "test. And this is yet another ", "test. Finally this is the last Test."}), + std::make_tuple("false", "Leading", input_1, std::vector<std::string_view>{"This is a ", ". This is another ", ". And this is yet another ", ". Finally this is the last Test."}), + std::make_tuple("true", "Trailing", input_1, std::vector<std::string_view>{"This is a test", ". This is another test", ". And this is yet another test", ". Finally this is the last Test."}), + std::make_tuple("false", "Trailing", input_1, std::vector<std::string_view>{"This is a ", ". This is another ", ". And this is yet another ", ". Finally this is the last Test."}), + std::make_tuple("true", "Leading", input_2, std::vector<std::string_view>{"This is a ", "test. This is another ", "test. And this is yet another ", "test. Finally this is the last ", "test"}), + std::make_tuple("true", "Trailing", input_2, std::vector<std::string_view>{"This is a test", ". This is another test", ". And this is yet another test", ". Finally this is the last test"})); + + split_content->setProperty(SplitContent::KeepByteSequence, keep_byte_sequence); + split_content->setProperty(SplitContent::ByteSequenceLocationProperty, byte_sequence_location); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == expected_splits.size()); + + CHECK(controller.plan->getContent(original[0]) == input); + + for (size_t i = 0; i < expected_splits.size(); ++i) { + auto split_i = controller.plan->getContent(splits[i]); + auto expected_i = expected_splits[i]; + CHECK(split_i == expected_i); + } +} + +TEST_CASE("SmallSplits", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "false"); + split_content->setProperty(SplitContent::ByteSequence, "FFFF"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 0xFF, 0xFF, 0xFF, 5, 4, 3, 2, 1); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 2); + + const auto expected_split_1 = createByteVector(1, 2, 3, 4, 5); + const auto expected_split_2 = createByteVector(0xFF, 5, 4, 3, 2, 1); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); + CHECK(controller.plan->getContentAsBytes(*splits[1]) == expected_split_2); +} + +TEST_CASE("WithSingleByteSplit", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "false"); + split_content->setProperty(SplitContent::ByteSequence, "FF"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 0xFF, 5, 4, 3, 2, 1); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + const auto original = trigger_results.at(processors::SplitContent::Original); + const auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 2); + + const auto expected_split_1 = createByteVector(1, 2, 3, 4, 5); + const auto expected_split_2 = createByteVector(5, 4, 3, 2, 1); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); + CHECK(controller.plan->getContentAsBytes(*splits[1]) == expected_split_2); +} + +TEST_CASE("WithLargerSplit", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "false"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + const auto original = trigger_results.at(processors::SplitContent::Original); + const auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 2); + + const auto expected_split_1 = createByteVector(1, 2, 3, 4); + const auto expected_split_2 = createByteVector(5, 5, 4, 3, 2, 1); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); + CHECK(controller.plan->getContentAsBytes(*splits[1]) == expected_split_2); +} + +TEST_CASE("KeepingSequence", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "true"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + const auto original = trigger_results.at(processors::SplitContent::Original); + const auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 2); + + const auto expected_split_1 = createByteVector(1, 2, 3, 4, 5, 5, 5, 5); + const auto expected_split_2 = createByteVector(5, 5, 4, 3, 2, 1); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); + CHECK(controller.plan->getContentAsBytes(*splits[1]) == expected_split_2); +} + +TEST_CASE("EndsWithSequence", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "false"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 5, 5, 5); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 1); + + auto expected_split = createByteVector(1, 2, 3, 4); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split); +} + +TEST_CASE("EndsWithSequenceAndKeepSequence", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "true"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(1, 2, 3, 4, 5, 5, 5, 5); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 1); + + auto expected_split_1 = createByteVector(1, 2, 3, 4, 5, 5, 5, 5); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); +} + +TEST_CASE("StartsWithSequence", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "false"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(5, 5, 5, 5, 1, 2, 3, 4); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 1); + + auto expected_split = createByteVector(1, 2, 3, 4); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split); +} + +TEST_CASE("StartsWithSequenceAndKeepSequence", "[NiFi]") { + const auto split_content = std::make_shared<SplitContent>("SplitContent"); + minifi::test::SingleProcessorTestController controller{split_content}; + + split_content->setProperty(SplitContent::KeepByteSequence, "true"); + split_content->setProperty(SplitContent::ByteSequence, "05050505"); + + const auto input_data = createByteVector(5, 5, 5, 5, 1, 2, 3, 4); + std::string_view input(reinterpret_cast<const char*>(input_data.data()), input_data.size()); + + auto trigger_results = controller.trigger(input); + + auto original = trigger_results.at(processors::SplitContent::Original); + auto splits = trigger_results.at(processors::SplitContent::Splits); + + REQUIRE(original.size() == 1); + REQUIRE(splits.size() == 2); + + auto expected_split_1 = createByteVector(5, 5, 5, 5); + auto expected_split_2 = createByteVector(1, 2, 3, 4); + + CHECK(controller.plan->getContentAsBytes(*splits[0]) == expected_split_1); + CHECK(controller.plan->getContentAsBytes(*splits[1]) == expected_split_2); +} + Review Comment: If this was Leading, that would be a single output flow file with (5, 5, 5, 5, 1, 2, 3, 4), right? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
