IcingaObjectGroups: Respect PrefetchCache

This commit is contained in:
Markus Frosch 2016-10-31 17:18:13 +01:00
parent 672a45b26e
commit 2b227a356b
1 changed files with 17 additions and 14 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Exception\NotFoundError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Iterator; use Iterator;
use Countable; use Countable;
@ -26,6 +27,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
public function __construct(IcingaObject $object) public function __construct(IcingaObject $object)
{ {
$this->object = $object; $this->object = $object;
if (! $object->hasBeenLoadedFromDb() && PrefetchCache::shouldBeUsed()) {
/** @var IcingaObjectGroup $class */
$class = $this->getGroupClass();
$class::prefetchAll($this->object->getConnection());
}
} }
public function count() public function count()
@ -161,6 +168,7 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
return $this; return $this;
} }
/** @var IcingaObjectGroup $class */
$class = $this->getGroupClass(); $class = $this->getGroupClass();
if ($group instanceof $class) { if ($group instanceof $class) {
@ -169,28 +177,24 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
} elseif (is_string($group)) { } elseif (is_string($group)) {
$connection = $this->object->getConnection(); $connection = $this->object->getConnection();
try {
// TODO: fix this, prefetch or whatever - this is expensive $this->groups[$group] = $class::load($group, $connection);
$query = $this->object->getDb()->select()->from( } catch (NotFoundError $e) {
$this->getGroupTableName()
)->where('object_name = ?', $group);
$groups = $class::loadAll($connection, $query, 'object_name');
if (! array_key_exists($group, $groups)) {
switch ($onError) { switch ($onError) {
case 'autocreate': case 'autocreate':
$groups[$group] = $class::create(array( $newGroup = $class::create(array(
'object_type' => 'object', 'object_type' => 'object',
'object_name' => $group 'object_name' => $group
)); ));
$groups[$group]->store($connection); $newGroup->store($connection);
// TODO $this->groups[$group] = $newGroup;
break;
case 'fail': case 'fail':
throw new ProgrammingError( throw new NotFoundError(
'The group "%s" doesn\'t exists.', 'The group "%s" doesn\'t exists.',
$group $group
); );
break; break;
case 'ignore': case 'ignore':
return $this; return $this;
} }
@ -202,7 +206,6 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
); );
} }
$this->groups[$group] = $groups[$group];
$this->modified = true; $this->modified = true;
$this->refreshIndex(); $this->refreshIndex();