Skip to content

Commit

Permalink
Avoid evaluating the same filter twice for the same target
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 2, 2022
1 parent a698b9c commit 9250d88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
24 changes: 12 additions & 12 deletions lib/config/applyrule-targeted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ const std::vector<ApplyRule::Ptr>& ApplyRule::GetTargetedServiceRules(const Type
bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, ApplyRule::PerSourceType& rules)
{
if (targetType == "Host") {
std::vector<const String *> hosts;
std::set<String> hosts;

if (GetTargetHosts(rule->m_Filter.get(), hosts)) {
for (auto host : hosts) {
rules.Targeted[*host].ForHost.emplace_back(rule);
for (const auto& host : hosts) {
rules.Targeted[host].ForHost.emplace_back(rule);
}

return true;
}
} else if (targetType == "Service") {
std::vector<std::pair<const String *, const String *>> services;
std::set<std::pair<const String, const String>> services;

if (GetTargetServices(rule->m_Filter.get(), services)) {
for (auto service : services) {
rules.Targeted[*service.first].ForServices[*service.second].emplace_back(rule);
for (const auto& service : services) {
rules.Targeted[service.first].ForServices[service.second].emplace_back(rule);
}

return true;
Expand All @@ -88,15 +88,15 @@ bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& target
}

/**
* If the given assign filter is like the following, extract the host names ("H", "h", ...) into the vector:
* If the given assign filter is like the following, extract the host names ("H", "h", ...) into the set:
*
* host.name == "H" [ || host.name == "h" ... ]
*
* The order of operands of || == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts)
bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::set<String>& hosts)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));

Expand All @@ -108,23 +108,23 @@ bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector<const Strin
auto name (GetComparedName(assignFilter, "host"));

if (name) {
hosts.emplace_back(name);
hosts.emplace(*name);
return true;
}

return false;
}

/**
* If the given assign filter is like the following, extract the host+service names ("H"+"S", "h"+"s", ...) into the vector:
* If the given assign filter is like the following, extract the host+service names ("H"+"S", "h"+"s", ...) into the set:
*
* host.name == "H" && service.name == "S" [ || host.name == "h" && service.name == "s" ... ]
*
* The order of operands of || && == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services)
bool ApplyRule::GetTargetServices(Expression* assignFilter, std::set<std::pair<const String, const String>>& services)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));

Expand All @@ -136,7 +136,7 @@ bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector<std::pai
auto service (GetTargetService(assignFilter));

if (service.first) {
services.emplace_back(service);
services.emplace(std::make_pair(*service.first, *service.second));
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/config/applyrule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class ApplyRule : public SharedObject
/*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForHost
* contains all apply rules like apply T "x" to Host { ... }
* which target only specific hosts incl. "H", e. g. via
* which target only specific hosts incl. "H", e.g. via
* assign where host.name == "H" || host.name == "h".
*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForServices["S"]
* contains all apply rules like apply T "x" to Service { ... }
* which target only specific services on specific hosts incl. "H!S",
* e. g. via assign where host.name == "H" && service.name == "S".
* which target only specific services on specific hosts,
* e.g. via assign where host.name == "H" && service.name == "S".
*
* m_Rules[T::TypeInstance.get()].Regular[C::TypeInstance.get()]
* contains all other apply rules like apply T "x" to C { ... }.
Expand Down Expand Up @@ -98,8 +98,8 @@ class ApplyRule : public SharedObject
static RuleMap m_Rules;

static bool AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, PerSourceType& rules);
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 GetTargetHosts(Expression* assignFilter, std::set<String>& hosts);
static bool GetTargetServices(Expression* assignFilter, std::set<std::pair<const String, const String>>& services);
static std::pair<const String *, const String *> GetTargetService(Expression* assignFilter);
static const String * GetComparedName(Expression* assignFilter, const char * lcType);
static bool IsNameIndexer(Expression* exp, const char * lcType);
Expand Down

0 comments on commit 9250d88

Please sign in to comment.