From b03f5c55ca26a3ed9142eac2aa89e286b40bb130 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 3 Dec 2015 19:43:08 +0100 Subject: [PATCH] DbObject: first attempt to seriously use prefetch --- library/Director/Data/Db/DbObject.php | 29 ++++++++++++++----- .../Director/IcingaConfig/IcingaConfig.php | 4 +++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index dd6daa24..c2474bb5 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -79,6 +79,11 @@ abstract class DbObject */ protected static $prefetched = array(); + /** + * object_name => id map for prefetched objects + */ + protected static $prefetchedNames = array(); + /** * Constructor is not accessible and should not be overridden */ @@ -112,7 +117,6 @@ abstract class DbObject return true; } - /************************************************************************\ * Nachfolgend finden sich ein paar Hooks, die bei Bedarf überschrieben * * werden können. Wann immer möglich soll darauf verzichtet werden, * @@ -438,7 +442,7 @@ abstract class DbObject public function getKeyParams() { - $params = array();; + $params = array(); $key = $this->getKeyName(); if (is_array($key)) { foreach ($key as $k) { @@ -854,7 +858,7 @@ abstract class DbObject protected static function getPrefetched($key) { if (static::hasPrefetched($key)) { - return static::$prefetched[get_called_class()][$id]; + return self::$prefetched[get_called_class()][$key]; } else { return false; } @@ -862,8 +866,9 @@ abstract class DbObject protected static function hasPrefetched($key) { - if (array_key_exists(get_called_class(), self::$prefetched)) { - return array_key_exists($id, self::$prefetched[get_called_class()]); + $class = get_called_class(); + if (array_key_exists($class, self::$prefetched)) { + return array_key_exists($key, self::$prefetched[$class]); } else { return false; } @@ -871,7 +876,9 @@ abstract class DbObject public static function loadWithAutoIncId($id, DbConnection $connection) { - // TODO: Index for prefetch? + if ($prefetched = static::getPrefetched($id)) { + return $prefetched; + } $class = get_called_class(); $obj = new $class(); @@ -921,9 +928,15 @@ abstract class DbObject return $objects; } - public static function prefetchAll(DbConnection $connection) + public static function prefetchAll(DbConnection $connection, $force = false) { - return self::$prefetched[get_called_class()] = static::fetchAll($connection); + $class = get_called_class(); + + if ($force || ! array_key_exists($class, self::$prefetched)) { + self::$prefetched[$class] = static::loadAll($connection); + } + + return self::$prefetched[$class]; } public static function exists($id, DbConnection $connection) diff --git a/library/Director/IcingaConfig/IcingaConfig.php b/library/Director/IcingaConfig/IcingaConfig.php index bb90c931..5dd8d2af 100644 --- a/library/Director/IcingaConfig/IcingaConfig.php +++ b/library/Director/IcingaConfig/IcingaConfig.php @@ -7,6 +7,7 @@ use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Util; use Icinga\Module\Director\Objects\IcingaCommand; use Icinga\Module\Director\Objects\IcingaHost; +use Icinga\Module\Director\Objects\IcingaEndpoint; use Icinga\Web\Hook; use Exception; @@ -240,6 +241,9 @@ throw $e; ); + // TODO: combine this with real fetches, this is a test right now + IcingaEndpoint::prefetchAll($this->connection); + $this ->createFileFromDb('zone') ->createFileFromDb('endpoint')