Skip to content
Permalink
Browse files

A couple micro-optimizations related to counting results from ResultSet

  • Loading branch information...
lorenzo committed Sep 22, 2014
1 parent e91831d commit cefcfcf7b3faf21c6637ec43cc1d5e2043af00ea
Showing with 13 additions and 3 deletions.
  1. +4 −0 src/Datasource/ResultSetDecorator.php
  2. +5 −3 src/ORM/ResultSet.php
  3. +4 −0 tests/TestCase/ORM/QueryTest.php
@@ -37,6 +37,10 @@ class ResultSetDecorator extends Collection implements Countable, Serializable,
* @return int
*/
public function count() {
if ($this->getInnerIterator() instanceof Countable) {
return $this->getInnerIterator()->count();
}
return count($this->toArray());
}
@@ -21,6 +21,7 @@
use Iterator;
use JsonSerializable;
use Serializable;
use SplFixedArray;
/**
* Represents the results obtained after executing a query for a specific table
@@ -133,9 +134,10 @@ public function __construct($query, $statement) {
$this->_hydrate = $this->_query->hydrate();
$this->_entityClass = $repository->entityClass();
$this->_useBuffering = $query->bufferResults();
$this->count();
if ($statement) {
$this->count();
if ($this->_useBuffering) {
$this->_results = new SplFixedArray($this->_count);
}
}
@@ -475,7 +477,7 @@ protected function _castValues($table, $values) {
*/
protected function _bufferResult($result) {
if ($this->_useBuffering) {
$this->_results[] = $result;
$this->_results[$this->_index] = $result;
}
}
@@ -966,6 +966,10 @@ public function testResultsAreWrappedInMapReduce() {
->method('fetch')
->will($this->onConsecutiveCalls(['a' => 1], ['a' => 2], false));
$statement->expects($this->once())
->method('rowCount')
->will($this->returnValue(2));
$query->expects($this->once())
->method('execute')
->will($this->returnValue($statement));

0 comments on commit cefcfcf

Please sign in to comment.
You can’t perform that action at this time.