lordgamez commented on code in PR #2221:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2221#discussion_r3795339730


##########
extensions/standard-processors/processors/JoinEnrichmentAttributes.cpp:
##########
@@ -0,0 +1,160 @@
+/**
+ * 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 "JoinEnrichmentAttributes.h"
+
+#include "core/Resource.h"
+#include "minifi-cpp/core/ProcessSession.h"
+#include "utils/AttributeErrors.h"
+#include "utils/EnrichmentUtils.h"
+#include "utils/ProcessorConfigUtils.h"
+
+namespace org::apache::nifi::minifi::standard {
+const core::Relationship JoinEnrichmentAttributes::Self("__self__", "Marks the 
FlowFile to be owned by this processor");
+
+void JoinEnrichmentAttributes::initialize() {
+  setSupportedProperties(Properties);
+  setSupportedRelationships(Relationships);
+  ProcessorImpl::initialize();
+}
+
+void JoinEnrichmentAttributes::onSchedule(core::ProcessContext& context, 
core::ProcessSessionFactory& session_factory) {
+  using namespace std::literals::chrono_literals;
+  if (const auto timeout = utils::parseOptionalDurationProperty(context, 
TimeoutProperty); timeout && *timeout > 0ms) {
+    time_out_tracker_.emplace(*timeout);
+  }
+  max_batch_size_ = utils::parseOptionalU64Property(context, MaxBatchSize);
+  if (max_batch_size_ && *max_batch_size_ == 0) {
+    throw Exception(PROCESSOR_EXCEPTION, "Max Batch Size property is invalid");
+  }
+  ProcessorImpl::onSchedule(context, session_factory);
+}
+
+namespace {
+bool checkRequiredAttributes(const core::FlowFile& flow_file) {
+  return flow_file.getAttribute(ENRICHMENT_ROLE).has_value() && 
flow_file.getAttribute(ENRICHMENT_GROUP_ID).has_value();
+}
+}  // namespace
+
+void JoinEnrichmentAttributes::join(const std::shared_ptr<core::FlowFile>& 
original, const std::shared_ptr<core::FlowFile>& enrichment,
+    core::ProcessSession& session) const {
+  const auto cloned = session.clone(*original);
+  for (const auto& [k, v] : enrichment->getAttributes()) {
+    if (k != ENRICHMENT_ROLE) {
+      cloned->setAttribute(k, v);
+    }
+  }
+  cloned->setAttribute(ENRICHMENT_ROLE, "JOINED");
+  if (!std::ranges::contains(session_flow_files_, original->getUUID())) {
+    session.add(original);
+  }
+  if (!std::ranges::contains(session_flow_files_, enrichment->getUUID())) {
+    session.add(enrichment);
+  }
+  session.transfer(original, Original);
+  session.transfer(enrichment, Original);
+  session.transfer(cloned, Joined);
+}
+
+void JoinEnrichmentAttributes::handleFlowFile(std::shared_ptr<core::FlowFile> 
flow_file, core::ProcessSession& session,
+    const std::chrono::steady_clock::time_point current_time) {
+  if (!checkRequiredAttributes(*flow_file)) {
+    logger_->log_warn("{} is missing enrichment.group.id and/or 
enrichment.role, routing it to Invalid", flow_file->getId());
+    session.transfer(flow_file, Invalid);
+    return;
+  }
+
+  const auto role = flow_file->getAttribute(ENRICHMENT_ROLE) | 
utils::toExpected(make_error_code(core::AttributeErrorCode::MissingAttribute)) |
+      utils::andThen(parsing::parseEnum<EnrichmentRole>);
+  if (!role) {
+    logger_->log_warn("{} has invalid role due to {}", flow_file->getId(), 
role.error());
+    session.transfer(flow_file, Invalid);
+    return;
+  }
+
+  std::string group_id = *(flow_file->getAttribute(ENRICHMENT_GROUP_ID));
+
+  auto& my_map = role == EnrichmentRole::ENRICHMENT ? enrichments_ : 
originals_;
+  auto& pair_map = role == EnrichmentRole::ENRICHMENT ? originals_ : 
enrichments_;
+
+  if (const auto previous_node = my_map.extract(group_id)) {
+    logger_->log_warn("Encountered duplicate {} for {}, routing both to 
Invalid", magic_enum::enum_name(*role), group_id);
+    session.transfer(flow_file, Invalid);
+    session.transfer(previous_node.mapped(), Invalid);
+    if (!std::ranges::contains(session_flow_files_, 
previous_node.mapped()->getUUID())) {
+      session.add(previous_node.mapped());
+    }
+    return;
+  }
+
+  if (const auto pair_node = pair_map.extract(group_id)) {
+    logger_->log_trace("Match found");

Review Comment:
   We could log the group id to see which group id the match was found for.



##########
extensions/standard-processors/processors/JoinEnrichmentAttributes.h:
##########
@@ -0,0 +1,139 @@
+/**
+ * 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 <string>
+
+#include "core/FlowFileStore.h"
+#include "core/ProcessorImpl.h"
+#include "core/PropertyDefinitionBuilder.h"
+#include "minifi-cpp/core/PropertyDefinition.h"
+#include "utils/Enum.h"
+#include "utils/RegexUtils.h"
+
+namespace org::apache::nifi::minifi::standard {
+
+namespace join_enrichment_attributes {
+class TimeOutTracker {
+ public:
+  explicit TimeOutTracker(std::chrono::steady_clock::duration timeout) : 
time_out_(timeout) {
+  }
+  TimeOutTracker(const TimeOutTracker&) = delete;
+  TimeOutTracker& operator=(const TimeOutTracker&) = delete;
+  TimeOutTracker(TimeOutTracker&&) = delete;
+  TimeOutTracker& operator=(TimeOutTracker&&) = delete;
+  ~TimeOutTracker() = default;
+
+  void track(std::string id, std::chrono::steady_clock::time_point timestamp) {
+    queue_.emplace_back(timestamp, std::move(id));
+  }
+
+  std::vector<std::string> 
getTimedOutFlowFiles(std::chrono::steady_clock::time_point current_time) {
+    std::vector<std::string> result;
+    // Even with 0 time_out_, we won't return just added FlowFiles
+    while (!queue_.empty() && queue_.front().timestamp + time_out_ < 
current_time) {
+      result.push_back(std::move(queue_.front().group_name));
+      queue_.pop_front();
+    }
+    return result;
+  }
+
+ private:
+  struct TimeStampedGroup {
+    std::chrono::steady_clock::time_point timestamp;
+    std::string group_name;
+  };
+
+  std::chrono::steady_clock::duration time_out_;
+  std::deque<TimeStampedGroup> queue_;
+};
+}  // namespace join_enrichment_attributes
+
+using MapType = std::unordered_map<std::string, 
std::shared_ptr<core::FlowFile>, utils::string::transparent_string_hash, 
std::equal_to<>>;

Review Comment:
   This could be renamed to be a bit more descriptive.



##########
extensions/standard-processors/processors/JoinEnrichmentAttributes.cpp:
##########
@@ -0,0 +1,160 @@
+/**
+ * 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 "JoinEnrichmentAttributes.h"
+
+#include "core/Resource.h"
+#include "minifi-cpp/core/ProcessSession.h"
+#include "utils/AttributeErrors.h"
+#include "utils/EnrichmentUtils.h"
+#include "utils/ProcessorConfigUtils.h"
+
+namespace org::apache::nifi::minifi::standard {
+const core::Relationship JoinEnrichmentAttributes::Self("__self__", "Marks the 
FlowFile to be owned by this processor");
+
+void JoinEnrichmentAttributes::initialize() {
+  setSupportedProperties(Properties);
+  setSupportedRelationships(Relationships);
+  ProcessorImpl::initialize();
+}
+
+void JoinEnrichmentAttributes::onSchedule(core::ProcessContext& context, 
core::ProcessSessionFactory& session_factory) {
+  using namespace std::literals::chrono_literals;
+  if (const auto timeout = utils::parseOptionalDurationProperty(context, 
TimeoutProperty); timeout && *timeout > 0ms) {
+    time_out_tracker_.emplace(*timeout);
+  }
+  max_batch_size_ = utils::parseOptionalU64Property(context, MaxBatchSize);
+  if (max_batch_size_ && *max_batch_size_ == 0) {
+    throw Exception(PROCESSOR_EXCEPTION, "Max Batch Size property is invalid");
+  }
+  ProcessorImpl::onSchedule(context, session_factory);
+}
+
+namespace {
+bool checkRequiredAttributes(const core::FlowFile& flow_file) {
+  return flow_file.getAttribute(ENRICHMENT_ROLE).has_value() && 
flow_file.getAttribute(ENRICHMENT_GROUP_ID).has_value();
+}
+}  // namespace
+
+void JoinEnrichmentAttributes::join(const std::shared_ptr<core::FlowFile>& 
original, const std::shared_ptr<core::FlowFile>& enrichment,
+    core::ProcessSession& session) const {
+  const auto cloned = session.clone(*original);
+  for (const auto& [k, v] : enrichment->getAttributes()) {
+    if (k != ENRICHMENT_ROLE) {
+      cloned->setAttribute(k, v);
+    }
+  }
+  cloned->setAttribute(ENRICHMENT_ROLE, "JOINED");
+  if (!std::ranges::contains(session_flow_files_, original->getUUID())) {
+    session.add(original);

Review Comment:
   Why is session.add needed here, but not when transferring flow files to 
Invalid or Self relationships?



-- 
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]

Reply via email to