IcingaTemplateResolver: implement a reverse index

This commit is contained in:
Thomas Gelf 2017-05-24 15:16:11 +02:00
parent 51e8af2266
commit 4f3f4e7f52

View File

@ -24,6 +24,8 @@ class IcingaTemplateResolver
protected static $idIdx = array(); protected static $idIdx = array();
protected static $reverseIdIdx = array();
protected static $nameIdx = array(); protected static $nameIdx = array();
protected static $idToName = array(); protected static $idToName = array();
@ -218,6 +220,32 @@ class IcingaTemplateResolver
return $ids; return $ids;
} }
public function listChildren($objectId = null)
{
if ($objectId === null) {
$objectId = $this->object->id;
}
if (array_key_exists($objectId, self::$reverseIdIdx[$this->type])) {
return self::$reverseIdIdx[$this->type][$objectId];
} else {
return array();
}
}
public function listChildIds($objectId = null)
{
return array_keys($this->listChildren($objectId));
}
public function listDescendantIds($objectId = null)
{
if ($objectId === null) {
$objectId = $this->object->id;
}
}
public function listInheritancePathIds($objectId = null) public function listInheritancePathIds($objectId = null)
{ {
return $this->uniquePathIds($this->listFullInheritancePathIds($objectId)); return $this->uniquePathIds($this->listFullInheritancePathIds($objectId));
@ -355,34 +383,45 @@ class IcingaTemplateResolver
$templates = $this->fetchTemplates(); $templates = $this->fetchTemplates();
$ids = array(); $ids = array();
$reverseIds = array();
$names = array(); $names = array();
$idToName = array(); $idToName = array();
$nameToId = array(); $nameToId = array();
foreach ($templates as $row) { foreach ($templates as $row) {
$idToName[$row->id] = $row->name; $id = $row->id;
$nameToId[$row->name] = $row->id; $idToName[$id] = $row->name;
$nameToId[$row->name] = $id;
if ($row->parent_id === null) { if ($row->parent_id === null) {
continue; continue;
} }
if (array_key_exists($row->id, $ids)) { $parentId = $row->parent_id;
$ids[$row->id][$row->parent_id] = $row->parent_name; $parentName = $row->parent_name;
$names[$row->name][$row->parent_name] = $row->parent_id;
if (array_key_exists($id, $ids)) {
$ids[$id][$parentId] = $parentName;
$names[$row->name][$parentName] = $row->parent_id;
} else { } else {
$ids[$row->id] = array( $ids[$id] = array(
$row->parent_id => $row->parent_name $parentId => $parentName
); );
$names[$row->name] = array( $names[$row->name] = array(
$row->parent_name => $row->parent_id $parentName => $parentId
); );
} }
if (! array_key_exists($parentId, $reverseIds)) {
$reverseIds[$parentId] = array();
}
$reverseIds[$parentId][$id] = $row->name;
} }
self::$idIdx[$type] = $ids; self::$idIdx[$type] = $ids;
self::$reverseIdIdx[$type] = $reverseIds;
self::$nameIdx[$type] = $names; self::$nameIdx[$type] = $names;
self::$templates[$type] = $templates; self::$templates[$type] = $templates; // TODO: this is unused, isn't it?
self::$idToName[$type] = $idToName; self::$idToName[$type] = $idToName;
self::$nameToId[$type] = $nameToId; self::$nameToId[$type] = $nameToId;
} }
@ -423,13 +462,15 @@ class IcingaTemplateResolver
public function refreshObject(IcingaObject $object) public function refreshObject(IcingaObject $object)
{ {
$type = $object->getShortTableName();
$name = $object->getObjectName();
$parentNames = $object->imports; $parentNames = $object->imports;
self::$nameIdx[$object->object_name] = $parentNames; self::$nameIdx[$type][$name] = $parentNames;
if ($object->hasBeenLoadedFromDb()) { if ($object->hasBeenLoadedFromDb()) {
$id = $object->getId(); $id = $object->getProperty('id');
if (! is_array($id)) { self::$idIdx[$type][$id] = $this->getIdsForNames($parentNames);
self::$idIdx[$id] = $this->getIdsForNames($parentNames); self::$idToName[$type][$id] = $name;
} self::$nameToId[$type][$name] = $id;
} }
return $this; return $this;
} }