Skip to content

Commit

Permalink
Refactored ContentIdList Strategy to rely on result wrapper object
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Feb 9, 2024
1 parent 6e12b51 commit be2af07
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\ContentList;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
use Ibexa\Core\Search\Indexer\ContentIdBatchList;
use Symfony\Component\Console\Input\InputInterface;

/**
Expand All @@ -23,9 +24,6 @@ final class ContentTypeInputGeneratorStrategy implements ContentIdListGeneratorS
{
private ContentService $contentService;

/** @var array<string, \Ibexa\Contracts\Core\Repository\Values\Content\ContentList> */
private static array $inMemoryContentListCache = [];

public function __construct(ContentService $contentService)
{
$this->contentService = $contentService;
Expand All @@ -34,13 +32,22 @@ public function __construct(ContentService $contentService)
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
public function getGenerator(InputInterface $input, int $iterationCount): Generator
public function getBatchList(InputInterface $input, int $batchSize): ContentIdBatchList
{
$contentList = $this->getContentList($input->getOption('content-type'));

return new ContentIdBatchList(
$this->buildGenerator($contentList, $batchSize),
$contentList->getTotalCount(),
);
}

private function buildGenerator(ContentList $contentList, int $batchSize): Generator
{
$contentIds = [];
foreach ($contentList as $content) {
$contentIds[] = $content->getVersionInfo()->getContentInfo()->getId();
if (count($contentIds) >= $iterationCount) {
if (count($contentIds) >= $batchSize) {
yield $contentIds;
$contentIds = [];
}
Expand All @@ -50,37 +57,23 @@ public function getGenerator(InputInterface $input, int $iterationCount): Genera
}
}

public function shouldPurgeIndex(): bool
public function shouldPurgeIndex(InputInterface $input): bool
{
return false;
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
public function getCount(InputInterface $input): int
{
return $this->getContentList($input->getOption('content-type'))->getTotalCount();
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
*/
private function getContentList(string $contentTypeIdentifier): ContentList
{
if (isset(self::$inMemoryContentListCache[$contentTypeIdentifier])) {
return self::$inMemoryContentListCache[$contentTypeIdentifier];
}

$filter = new Filter();
$filter
->withCriterion(
new Query\Criterion\ContentTypeIdentifier($contentTypeIdentifier)
)
;

self::$inMemoryContentListCache[$contentTypeIdentifier] = $this->contentService->find($filter);

return self::$inMemoryContentListCache[$contentTypeIdentifier];
return $this->contentService->find($filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@

namespace Ibexa\Bundle\Core\Command\Indexer;

use Generator;
use Ibexa\Core\Search\Indexer\ContentIdBatchList;
use Symfony\Component\Console\Input\InputInterface;

/**
* @internal
*/
interface ContentIdListGeneratorStrategyInterface
{
/**
* @return \Generator<int, array<int>>
*/
public function getGenerator(InputInterface $input, int $iterationCount): Generator;
public function shouldPurgeIndex(InputInterface $input): bool;

public function getCount(InputInterface $input): int;

public function shouldPurgeIndex(): bool;
public function getBatchList(InputInterface $input, int $batchSize): ContentIdBatchList;
}
34 changes: 23 additions & 11 deletions src/bundle/Core/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function count;
use DateTime;
use const DIRECTORY_SEPARATOR;
use Generator;
use Ibexa\Bundle\Core\Command\Indexer\ContentIdListGeneratorStrategyInterface;
use Ibexa\Contracts\Core\Persistence\Content\Location\Handler;
use Ibexa\Contracts\Core\Search\Content\IndexerGateway;
Expand Down Expand Up @@ -257,20 +256,20 @@ protected function indexIncrementally(

if ($since = $input->getOption('since')) {
$count = $this->gateway->countContentSince(new DateTime($since));
$generator = $this->gateway->getContentSince(new DateTime($since), $iterationCount);
$batchList = $this->gateway->getContentSince(new DateTime($since), $iterationCount);
$purge = false;
} elseif ($locationId = (int) $input->getOption('subtree')) {
$location = $this->locationHandler->load($locationId);
$count = $this->gateway->countContentInSubtree($location->pathString);
$generator = $this->gateway->getContentInSubtree($location->pathString, $iterationCount);
$batchList = $this->gateway->getContentInSubtree($location->pathString, $iterationCount);
$purge = false;
} elseif (!empty($input->getOption('content-type'))) {
$count = $this->contentIdListGeneratorStrategy->getCount($input);
$generator = $this->contentIdListGeneratorStrategy->getGenerator($input, $iterationCount);
$purge = $this->contentIdListGeneratorStrategy->shouldPurgeIndex();
$batchList = $this->contentIdListGeneratorStrategy->getBatchList($input, $iterationCount);
$count = $batchList->getCount();
$purge = $this->contentIdListGeneratorStrategy->shouldPurgeIndex($input);
} else {
$count = $this->gateway->countAllContent();
$generator = $this->gateway->getAllContent($iterationCount);
$batchList = $this->gateway->getAllContent($iterationCount);
$purge = !$input->getOption('no-purge');
}

Expand Down Expand Up @@ -306,13 +305,13 @@ protected function indexIncrementally(
if ($processCount > 1) {
$this->runParallelProcess(
$progress,
$generator,
$batchList,
(int)$processCount,
$commit
);
} else {
// if we only have one process, or less iterations to warrant running several, we index it all inline
foreach ($generator as $contentIds) {
foreach ($batchList as $contentIds) {
$this->searchIndexer->updateSearchIndex($contentIds, $commit);
$progress->advance(1);
}
Expand All @@ -329,14 +328,27 @@ protected function indexIncrementally(
}

/**
* @param iterable<int, array<int>> $results
*
* @return \Generator<int, array<int>>
*/
private function buildGenerator(iterable $results): \Generator
{
yield from $results;
}

/**
* @param iterable<int, array<int>> $batchList
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
private function runParallelProcess(
ProgressBar $progress,
Generator $generator,
iterable $batchList,
int $processCount,
bool $commit
): void {
$generator = $this->buildGenerator($batchList);
/** @var \Symfony\Component\Process\Process[]|null[] $processes */
$processes = array_fill(0, $processCount, null);
do {
Expand Down Expand Up @@ -378,7 +390,7 @@ private function runParallelProcess(
}

/**
* @param array $contentIds
* @param int[] $contentIds
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
Expand Down

0 comments on commit be2af07

Please sign in to comment.