-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1167: Fix CachingIterator::count()
on empty Cursor
#1118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Fix error: Cannot advance a completed or failed cursor.
* v1.16: PHPLIB-1167: Fix CachingIterator::count() on empty Cursor (mongodb#1118)
@@ -152,6 +150,8 @@ private function exhaustIterator(): void | |||
private function storeCurrentItem(): void | |||
{ | |||
if (! $this->iterator->valid()) { | |||
$this->iteratorExhausted = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at this last week but never submitted a review as I couldn't figure out a way to break the patch (^_^).
This addition struck me as a bit odd, since storeCurrentItem()
now thinks about iteratorExhausted
, but I suppose the alternative would have been copying
$this->iteratorExhausted = ! $this->iterator->valid();
...into the constructor.
I'll assume it's OK as-is, since the regression test definitely covers the original bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The private method could have been renamed.
In fact, this does 1 less call to $this->iterator->valid()
at each next()
call.
Fix PHPLIB-1167 from #1097 (comment)
Previous error when calling
CachingIterator::count()
on an empty Cursor (no result).After many tests, I have come to the conclusion that when
$iterator->valid()
isfalse
after arewind()
, it means that the$iterator
is empty. Therefore, we set the property$this->iteratorExhausted
totrue
so thenext()
is not called.