2016-06-30 10:34:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Db\Cache;
|
|
|
|
|
2017-07-31 16:12:56 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2016-06-30 10:34:34 +02:00
|
|
|
use Icinga\Module\Director\CustomVariable\CustomVariables;
|
|
|
|
use Icinga\Module\Director\Db;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
|
|
|
|
|
|
|
class CustomVariableCache
|
|
|
|
{
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
protected $rowsById = array();
|
|
|
|
|
|
|
|
protected $varsById = array();
|
|
|
|
|
|
|
|
public function __construct(IcingaObject $object)
|
|
|
|
{
|
2017-07-31 16:12:56 +02:00
|
|
|
Benchmark::measure('Initializing CustomVariableCache');
|
2016-10-09 14:02:22 +02:00
|
|
|
$connection = $object->getConnection();
|
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
|
|
|
|
$columns = array(
|
|
|
|
'id' => sprintf('v.%s', $object->getVarsIdColumn()),
|
|
|
|
'varname' => 'v.varname',
|
|
|
|
'varvalue' => 'v.varvalue',
|
|
|
|
'format' => 'v.format',
|
|
|
|
'checksum' => '(NULL)',
|
|
|
|
);
|
|
|
|
|
2016-10-09 14:43:04 +02:00
|
|
|
if ($connection->isPgsql()) {
|
|
|
|
if ($connection->hasPgExtension('pgcrypto')) {
|
|
|
|
$columns['checksum'] = "DIGEST(v.varvalue || ';' || v.format, 'sha1')";
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-09 14:02:22 +02:00
|
|
|
$columns['checksum'] = "UNHEX(SHA1(v.varvalue || ';' || v.format))";
|
|
|
|
}
|
2016-06-30 10:34:34 +02:00
|
|
|
|
|
|
|
$query = $db->select()->from(
|
|
|
|
array('v' => $object->getVarsTableName()),
|
2016-10-09 14:02:22 +02:00
|
|
|
$columns
|
2016-06-30 10:34:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($db->fetchAll($query) as $row) {
|
|
|
|
$id = $row->id;
|
|
|
|
unset($row->id);
|
|
|
|
|
2016-10-09 14:43:04 +02:00
|
|
|
if (is_resource($row->checksum)) {
|
|
|
|
$row->checksum = stream_get_contents($row->checksum);
|
|
|
|
}
|
|
|
|
|
2016-06-30 10:34:34 +02:00
|
|
|
if (array_key_exists($id, $this->rowsById)) {
|
|
|
|
$this->rowsById[$id][] = $row;
|
|
|
|
} else {
|
|
|
|
$this->rowsById[$id] = array($row);
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 16:12:56 +02:00
|
|
|
|
|
|
|
Benchmark::measure('Filled CustomVariableCache');
|
2016-06-30 10:34:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getVarsForObject(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$id = $object->id;
|
|
|
|
|
|
|
|
if (array_key_exists($id, $this->rowsById)) {
|
|
|
|
if (! array_key_exists($id, $this->varsById)) {
|
|
|
|
$this->varsById[$id] = CustomVariables::forStoredRows(
|
|
|
|
$this->rowsById[$id]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->varsById[$id];
|
|
|
|
} else {
|
|
|
|
return new CustomVariables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
unset($this->db);
|
|
|
|
}
|
|
|
|
}
|