Skip to content

Commit

Permalink
fix: split massive HIS code relationship queries into smaller ones
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyian committed Jul 2, 2024
1 parent c171c04 commit 9710738
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> $ids */
$ids = $query->query(
$this->queryArgsBuilder
Expand All @@ -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<int> $ids */
$ids = $query->query(
$this->queryArgsBuilder
Expand Down
49 changes: 49 additions & 0 deletions src/Infrastructure/Repository/WpQuerySplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Fau\DegreeProgram\Common\Infrastructure\Repository;

use Fau\DegreeProgram\Common\Application\Repository\CollectionCriteria;
use WP_Query;

final class WpQuerySplitter
{
public function __construct(
private WpQueryArgsBuilder $queryArgsBuilder
) {
}

/**
* @param CollectionCriteria $criteria
* @return CollectionCriteria
*/
public function maybeSplitQuery(CollectionCriteria $criteria): CollectionCriteria
{
$hisCodes = $criteria->hisCodes();

if (count($hisCodes) <= 1) {
return $criteria;
}

$ids = [];

foreach ($hisCodes as $hisCode) {
$criteria = $criteria->withHisCodes([$hisCode]);
$query = new WP_Query();
/** @var array<int> $ids */
$ids = array_merge(
$query->query(
$this->queryArgsBuilder
->build($criteria)
->args()
),
$ids
);
}

return $criteria
->withHisCodes([])
->withInclude($ids);
}
}

0 comments on commit 9710738

Please sign in to comment.