Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing partial reindexes #460

Draft
wants to merge 1 commit into
base: 0.6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .examples/laravel/app/Search/BlogReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Search;

use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;

class BlogReindexProvider implements ReindexProviderInterface
Expand All @@ -13,7 +14,7 @@ public function total(): int|null
return 3;
}

public function provide(): \Generator
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator
{
yield [
'id' => 1,
Expand Down
3 changes: 2 additions & 1 deletion .examples/mezzio/src/App/src/Search/BlogReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Search;

use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;

class BlogReindexProvider implements ReindexProviderInterface
Expand All @@ -13,7 +14,7 @@ public function total(): int|null
return 3;
}

public function provide(): \Generator
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator
{
yield [
'id' => 1,
Expand Down
3 changes: 2 additions & 1 deletion .examples/spiral/app/src/Search/BlogReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Search;

use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;

class BlogReindexProvider implements ReindexProviderInterface
Expand All @@ -13,7 +14,7 @@ public function total(): int|null
return 3;
}

public function provide(): \Generator
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator
{
yield [
'id' => 1,
Expand Down
3 changes: 2 additions & 1 deletion .examples/symfony/src/Search/BlogReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Search;

use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;

class BlogReindexProvider implements ReindexProviderInterface
Expand All @@ -13,7 +14,7 @@ public function total(): int|null
return 3;
}

public function provide(): \Generator
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator
{
yield [
'id' => 1,
Expand Down
3 changes: 2 additions & 1 deletion .examples/yii/src/Search/BlogReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Search;

use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;

class BlogReindexProvider implements ReindexProviderInterface
Expand All @@ -13,7 +14,7 @@ public function total(): int|null
return 3;
}

public function provide(): \Generator
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator
{
yield [
'id' => 1,
Expand Down
9 changes: 9 additions & 0 deletions integrations/symfony/src/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CmsIg\Seal\Integration\Symfony\Command;

use CmsIg\Seal\EngineRegistry;
use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -44,6 +45,8 @@ protected function configure(): void
$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('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.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -58,6 +61,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @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') ? PartialReindexConfig::createGeneratorFromArray(\explode(',', (string) $input->getOption('identifiers'))) : null,
);

foreach ($this->engineRegistry->getEngines() as $name => $engine) {
if ($engineName && $engineName !== $name) {
continue;
Expand All @@ -80,6 +88,7 @@ function (string $index, int $count, int|null $total) use ($progressBar) {
$progressBar->setMessage($index);
$progressBar->setProgress($count);
},
$partialReindexConfig,
);

$progressBar->finish();
Expand Down
6 changes: 4 additions & 2 deletions packages/seal/src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CmsIg\Seal\Adapter\AdapterInterface;
use CmsIg\Seal\Exception\DocumentNotFoundException;
use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;
use CmsIg\Seal\Schema\Schema;
use CmsIg\Seal\Search\Condition\IdentifierCondition;
Expand Down Expand Up @@ -138,6 +139,7 @@ public function reindex(
bool $dropIndex = false,
int $bulkSize = 100,
callable|null $progressCallback = null,
PartialReindexConfig|null $partialReindexConfig = null,
): void {
/** @var array<string, ReindexProviderInterface[]> $reindexProvidersPerIndex */
$reindexProvidersPerIndex = [];
Expand Down Expand Up @@ -165,12 +167,12 @@ public function reindex(
foreach ($reindexProviders as $reindexProvider) {
$this->bulk(
$index,
(function () use ($index, $reindexProvider, $bulkSize, $progressCallback) {
(function () use ($index, $reindexProvider, $bulkSize, $progressCallback, $partialReindexConfig) {
$count = 0;
$total = $reindexProvider->total();

$lastCount = -1;
foreach ($reindexProvider->provide() as $document) {
foreach ($reindexProvider->provide($partialReindexConfig) as $document) {
++$count;

yield $document;
Expand Down
2 changes: 2 additions & 0 deletions packages/seal/src/EngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CmsIg\Seal;

use CmsIg\Seal\Exception\DocumentNotFoundException;
use CmsIg\Seal\Reindex\PartialReindexConfig;
use CmsIg\Seal\Reindex\ReindexProviderInterface;
use CmsIg\Seal\Search\SearchBuilder;
use CmsIg\Seal\Task\TaskInterface;
Expand Down Expand Up @@ -96,5 +97,6 @@ public function reindex(
bool $dropIndex = false,
int $bulkSize = 100,
callable|null $progressCallback = null,
PartialReindexConfig|null $partialReindexConfig = null,
): void;
}
58 changes: 58 additions & 0 deletions packages/seal/src/Reindex/PartialReindexConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CMS-IG SEAL project.
*
* (c) Alexander Schranz <[email protected]>
*
* 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 \DateTimeInterface|\Closure $dateTimeBoundary, private \Generator|\Closure $identifiers)
{
}

public function getDateTimeBoundary(): \DateTimeInterface
{
$this->resolveIfClosure($this->dateTimeBoundary);

return $this->dateTimeBoundary;
}

public function getIdentifiers(): \Generator
{
$this->resolveIfClosure($this->identifiers);

return $this->identifiers;
}

public static function createConditional(\DateTimeInterface|\Closure|null $dateTimeBoundary, \Generator|\Closure|null $identifiers): self|null
{
if (null === $dateTimeBoundary && null === $identifiers) {
return null;
}

return new self($dateTimeBoundary, $identifiers);
}

public static function createGeneratorFromArray(array $array): \Generator
{
foreach ($array as $value) {
yield $value;
}
}

private function resolveIfClosure(mixed &$property): void
{
if ($property instanceof \Closure) {
$property = $property($this);
}
}
}
2 changes: 1 addition & 1 deletion packages/seal/src/Reindex/ReindexProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function total(): int|null;
*
* @return \Generator<array<string, mixed>>
*/
public function provide(): \Generator;
public function provide(PartialReindexConfig|null $partialReindexConfig = null): \Generator;

/**
* The name of the index for which the documents are for.
Expand Down
Loading