Skip to content

Commit

Permalink
Merge pull request #10264 from Icinga/DependencyGraph-ConfigObject
Browse files Browse the repository at this point in the history
DependencyGraph: use ConfigObject*, not Object*
  • Loading branch information
Al2Klimov authored Dec 18, 2024
2 parents 452386c + 3a09cf7 commit 383773e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 38 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
10 changes: 5 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,15 @@ 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);

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
24 changes: 12 additions & 12 deletions tools/mkclass/classcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,50 +903,50 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
m_Impl << "\t" << "if (oldValue) {" << std::endl
<< "\t\t" << "ObjectLock olock(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 */
if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", ";
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else
m_Impl << "<" << field.Type.TypeName << ">(";
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";

m_Impl << "ref).get());" << std::endl
<< "\t\t" << "}" << std::endl
<< "\t" << "}" << std::endl
<< "\t" << "if (newValue) {" << std::endl
<< "\t\t" << "ObjectLock olock(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 */
if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", ";
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else
m_Impl << "<" << field.Type.TypeName << ">(";
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";

m_Impl << "ref).get());" << std::endl
<< "\t\t" << "}" << std::endl
<< "\t" << "}" << std::endl;
} else {
m_Impl << "\t" << "if (!oldValue.IsEmpty())" << std::endl
<< "\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject";
<< "\t\tDependencyGraph::RemoveDependency(";

/* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", ";
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else
m_Impl << "<" << field.Type.TypeName << ">(";
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";

m_Impl << "oldValue).get());" << std::endl
<< "\t" << "if (!newValue.IsEmpty())" << std::endl
<< "\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject";
<< "\t\tDependencyGraph::AddDependency(";

/* Ew */
if (field.Type.TypeName == "Zone" && m_Library == "base")
m_Impl << "(\"Zone\", ";
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
else
m_Impl << "<" << field.Type.TypeName << ">(";
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";

m_Impl << "newValue).get());" << std::endl;
}
Expand Down

0 comments on commit 383773e

Please sign in to comment.