From 9710738442868d1d9951ff102ab150f1da537c85 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Tue, 2 Jul 2024 17:47:08 +0300 Subject: [PATCH] fix: split massive HIS code relationship queries into smaller ones --- ...abaseDegreeProgramCollectionRepository.php | 8 ++- .../Repository/WpQuerySplitter.php | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/Repository/WpQuerySplitter.php diff --git a/src/Infrastructure/Repository/WordPressDatabaseDegreeProgramCollectionRepository.php b/src/Infrastructure/Repository/WordPressDatabaseDegreeProgramCollectionRepository.php index 117f0ac..19023bd 100644 --- a/src/Infrastructure/Repository/WordPressDatabaseDegreeProgramCollectionRepository.php +++ b/src/Infrastructure/Repository/WordPressDatabaseDegreeProgramCollectionRepository.php @@ -17,13 +17,15 @@ final class WordPressDatabaseDegreeProgramCollectionRepository implements Degree { public function __construct( private DegreeProgramViewRepository $degreeProgramViewRepository, - private WpQueryArgsBuilder $queryArgsBuilder + private WpQueryArgsBuilder $queryArgsBuilder, + private WpQuerySplitter $querySplitter ) { } public function findRawCollection(CollectionCriteria $criteria): PaginationAwareCollection { $query = new WP_Query(); + $criteria = $this->querySplitter->maybeSplitQuery($criteria); /** @var array $ids */ $ids = $query->query( $this->queryArgsBuilder @@ -48,6 +50,10 @@ public function findRawCollection(CollectionCriteria $criteria): PaginationAware public function findTranslatedCollection(CollectionCriteria $criteria, string $languageCode): PaginationAwareCollection { $query = new WP_Query(); + $criteria = $this->querySplitter->maybeSplitQuery( + $criteria->withLanguage($languageCode) + ); + /** @var array $ids */ $ids = $query->query( $this->queryArgsBuilder diff --git a/src/Infrastructure/Repository/WpQuerySplitter.php b/src/Infrastructure/Repository/WpQuerySplitter.php new file mode 100644 index 0000000..5f2ae4e --- /dev/null +++ b/src/Infrastructure/Repository/WpQuerySplitter.php @@ -0,0 +1,49 @@ +hisCodes(); + + if (count($hisCodes) <= 1) { + return $criteria; + } + + $ids = []; + + foreach ($hisCodes as $hisCode) { + $criteria = $criteria->withHisCodes([$hisCode]); + $query = new WP_Query(); + /** @var array $ids */ + $ids = array_merge( + $query->query( + $this->queryArgsBuilder + ->build($criteria) + ->args() + ), + $ids + ); + } + + return $criteria + ->withHisCodes([]) + ->withInclude($ids); + } +}