[icinga-checkins] icinga.org: icingaweb2-module-director/feature/test-runner-12905: IcingaObjectGroups: Respect PrefetchCache

git at icinga.org git at icinga.org
Wed Nov 2 19:31:05 CET 2016


Module: icingaweb2-module-director
Branch: feature/test-runner-12905
Commit: 2b227a356b7eeae7642c3a5ffe339ac235a32a27
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=2b227a356b7eeae7642c3a5ffe339ac235a32a27

Author: Markus Frosch <markus.frosch at icinga.com>
Date:   Mon Oct 31 17:18:13 2016 +0100

IcingaObjectGroups: Respect PrefetchCache

---

 library/Director/Objects/IcingaObjectGroups.php |   31 +++++++++++++----------
 1 file changed, 17 insertions(+), 14 deletions(-)

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



More information about the icinga-checkins mailing list