Sync: speedup by moving some logic to userspace

This commit is contained in:
Thomas Gelf 2015-11-25 21:54:42 +01:00
parent 35776575a3
commit 99a427c217
2 changed files with 55 additions and 61 deletions

View File

@ -209,6 +209,58 @@ class Db extends DbConnection
return $this->fetchImportedRowsetRows($checksum, $columns);
}
public function fetchImportedRowsetRows($checksum, $columns)
{
$db = $this->db();
$query = $db->select()->from(
array('rsr' => 'imported_rowset_row'),
array(
'object_name' => 'r.object_name',
'property_name' => 'p.property_name',
'property_value' => 'p.property_value',
'format' => 'p.format'
)
)->join(
array('r' => 'imported_row'),
'rsr.row_checksum = r.checksum',
array()
)->join(
array('rp' => 'imported_row_property'),
'r.checksum = rp.row_checksum',
array()
)->join(
array('p' => 'imported_property'),
'p.checksum = rp.property_checksum',
array()
)->where('rsr.rowset_checksum = ?', $checksum)->order('r.object_name');
if ($columns === null) {
$columns = $this->listImportedRowsetColumnNames($checksum);
} else {
$query->where('p.property_name IN (?)', $columns);
}
$result = array();
$empty = (object) array();
foreach ($columns as $k => $v) {
$empty->$k = $v;
}
foreach ($db->fetchAll($query) as $row) {
if (! array_key_exists($row->object_name, $result)) {
$result[$row->object_name] = clone($empty);
}
if ($row->format === 'json') {
$result[$row->object_name]->{$row->property_name} = json_decode($row->property_value);
} else {
$result[$row->object_name]->{$row->property_name} = $row->property_value;
}
}
return $result;
}
public function getLatestImportedChecksum($source)
{
if ($this->getDbType() === 'pgsql') {
@ -250,60 +302,6 @@ class Db extends DbConnection
return $db->fetchCol($query);
}
public function createImportedRowsetRowsQuery($checksum, $columns = null)
{
$db = $this->db();
$query = $db->select()->from(
array('r' => 'imported_row'),
array()
)->join(
array('rsr' => 'imported_rowset_row'),
'rsr.row_checksum = r.checksum',
array()
)->where('rsr.rowset_checksum = ?', $checksum);
$propertyQuery = $db->select()->from(
array('rp' => 'imported_row_property'),
array(
'property_value' => 'p.property_value',
'format' => 'p.format',
'row_checksum' => 'rp.row_checksum'
)
)->join(
array('p' => 'imported_property'),
'rp.property_checksum = p.checksum',
array()
);
$fetchColumns = array('object_name' => 'r.object_name');
if ($columns === null) {
$columns = $this->listImportedRowsetColumnNames($checksum);
}
foreach ($columns as $c) {
$fetchColumns[$c] = sprintf('p_%s.property_value', $c);
$fetchColumns[$c . '__f'] = sprintf('p_%s.format', $c);
$p = clone($propertyQuery);
$query->joinLeft(
array(sprintf('p_' . $c) => $p->where('p.property_name = ?', $c)),
sprintf('p_%s.row_checksum = r.checksum', $c),
array()
);
}
$query->columns($fetchColumns);
return $query;
}
public function fetchImportedRowsetRows($checksum, $columns = null)
{
return $this->db()->fetchAll(
$this->createImportedRowsetRowsQuery($checksum, $columns)
);
}
public function enumCommands()
{
return $this->enumIcingaObjects('command');

View File

@ -85,22 +85,18 @@ class Sync
} else {
$parts = explode('.', $var);
$main = array_shift($parts);
if ($row->{$main . '__f'} === 'json') {
$val = json_decode($row->$main);
return $this->getDeepValue($val, $parts);
} else {
if (! is_object($row->$main)) {
die('Data is not nested, cannot access ...');
}
}
if ($row->{$var . '__f'} === 'json') {
return json_decode($val);
return $this->getDeepValue($row->$main, $parts);
}
return $val;
}
$func = function ($match) use ($row) {
// TODO allow to access deep value also here
return $row->{$match[1]};
};