Skip to content

Commit

Permalink
Centralize logic for class definition reindexing (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-moser authored Aug 8, 2024
1 parent 50eedd2 commit b9a4de6
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 61 deletions.
3 changes: 3 additions & 0 deletions config/services/search/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ services:

Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\CachedSearchIndexMappingServiceInterface:
class: Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\CachedSearchIndexMappingService

Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface:
class: Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexService
24 changes: 7 additions & 17 deletions src/Command/DeploymentReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\CommandAlreadyRunningException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ReindexServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
use Pimcore\Console\AbstractCommand;
use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Component\Console\Command\LockableTrait;
Expand All @@ -36,10 +34,8 @@ final class DeploymentReindexCommand extends AbstractCommand
use LockableTrait;

public function __construct(
private readonly DataObjectIndexHandler $indexHandler,
private readonly EnqueueServiceInterface $enqueueService,
private readonly ReindexServiceInterface $reindexService,
private readonly SettingsStoreServiceInterface $settingsStoreService,
private readonly ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
string $name = null
) {
parent::__construct($name);
Expand Down Expand Up @@ -73,18 +69,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$classes = $classesList->load();

foreach ($classes as $classDefinition) {
$classDefinitionId = $classDefinition->getId();
$currentCheckSum = $this->indexHandler->getClassMappingCheckSum(
$this->indexHandler->getMappingProperties($classDefinition)
);
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinitionId);

if ($storedCheckSum !== $currentCheckSum) {
$updatedIds[] = $classDefinitionId;
$updated = $this->classDefinitionReindexService
->reindexClassDefinition($classDefinition, true, true)
;

$this
->reindexService
->reindexClassDefinition($classDefinition);
if ($updated) {
$updatedIds[] = $classDefinition->getId();
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Exception/ClassDefinitionIndexUpdateFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use Pimcore\Extension\Bundle\Exception\RuntimeException;

/**
* @internal
*/
final class ClassDefinitionIndexUpdateFailedException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
30 changes: 8 additions & 22 deletions src/MessageHandler/UpdateClassMappingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Message\UpdateClassMappingMessage;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

Expand All @@ -33,9 +32,8 @@ final class UpdateClassMappingHandler
use LoggerAwareTrait;

public function __construct(
private readonly DataObjectIndexHandler $dataObjectMappingHandler,
private readonly EnqueueServiceInterface $enqueueService,
private readonly SettingsStoreServiceInterface $settingsStoreService,
private readonly ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
) {
}

Expand All @@ -47,30 +45,18 @@ public function __invoke(UpdateClassMappingMessage $message): void
$classDefinition = $message->getClassDefinition();
$dispatch = $message->isDispatchQueueMessages();

$mappingProperties = $this->dataObjectMappingHandler->getMappingProperties($classDefinition);
$currentCheckSum = $this->dataObjectMappingHandler->getClassMappingCheckSum($mappingProperties);
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinition->getId());
$changed = $this->classDefinitionReindexService->reindexClassDefinition(
$classDefinition,
true,
$dispatch
);

if ($storedCheckSum === $currentCheckSum) {
if (!$changed) {
return;
}

$this->dataObjectMappingHandler
->reindexMapping(
context: $classDefinition,
mappingProperties: $mappingProperties
);

$this->settingsStoreService->storeClassMapping(
classDefinitionId: $classDefinition->getId(),
data: $this->dataObjectMappingHandler->getClassMappingCheckSum(
$mappingProperties
)
);

if ($dispatch) {
$this->enqueueService
->enqueueByClassDefinition($classDefinition)
->dispatchQueueMessages();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition;

/**
* @internal
*/
final readonly class ClassDefinitionReindexService implements ClassDefinitionReindexServiceInterface
{
public function __construct(
private DataObjectIndexHandler $dataObjectIndexHandler,
private EnqueueServiceInterface $enqueueService,
private SettingsStoreServiceInterface $settingsStoreService,
) {
}

public function reindexClassDefinition(
ClassDefinition $classDefinition,
bool $skipIfClassNotChanged = false,
bool $enqueueItems = false,
): bool {
try {
$changed = $this->reindexMapping($classDefinition, $skipIfClassNotChanged);

if ($changed && $enqueueItems) {
$this->enqueueService->enqueueByClassDefinition($classDefinition);
}

return $changed;
} catch (Exception $exception) {
throw new ClassDefinitionIndexUpdateFailedException(
message: $exception->getMessage(),
previous: $exception
);
}
}

/**
* @throws Exception
*/
private function reindexMapping(
ClassDefinition $classDefinition,
bool $skipIfClassNotChanged
): bool {
$mappingProperties = $this->dataObjectIndexHandler->getMappingProperties($classDefinition);
$currentCheckSum = $this->dataObjectIndexHandler->getClassMappingCheckSum($mappingProperties);
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinition->getId());

if ($skipIfClassNotChanged && $storedCheckSum === $currentCheckSum) {
return false;
}

$this->dataObjectIndexHandler
->reindexMapping(
context: $classDefinition,
mappingProperties: $mappingProperties
);

$this->settingsStoreService->storeClassMapping(
classDefinitionId: $classDefinition->getId(),
data: $this->dataObjectIndexHandler->getClassMappingCheckSum(
$mappingProperties
)
);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition;

use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException;
use Pimcore\Model\DataObject\ClassDefinition;

/**
* @internal
*/
interface ClassDefinitionReindexServiceInterface
{
/**
* @throws ClassDefinitionIndexUpdateFailedException
*/
public function reindexClassDefinition(
ClassDefinition $classDefinition,
bool $skipIfClassNotChanged = false,
bool $enqueueItems = false,
): bool;
}
28 changes: 6 additions & 22 deletions src/Service/SearchIndex/ReindexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\AssetIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DocumentIndexHandler;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Listing;

Expand All @@ -33,9 +32,8 @@
public function __construct(
private AssetIndexHandler $assetIndexHandler,
private DocumentIndexHandler $documentIndexHandler,
private DataObjectIndexHandler $dataObjectIndexHandler,
private EnqueueServiceInterface $enqueueService,
private SettingsStoreServiceInterface $settingsStoreService,
private ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
) {

}
Expand Down Expand Up @@ -85,26 +83,12 @@ public function reindexClassDefinition(
ClassDefinition $classDefinition,
bool $enqueueElements = true
): ReindexService {
$mappingProperties = $this->dataObjectIndexHandler->getMappingProperties($classDefinition);

$this
->dataObjectIndexHandler
->reindexMapping(
context: $classDefinition,
mappingProperties: $mappingProperties
);

$this->settingsStoreService->storeClassMapping(
classDefinitionId: $classDefinition->getId(),
data: $this->dataObjectIndexHandler->getClassMappingCheckSum($mappingProperties)
$this->classDefinitionReindexService->reindexClassDefinition(
$classDefinition,
false,
$enqueueElements
);

if ($enqueueElements) {
$this
->enqueueService
->enqueueByClassDefinition($classDefinition);
}

return $this;
}

Expand Down

0 comments on commit b9a4de6

Please sign in to comment.