Skip to content

Commit

Permalink
Introduce Type::GetAllConfigTypesSortedByLoadDependencies()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Sep 16, 2024
1 parent 5cc034b commit 4037b60
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/base/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum class InitializePriority {
RegisterBuiltinTypes,
RegisterFunctions,
RegisterTypes,
SortTypes,
EvaluateConfigFragments,
Default,
FreezeNamespaces,
Expand Down
66 changes: 66 additions & 0 deletions lib/base/type.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/type.hpp"
#include "base/atomic.hpp"
#include "base/configobject.hpp"
#include "base/debug.hpp"
#include "base/scriptglobal.hpp"
#include "base/namespace.hpp"
#include "base/objectlock.hpp"
#include <algorithm>
#include <functional>

using namespace icinga;

Expand Down Expand Up @@ -32,6 +37,61 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() {
Type::Register(type);
}, InitializePriority::RegisterTypeType);

static std::vector<Type::Ptr> l_SortedByLoadDependencies;
static Atomic l_SortingByLoadDependenciesDone (false);

INITIALIZE_ONCE_WITH_PRIORITY([] {
auto types (Type::GetAllTypes());

types.erase(std::remove_if(types.begin(), types.end(), [](auto& type) {
return !ConfigObject::TypeInstance->IsAssignableFrom(type);
}), types.end());

// Depth-first search
std::unordered_set<Type*> unsorted;
std::unordered_set<Type*> visited;
std::vector<Type::Ptr> sorted;

for (auto type : types) {
unsorted.emplace(type.get());
}

std::function<void(Type*)> visit;
visit = ([&visit, &unsorted, &visited, &sorted](Type* type) {
if (unsorted.find(type) == unsorted.end()) {
return;
}

VERIFY(visited.emplace(type).second); // Detect cycles in type dependencies

for (auto dep : type->GetLoadDependencies()) {
visit(dep);
}

unsorted.erase(type);
sorted.emplace_back(type);
});

while (!unsorted.empty()) {
visit(*unsorted.begin());
}

VERIFY(sorted.size() == types.size());

visited.clear();

for (auto& t : sorted) {
for (auto dep : t->GetLoadDependencies()) {
VERIFY(visited.find(dep) != visited.end());
}

VERIFY(visited.emplace(t.get()).second);
}

std::swap(sorted, l_SortedByLoadDependencies);
l_SortingByLoadDependenciesDone.store(true);
}, InitializePriority::SortTypes);

String Type::ToString() const
{
return "type '" + GetName() + "'";
Expand Down Expand Up @@ -72,6 +132,12 @@ std::vector<Type::Ptr> Type::GetAllTypes()
return types;
}

const std::vector<Type::Ptr>& Type::GetAllConfigTypesSortedByLoadDependencies()
{
VERIFY(l_SortingByLoadDependenciesDone.load());
return l_SortedByLoadDependencies;
}

String Type::GetPluralName() const
{
String name = GetName();
Expand Down
1 change: 1 addition & 0 deletions lib/base/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Type : public Object
static void Register(const Type::Ptr& type);
static Type::Ptr GetByName(const String& name);
static std::vector<Type::Ptr> GetAllTypes();
static const std::vector<Ptr>& GetAllConfigTypesSortedByLoadDependencies();

void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
Value GetField(int id) const override;
Expand Down

0 comments on commit 4037b60

Please sign in to comment.