Skip to content

Commit

Permalink
SimpleQuery: Implement interface Iterator to benchmark result iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Meyer committed May 19, 2015
1 parent a1276fd commit cf989a0
Showing 1 changed file with 68 additions and 9 deletions.
77 changes: 68 additions & 9 deletions library/Icinga/Data/SimpleQuery.php
Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -101,23 +109,74 @@ public function __construct($ds, $columns = null)
protected function init() {}

/**
* Return a iterable for this query's result
* Get the data source
*
* @return Iterator
* @return mixed
*/
public function getIterator()
public function getDatasource()
{
return $this->ds->query($this);
return $this->ds;
}

/**
* Get the data source
* 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 mixed
* @return object
*/
public function getDatasource()
public function current()
{
return $this->ds;
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();
}

/**
Expand Down

0 comments on commit cf989a0

Please sign in to comment.