Skip to content

Commit

Permalink
[5.x] Fix slowdown caused by status PR (#9928)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Apr 19, 2024
1 parent d709cf7 commit afec6d8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Stache/Query/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Entries\EntryCollection;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Support\Arr;

Expand Down Expand Up @@ -69,6 +70,39 @@ protected function getFilteredKeys()
: $this->getKeysFromCollectionsWithWheres($collections, $this->wheres);
}

private function addCollectionWheres(): void
{
$this->collections = $this->getCollectionWheres();
}

private function getCollectionWheres(): array
{
// If the collections property isn't empty, it means the user has explicitly
// queried for them. In that case, we'll use them and skip the auto-detection.
if (! empty($this->collections)) {
return $this->collections;
}

// Otherwise, we'll detect them by looking at where clauses targeting the "id" column.
$ids = collect($this->wheres)->where('column', 'id')->flatMap(fn ($where) => $where['values'] ?? [$where['value']]);

// If no IDs were queried, fall back to all collections.
if ($ids->isEmpty()) {
return Collection::handles()->all();
}

return Blink::once('entry-to-collection-map', function () {
return Collection::handles()
->flatMap(fn ($collection) => $this->getWhereColumnKeysFromStore($collection, ['column' => 'collectionHandle']))
->keys()
->mapWithKeys(function ($value) {
[$collection, $id] = explode('::', $value);

return [$id => $collection];
});
})->only($ids->all())->unique()->values()->all();
}

protected function getKeysFromCollections($collections)
{
return collect($collections)->flatMap(function ($collection) {
Expand Down Expand Up @@ -146,6 +180,8 @@ protected function getWhereColumnKeyValuesByIndex($column)

public function whereStatus(string $status)
{
$this->addCollectionWheres();

if (! in_array($status, self::STATUSES)) {
throw new \Exception("Invalid status [$status]");
}
Expand Down

0 comments on commit afec6d8

Please sign in to comment.