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

fix(doctrine): memory leak when using the iterator mongodb #6878

Open
wants to merge 1 commit into
base: 3.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Doctrine/Odm/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@

private readonly int $totalItems;

private readonly int $count;

public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)

Check failure on line 43 in src/Doctrine/Odm/Paginator.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property ApiPlatform\Doctrine\Odm\Paginator::$mongoDbOdmIterator is never read, only written.
Copy link
Contributor

@GromNaN GromNaN Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can forget it.

Suggested change
public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)
public function __construct(Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)

{
$array = $mongoDbOdmIterator->toArray();

Check warning on line 45 in src/Doctrine/Odm/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Paginator.php#L45

Added line #L45 was not covered by tests
$resultsFacetInfo = $this->getFacetInfo('results');
$this->getFacetInfo('count');
$this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $array[0]['results']));

Check warning on line 48 in src/Doctrine/Odm/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Paginator.php#L48

Added line #L48 was not covered by tests

/*
* Since the {@see \MongoDB\Driver\Cursor} class does not expose information about
* skip/limit parameters of the query, the values set in the facet stage are used instead.
*/
$this->firstResult = $this->getStageInfo($resultsFacetInfo, '$skip');
$this->maxResults = $this->hasLimitZeroStage($resultsFacetInfo) ? 0 : $this->getStageInfo($resultsFacetInfo, '$limit');
$this->totalItems = $mongoDbOdmIterator->toArray()[0]['count'][0]['count'] ?? 0;
$this->totalItems = $array[0]['count'][0]['count'] ?? 0;
$this->count = is_countable($array[0]['results']) ? \count($array[0]['results']) : 0;

Check warning on line 57 in src/Doctrine/Odm/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Paginator.php#L56-L57

Added lines #L56 - L57 were not covered by tests
}

/**
Expand Down Expand Up @@ -97,15 +102,15 @@
*/
public function getIterator(): \Traversable
{
return $this->iterator ?? $this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $this->mongoDbOdmIterator->toArray()[0]['results']));
return $this->iterator;

Check warning on line 105 in src/Doctrine/Odm/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Paginator.php#L105

Added line #L105 was not covered by tests
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return is_countable($this->mongoDbOdmIterator->toArray()[0]['results']) ? \count($this->mongoDbOdmIterator->toArray()[0]['results']) : 0;
return $this->count;

Check warning on line 113 in src/Doctrine/Odm/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Paginator.php#L113

Added line #L113 was not covered by tests
}

/**
Expand Down
Loading