From 56afb123516df1cb9da00cf203514c8fbf679ea2 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Fri, 13 Dec 2024 15:58:26 +0100 Subject: [PATCH] Generalize the ReindexConfig --- .../symfony/src/Command/ReindexCommand.php | 27 ++---- packages/seal/src/Engine.php | 19 ++-- packages/seal/src/EngineInterface.php | 7 +- .../seal/src/Reindex/PartialReindexConfig.php | 40 -------- packages/seal/src/Reindex/ReindexConfig.php | 93 +++++++++++++++++++ .../src/Reindex/ReindexProviderInterface.php | 2 +- 6 files changed, 114 insertions(+), 74 deletions(-) delete mode 100644 packages/seal/src/Reindex/PartialReindexConfig.php create mode 100644 packages/seal/src/Reindex/ReindexConfig.php diff --git a/integrations/symfony/src/Command/ReindexCommand.php b/integrations/symfony/src/Command/ReindexCommand.php index 2586c021..56090160 100644 --- a/integrations/symfony/src/Command/ReindexCommand.php +++ b/integrations/symfony/src/Command/ReindexCommand.php @@ -14,7 +14,7 @@ namespace CmsIg\Seal\Integration\Symfony\Command; use CmsIg\Seal\EngineRegistry; -use CmsIg\Seal\Reindex\PartialReindexConfig; +use CmsIg\Seal\Reindex\ReindexConfig; use CmsIg\Seal\Reindex\ReindexProviderInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -44,7 +44,7 @@ protected function configure(): void $this->addOption('engine', null, InputOption::VALUE_REQUIRED, 'The name of the engine to create the schema for.'); $this->addOption('index', null, InputOption::VALUE_REQUIRED, 'The name of the index to create the schema for.'); $this->addOption('drop', null, InputOption::VALUE_NONE, 'Drop the index before reindexing.'); - $this->addOption('bulk-size', null, InputOption::VALUE_REQUIRED, 'The bulk size for reindexing, defaults to 100.'); + $this->addOption('bulk-size', null, InputOption::VALUE_REQUIRED, 'The bulk size for reindexing, defaults to 100.', 100); $this->addOption('datetime-boundary', null, InputOption::VALUE_REQUIRED, 'Do a partial update and limit to only documents that have been changed since a given datetime object.'); $this->addOption('identifiers', null, InputOption::VALUE_REQUIRED, 'Do a partial update and limit to only a comma-separated list of identifiers.'); } @@ -54,17 +54,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $ui = new SymfonyStyle($input, $output); /** @var string|null $engineName */ $engineName = $input->getOption('engine'); - /** @var string|null $indexName */ - $indexName = $input->getOption('index'); - /** @var bool $drop */ - $drop = $input->getOption('drop'); - /** @var int $bulkSize */ - $bulkSize = ((int) $input->getOption('bulk-size')) ?: 100; // @phpstan-ignore-line - - $partialReindexConfig = PartialReindexConfig::createConditional( - $input->getOption('datetime-boundary') ? new \DateTimeImmutable($input->getOption('datetime-boundary')) : null, - $input->getOption('identifiers') ? \explode(',', (string) $input->getOption('identifiers')) : null, - ); + + $reindexConfig = ReindexConfig::create() + ->withIndex($input->getOption('index')) + ->withBulkSize((int) $input->getOption('bulk-size')) + ->withDropIndex((bool) $input->getOption('drop')) + ->withDateTimeBoundary($input->getOption('datetime-boundary') ? new \DateTimeImmutable($input->getOption('datetime-boundary')) : null) + ->withIdentifiers(\explode(',', (string) $input->getOption('identifiers'))); foreach ($this->engineRegistry->getEngines() as $name => $engine) { if ($engineName && $engineName !== $name) { @@ -77,9 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $engine->reindex( $this->reindexProviders, - $indexName, - $drop, - $bulkSize, + $reindexConfig, function (string $index, int $count, int|null $total) use ($progressBar) { if (null !== $total) { $progressBar->setMaxSteps($total); @@ -88,7 +82,6 @@ function (string $index, int $count, int|null $total) use ($progressBar) { $progressBar->setMessage($index); $progressBar->setProgress($count); }, - $partialReindexConfig, ); $progressBar->finish(); diff --git a/packages/seal/src/Engine.php b/packages/seal/src/Engine.php index 553efab3..e5131bf8 100644 --- a/packages/seal/src/Engine.php +++ b/packages/seal/src/Engine.php @@ -15,7 +15,7 @@ use CmsIg\Seal\Adapter\AdapterInterface; use CmsIg\Seal\Exception\DocumentNotFoundException; -use CmsIg\Seal\Reindex\PartialReindexConfig; +use CmsIg\Seal\Reindex\ReindexConfig; use CmsIg\Seal\Reindex\ReindexProviderInterface; use CmsIg\Seal\Schema\Schema; use CmsIg\Seal\Search\Condition\IdentifierCondition; @@ -135,11 +135,8 @@ public function dropSchema(array $options = []): TaskInterface|null public function reindex( iterable $reindexProviders, - string|null $index = null, - bool $dropIndex = false, - int $bulkSize = 100, + ReindexConfig $reindexConfig, callable|null $progressCallback = null, - PartialReindexConfig|null $partialReindexConfig = null, ): void { /** @var array $reindexProvidersPerIndex */ $reindexProvidersPerIndex = []; @@ -148,13 +145,13 @@ public function reindex( continue; } - if ($reindexProvider::getIndex() === $index || null === $index) { + if ($reindexProvider::getIndex() === $reindexConfig->getIndex() || null === $reindexConfig->getIndex()) { $reindexProvidersPerIndex[$reindexProvider::getIndex()][] = $reindexProvider; } } foreach ($reindexProvidersPerIndex as $index => $reindexProviders) { - if ($dropIndex && $this->existIndex($index)) { + if ($reindexConfig->shouldDropIndex() && $this->existIndex($index)) { $task = $this->dropIndex($index, ['return_slow_promise_result' => true]); $task->wait(); $task = $this->createIndex($index, ['return_slow_promise_result' => true]); @@ -167,18 +164,18 @@ public function reindex( foreach ($reindexProviders as $reindexProvider) { $this->bulk( $index, - (function () use ($index, $reindexProvider, $bulkSize, $progressCallback, $partialReindexConfig) { + (function () use ($index, $reindexProvider, $reindexConfig, $progressCallback) { $count = 0; $total = $reindexProvider->total(); $lastCount = -1; - foreach ($reindexProvider->provide($partialReindexConfig) as $document) { + foreach ($reindexProvider->provide($reindexConfig) as $document) { ++$count; yield $document; if (null !== $progressCallback - && 0 === ($count % $bulkSize) + && 0 === ($count % $reindexConfig->getBulkSize()) ) { $lastCount = $count; $progressCallback($index, $count, $total); @@ -192,7 +189,7 @@ public function reindex( } })(), [], - $bulkSize, + $reindexConfig->getBulkSize(), ); } } diff --git a/packages/seal/src/EngineInterface.php b/packages/seal/src/EngineInterface.php index c7342a82..cebf88f2 100644 --- a/packages/seal/src/EngineInterface.php +++ b/packages/seal/src/EngineInterface.php @@ -14,7 +14,7 @@ namespace CmsIg\Seal; use CmsIg\Seal\Exception\DocumentNotFoundException; -use CmsIg\Seal\Reindex\PartialReindexConfig; +use CmsIg\Seal\Reindex\ReindexConfig; use CmsIg\Seal\Reindex\ReindexProviderInterface; use CmsIg\Seal\Search\SearchBuilder; use CmsIg\Seal\Task\TaskInterface; @@ -93,10 +93,7 @@ public function dropSchema(array $options = []): TaskInterface|null; */ public function reindex( iterable $reindexProviders, - string|null $index = null, - bool $dropIndex = false, - int $bulkSize = 100, + ReindexConfig $reindexConfig, callable|null $progressCallback = null, - PartialReindexConfig|null $partialReindexConfig = null, ): void; } diff --git a/packages/seal/src/Reindex/PartialReindexConfig.php b/packages/seal/src/Reindex/PartialReindexConfig.php deleted file mode 100644 index 70b767db..00000000 --- a/packages/seal/src/Reindex/PartialReindexConfig.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace CmsIg\Seal\Reindex; - -final class PartialReindexConfig -{ - public function __construct(private readonly \DateTimeInterface|null $dateTimeBoundary = null, private readonly array $identifiers = []) - { - } - - public function getDateTimeBoundary(): \DateTimeInterface|null - { - return $this->dateTimeBoundary; - } - - public function getIdentifiers(): array - { - return $this->identifiers; - } - - public static function createConditional(\DateTimeInterface|null $dateTimeBoundary, array|null $identifiers): self|null - { - if (!$dateTimeBoundary instanceof \DateTimeInterface && null === $identifiers) { - return null; - } - - return new self($dateTimeBoundary, $identifiers); - } -} diff --git a/packages/seal/src/Reindex/ReindexConfig.php b/packages/seal/src/Reindex/ReindexConfig.php new file mode 100644 index 00000000..e3826ee8 --- /dev/null +++ b/packages/seal/src/Reindex/ReindexConfig.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CmsIg\Seal\Reindex; + +final class ReindexConfig +{ + private string|null $index = null; + private bool $dropIndex = false; + private int $bulkSize = 100; + private \DateTimeInterface|null $dateTimeBoundary = null; + private array $identifiers = []; + + public function getIndex(): string|null + { + return $this->index; + } + + public function shouldDropIndex(): bool + { + return $this->dropIndex; + } + + public function getBulkSize(): int + { + return $this->bulkSize; + } + + public function getDateTimeBoundary(): \DateTimeInterface|null + { + return $this->dateTimeBoundary; + } + + public function getIdentifiers(): array + { + return $this->identifiers; + } + + public function withDateTimeBoundary(\DateTimeInterface|null $dateTimeBoundary): self + { + $clone = clone $this; + $clone->dateTimeBoundary = $dateTimeBoundary; + + return $clone; + } + + public function withIdentifiers(array $identifiers): self + { + $clone = clone $this; + $clone->identifiers = $identifiers; + + return $clone; + } + + public function withBulkSize(int $bulkSize): self + { + $clone = clone $this; + $clone->bulkSize = $bulkSize; + + return $clone; + } + + public function withIndex(string|null $index): self + { + $clone = clone $this; + $clone->index = $index; + + return $clone; + } + + public function withDropIndex(bool $dropIndex): self + { + $clone = clone $this; + $clone->dropIndex = $dropIndex; + + return $clone; + } + + public static function create(): self + { + return new self(); + } +} diff --git a/packages/seal/src/Reindex/ReindexProviderInterface.php b/packages/seal/src/Reindex/ReindexProviderInterface.php index 589f215c..53dee4b5 100644 --- a/packages/seal/src/Reindex/ReindexProviderInterface.php +++ b/packages/seal/src/Reindex/ReindexProviderInterface.php @@ -25,7 +25,7 @@ public function total(): int|null; * * @return \Generator> */ - public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator; + public function provide(ReindexConfig $reindexConfig): \Generator; /** * The name of the index for which the documents are for.