Skip to content

Commit 3340357

Browse files
Bugfix for mid-set rewind() in PrefetchIterator (#907)
1 parent 1ebff68 commit 3340357

4 files changed

Lines changed: 231 additions & 74 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414
### Fixed
1515
- PrefetchIterator::key() should return 0 instead of NULL on a fresh PrefetchIterator
1616
- PrefetchIterator::next() shouldn't skip fetched results after PrefetchIterator::count() on a fresh PrefetchIterator
17+
- PrefetchIterator::rewind() no longer results in duplicate documents when invoked mid-set
1718
- fixed incorrect median function
1819

1920
### Changed

src/Plugin/PrefetchIterator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ public function rewind(): void
173173
{
174174
$this->position = 0;
175175

176-
// this condition prevents erroneously fetching the next set of results if a count is done before the iterator is used
177-
if ($this->start !== $this->options['prefetch']) {
176+
// this condition prevents needlessly re-fetching if the iterator hasn't moved past its first set of results yet
177+
// (this includes when a count is done before the iterator is used)
178+
if ($this->start > $this->options['prefetch']) {
178179
$this->start = 0;
180+
$this->result = null;
181+
$this->documents = null;
179182

180183
if (null !== $this->cursormark) {
181184
$this->cursormark = '*';

tests/Integration/AbstractTechproductsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,44 @@ public function testPrefetchIteratorWithoutAndWithCursormark()
18871887
$this->assertSame($without, $with);
18881888
}
18891889

1890+
public function testPrefetchIteratorManualRewind()
1891+
{
1892+
$select = self::$client->createSelect();
1893+
$select->addSort('id', SelectQuery::SORT_ASC);
1894+
/** @var PrefetchIterator $prefetch */
1895+
$prefetch = self::$client->getPlugin('prefetchiterator');
1896+
$prefetch->setPrefetch(5);
1897+
$prefetch->setQuery($select);
1898+
1899+
// check if valid (this will fetch the first set of documents)
1900+
$this->assertTrue($prefetch->valid());
1901+
// check that we're at position 0
1902+
$this->assertSame(0, $prefetch->key());
1903+
// current document is the one with lowest alphabetical id in techproducts
1904+
$this->assertSame('0579B002', $prefetch->current()->id);
1905+
1906+
// move to an arbitrary point past the first set of fetched documents
1907+
while (12 > $prefetch->key()) {
1908+
$prefetch->next();
1909+
// this ensures the next set will be fetched when we've passed the end of a set
1910+
$this->assertTrue($prefetch->valid());
1911+
}
1912+
1913+
// check that we've reached the expected document at position 12
1914+
$this->assertSame(12, $prefetch->key());
1915+
$this->assertSame('NOK', $prefetch->current()->id);
1916+
1917+
// this resets the position and clears the last fetched result
1918+
$prefetch->rewind();
1919+
1920+
// check if valid (this will re-fetch the first set of documents)
1921+
$this->assertTrue($prefetch->valid());
1922+
// check that we're back at position 0
1923+
$this->assertSame(0, $prefetch->key());
1924+
// current document is once again the one with lowest alphabetical id in techproducts
1925+
$this->assertSame('0579B002', $prefetch->current()->id);
1926+
}
1927+
18901928
public function testExtractIntoDocument()
18911929
{
18921930
$extract = self::$client->createExtract();

0 commit comments

Comments
 (0)