DependencyGraph: use ConfigObject*, not Object*

This saves dynamic_cast<ConfigObject*> + if() on every item of GetChildren().
This commit is contained in:
Alexander A. Klimov 2024-12-04 11:19:37 +01:00
parent 188ba53b74
commit 3a09cf72d6
6 changed files with 26 additions and 38 deletions

View File

@ -5,15 +5,15 @@
using namespace icinga; using namespace icinga;
std::mutex DependencyGraph::m_Mutex; 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); std::unique_lock<std::mutex> lock(m_Mutex);
m_Dependencies[parent][child]++; 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); std::unique_lock<std::mutex> lock(m_Mutex);
@ -32,16 +32,15 @@ void DependencyGraph::RemoveDependency(Object* child, Object* parent)
m_Dependencies.erase(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); std::unique_lock<std::mutex> lock(m_Mutex);
auto it = m_Dependencies.find(parent.get()); auto it = m_Dependencies.find(parent.get());
if (it != m_Dependencies.end()) { if (it != m_Dependencies.end()) {
typedef std::pair<Object *, int> kv_pair; for (auto& kv : it->second) {
for (const kv_pair& kv : it->second) {
objects.emplace_back(kv.first); objects.emplace_back(kv.first);
} }
} }

View File

@ -4,7 +4,7 @@
#define DEPENDENCYGRAPH_H #define DEPENDENCYGRAPH_H
#include "base/i2-base.hpp" #include "base/i2-base.hpp"
#include "base/object.hpp" #include "base/configobject.hpp"
#include <map> #include <map>
#include <mutex> #include <mutex>
@ -18,15 +18,15 @@ namespace icinga {
class DependencyGraph class DependencyGraph
{ {
public: public:
static void AddDependency(Object* child, Object* parent); static void AddDependency(ConfigObject* child, ConfigObject* parent);
static void RemoveDependency(Object* child, Object* parent); static void RemoveDependency(ConfigObject* child, ConfigObject* parent);
static std::vector<Object::Ptr> GetChildren(const Object::Ptr& parent); static std::vector<ConfigObject::Ptr> GetChildren(const ConfigObject::Ptr& parent);
private: private:
DependencyGraph(); DependencyGraph();
static std::mutex m_Mutex; 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;
}; };
} }

View File

@ -520,7 +520,7 @@ String ScriptUtils::MsiGetComponentPathShim(const String& component)
Array::Ptr ScriptUtils::TrackParents(const Object::Ptr& child) 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) double ScriptUtils::Ptr(const Object::Ptr& object)

View File

@ -319,12 +319,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
return false; return false;
} }
for (const Object::Ptr& pobj : parents) { for (auto& parentObj : parents) {
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);
if (!parentObj)
continue;
DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie); DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie);
} }

View File

@ -208,13 +208,7 @@ bool ObjectQueryHandler::HandleRequest(
Array::Ptr used_by = new Array(); Array::Ptr used_by = new Array();
metaAttrs.emplace_back("used_by", used_by); metaAttrs.emplace_back("used_by", used_by);
for (auto& pobj : DependencyGraph::GetChildren(obj)) for (auto& configObj : DependencyGraph::GetChildren(obj)) {
{
ConfigObject::Ptr configObj = dynamic_pointer_cast<ConfigObject>(pobj);
if (!configObj)
continue;
used_by->Add(new Dictionary({ used_by->Add(new Dictionary({
{ "type", configObj->GetReflectionType()->GetName() }, { "type", configObj->GetReflectionType()->GetName() },
{ "name", configObj->GetName() } { "name", configObj->GetName() }

View File

@ -903,13 +903,13 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
m_Impl << "\t" << "if (oldValue) {" << std::endl m_Impl << "\t" << "if (oldValue) {" << std::endl
<< "\t\t" << "ObjectLock olock(oldValue);" << std::endl << "\t\t" << "ObjectLock olock(oldValue);" << std::endl
<< "\t\t" << "for (const String& ref : oldValue) {" << std::endl << "\t\t" << "for (const String& ref : oldValue) {" << std::endl
<< "\t\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject"; << "\t\t\tDependencyGraph::RemoveDependency(";
/* Ew */ /* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base") if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", "; m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else else
m_Impl << "<" << field.Type.TypeName << ">("; m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
m_Impl << "ref).get());" << std::endl m_Impl << "ref).get());" << std::endl
<< "\t\t" << "}" << std::endl << "\t\t" << "}" << std::endl
@ -917,36 +917,36 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
<< "\t" << "if (newValue) {" << std::endl << "\t" << "if (newValue) {" << std::endl
<< "\t\t" << "ObjectLock olock(newValue);" << std::endl << "\t\t" << "ObjectLock olock(newValue);" << std::endl
<< "\t\t" << "for (const String& ref : newValue) {" << std::endl << "\t\t" << "for (const String& ref : newValue) {" << std::endl
<< "\t\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject"; << "\t\t\tDependencyGraph::AddDependency(";
/* Ew */ /* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base") if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", "; m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else else
m_Impl << "<" << field.Type.TypeName << ">("; m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
m_Impl << "ref).get());" << std::endl m_Impl << "ref).get());" << std::endl
<< "\t\t" << "}" << std::endl << "\t\t" << "}" << std::endl
<< "\t" << "}" << std::endl; << "\t" << "}" << std::endl;
} else { } else {
m_Impl << "\t" << "if (!oldValue.IsEmpty())" << std::endl m_Impl << "\t" << "if (!oldValue.IsEmpty())" << std::endl
<< "\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject"; << "\t\tDependencyGraph::RemoveDependency(";
/* Ew */ /* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base") if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", "; m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else else
m_Impl << "<" << field.Type.TypeName << ">("; m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
m_Impl << "oldValue).get());" << std::endl m_Impl << "oldValue).get());" << std::endl
<< "\t" << "if (!newValue.IsEmpty())" << std::endl << "\t" << "if (!newValue.IsEmpty())" << std::endl
<< "\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject"; << "\t\tDependencyGraph::AddDependency(";
/* Ew */ /* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base") if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", "; m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else else
m_Impl << "<" << field.Type.TypeName << ">("; m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
m_Impl << "newValue).get());" << std::endl; m_Impl << "newValue).get());" << std::endl;
} }