File: dependencygraph.cpp

package info (click to toggle)
icinga2 2.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,040 kB
  • sloc: cpp: 97,870; sql: 3,261; cs: 1,636; yacc: 1,584; sh: 1,009; ansic: 890; lex: 420; python: 80; makefile: 62; javascript: 12
file content (72 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/dependencygraph.hpp"

using namespace icinga;

std::mutex DependencyGraph::m_Mutex;
DependencyGraph::DependencyMap DependencyGraph::m_Dependencies;

void DependencyGraph::AddDependency(ConfigObject* child, ConfigObject* parent)
{
	std::unique_lock<std::mutex> lock(m_Mutex);
	if (auto [it, inserted] = m_Dependencies.insert(Edge(parent, child)); !inserted) {
		m_Dependencies.modify(it, [](Edge& e) { e.count++; });
	}
}

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

	if (auto it(m_Dependencies.find(Edge(parent, child))); it != m_Dependencies.end()) {
		if (it->count > 1) {
			// Remove a duplicate edge from child to node, i.e. decrement the corresponding counter.
			m_Dependencies.modify(it, [](Edge& e) { e.count--; });
		} else {
			// Remove the last edge from child to node (decrementing the counter would set it to 0),
			// thus remove that connection from the data structure completely.
			m_Dependencies.erase(it);
		}
	}
}

/**
 * Returns all the parent objects of the given child object.
 *
 * @param child The child object.
 *
 * @returns A list of the parent objects.
 */
std::vector<ConfigObject::Ptr> DependencyGraph::GetParents(const ConfigObject::Ptr& child)
{
	std::vector<ConfigObject::Ptr> objects;

	std::unique_lock lock(m_Mutex);
	auto [begin, end] = m_Dependencies.get<2>().equal_range(child.get());
	std::transform(begin, end, std::back_inserter(objects), [](const Edge& edge) {
		return edge.parent;
	});

	return objects;
}

/**
 * Returns all the dependent objects of the given parent object.
 *
 * @param parent The parent object.
 *
 * @returns A list of the dependent objects.
 */
std::vector<ConfigObject::Ptr> DependencyGraph::GetChildren(const ConfigObject::Ptr& parent)
{
	std::vector<ConfigObject::Ptr> objects;

	std::unique_lock lock(m_Mutex);
	auto [begin, end] = m_Dependencies.get<1>().equal_range(parent.get());
	std::transform(begin, end, std::back_inserter(objects), [](const Edge& edge) {
		return edge.child;
	});

	return objects;
}