martinzink commented on a change in pull request #1188: URL: https://github.com/apache/nifi-minifi-cpp/pull/1188#discussion_r724021041
########## File path: libminifi/include/utils/TextFragmentUtils.h ########## @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#pragma once + +#include <string> +#include "Export.h" + +namespace org::apache::nifi::minifi::utils { +class TextFragmentUtils { Review comment: good idea, however creating a utils namespace to processors would shadow the `org::apache::nifi::minifi::utils` in favor of the `org::apache::nifi::minifi::processors::utils` namespace when referencing the usual way e.g. `utils::Identifier` how about extensions/standard-processors/processors/utils/TextFragmentUtils.h with the `org::apache::nifi::minifi::processors::textfragmentutils` namespace? ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); + } + } + + std::string max_buffer_size_str; + if (context->getProperty(MaxBufferSize.getName(), max_buffer_size_str)) { + uint64_t max_buffer_size = core::DataSizeValue(max_buffer_size_str).getValue(); + if (max_buffer_size > 0) { + buffer_.setMaxSize(max_buffer_size); + logger_->log_trace("The Buffer maximum size is configured to be %d", max_buffer_size); + } + } + + context->getProperty(PatternLoc.getName(), pattern_location_); + + std::string pattern_str; + if (context->getProperty(Pattern.getName(), pattern_str)) { + pattern_ = std::regex(pattern_str); + logger_->log_trace("The Pattern is configured to be %s", pattern_str); + } +} + +int64_t DefragTextFlowFiles::LastPatternFinder::process(const std::shared_ptr<io::BaseStream>& stream) { + if (nullptr == stream) + return 0; + std::vector<uint8_t> buffer; + const auto ret = stream->read(buffer, stream->size()); + if (io::isError(ret)) + return -1; + std::string content(buffer.begin(), buffer.end()); Review comment: Yeah, thats was my original idea as well, but size_t InputStream::read(std::string &str, bool widen) assumes the first bytes read from the stream are the string length, but this is usually not the case. Take a look at `OutputStream::write(const std::string& str, bool widen)` vs `OutputStream::write(const std::vector<uint8_t>& buffer, size_t len)` and `InputStream::read(std::string &str, bool widen)` vs `InputStream::read(std::vector<uint8_t>& buffer, size_t len)` ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); Review comment: changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/27fe80db12728ddd003dc0f52288c3f461bb3280 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); Review comment: Nope, thats a good point removed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/424ff83aad64c9a80c7008f090cc2ff4ece4c14a ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.h ########## @@ -0,0 +1,121 @@ +/** + * 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. + */ + +#pragma once + +#include <regex> +#include <memory> +#include <string> +#include <set> + +#include "core/Processor.h" +#include "utils/FlowFileStore.h" +#include "utils/Enum.h" +#include "serialization/PayloadSerializer.h" + + +namespace org::apache::nifi::minifi::processors { + +class DefragTextFlowFiles : public core::Processor { + public: + explicit DefragTextFlowFiles(const std::string& name, const utils::Identifier& uuid = {}) + : Processor(name, uuid) { + logger_ = logging::LoggerFactory<DefragTextFlowFiles>::getLogger(); + } + EXTENSIONAPI static core::Relationship Self; + EXTENSIONAPI static core::Relationship Success; + EXTENSIONAPI static core::Relationship Original; + EXTENSIONAPI static core::Relationship Failure; + + EXTENSIONAPI static core::Property Pattern; + EXTENSIONAPI static core::Property PatternLoc; + EXTENSIONAPI static core::Property MaxBufferAge; + EXTENSIONAPI static core::Property MaxBufferSize; Review comment: good point, made them const in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d72e19a90d8c596b301ca47d2fb8459848d806d ########## File path: libminifi/include/utils/FlowFileStore.h ########## @@ -0,0 +1,51 @@ +/** + * 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. + */ + +#pragma once + +#include <memory> +#include <unordered_set> +#include <utility> + +#include "core/FlowFile.h" +namespace org::apache::nifi::minifi::utils { + +class FlowFileStore { + public: + std::unordered_set<std::shared_ptr<core::FlowFile>> getNewFlowFiles() { + bool hasNewFlowFiles = true; + if (!has_new_flow_file_.compare_exchange_strong(hasNewFlowFiles, false, std::memory_order_acquire, std::memory_order_relaxed)) { + return {}; + } + std::lock_guard<std::mutex> guard(flow_file_mutex_); + return std::move(incoming_files_); + } + + void put(const std::shared_ptr<core::FlowFile>& flowFile) { + { + std::lock_guard<std::mutex> guard(flow_file_mutex_); + incoming_files_.emplace(std::move(flowFile)); Review comment: I addressed this in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/8c801f80f17dc35684c2def5f4b13aaf1b264f2f ########## File path: libminifi/include/utils/FlowFileStore.h ########## @@ -0,0 +1,51 @@ +/** + * 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. + */ + +#pragma once + +#include <memory> +#include <unordered_set> +#include <utility> + +#include "core/FlowFile.h" +namespace org::apache::nifi::minifi::utils { + +class FlowFileStore { Review comment: good idea changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/8c801f80f17dc35684c2def5f4b13aaf1b264f2f ########## File path: libminifi/include/utils/TextFragmentUtils.h ########## @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#pragma once + +#include <string> +#include "Export.h" + +namespace org::apache::nifi::minifi::utils { +class TextFragmentUtils { + public: + MINIFIAPI static const char *BASE_NAME_ATTRIBUTE; + MINIFIAPI static const char *POST_NAME_ATTRIBUTE; + MINIFIAPI static const char *OFFSET_ATTRIBUTE; Review comment: good idea changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/8c801f80f17dc35684c2def5f4b13aaf1b264f2f ########## File path: libminifi/test/ReadFromFlowFileTestProcessor.h ########## @@ -0,0 +1,60 @@ +/** + * 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 <string> +#include <memory> + +#include "core/Processor.h" +#include "core/ProcessSession.h" +#include "core/Core.h" +#include "core/Resource.h" + +#pragma once + +namespace org::apache::nifi::minifi::processors { + +class ReadFromFlowFileTestProcessor : public core::Processor { + public: + static const std::string OnScheduleLogStr; + static const std::string OnTriggerLogStr; + static const std::string OnUnScheduleLogStr; Review comment: fair enough, made them into inline static constexpr const char* in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/18ca91b7df6d97278af30ecc7537d2f55767071b ########## File path: libminifi/test/ReadFromFlowFileTestProcessor.cpp ########## @@ -0,0 +1,67 @@ +/** + * 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 "ReadFromFlowFileTestProcessor.h" + +#include <string> +#include <utility> +#include <vector> + +namespace org::apache::nifi::minifi::processors { + +const std::string ReadFromFlowFileTestProcessor::OnScheduleLogStr = "ReadFromFlowFileTestProcessor::onSchedule executed"; +const std::string ReadFromFlowFileTestProcessor::OnTriggerLogStr = "ReadFromFlowFileTestProcessor::onTrigger executed"; +const std::string ReadFromFlowFileTestProcessor::OnUnScheduleLogStr = "ReadFromFlowFileTestProcessor::onUnSchedule"; + +core::Relationship ReadFromFlowFileTestProcessor::Success("success", "success operational on the flow record"); + +void ReadFromFlowFileTestProcessor::initialize() { + setSupportedRelationships({ Success }); +} + +void ReadFromFlowFileTestProcessor::onSchedule(core::ProcessContext*, core::ProcessSessionFactory*) { + logger_->log_info("%s", OnScheduleLogStr); +} + +namespace { +struct ReadFlowFileIntoBuffer : public InputStreamCallback { + std::vector<uint8_t> buffer_; + + int64_t process(const std::shared_ptr<io::BaseStream> &stream) override { + size_t bytes_read = stream->read(buffer_, stream->size()); + return io::isError(bytes_read) ? -1 : gsl::narrow<int64_t>(bytes_read); + } +}; Review comment: I will take a look for sure, imo passing a lambda would be so much easier in this trivial case. ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); + } + } + + std::string max_buffer_size_str; + if (context->getProperty(MaxBufferSize.getName(), max_buffer_size_str)) { + uint64_t max_buffer_size = core::DataSizeValue(max_buffer_size_str).getValue(); + if (max_buffer_size > 0) { + buffer_.setMaxSize(max_buffer_size); + logger_->log_trace("The Buffer maximum size is configured to be %d", max_buffer_size); + } + } + + context->getProperty(PatternLoc.getName(), pattern_location_); + + std::string pattern_str; + if (context->getProperty(Pattern.getName(), pattern_str)) { + pattern_ = std::regex(pattern_str); + logger_->log_trace("The Pattern is configured to be %s", pattern_str); + } +} + +int64_t DefragTextFlowFiles::LastPatternFinder::process(const std::shared_ptr<io::BaseStream>& stream) { + if (nullptr == stream) + return 0; + std::vector<uint8_t> buffer; + const auto ret = stream->read(buffer, stream->size()); + if (io::isError(ret)) + return -1; + std::string content(buffer.begin(), buffer.end()); + searchContent(content); + + return 0; +} + +void DefragTextFlowFiles::LastPatternFinder::searchContent(const std::string &content) { + auto matches_begin = std::sregex_iterator(content.begin(), content.end(), pattern_); + auto number_of_matches = std::distance(matches_begin, std::sregex_iterator()); + if (number_of_matches > 0) { + auto last_match = matches_begin; + for (int i = 1; i < number_of_matches; ++i) + last_match++; + last_pattern_location = last_match->position(0); + if (pattern_location_ == PatternLocation::END_OF_MESSAGE) + last_pattern_location.value() += last_match->str(0).size(); Review comment: good idea thanks, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.h ########## @@ -0,0 +1,121 @@ +/** + * 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. + */ + +#pragma once + +#include <regex> +#include <memory> +#include <string> +#include <set> + +#include "core/Processor.h" +#include "utils/FlowFileStore.h" +#include "utils/Enum.h" +#include "serialization/PayloadSerializer.h" + + +namespace org::apache::nifi::minifi::processors { + +class DefragTextFlowFiles : public core::Processor { + public: + explicit DefragTextFlowFiles(const std::string& name, const utils::Identifier& uuid = {}) + : Processor(name, uuid) { + logger_ = logging::LoggerFactory<DefragTextFlowFiles>::getLogger(); + } + EXTENSIONAPI static core::Relationship Self; + EXTENSIONAPI static core::Relationship Success; + EXTENSIONAPI static core::Relationship Original; + EXTENSIONAPI static core::Relationship Failure; + + EXTENSIONAPI static core::Property Pattern; + EXTENSIONAPI static core::Property PatternLoc; + EXTENSIONAPI static core::Property MaxBufferAge; + EXTENSIONAPI static core::Property MaxBufferSize; + + void initialize() override; + void onSchedule(core::ProcessContext* context, core::ProcessSessionFactory* sessionFactory) override; + void onTrigger(core::ProcessContext* context, core::ProcessSession* session) override; + void restore(const std::shared_ptr<core::FlowFile>& flowFile) override; + std::set<std::shared_ptr<core::Connectable>> getOutGoingConnections(const std::string &relationship) const override; + + SMART_ENUM(PatternLocation, + (END_OF_MESSAGE, "End of Message"), + (START_OF_MESSAGE, "Start of Message") + ) + + class LastPatternFinder : public InputStreamCallback { + public: + LastPatternFinder(const std::regex& pattern, PatternLocation pattern_location) : pattern_(pattern), pattern_location_(pattern_location) {} + ~LastPatternFinder() override = default; + int64_t process(const std::shared_ptr<io::BaseStream>& stream) override; + + bool foundPattern() const { return last_pattern_location.has_value(); } Review comment: good idea thanks, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); + } + } + + std::string max_buffer_size_str; + if (context->getProperty(MaxBufferSize.getName(), max_buffer_size_str)) { + uint64_t max_buffer_size = core::DataSizeValue(max_buffer_size_str).getValue(); + if (max_buffer_size > 0) { + buffer_.setMaxSize(max_buffer_size); + logger_->log_trace("The Buffer maximum size is configured to be %d", max_buffer_size); + } + } + + context->getProperty(PatternLoc.getName(), pattern_location_); + + std::string pattern_str; + if (context->getProperty(Pattern.getName(), pattern_str)) { + pattern_ = std::regex(pattern_str); + logger_->log_trace("The Pattern is configured to be %s", pattern_str); + } +} + +int64_t DefragTextFlowFiles::LastPatternFinder::process(const std::shared_ptr<io::BaseStream>& stream) { + if (nullptr == stream) + return 0; + std::vector<uint8_t> buffer; + const auto ret = stream->read(buffer, stream->size()); + if (io::isError(ret)) + return -1; + std::string content(buffer.begin(), buffer.end()); + searchContent(content); + + return 0; +} + +void DefragTextFlowFiles::LastPatternFinder::searchContent(const std::string &content) { + auto matches_begin = std::sregex_iterator(content.begin(), content.end(), pattern_); + auto number_of_matches = std::distance(matches_begin, std::sregex_iterator()); + if (number_of_matches > 0) { + auto last_match = matches_begin; + for (int i = 1; i < number_of_matches; ++i) + last_match++; + last_pattern_location = last_match->position(0); + if (pattern_location_ == PatternLocation::END_OF_MESSAGE) + last_pattern_location.value() += last_match->str(0).size(); + } else { + last_pattern_location = std::nullopt; + } +} + + +void DefragTextFlowFiles::onTrigger(core::ProcessContext*, core::ProcessSession* session) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + auto flowFiles = flow_file_store_.getNewFlowFiles(); + for (auto& file : flowFiles) { + processNextFragment(session, file); + } + std::shared_ptr<core::FlowFile> original_flow_file = session->get(); + processNextFragment(session, original_flow_file); + if (buffer_.maxAgeReached() || buffer_.maxSizeReached()) { + buffer_.flushAndReplace(session, Failure, nullptr); + } +} + +void DefragTextFlowFiles::processNextFragment(core::ProcessSession *session, const std::shared_ptr<core::FlowFile>& next_fragment) { + if (next_fragment) { Review comment: good idea thanks, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); + } + } + + std::string max_buffer_size_str; + if (context->getProperty(MaxBufferSize.getName(), max_buffer_size_str)) { + uint64_t max_buffer_size = core::DataSizeValue(max_buffer_size_str).getValue(); + if (max_buffer_size > 0) { + buffer_.setMaxSize(max_buffer_size); + logger_->log_trace("The Buffer maximum size is configured to be %d", max_buffer_size); + } + } + + context->getProperty(PatternLoc.getName(), pattern_location_); + + std::string pattern_str; + if (context->getProperty(Pattern.getName(), pattern_str)) { + pattern_ = std::regex(pattern_str); + logger_->log_trace("The Pattern is configured to be %s", pattern_str); + } +} + +int64_t DefragTextFlowFiles::LastPatternFinder::process(const std::shared_ptr<io::BaseStream>& stream) { + if (nullptr == stream) + return 0; + std::vector<uint8_t> buffer; + const auto ret = stream->read(buffer, stream->size()); + if (io::isError(ret)) + return -1; + std::string content(buffer.begin(), buffer.end()); + searchContent(content); + + return 0; +} + +void DefragTextFlowFiles::LastPatternFinder::searchContent(const std::string &content) { + auto matches_begin = std::sregex_iterator(content.begin(), content.end(), pattern_); + auto number_of_matches = std::distance(matches_begin, std::sregex_iterator()); + if (number_of_matches > 0) { + auto last_match = matches_begin; + for (int i = 1; i < number_of_matches; ++i) + last_match++; Review comment: good idea thanks, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.h ########## @@ -0,0 +1,121 @@ +/** + * 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. + */ + +#pragma once + +#include <regex> +#include <memory> +#include <string> +#include <set> + +#include "core/Processor.h" +#include "utils/FlowFileStore.h" +#include "utils/Enum.h" +#include "serialization/PayloadSerializer.h" + + +namespace org::apache::nifi::minifi::processors { + +class DefragTextFlowFiles : public core::Processor { + public: + explicit DefragTextFlowFiles(const std::string& name, const utils::Identifier& uuid = {}) + : Processor(name, uuid) { + logger_ = logging::LoggerFactory<DefragTextFlowFiles>::getLogger(); Review comment: changed them in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59#diff-931df1a866cf141d67c5cd42077afee87dd808b00e60791da2d5da211cb7bd93R100 and https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/487cee666964fad15f39dfd0ece7e5cfae519fe0 ########## File path: extensions/standard-processors/processors/DefragTextFlowFiles.cpp ########## @@ -0,0 +1,336 @@ +/** + * 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 "DefragTextFlowFiles.h" + +#include <vector> + +#include "core/Resource.h" +#include "serialization/PayloadSerializer.h" +#include "utils/TextFragmentUtils.h" + + +namespace org::apache::nifi::minifi::processors { + +core::Relationship DefragTextFlowFiles::Success("success", "Flowfiles that have no fragmented messages in them"); +core::Relationship DefragTextFlowFiles::Original("original", "The FlowFiles that were used to create the defragmented flowfiles"); +core::Relationship DefragTextFlowFiles::Failure("failure", "Flowfiles that failed the defragmentation process"); +core::Relationship DefragTextFlowFiles::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +core::Property DefragTextFlowFiles::Pattern( + core::PropertyBuilder::createProperty("Pattern") + ->withDescription("A regular expression to match at the start or end of messages.") + ->withDefaultValue("")->isRequired(true)->build()); + +core::Property DefragTextFlowFiles::PatternLoc( + core::PropertyBuilder::createProperty("Pattern Location")->withDescription("Where to look for the pattern.") + ->withAllowableValues(PatternLocation::values()) + ->withDefaultValue(toString(PatternLocation::START_OF_MESSAGE))->build()); + + +core::Property DefragTextFlowFiles::MaxBufferSize( + core::PropertyBuilder::createProperty("Max Buffer Size") + ->withDescription("The maximum buffer size, if the buffer exceeds this, it will be transferred to failure. Expected format is <size> <data unit>") + ->withType(core::StandardValidators::get().DATA_SIZE_VALIDATOR)->build()); + +core::Property DefragTextFlowFiles::MaxBufferAge( + core::PropertyBuilder::createProperty("Max Buffer Age")-> + withDescription("The maximum age of a buffer after which the buffer will be transferred to failure. Expected format is <duration> <time unit>")->build()); + +void DefragTextFlowFiles::initialize() { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + setSupportedRelationships({Success, Original, Failure}); + setSupportedProperties({Pattern, PatternLoc, MaxBufferAge, MaxBufferSize}); +} + +void DefragTextFlowFiles::onSchedule(core::ProcessContext* context, core::ProcessSessionFactory*) { + std::lock_guard<std::mutex> defrag_lock(defrag_mutex_); + + std::string max_buffer_age_str; + if (context->getProperty(MaxBufferAge.getName(), max_buffer_age_str)) { + core::TimeUnit unit; + uint64_t max_buffer_age; + if (core::Property::StringToTime(max_buffer_age_str, max_buffer_age, unit) && core::Property::ConvertTimeUnitToMS(max_buffer_age, unit, max_buffer_age)) { + buffer_.setMaxAge(max_buffer_age); + logger_->log_trace("The Buffer maximum age is configured to be %d", max_buffer_age); + } + } + + std::string max_buffer_size_str; + if (context->getProperty(MaxBufferSize.getName(), max_buffer_size_str)) { + uint64_t max_buffer_size = core::DataSizeValue(max_buffer_size_str).getValue(); + if (max_buffer_size > 0) { + buffer_.setMaxSize(max_buffer_size); + logger_->log_trace("The Buffer maximum size is configured to be %d", max_buffer_size); + } + } + + context->getProperty(PatternLoc.getName(), pattern_location_); + + std::string pattern_str; + if (context->getProperty(Pattern.getName(), pattern_str)) { + pattern_ = std::regex(pattern_str); + logger_->log_trace("The Pattern is configured to be %s", pattern_str); + } +} + +int64_t DefragTextFlowFiles::LastPatternFinder::process(const std::shared_ptr<io::BaseStream>& stream) { + if (nullptr == stream) + return 0; + std::vector<uint8_t> buffer; + const auto ret = stream->read(buffer, stream->size()); + if (io::isError(ret)) + return -1; + std::string content(buffer.begin(), buffer.end()); Review comment: Thanks I misunderstood it :+1: changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/5d0ae953496ac5710668b1f299efb8e3d2b63d59 for the resize and read version As for the second suggestion there is no current support for string_view in `<regex>` (there is a proposal for it though https://lists.isocpp.org/std-proposals/att-0008/Dxxxx_string_view_support_for_regex.pdf) ########## File path: libminifi/test/ReadFromFlowFileTestProcessor.h ########## @@ -0,0 +1,60 @@ +/** + * 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 <string> +#include <memory> + +#include "core/Processor.h" +#include "core/ProcessSession.h" +#include "core/Core.h" +#include "core/Resource.h" + +#pragma once + +namespace org::apache::nifi::minifi::processors { + +class ReadFromFlowFileTestProcessor : public core::Processor { + public: + static const std::string OnScheduleLogStr; + static const std::string OnTriggerLogStr; + static const std::string OnUnScheduleLogStr; + + explicit ReadFromFlowFileTestProcessor(const std::string& name, const utils::Identifier& uuid = utils::Identifier()) + : Processor(name, uuid), logger_(logging::LoggerFactory<ReadFromFlowFileTestProcessor>::getLogger()) { Review comment: :+1: Added it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/18ca91b7df6d97278af30ecc7537d2f55767071b ########## File path: libminifi/test/ReadFromFlowFileTestProcessor.cpp ########## @@ -0,0 +1,67 @@ +/** + * 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 "ReadFromFlowFileTestProcessor.h" + +#include <string> +#include <utility> +#include <vector> + +namespace org::apache::nifi::minifi::processors { + +const std::string ReadFromFlowFileTestProcessor::OnScheduleLogStr = "ReadFromFlowFileTestProcessor::onSchedule executed"; +const std::string ReadFromFlowFileTestProcessor::OnTriggerLogStr = "ReadFromFlowFileTestProcessor::onTrigger executed"; +const std::string ReadFromFlowFileTestProcessor::OnUnScheduleLogStr = "ReadFromFlowFileTestProcessor::onUnSchedule"; + +core::Relationship ReadFromFlowFileTestProcessor::Success("success", "success operational on the flow record"); + +void ReadFromFlowFileTestProcessor::initialize() { + setSupportedRelationships({ Success }); +} + +void ReadFromFlowFileTestProcessor::onSchedule(core::ProcessContext*, core::ProcessSessionFactory*) { + logger_->log_info("%s", OnScheduleLogStr); +} + +namespace { +struct ReadFlowFileIntoBuffer : public InputStreamCallback { + std::vector<uint8_t> buffer_; + + int64_t process(const std::shared_ptr<io::BaseStream> &stream) override { + size_t bytes_read = stream->read(buffer_, stream->size()); + return io::isError(bytes_read) ? -1 : gsl::narrow<int64_t>(bytes_read); + } +}; +} + +void ReadFromFlowFileTestProcessor::onTrigger(core::ProcessContext* context, core::ProcessSession* session) { + logger_->log_info("%s", OnTriggerLogStr); Review comment: :+1: Added it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/18ca91b7df6d97278af30ecc7537d2f55767071b ########## File path: libminifi/test/WriteToFlowFileTestProcessor.cpp ########## @@ -0,0 +1,74 @@ +/** + * 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 "WriteToFlowFileTestProcessor.h" + +#include <string> +#include <vector> + +namespace org::apache::nifi::minifi::processors { + +const std::string WriteToFlowFileTestProcessor::OnScheduleLogStr = "WriteToFlowFileTestProcessor::onSchedule executed"; +const std::string WriteToFlowFileTestProcessor::OnTriggerLogStr = "WriteToFlowFileTestProcessor::onTrigger executed"; +const std::string WriteToFlowFileTestProcessor::OnUnScheduleLogStr = "WriteToFlowFileTestProcessor::onUnSchedule"; + +core::Relationship WriteToFlowFileTestProcessor::Success("success", "success operational on the flow record"); + + +void WriteToFlowFileTestProcessor::initialize() { + setSupportedProperties({}); + setSupportedRelationships({Success}); +} + +void WriteToFlowFileTestProcessor::onSchedule(core::ProcessContext*, core::ProcessSessionFactory*) { + logger_->log_info("%s", OnScheduleLogStr); +} + +namespace { +struct WriteBufferToFlowFile : public OutputStreamCallback { + const std::vector<uint8_t> buffer_; + + explicit WriteBufferToFlowFile(const std::string& buffer) : buffer_(buffer.begin(), buffer.end()) {} Review comment: good idea, changed it in https://github.com/apache/nifi-minifi-cpp/commit/18ca91b7df6d97278af30ecc7537d2f55767071b ########## File path: libminifi/test/ReadFromFlowFileTestProcessor.h ########## @@ -0,0 +1,60 @@ +/** + * 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 <string> +#include <memory> + +#include "core/Processor.h" +#include "core/ProcessSession.h" +#include "core/Core.h" +#include "core/Resource.h" + +#pragma once + +namespace org::apache::nifi::minifi::processors { + +class ReadFromFlowFileTestProcessor : public core::Processor { + public: + static const std::string OnScheduleLogStr; + static const std::string OnTriggerLogStr; + static const std::string OnUnScheduleLogStr; + + explicit ReadFromFlowFileTestProcessor(const std::string& name, const utils::Identifier& uuid = utils::Identifier()) + : Processor(name, uuid), logger_(logging::LoggerFactory<ReadFromFlowFileTestProcessor>::getLogger()) { + } + + static constexpr char const* ProcessorName = "ReadFromFlowFileTestProcessor"; + static core::Relationship Success; + + public: + void onSchedule(core::ProcessContext *context, core::ProcessSessionFactory *sessionFactory) override; + void onTrigger(core::ProcessContext *context, core::ProcessSession *session) override; + void initialize() override; + void onUnSchedule() override; + + const std::string& getContent() { + return content_; + } + + private: + std::shared_ptr<logging::Logger> logger_; + std::string content_; +}; + +REGISTER_RESOURCE(ReadFromFlowFileTestProcessor, "ReadFromFlowFileTestProcessor (only for testing purposes)"); Review comment: moved it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/12da801959219f8a2dd662b47e16935e50c44be4 ########## File path: libminifi/include/utils/TextFragmentUtils.h ########## @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#pragma once + +#include <string> +#include "Export.h" + +namespace org::apache::nifi::minifi::utils { +class TextFragmentUtils { + public: + MINIFIAPI static const char *BASE_NAME_ATTRIBUTE; + MINIFIAPI static const char *POST_NAME_ATTRIBUTE; + MINIFIAPI static const char *OFFSET_ATTRIBUTE; Review comment: whoops sorry my bad, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/221944d3b728a10d4ccd9b68e3fcce2b8fb7b4e0 ########## File path: libminifi/test/WriteToFlowFileTestProcessor.cpp ########## @@ -0,0 +1,74 @@ +/** + * 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 "WriteToFlowFileTestProcessor.h" + +#include <string> +#include <vector> + +#include <gsl-lite/gsl-lite.hpp> Review comment: I see, I'll keep this in mind :+1: https://github.com/apache/nifi-minifi-cpp/pull/1188/commits/cb31e7e8c6b24f36dd140c6622c87cc0b0131d0d -- 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]
