SimpleQuery: Implement interface Iterator to benchmark result iteration

This commit is contained in:
Johannes Meyer 2015-05-19 09:41:55 +02:00
parent a1276fd709
commit cf989a0f7f
1 changed files with 70 additions and 11 deletions

View File

@ -3,12 +3,13 @@
namespace Icinga\Data;
use Iterator;
use IteratorAggregate;
use Icinga\Application\Benchmark;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
class SimpleQuery implements QueryInterface, Queryable, IteratorAggregate
class SimpleQuery implements QueryInterface, Queryable, Iterator
{
/**
* Query data source
@ -17,6 +18,13 @@ class SimpleQuery implements QueryInterface, Queryable, IteratorAggregate
*/
protected $ds;
/**
* This query's iterator
*
* @var Iterator
*/
protected $iterator;
/**
* The target you are going to query
*
@ -100,16 +108,6 @@ class SimpleQuery implements QueryInterface, Queryable, IteratorAggregate
*/
protected function init() {}
/**
* Return a iterable for this query's result
*
* @return Iterator
*/
public function getIterator()
{
return $this->ds->query($this);
}
/**
* Get the data source
*
@ -120,6 +118,67 @@ class SimpleQuery implements QueryInterface, Queryable, IteratorAggregate
return $this->ds;
}
/**
* Start or rewind the iteration
*/
public function rewind()
{
if ($this->iterator === null) {
$iterator = $this->ds->query($this);
if ($iterator instanceof IteratorAggregate) {
$this->iterator = $iterator->getIterator();
} else {
$this->iterator = $iterator;
}
}
$this->iterator->rewind();
Benchmark::measure('Query result iteration started');
}
/**
* Fetch and return the current row of this query's result
*
* @return object
*/
public function current()
{
return $this->iterator->current();
}
/**
* Return whether the current row of this query's result is valid
*
* @return bool
*/
public function valid()
{
if (! $this->iterator->valid()) {
Benchmark::measure('Query result iteration finished');
return false;
}
return true;
}
/**
* Return the key for the current row of this query's result
*
* @return mixed
*/
public function key()
{
return $this->iterator->key();
}
/**
* Advance to the next row of this query's result
*/
public function next()
{
$this->iterator->next();
}
/**
* Choose a table and the columns you are interested in
*