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
NEXT-11081 - Improve performance of RepositoryIterator and remove a possible next request #3602
base: trunk
Are you sure you want to change the base?
Conversation
$builder = new ProductBuilder(new IdsCollection(), 'product1'); | ||
$builder->price(1); | ||
$productRepository->create([$builder->build()], $context); | ||
|
||
$builder = new ProductBuilder(new IdsCollection(), 'product2'); | ||
$builder->price(1); | ||
$productRepository->create([$builder->build()], $context); | ||
|
||
$builder = new ProductBuilder(new IdsCollection(), 'product3'); | ||
$builder->price(1); | ||
$productRepository->create([$builder->build()], $context); |
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.
create the products with one create
call. to make the test faster
$criteria->setLimit(2); | ||
|
||
$searchesCount = 0; | ||
$eventDispatcher->addListener(EntitySearchedEvent::class, function (EntitySearchedEvent $event) use (&$searchesCount): void { |
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.
this is evil as nobody will remove your addListener
. use $this->addEventListener
that does that for you
$iterator = new RepositoryIterator($productRepository, $context, $criteria); | ||
|
||
while ($iterator->fetchIds() !== null) { | ||
// fetch all ids and count searches |
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 would collect the data and assert if we really got so many results back.
@@ -56,6 +58,23 @@ public function __construct( | |||
$this->context = clone $context; | |||
} | |||
|
|||
public function reset(): void |
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.
Maybe name it rewind
? https://www.php.net/manual/en/function.rewind.php That's what php use for such things.
A simple php doc would also be nice:
Rewinds the Iterator to the beginning
@@ -71,19 +90,29 @@ public function getTotal(): int | |||
*/ | |||
public function fetchIds(): ?array | |||
{ | |||
if ($this->endReached) { |
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.
What about
$repositoryIterator->loop(function(array $entities) {
});
$repositoryIterator->loopIds(function(array $ids) {
});
In this case we don't have to leak all of this internal "rewind" etc
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.
Good thought. How would one break this loop? Return false? In my suggestions of the getIterator implementation you have the option to just not continue using the iterator. But I like the closure approach. Feels not very php though
0. Context
This is a follow-up pull request to #1311 . The reasoning did not change but I rebased, add a test and moved the change from the CartRuleLoader into the RuleLoader as this has been moved.
1. Why is this change necessary?
The RuleLoader already makes this educated guess that there won't be a next result when the current iteration doesn't fill the required criteria limit. Why don't do this in general?
2. What does this change do, exactly?
Add an other state to the already stateful RepositoryIterator that knows when to stop.
During test writing I noticed, that path coverage needs a different entity definition, that is iterated differently. But to reset an iterator you need to know how to reset the criteria. So I added a reset method as well.
3. Describe each step to reproduce the issue or behaviour.
4. Please link to the relevant issues (if any).
https://issues.shopware.com/issues/NEXT-11081
5. Checklist