fgerlits commented on code in PR #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098#discussion_r2960984472


##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,93 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) 
const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, 
const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, 
ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, 
ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if 
(process_group->findControllerService(processor_or_controller_uuid.to_string(), 
ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if 
(entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(),
 ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const 
std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, 
std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  std::scoped_lock lock(mutex_);
+  if (id.empty() || controller_service_node == nullptr || 
alternative_keys.contains(id)) {
     return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  controller_service_nodes_[id] = serviceNode;
-  return true;
+  }
+  auto [_it, success] = services_.emplace(std::move(id), 
ServiceEntry{.controller_service_node = std::move(controller_service_node), 
.parent_group = parent_group});
+  return success;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, ProcessGroup* 
process_group) {
-  if (id.empty() || process_group == nullptr)
-    return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  process_groups_.emplace(id, gsl::make_not_null(process_group));
-  return true;
-}
 
 void ControllerServiceNodeMap::clear() {
-  std::lock_guard<std::mutex> lock(mutex_);
-  for (const auto& [id, node] : controller_service_nodes_) {
-    node->disable();
+  std::scoped_lock lock(mutex_);
+  for (const auto& node: services_ | std::views::values) {
+    node.controller_service_node->disable();

Review Comment:
   can we call this `service_entry`, please?
   ```suggestion
     for (const auto& service_entry : services_ | std::views::values) {
       service_entry.controller_service_node->disable();
   ```



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