Skip to content
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

PHPLIB-1167: Fix CachingIterator::count() on empty Cursor #1118

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jun 22, 2023

Fix PHPLIB-1167 from #1097 (comment)

Previous error when calling CachingIterator::count() on an empty Cursor (no result).

Cannot advance a completed or failed cursor.

After many tests, I have come to the conclusion that when $iterator->valid() is false after a rewind(), it means that the $iterator is empty. Therefore, we set the property $this->iteratorExhausted to true so the next() is not called.

Fix error: Cannot advance a completed or failed cursor.
@GromNaN GromNaN merged commit 0d13981 into mongodb:v1.16 Jun 26, 2023
11 checks passed
@GromNaN GromNaN deleted the PHPLIB-1167 branch June 26, 2023 08:24
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jun 26, 2023
* 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;
Copy link
Member

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.

Copy link
Member Author

@GromNaN GromNaN Jun 26, 2023

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.

GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jun 26, 2023
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jun 27, 2023
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jun 27, 2023
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jul 7, 2023
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jul 18, 2023
GromNaN added a commit to GromNaN/mongo-php-library that referenced this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants