mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
refactor: modest speedup to database config population (#6636)
use references to assign values into the config array more efficiently, prevents a lot of memory churn caused by the recursive approach
This commit is contained in:
committed by
Neil Lathwood
parent
1ce58cd33d
commit
595e75bd14
@ -33,41 +33,39 @@ function mergedb()
|
|||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
$clone = $config;
|
$db_config = array();
|
||||||
foreach (dbFetchRows('select config_name,config_value from config') as $obj) {
|
foreach (dbFetchRows('SELECT `config_name`,`config_value` FROM `config`') as $obj) {
|
||||||
$clone = array_replace_recursive($clone, mergecnf($obj));
|
assign_array_by_path($db_config, $obj['config_name'], $obj['config_value']);
|
||||||
}
|
}
|
||||||
$config = array_replace_recursive($clone, $config);
|
$config = array_replace_recursive($db_config, $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $obj
|
* Assign a value into the passed array by a path
|
||||||
* @return array
|
* 'snmp.version' = 'v1' becomes $arr['snmp']['version'] = 'v1'
|
||||||
|
*
|
||||||
|
* @param array $arr the array to insert the value into, will be modified in place
|
||||||
|
* @param string $path the path to insert the value at
|
||||||
|
* @param mixed $value the value to insert, will be type cast
|
||||||
|
* @param string $separator path separator
|
||||||
*/
|
*/
|
||||||
function mergecnf($obj)
|
function assign_array_by_path(&$arr, $path, $value, $separator = '.')
|
||||||
{
|
{
|
||||||
$pointer = array();
|
// type cast value. Is this needed here?
|
||||||
$val = $obj['config_value'];
|
if (filter_var($value, FILTER_VALIDATE_INT)) {
|
||||||
$obj = $obj['config_name'];
|
$value = (int) $value;
|
||||||
$obj = explode('.', $obj, 2);
|
} elseif (filter_var($value, FILTER_VALIDATE_FLOAT)) {
|
||||||
if (!isset($obj[1])) {
|
$value = (float) $value;
|
||||||
if (filter_var($val, FILTER_VALIDATE_INT)) {
|
} elseif (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) {
|
||||||
$val = (int) $val;
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
||||||
} elseif (filter_var($val, FILTER_VALIDATE_FLOAT)) {
|
|
||||||
$val = (float) $val;
|
|
||||||
} elseif (filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) {
|
|
||||||
$val = filter_var($val, FILTER_VALIDATE_BOOLEAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($obj[0])) {
|
|
||||||
return array($obj[0] => $val);
|
|
||||||
} else {
|
|
||||||
return array($val);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$pointer[$obj[0]] = mergecnf(array('config_name' => $obj[1], 'config_value' => $val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $pointer;
|
$keys = explode($separator, $path);
|
||||||
}//end mergecnf()
|
|
||||||
|
// walk the array creating keys if they don't exist
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$arr = &$arr[$key];
|
||||||
|
}
|
||||||
|
// assign the variable
|
||||||
|
$arr = $value;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user