Skip to content

Commit

Permalink
Clear scroll context also when empty page was received (#1660)
Browse files Browse the repository at this point in the history
Scroll contexts should always be cleared when they're not used
anymore. Retrieving an empty page from a scroll means it's finished
and the context can be cleared.
  • Loading branch information
mjanser authored and ruflin committed Sep 5, 2019
1 parent 838160b commit 9990f64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file based on the
### Improvements
* Added `native_function_invocation` CS rule [#1606](https://github.com/ruflin/Elastica/pull/1606)
* Elasticsearch test version changed from 6.5.2 to 6.6.1 [#1620](https://github.com/ruflin/Elastica/pull/1620)
* Clear scroll context also when empty page was received [#1660](https://github.com/ruflin/Elastica/pull/1660)

### Deprecated

Expand Down
10 changes: 8 additions & 2 deletions lib/Elastica/Scroll.php
Expand Up @@ -85,6 +85,7 @@ public function next()
} else {
// If there are no pages left, we do not need to query ES.
$this->clear();
$this->_currentResultSet = null;
}
}

Expand Down Expand Up @@ -147,7 +148,6 @@ public function clear()

// Reset scroll ID so valid() returns false.
$this->_nextScrollId = null;
$this->_currentResultSet = null;
}
}

Expand All @@ -164,7 +164,13 @@ protected function _setScrollId(ResultSet $resultSet)

$this->_currentResultSet = $resultSet;
++$this->currentPage;
$this->_nextScrollId = $resultSet->getResponse()->isOk() && $resultSet->count() > 0 ? $resultSet->getResponse()->getScrollId() : null;
$this->_nextScrollId = null;
if ($resultSet->getResponse()->isOk()) {
$this->_nextScrollId = $resultSet->getResponse()->getScrollId();
if (0 === $resultSet->count()) {
$this->clear();
}
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/Elastica/ScrollTest.php
Expand Up @@ -95,6 +95,8 @@ public function testEmptyScroll()

$this->assertEquals(0, $scroll->current()->count());
$this->assertFalse($scroll->valid());

$this->_assertOpenSearchContexts($search->getClient(), 0);
}

/**
Expand Down

0 comments on commit 9990f64

Please sign in to comment.