From 6660456f5c7c0c301be6b8e619f2ec5ceb195e77 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 25 Feb 2011 21:37:24 +0100 Subject: [PATCH] some cosmetics & more performance enhancements --- .../Iterator/DataAggregationIterator.php | 32 +++++++++++-------- lib/Interpreter/Iterator/DataIterator.php | 10 +++--- lib/Interpreter/MeterInterpreter.php | 7 ++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/Interpreter/Iterator/DataAggregationIterator.php b/lib/Interpreter/Iterator/DataAggregationIterator.php index 5f32e02..6d161b1 100644 --- a/lib/Interpreter/Iterator/DataAggregationIterator.php +++ b/lib/Interpreter/Iterator/DataAggregationIterator.php @@ -32,9 +32,9 @@ use Doctrine\DBAL; * @package default */ class DataAggregationIterator implements \Iterator, \Countable { - protected $current; - protected $key; // key - protected $size; // total readings in PDOStatement + protected $current; // the current data + protected $key; // key + protected $size; // total readings in PDOStatement protected $iterator; // subiterator /** @@ -44,30 +44,36 @@ class DataAggregationIterator implements \Iterator, \Countable { * @param integer $size * @param integer $tuples */ - public function __construct(\PDOStatement $stmt, $size, $tuples) { - $this->iterator = new DataIterator($stmt, $size); + public function __construct(\PDOStatement $stmt, $rows, $count) { + $this->iterator = new DataIterator($stmt, $rows); - $this->packageSize = floor($size / $tuples); - $this->size = (int) $tuples; + $this->packageSize = floor($rows / $count); + $this->size = $count; } /** * Aggregate data */ public function next() { - $current = array(0, 0, 0); - for ($i = 0; $i < $this->packageSize && $this->iterator->valid(); $i++, $this->iterator->next()) { + $this->current = array(0, 0, 0); + for ($i = 0; $i < $this->packageSize; $i++, $this->iterator->next()) { $tuple = $this->iterator->current(); - $current[0] = $tuple[0]; - $current[1] += $tuple[1]; - $current[2] += $tuple[2]; + $this->current[0] = $tuple[0]; + $this->current[1] += $tuple[1]; + $this->current[2] += $tuple[2]; } $this->key++; - return $this->current = $current; + return $this->current; } + /** + * Rewind the iterator + * + * Should only be called once + * PDOStatement hasn't a rewind() + */ public function rewind() { $this->iterator->rewind(); // skip first readings to get an even divisor diff --git a/lib/Interpreter/Iterator/DataIterator.php b/lib/Interpreter/Iterator/DataIterator.php index 1cad3fd..ad3f590 100644 --- a/lib/Interpreter/Iterator/DataIterator.php +++ b/lib/Interpreter/Iterator/DataIterator.php @@ -31,10 +31,10 @@ use Doctrine\DBAL; * @package default */ class DataIterator implements \Iterator, \Countable { - protected $current; // the current data - protected $key; // key - protected $stmt; // PDOStatement - protected $size; // total readings in PDOStatement + protected $current; // the current data + protected $key; // key + protected $stmt; // PDOStatement + protected $size; // total readings in PDOStatement /** * Constructor @@ -81,7 +81,7 @@ class DataIterator implements \Iterator, \Countable { * Rewind the iterator * * Should only be called once - * PDOStatements doest support rewind() + * PDOStatement hasn't a rewind() aquivalent */ public function rewind() { $this->key = 0; diff --git a/lib/Interpreter/MeterInterpreter.php b/lib/Interpreter/MeterInterpreter.php index adb2d9a..6cbf147 100644 --- a/lib/Interpreter/MeterInterpreter.php +++ b/lib/Interpreter/MeterInterpreter.php @@ -45,7 +45,7 @@ class MeterInterpreter extends Interpreter { * @return float total consumption */ public function getConsumption() { - if (is_null($this->consumption)) throw new \Excpetion('Data has to be processed first!'); + if (is_null($this->consumption)) throw new \Exception('Data has to be processed first!'); return $this->consumption / $this->resolution; } @@ -55,7 +55,7 @@ class MeterInterpreter extends Interpreter { * @return array (0 => timestamp, 1 => value) */ public function getMin() { - if (is_null($this->min)) throw new \Excpetion('Data has to be processed first!'); + if (is_null($this->min)) throw new \Exception('Data has to be processed first!'); return $this->min; } @@ -65,7 +65,7 @@ class MeterInterpreter extends Interpreter { * @return array (0 => timestamp, 1 => value) */ public function getMax() { - if (is_null($this->max)) throw new \Excpetion('Data has to be processed first!'); + if (is_null($this->max)) throw new \Exception('Data has to be processed first!'); return $this->max; } @@ -97,7 +97,6 @@ class MeterInterpreter extends Interpreter { $next = $pulses->current(); while ($pulses->valid()) { - Util\Debug::log('after valid()', $last, $next); $tuple = $callback($this->raw2differential($last, $next)); if (is_null($this->max) || $tuple[1] > $this->max[1]) {