DbObject: first attempt to seriously use prefetch

This commit is contained in:
Thomas Gelf 2015-12-03 19:43:08 +01:00
parent f49f14376b
commit b03f5c55ca
2 changed files with 25 additions and 8 deletions

View File

@ -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)

View File

@ -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')