Commit 5341fdb6 authored by Bas de Nooijer's avatar Bas de Nooijer

Reset prefetch iterator if prefetch or query settings are changed (#219)

parent 45274934
...@@ -104,6 +104,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable ...@@ -104,6 +104,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
*/ */
public function setPrefetch($value) public function setPrefetch($value)
{ {
$this->resetData();
return $this->setOption('prefetch', $value); return $this->setOption('prefetch', $value);
} }
...@@ -126,6 +127,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable ...@@ -126,6 +127,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
public function setQuery($query) public function setQuery($query)
{ {
$this->query = $query; $this->query = $query;
$this->resetData();
return $this; return $this;
} }
...@@ -225,4 +227,15 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable ...@@ -225,4 +227,15 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
$this->documents = $this->result->getDocuments(); $this->documents = $this->result->getDocuments();
$this->start += $this->getPrefetch(); $this->start += $this->getPrefetch();
} }
/**
* Reset any cached data / position
*/
protected function resetData()
{
$this->position = null;
$this->result = null;
$this->documents = null;
$this->start = 0;
}
} }
...@@ -90,6 +90,8 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase ...@@ -90,6 +90,8 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase
{ {
$result = $this->getResult(); $result = $this->getResult();
$mockClient = $this->getMock('Solarium\Core\Client\Client', array('execute')); $mockClient = $this->getMock('Solarium\Core\Client\Client', array('execute'));
// Important: if prefetch or query settings are not changed, the query should be executed only once!
$mockClient->expects($this->exactly(1))->method('execute')->will($this->returnValue($result)); $mockClient->expects($this->exactly(1))->method('execute')->will($this->returnValue($result));
$this->plugin->initPlugin($mockClient, array()); $this->plugin->initPlugin($mockClient, array());
...@@ -110,6 +112,58 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase ...@@ -110,6 +112,58 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($result->getDocuments(), $results2); $this->assertEquals($result->getDocuments(), $results2);
} }
public function testIteratorResetOnSetPrefetch()
{
$result = $this->getResult();
$mockClient = $this->getMock('Solarium\Core\Client\Client', array('execute'));
$mockClient->expects($this->exactly(2))->method('execute')->will($this->returnValue($result));
$this->plugin->initPlugin($mockClient, array());
$this->plugin->setQuery($this->query);
$results1 = array();
foreach ($this->plugin as $doc) {
$results1[] = $doc;
}
$this->plugin->setPrefetch(1000);
// the second foreach should trigger a reset and a second query execution (checked by mock)
$results2 = array();
foreach ($this->plugin as $doc) {
$results2[] = $doc;
}
$this->assertEquals($result->getDocuments(), $results1);
$this->assertEquals($result->getDocuments(), $results2);
}
public function testIteratorResetOnSetQuery()
{
$result = $this->getResult();
$mockClient = $this->getMock('Solarium\Core\Client\Client', array('execute'));
$mockClient->expects($this->exactly(2))->method('execute')->will($this->returnValue($result));
$this->plugin->initPlugin($mockClient, array());
$this->plugin->setQuery($this->query);
$results1 = array();
foreach ($this->plugin as $doc) {
$results1[] = $doc;
}
$this->plugin->setQuery($this->query);
// the second foreach should trigger a reset and a second query execution (checked by mock)
$results2 = array();
foreach ($this->plugin as $doc) {
$results2[] = $doc;
}
$this->assertEquals($result->getDocuments(), $results1);
$this->assertEquals($result->getDocuments(), $results2);
}
public function getResult() public function getResult()
{ {
$numFound = 5; $numFound = 5;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment