Skip to content

Commit

Permalink
Bugfix for mid-set rewind() in PrefetchIterator (#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascorthals authored Dec 30, 2020
1 parent 1ebff68 commit 3340357
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- PrefetchIterator::key() should return 0 instead of NULL on a fresh PrefetchIterator
- PrefetchIterator::next() shouldn't skip fetched results after PrefetchIterator::count() on a fresh PrefetchIterator
- PrefetchIterator::rewind() no longer results in duplicate documents when invoked mid-set
- fixed incorrect median function

### Changed
Expand Down
7 changes: 5 additions & 2 deletions src/Plugin/PrefetchIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ public function rewind(): void
{
$this->position = 0;

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

if (null !== $this->cursormark) {
$this->cursormark = '*';
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/AbstractTechproductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,44 @@ public function testPrefetchIteratorWithoutAndWithCursormark()
$this->assertSame($without, $with);
}

public function testPrefetchIteratorManualRewind()
{
$select = self::$client->createSelect();
$select->addSort('id', SelectQuery::SORT_ASC);
/** @var PrefetchIterator $prefetch */
$prefetch = self::$client->getPlugin('prefetchiterator');
$prefetch->setPrefetch(5);
$prefetch->setQuery($select);

// check if valid (this will fetch the first set of documents)
$this->assertTrue($prefetch->valid());
// check that we're at position 0
$this->assertSame(0, $prefetch->key());
// current document is the one with lowest alphabetical id in techproducts
$this->assertSame('0579B002', $prefetch->current()->id);

// move to an arbitrary point past the first set of fetched documents
while (12 > $prefetch->key()) {
$prefetch->next();
// this ensures the next set will be fetched when we've passed the end of a set
$this->assertTrue($prefetch->valid());
}

// check that we've reached the expected document at position 12
$this->assertSame(12, $prefetch->key());
$this->assertSame('NOK', $prefetch->current()->id);

// this resets the position and clears the last fetched result
$prefetch->rewind();

// check if valid (this will re-fetch the first set of documents)
$this->assertTrue($prefetch->valid());
// check that we're back at position 0
$this->assertSame(0, $prefetch->key());
// current document is once again the one with lowest alphabetical id in techproducts
$this->assertSame('0579B002', $prefetch->current()->id);
}

public function testExtractIntoDocument()
{
$extract = self::$client->createExtract();
Expand Down
Loading

0 comments on commit 3340357

Please sign in to comment.