Skip to content

Commit

Permalink
Merge pull request #13 from RRZE-Webteam/FAU-428
Browse files Browse the repository at this point in the history
[FAU-428] Split massive HIS code relationship queries into smaller ones
  • Loading branch information
zhyian authored Jul 3, 2024
2 parents c171c04 + 9710738 commit fc1f347
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 fc1f347

Please sign in to comment.