Improve the parsing of replies to INFO.

This commit is contained in:
Daniele Alessandri
2011-03-06 12:30:42 +01:00
parent 798e2b18dc
commit 6f46fa1f2d
+35 -13
View File
@@ -3103,21 +3103,44 @@ class Info extends \Predis\MultiBulkCommand {
$info = array();
$infoLines = explode("\r\n", $data, -1);
foreach ($infoLines as $row) {
list($k, $v) = explode(':', $row);
@list($k, $v) = explode(':', $row);
if ($row === '' || !isset($v)) {
continue;
}
if (!preg_match('/^db\d+$/', $k)) {
if ($k === 'allocation_stats') {
$info[$k] = $this->parseAllocationStats($v);
continue;
}
$info[$k] = $v;
}
else {
$db = array();
foreach (explode(',', $v) as $dbvar) {
list($dbvk, $dbvv) = explode('=', $dbvar);
$db[trim($dbvk)] = $dbvv;
}
$info[$k] = $db;
$info[$k] = $this->parseDatabaseStats($v);
}
}
return $info;
}
protected function parseDatabaseStats($str) {
$db = array();
foreach (explode(',', $str) as $dbvar) {
list($dbvk, $dbvv) = explode('=', $dbvar);
$db[trim($dbvk)] = $dbvv;
}
return $db;
}
protected function parseAllocationStats($str) {
$stats = array();
foreach (explode(',', $str) as $kv) {
@list($size, $objects, $extra) = explode('=', $kv);
// hack to prevent incorrect values when parsing the >=256 key
if (isset($extra)) {
$size = ">=$objects";
$objects = $extra;
}
$stats[$size] = $objects;
}
return $stats;
}
}
class Info_v24 extends Info {
@@ -3136,15 +3159,14 @@ class Info_v24 extends Info {
}
list($k, $v) = explode(':', $row);
if (!preg_match('/^db\d+$/', $k)) {
if ($k === 'allocation_stats') {
$current[$k] = $this->parseAllocationStats($v);
continue;
}
$current[$k] = $v;
}
else {
$db = array();
foreach (explode(',', $v) as $dbvar) {
list($dbvk, $dbvv) = explode('=', $dbvar);
$db[trim($dbvk)] = $dbvv;
}
$current[$k] = $db;
$current[$k] = $this->parseDatabaseStats($v);
}
}
return $info;