Optimize variable names and positions, cast array direct to object

This commit is contained in:
Alexander Klimov 2014-07-18 12:30:39 +02:00
parent f9a274d079
commit 5ea02b41ea

View File

@ -4,6 +4,7 @@
namespace Icinga\Protocol\File; namespace Icinga\Protocol\File;
use Exception;
use Icinga\Data\DatasourceInterface; use Icinga\Data\DatasourceInterface;
/** /**
@ -15,6 +16,8 @@ use Icinga\Data\DatasourceInterface;
*/ */
class Reader implements DatasourceInterface class Reader implements DatasourceInterface
{ {
private static $EOLLen = null;
/** /**
* Name of the file to read * Name of the file to read
* *
@ -34,6 +37,9 @@ class Reader implements DatasourceInterface
*/ */
public function __construct($config) public function __construct($config)
{ {
if (self::$EOLLen === null) {
self::$EOLLen = strlen(PHP_EOL);
}
$this->config = $config; $this->config = $config;
$this->filename = $config->filename; $this->filename = $config->filename;
} }
@ -57,10 +63,7 @@ class Reader implements DatasourceInterface
{ {
$all = array(); $all = array();
foreach ($this->fetchPairs($query) as $index => $value) { foreach ($this->fetchPairs($query) as $index => $value) {
$all[$index] = new \stdClass(); $all[$index] = (object) $value;
foreach ($value as $key => $value_2) {
$all[$index]->{$key} = $value_2;
}
} }
return $all; return $all;
} }
@ -87,9 +90,9 @@ class Reader implements DatasourceInterface
public function fetchColumn(Query $query) public function fetchColumn(Query $query)
{ {
$column = array(); $column = array();
foreach ($this->fetchPairs($query) as $value) { foreach ($this->fetchPairs($query) as $pair) {
foreach ($value as $value_2) { foreach ($pair as $value) {
$column[] = $value_2; $column[] = $value;
break; break;
} }
} }
@ -139,10 +142,10 @@ class Reader implements DatasourceInterface
public function validateLine($line, Query $query) public function validateLine($line, Query $query)
{ {
$data = array(); $data = array();
$PCRE_result = @preg_match($this->config->fields, $line, $data); $PCREResult = @preg_match($this->config->fields, $line, $data);
if ($PCRE_result === false) { if ($PCREResult === false) {
throw new \Exception('Failed parsing regular expression!'); throw new Exception('Failed parsing regular expression!');
} else if ($PCRE_result === 1) { } else if ($PCREResult === 1) {
foreach ($query->getFilters() as $filter) { foreach ($query->getFilters() as $filter) {
if (strpos($line, $filter) === false) { if (strpos($line, $filter) === false) {
return false; return false;
@ -167,21 +170,20 @@ class Reader implements DatasourceInterface
*/ */
public function read(Query $query) public function read(Query $query)
{ {
$skip_lines = $query->getOffset(); $skipLines = $query->getOffset();
$read_lines = $query->getLimit(); $readLines = $query->getLimit();
if ($skip_lines === null) { if ($skipLines === null) {
$skip_lines = 0; $skipLines = 0;
} }
return $this->{$query->sortDesc() ? 'readFromEnd' : 'readFromStart'}($skip_lines, $read_lines, $query); return $this->{$query->sortDesc() ? 'readFromEnd' : 'readFromStart'}($skipLines, $readLines, $query);
} }
/** /**
* Backend for $this->read * Backend for $this->read
* Direction: LIFO * Direction: LIFO
*/ */
public function readFromEnd($skip_lines, $read_lines, Query $query) public function readFromEnd($skipLines, $readLines, Query $query)
{ {
$PHP_EOL_len = strlen(PHP_EOL);
$lines = array(); $lines = array();
$s = ''; $s = '';
$f = @fopen($this->filename, 'rb'); $f = @fopen($this->filename, 'rb');
@ -191,21 +193,21 @@ class Reader implements DatasourceInterface
if (ftell($f) === 0) { if (ftell($f) === 0) {
return array(); return array();
} }
while ($read_lines === null || count($lines) < $read_lines) { while ($readLines === null || count($lines) < $readLines) {
$c = $this->fgetc($f, $buffer); $c = $this->fgetc($f, $buffer);
if ($c === false) { if ($c === false) {
$l = $this->validateLine($s, $query); $l = $this->validateLine($s, $query);
if (!($l === false || $skip_lines)) { if (!($l === false || $skipLines)) {
$lines[] = $l; $lines[] = $l;
} }
break; break;
} }
$s = $c . $s; $s = $c . $s;
if (strpos($s, PHP_EOL) === 0) { if (strpos($s, PHP_EOL) === 0) {
$l = $this->validateLine((string)substr($s, $PHP_EOL_len), $query); $l = $this->validateLine((string)substr($s, self::$EOLLen), $query);
if ($l !== false) { if ($l !== false) {
if ($skip_lines) { if ($skipLines) {
$skip_lines--; $skipLines--;
} else { } else {
$lines[] = $l; $lines[] = $l;
} }
@ -249,20 +251,19 @@ class Reader implements DatasourceInterface
* Backend for $this->read * Backend for $this->read
* Direction: FIFO * Direction: FIFO
*/ */
public function readFromStart($skip_lines, $read_lines, Query $query) public function readFromStart($skipLines, $readLines, Query $query)
{ {
$PHP_EOL_len = strlen(PHP_EOL);
$lines = array(); $lines = array();
$s = ''; $s = '';
$f = @fopen($this->filename, 'rb'); $f = @fopen($this->filename, 'rb');
if ($f !== false) { if ($f !== false) {
$buffer = ''; $buffer = '';
while ($read_lines === null || count($lines) < $read_lines) { while ($readLines === null || count($lines) < $readLines) {
if (strlen($buffer) === 0) { if (strlen($buffer) === 0) {
$buffer = fread($f, 4096); $buffer = fread($f, 4096);
if (strlen($buffer) === 0) { if (strlen($buffer) === 0) {
$l = $this->validateLine($s, $query); $l = $this->validateLine($s, $query);
if (!($l === false || $skip_lines)) { if (!($l === false || $skipLines)) {
$lines[] = $l; $lines[] = $l;
} }
break; break;
@ -271,10 +272,10 @@ class Reader implements DatasourceInterface
$s .= substr($buffer, 0, 1); $s .= substr($buffer, 0, 1);
$buffer = substr($buffer, 1); $buffer = substr($buffer, 1);
if (strpos($s, PHP_EOL) !== false) { if (strpos($s, PHP_EOL) !== false) {
$l = $this->validateLine((string)substr($s, 0, strlen($s) - $PHP_EOL_len), $query); $l = $this->validateLine((string)substr($s, 0, strlen($s) - self::$EOLLen), $query);
if ($l !== false) { if ($l !== false) {
if ($skip_lines) { if ($skipLines) {
$skip_lines--; $skipLines--;
} else { } else {
$lines[] = $l; $lines[] = $l;
} }
@ -294,7 +295,6 @@ class Reader implements DatasourceInterface
* @return int * @return int
*/ */
public function count(Query $query) { public function count(Query $query) {
$PHP_EOL_len = strlen(PHP_EOL);
$lines = 0; $lines = 0;
$s = ''; $s = '';
$f = @fopen($this->filename, 'rb'); $f = @fopen($this->filename, 'rb');
@ -313,7 +313,7 @@ class Reader implements DatasourceInterface
$s .= substr($buffer, 0, 1); $s .= substr($buffer, 0, 1);
$buffer = substr($buffer, 1); $buffer = substr($buffer, 1);
if (strpos($s, PHP_EOL) !== false) { if (strpos($s, PHP_EOL) !== false) {
if ($this->validateLine((string)substr($s, 0, strlen($s) - $PHP_EOL_len), $query) !== false) { if ($this->validateLine((string)substr($s, 0, strlen($s) - self::$EOLLen), $query) !== false) {
$lines++; $lines++;
} }
$s = ''; $s = '';