From 957be31cd79b8823e59a739a473bb151a8c8c72d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 8 Nov 2022 14:45:52 +0100 Subject: [PATCH] T::EvaluateApplyRule(): K. I. S. S. while iterating over a dict to also save locks and malloc(). --- lib/icinga/dependency-apply.cpp | 9 +++++---- lib/icinga/notification-apply.cpp | 9 +++++---- lib/icinga/scheduleddowntime-apply.cpp | 9 +++++---- lib/icinga/service-apply.cpp | 9 +++++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index cd4aff3fc..64a700d7e 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -112,12 +112,13 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di)); Dictionary::Ptr dict = vinstances; + ObjectLock olock (dict); - for (const String& key : dict->GetKeys()) { - frame.Locals->Set(rule.GetFKVar(), key); - frame.Locals->Set(rule.GetFVVar(), dict->Get(key)); + for (auto& kv : dict) { + frame.Locals->Set(rule.GetFKVar(), kv.first); + frame.Locals->Set(rule.GetFVVar(), kv.second); - if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter)) + if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) match = true; } } diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index 51408fb24..24e6f5fbf 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -111,12 +111,13 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di)); Dictionary::Ptr dict = vinstances; + ObjectLock olock (dict); - for (const String& key : dict->GetKeys()) { - frame.Locals->Set(rule.GetFKVar(), key); - frame.Locals->Set(rule.GetFVVar(), dict->Get(key)); + for (auto& kv : dict) { + frame.Locals->Set(rule.GetFKVar(), kv.first); + frame.Locals->Set(rule.GetFVVar(), kv.second); - if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter)) + if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) match = true; } } diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 7aea69ebf..558dcfd86 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -110,12 +110,13 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di)); Dictionary::Ptr dict = vinstances; + ObjectLock olock (dict); - for (const String& key : dict->GetKeys()) { - frame.Locals->Set(rule.GetFKVar(), key); - frame.Locals->Set(rule.GetFVVar(), dict->Get(key)); + for (auto& kv : dict) { + frame.Locals->Set(rule.GetFKVar(), kv.first); + frame.Locals->Set(rule.GetFVVar(), kv.second); - if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter)) + if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) match = true; } } diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index d6d6c1b35..ab472a47a 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -101,12 +101,13 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di)); Dictionary::Ptr dict = vinstances; + ObjectLock olock (dict); - for (const String& key : dict->GetKeys()) { - frame.Locals->Set(rule.GetFKVar(), key); - frame.Locals->Set(rule.GetFVVar(), dict->Get(key)); + for (auto& kv : dict) { + frame.Locals->Set(rule.GetFKVar(), kv.first); + frame.Locals->Set(rule.GetFVVar(), kv.second); - if (EvaluateApplyRuleInstance(host, rule.GetName() + key, frame, rule, skipFilter)) + if (EvaluateApplyRuleInstance(host, rule.GetName() + kv.first, frame, rule, skipFilter)) match = true; } }