From 038a5e8ef6134c0e9483f0cf2975d6f6212f67cd Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 27 Oct 2022 16:54:09 +0200 Subject: [PATCH] Unify storages of regular/targeted apply rules: std::vector --- lib/config/applyrule-targeted.cpp | 14 +++++--------- lib/config/applyrule.cpp | 14 +++++++------- lib/config/applyrule.hpp | 8 ++++---- lib/icinga/dependency-apply.cpp | 8 ++++---- lib/icinga/notification-apply.cpp | 8 ++++---- lib/icinga/scheduleddowntime-apply.cpp | 8 ++++---- lib/icinga/service-apply.cpp | 4 ++-- 7 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/config/applyrule-targeted.cpp b/lib/config/applyrule-targeted.cpp index 1a2ce564e..f38f81f21 100644 --- a/lib/config/applyrule-targeted.cpp +++ b/lib/config/applyrule-targeted.cpp @@ -68,16 +68,14 @@ const std::vector& ApplyRule::GetTargetedServiceRules(const Type * * @returns Whether the rule has been added to the "index". */ -bool ApplyRule::AddTargetedRule(ApplyRule& rule, const String& sourceType, const String& targetType, ApplyRule::PerTypes& rules) +bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& sourceType, const String& targetType, ApplyRule::PerTypes& rules) { if (targetType == "Host") { std::vector hosts; - if (GetTargetHosts(rule.m_Filter.get(), hosts)) { - ApplyRule::Ptr sharedRule = new ApplyRule(std::move(rule)); - + if (GetTargetHosts(rule->m_Filter.get(), hosts)) { for (auto host : hosts) { - rules.Targeted[*host].ForHost.emplace_back(sharedRule); + rules.Targeted[*host].ForHost.emplace_back(rule); } return true; @@ -85,11 +83,9 @@ bool ApplyRule::AddTargetedRule(ApplyRule& rule, const String& sourceType, const } else if (targetType == "Service") { std::vector> services; - if (GetTargetServices(rule.m_Filter.get(), services)) { - ApplyRule::Ptr sharedRule = new ApplyRule(std::move(rule)); - + if (GetTargetServices(rule->m_Filter.get(), services)) { for (auto service : services) { - rules.Targeted[*service.first].ForServices[*service.second].emplace_back(sharedRule); + rules.Targeted[*service.first].ForServices[*service.second].emplace_back(rule); } return true; diff --git a/lib/config/applyrule.cpp b/lib/config/applyrule.cpp index cd268269e..f56d7eb02 100644 --- a/lib/config/applyrule.cpp +++ b/lib/config/applyrule.cpp @@ -81,7 +81,7 @@ void ApplyRule::AddRule(const String& sourceType, const String& targetType, cons } } - ApplyRule rule (name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope); + ApplyRule::Ptr rule = new ApplyRule(name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope); auto& rules (m_Rules[Type::GetByName(sourceType).get()][Type::GetByName(*actualTargetType).get()]); if (!AddTargetedRule(rule, sourceType, *actualTargetType, rules)) { @@ -144,7 +144,7 @@ bool ApplyRule::HasMatches() const return m_HasMatches; } -std::vector& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType) +const std::vector& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType) { auto perSourceType (m_Rules.find(sourceType.get())); @@ -156,7 +156,7 @@ std::vector& ApplyRule::GetRules(const Type::Ptr& sourceType, const T } } - static std::vector noRules; + static const std::vector noRules; return noRules; } @@ -183,17 +183,17 @@ void ApplyRule::CheckMatches(bool silent) } for (auto rule : targeted) { - CheckMatches(*rule, perSourceType.first, silent); + CheckMatches(rule, perSourceType.first, silent); } } } } -void ApplyRule::CheckMatches(const ApplyRule& rule, Type* sourceType, bool silent) +void ApplyRule::CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent) { - if (!rule.HasMatches() && !silent) { + if (!rule->HasMatches() && !silent) { Log(LogWarning, "ApplyRule") - << "Apply rule '" << rule.GetName() << "' (" << rule.GetDebugInfo() << ") for type '" + << "Apply rule '" << rule->GetName() << "' (" << rule->GetDebugInfo() << ") for type '" << sourceType->GetName() << "' does not match anywhere!"; } } diff --git a/lib/config/applyrule.hpp b/lib/config/applyrule.hpp index 37ec04cfd..49f964b50 100644 --- a/lib/config/applyrule.hpp +++ b/lib/config/applyrule.hpp @@ -29,7 +29,7 @@ public: struct PerTypes { - std::vector Regular; + std::vector Regular; std::unordered_map Targeted; }; @@ -69,7 +69,7 @@ public: static void AddRule(const String& sourceType, const String& targetType, const String& name, const Expression::Ptr& expression, const Expression::Ptr& filter, const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm, bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope); - static std::vector& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType); + static const std::vector& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType); static const std::vector& GetTargetedHostRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host); static const std::vector& GetTargetedServiceRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host, const String& service); @@ -79,7 +79,7 @@ public: static const std::vector& GetTargetTypes(const String& sourceType); static void CheckMatches(bool silent); - static void CheckMatches(const ApplyRule& rule, Type* sourceType, bool silent); + static void CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent); private: String m_Name; @@ -97,7 +97,7 @@ private: static TypeMap m_Types; static RuleMap m_Rules; - static bool AddTargetedRule(ApplyRule& rule, const String& sourceType, const String& targetType, ApplyRule::PerTypes& rules); + static bool AddTargetedRule(const ApplyRule::Ptr& rule, const String& sourceType, const String& targetType, ApplyRule::PerTypes& rules); static bool GetTargetHosts(Expression* assignFilter, std::vector& hosts); static bool GetTargetServices(Expression* assignFilter, std::vector>& services); static std::pair GetTargetService(Expression* assignFilter); diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index 7b9503fb9..2ad18d83e 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -137,8 +137,8 @@ void Dependency::EvaluateApplyRules(const Host::Ptr& host) CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Host::TypeInstance)) { - if (EvaluateApplyRule(host, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(host, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedHostRules(Dependency::TypeInstance, Host::TypeInstance, host->GetName())) { @@ -152,8 +152,8 @@ void Dependency::EvaluateApplyRules(const Service::Ptr& service) CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Service::TypeInstance)) { - if (EvaluateApplyRule(service, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(service, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedServiceRules(Dependency::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index d5d3d4870..177e44700 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -137,8 +137,8 @@ void Notification::EvaluateApplyRules(const Host::Ptr& host) for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Host::TypeInstance)) { - if (EvaluateApplyRule(host, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(host, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedHostRules(Notification::TypeInstance, Host::TypeInstance, host->GetName())) { @@ -152,8 +152,8 @@ void Notification::EvaluateApplyRules(const Service::Ptr& service) CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Service::TypeInstance)) { - if (EvaluateApplyRule(service, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(service, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedServiceRules(Notification::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 893c42aca..ad11a4b4e 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -135,8 +135,8 @@ void ScheduledDowntime::EvaluateApplyRules(const Host::Ptr& host) CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Host::TypeInstance)) { - if (EvaluateApplyRule(host, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(host, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedHostRules(ScheduledDowntime::TypeInstance, Host::TypeInstance, host->GetName())) { @@ -150,8 +150,8 @@ void ScheduledDowntime::EvaluateApplyRules(const Service::Ptr& service) CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Service::TypeInstance)) { - if (EvaluateApplyRule(service, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(service, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedServiceRules(ScheduledDowntime::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index e14342948..4fa878743 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -124,8 +124,8 @@ void Service::EvaluateApplyRules(const Host::Ptr& host) CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); for (auto& rule : ApplyRule::GetRules(Service::TypeInstance, Host::TypeInstance)) { - if (EvaluateApplyRule(host, rule)) - rule.AddMatch(); + if (EvaluateApplyRule(host, *rule)) + rule->AddMatch(); } for (auto& rule : ApplyRule::GetTargetedHostRules(Service::TypeInstance, Host::TypeInstance, host->GetName())) {