Unify storages of regular/targeted apply rules: std::vector<ApplyRule::Ptr>

This commit is contained in:
Alexander A. Klimov 2022-10-27 16:54:09 +02:00
parent a56ad38ad3
commit 038a5e8ef6
7 changed files with 30 additions and 34 deletions

View File

@ -68,16 +68,14 @@ const std::vector<ApplyRule::Ptr>& ApplyRule::GetTargetedServiceRules(const Type
* *
* @returns Whether the rule has been added to the "index". * @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") { if (targetType == "Host") {
std::vector<const String *> hosts; std::vector<const String *> hosts;
if (GetTargetHosts(rule.m_Filter.get(), hosts)) { if (GetTargetHosts(rule->m_Filter.get(), hosts)) {
ApplyRule::Ptr sharedRule = new ApplyRule(std::move(rule));
for (auto host : hosts) { for (auto host : hosts) {
rules.Targeted[*host].ForHost.emplace_back(sharedRule); rules.Targeted[*host].ForHost.emplace_back(rule);
} }
return true; return true;
@ -85,11 +83,9 @@ bool ApplyRule::AddTargetedRule(ApplyRule& rule, const String& sourceType, const
} else if (targetType == "Service") { } else if (targetType == "Service") {
std::vector<std::pair<const String *, const String *>> services; std::vector<std::pair<const String *, const String *>> services;
if (GetTargetServices(rule.m_Filter.get(), services)) { if (GetTargetServices(rule->m_Filter.get(), services)) {
ApplyRule::Ptr sharedRule = new ApplyRule(std::move(rule));
for (auto service : 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; return true;

View File

@ -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()]); auto& rules (m_Rules[Type::GetByName(sourceType).get()][Type::GetByName(*actualTargetType).get()]);
if (!AddTargetedRule(rule, sourceType, *actualTargetType, rules)) { if (!AddTargetedRule(rule, sourceType, *actualTargetType, rules)) {
@ -144,7 +144,7 @@ bool ApplyRule::HasMatches() const
return m_HasMatches; return m_HasMatches;
} }
std::vector<ApplyRule>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType) const std::vector<ApplyRule::Ptr>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType)
{ {
auto perSourceType (m_Rules.find(sourceType.get())); auto perSourceType (m_Rules.find(sourceType.get()));
@ -156,7 +156,7 @@ std::vector<ApplyRule>& ApplyRule::GetRules(const Type::Ptr& sourceType, const T
} }
} }
static std::vector<ApplyRule> noRules; static const std::vector<ApplyRule::Ptr> noRules;
return noRules; return noRules;
} }
@ -183,17 +183,17 @@ void ApplyRule::CheckMatches(bool silent)
} }
for (auto rule : targeted) { 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") 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!"; << sourceType->GetName() << "' does not match anywhere!";
} }
} }

View File

@ -29,7 +29,7 @@ public:
struct PerTypes struct PerTypes
{ {
std::vector<ApplyRule> Regular; std::vector<ApplyRule::Ptr> Regular;
std::unordered_map<String /* host */, PerHost> Targeted; std::unordered_map<String /* host */, PerHost> Targeted;
}; };
@ -69,7 +69,7 @@ public:
static void AddRule(const String& sourceType, const String& targetType, const String& name, const Expression::Ptr& expression, 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, 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); bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope);
static std::vector<ApplyRule>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType); static const std::vector<ApplyRule::Ptr>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType);
static const std::vector<ApplyRule::Ptr>& GetTargetedHostRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host); static const std::vector<ApplyRule::Ptr>& GetTargetedHostRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host);
static const std::vector<ApplyRule::Ptr>& GetTargetedServiceRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host, const String& service); static const std::vector<ApplyRule::Ptr>& GetTargetedServiceRules(const Type::Ptr& sourceType, const Type::Ptr& targetType, const String& host, const String& service);
@ -79,7 +79,7 @@ public:
static const std::vector<String>& GetTargetTypes(const String& sourceType); static const std::vector<String>& GetTargetTypes(const String& sourceType);
static void CheckMatches(bool silent); 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: private:
String m_Name; String m_Name;
@ -97,7 +97,7 @@ private:
static TypeMap m_Types; static TypeMap m_Types;
static RuleMap m_Rules; 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<const String *>& hosts); static bool GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts);
static bool GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services); static bool GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services);
static std::pair<const String *, const String *> GetTargetService(Expression* assignFilter); static std::pair<const String *, const String *> GetTargetService(Expression* assignFilter);

View File

@ -137,8 +137,8 @@ void Dependency::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Host::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRule(host, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedHostRules(Dependency::TypeInstance, Host::TypeInstance, host->GetName())) { 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() + "'"); CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Service::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule)) if (EvaluateApplyRule(service, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedServiceRules(Dependency::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { for (auto& rule : ApplyRule::GetTargetedServiceRules(Dependency::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) {

View File

@ -137,8 +137,8 @@ void Notification::EvaluateApplyRules(const Host::Ptr& host)
for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Host::TypeInstance)) for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Host::TypeInstance))
{ {
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRule(host, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedHostRules(Notification::TypeInstance, Host::TypeInstance, host->GetName())) { 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() + "'"); CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Service::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule)) if (EvaluateApplyRule(service, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedServiceRules(Notification::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { for (auto& rule : ApplyRule::GetTargetedServiceRules(Notification::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) {

View File

@ -135,8 +135,8 @@ void ScheduledDowntime::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Host::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRule(host, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedHostRules(ScheduledDowntime::TypeInstance, Host::TypeInstance, host->GetName())) { 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() + "'"); CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Service::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule)) if (EvaluateApplyRule(service, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedServiceRules(ScheduledDowntime::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) { for (auto& rule : ApplyRule::GetTargetedServiceRules(ScheduledDowntime::TypeInstance, Service::TypeInstance, service->GetHost()->GetName(), service->GetName())) {

View File

@ -124,8 +124,8 @@ void Service::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Service::TypeInstance, Host::TypeInstance)) { for (auto& rule : ApplyRule::GetRules(Service::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRule(host, *rule))
rule.AddMatch(); rule->AddMatch();
} }
for (auto& rule : ApplyRule::GetTargetedHostRules(Service::TypeInstance, Host::TypeInstance, host->GetName())) { for (auto& rule : ApplyRule::GetTargetedHostRules(Service::TypeInstance, Host::TypeInstance, host->GetName())) {