Skip to content

Commit

Permalink
DependencyGraph: use ConfigObject*, not Object*
Browse files Browse the repository at this point in the history
This saves dynamic_cast<ConfigObject*> + if() on every item of GetChildren().
  • Loading branch information
Al2Klimov committed Dec 16, 2024
1 parent 188ba53 commit 6db2662
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
13 changes: 6 additions & 7 deletions lib/base/dependencygraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using namespace icinga;

std::mutex DependencyGraph::m_Mutex;
std::map<Object *, std::map<Object *, int> > DependencyGraph::m_Dependencies;
std::map<ConfigObject*, std::map<ConfigObject*, int>> DependencyGraph::m_Dependencies;

void DependencyGraph::AddDependency(Object* child, Object* parent)
void DependencyGraph::AddDependency(ConfigObject* child, ConfigObject* parent)
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Dependencies[parent][child]++;
}

void DependencyGraph::RemoveDependency(Object* child, Object* parent)
void DependencyGraph::RemoveDependency(ConfigObject* child, ConfigObject* parent)
{
std::unique_lock<std::mutex> lock(m_Mutex);

Expand All @@ -32,16 +32,15 @@ void DependencyGraph::RemoveDependency(Object* child, Object* parent)
m_Dependencies.erase(parent);
}

std::vector<Object::Ptr> DependencyGraph::GetChildren(const Object::Ptr& parent)
std::vector<ConfigObject::Ptr> DependencyGraph::GetChildren(const ConfigObject::Ptr& parent)
{
std::vector<Object::Ptr> objects;
std::vector<ConfigObject::Ptr> objects;

std::unique_lock<std::mutex> lock(m_Mutex);
auto it = m_Dependencies.find(parent.get());

if (it != m_Dependencies.end()) {
typedef std::pair<Object *, int> kv_pair;
for (const kv_pair& kv : it->second) {
for (auto& kv : it->second) {
objects.emplace_back(kv.first);
}
}
Expand Down
20 changes: 15 additions & 5 deletions lib/base/dependencygraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define DEPENDENCYGRAPH_H

#include "base/i2-base.hpp"
#include "base/object.hpp"
#include "base/configobject.hpp"
#include <map>
#include <mutex>

Expand All @@ -18,15 +18,25 @@ namespace icinga {
class DependencyGraph
{
public:
static void AddDependency(Object* child, Object* parent);
static void RemoveDependency(Object* child, Object* parent);
static std::vector<Object::Ptr> GetChildren(const Object::Ptr& parent);
static void AddDependency(ConfigObject* child, ConfigObject* parent);
static void RemoveDependency(ConfigObject* child, ConfigObject* parent);
static std::vector<ConfigObject::Ptr> GetChildren(const ConfigObject::Ptr& parent);

static void AddDependency(ObjectImpl<ConfigObject>* child, ConfigObject* parent)
{
AddDependency(static_cast<ConfigObject*>(child), parent);
}

static void RemoveDependency(ObjectImpl<ConfigObject>* child, ConfigObject* parent)
{
RemoveDependency(static_cast<ConfigObject*>(child), parent);
}

private:
DependencyGraph();

static std::mutex m_Mutex;
static std::map<Object *, std::map<Object *, int> > m_Dependencies;
static std::map<ConfigObject*, std::map<ConfigObject*, int>> m_Dependencies;
};

}
Expand Down
2 changes: 1 addition & 1 deletion lib/base/scriptutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ String ScriptUtils::MsiGetComponentPathShim(const String& component)

Array::Ptr ScriptUtils::TrackParents(const Object::Ptr& child)
{
return Array::FromVector(DependencyGraph::GetChildren(child));
return Array::FromVector(DependencyGraph::GetChildren(dynamic_pointer_cast<ConfigObject>(child)));
}

double ScriptUtils::Ptr(const Object::Ptr& object)
Expand Down
7 changes: 1 addition & 6 deletions lib/remote/configobjectutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
return false;
}

for (const Object::Ptr& pobj : parents) {
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);

if (!parentObj)
continue;

for (auto& parentObj : parents) {
DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie);
}

Expand Down
8 changes: 1 addition & 7 deletions lib/remote/objectqueryhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,7 @@ bool ObjectQueryHandler::HandleRequest(
Array::Ptr used_by = new Array();
metaAttrs.emplace_back("used_by", used_by);

for (auto& pobj : DependencyGraph::GetChildren(obj))
{
ConfigObject::Ptr configObj = dynamic_pointer_cast<ConfigObject>(pobj);

if (!configObj)
continue;

for (auto& configObj : DependencyGraph::GetChildren(obj)) {
used_by->Add(new Dictionary({
{ "type", configObj->GetReflectionType()->GetName() },
{ "name", configObj->GetName() }
Expand Down

0 comments on commit 6db2662

Please sign in to comment.