Skip to content

Commit b9a4de6

Browse files
authored
Centralize logic for class definition reindexing (#200)
1 parent 50eedd2 commit b9a4de6

File tree

7 files changed

+174
-61
lines changed

7 files changed

+174
-61
lines changed

config/services/search/index.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ services:
7676

7777
Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\CachedSearchIndexMappingServiceInterface:
7878
class: Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\CachedSearchIndexMappingService
79+
80+
Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface:
81+
class: Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexService

src/Command/DeploymentReindexCommand.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818

1919
use Exception;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Exception\CommandAlreadyRunningException;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
22-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
23-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ReindexServiceInterface;
24-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
2523
use Pimcore\Console\AbstractCommand;
2624
use Pimcore\Model\DataObject\ClassDefinition;
2725
use Symfony\Component\Console\Command\LockableTrait;
@@ -36,10 +34,8 @@ final class DeploymentReindexCommand extends AbstractCommand
3634
use LockableTrait;
3735

3836
public function __construct(
39-
private readonly DataObjectIndexHandler $indexHandler,
4037
private readonly EnqueueServiceInterface $enqueueService,
41-
private readonly ReindexServiceInterface $reindexService,
42-
private readonly SettingsStoreServiceInterface $settingsStoreService,
38+
private readonly ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
4339
string $name = null
4440
) {
4541
parent::__construct($name);
@@ -73,18 +69,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7369
$classes = $classesList->load();
7470

7571
foreach ($classes as $classDefinition) {
76-
$classDefinitionId = $classDefinition->getId();
77-
$currentCheckSum = $this->indexHandler->getClassMappingCheckSum(
78-
$this->indexHandler->getMappingProperties($classDefinition)
79-
);
80-
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinitionId);
81-
82-
if ($storedCheckSum !== $currentCheckSum) {
83-
$updatedIds[] = $classDefinitionId;
72+
$updated = $this->classDefinitionReindexService
73+
->reindexClassDefinition($classDefinition, true, true)
74+
;
8475

85-
$this
86-
->reindexService
87-
->reindexClassDefinition($classDefinition);
76+
if ($updated) {
77+
$updatedIds[] = $classDefinition->getId();
8878
}
8979
}
9080

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;
18+
19+
use Pimcore\Extension\Bundle\Exception\RuntimeException;
20+
21+
/**
22+
* @internal
23+
*/
24+
final class ClassDefinitionIndexUpdateFailedException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
25+
{
26+
}

src/MessageHandler/UpdateClassMappingHandler.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
use Exception;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Message\UpdateClassMappingMessage;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
22-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
23-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
2423
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
2524
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2625

@@ -33,9 +32,8 @@ final class UpdateClassMappingHandler
3332
use LoggerAwareTrait;
3433

3534
public function __construct(
36-
private readonly DataObjectIndexHandler $dataObjectMappingHandler,
3735
private readonly EnqueueServiceInterface $enqueueService,
38-
private readonly SettingsStoreServiceInterface $settingsStoreService,
36+
private readonly ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
3937
) {
4038
}
4139

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

50-
$mappingProperties = $this->dataObjectMappingHandler->getMappingProperties($classDefinition);
51-
$currentCheckSum = $this->dataObjectMappingHandler->getClassMappingCheckSum($mappingProperties);
52-
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinition->getId());
48+
$changed = $this->classDefinitionReindexService->reindexClassDefinition(
49+
$classDefinition,
50+
true,
51+
$dispatch
52+
);
5353

54-
if ($storedCheckSum === $currentCheckSum) {
54+
if (!$changed) {
5555
return;
5656
}
5757

58-
$this->dataObjectMappingHandler
59-
->reindexMapping(
60-
context: $classDefinition,
61-
mappingProperties: $mappingProperties
62-
);
63-
64-
$this->settingsStoreService->storeClassMapping(
65-
classDefinitionId: $classDefinition->getId(),
66-
data: $this->dataObjectMappingHandler->getClassMappingCheckSum(
67-
$mappingProperties
68-
)
69-
);
70-
7158
if ($dispatch) {
7259
$this->enqueueService
73-
->enqueueByClassDefinition($classDefinition)
7460
->dispatchQueueMessages();
7561
}
7662
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition;
18+
19+
use Exception;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
22+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
24+
use Pimcore\Model\DataObject\ClassDefinition;
25+
26+
/**
27+
* @internal
28+
*/
29+
final readonly class ClassDefinitionReindexService implements ClassDefinitionReindexServiceInterface
30+
{
31+
public function __construct(
32+
private DataObjectIndexHandler $dataObjectIndexHandler,
33+
private EnqueueServiceInterface $enqueueService,
34+
private SettingsStoreServiceInterface $settingsStoreService,
35+
) {
36+
}
37+
38+
public function reindexClassDefinition(
39+
ClassDefinition $classDefinition,
40+
bool $skipIfClassNotChanged = false,
41+
bool $enqueueItems = false,
42+
): bool {
43+
try {
44+
$changed = $this->reindexMapping($classDefinition, $skipIfClassNotChanged);
45+
46+
if ($changed && $enqueueItems) {
47+
$this->enqueueService->enqueueByClassDefinition($classDefinition);
48+
}
49+
50+
return $changed;
51+
} catch (Exception $exception) {
52+
throw new ClassDefinitionIndexUpdateFailedException(
53+
message: $exception->getMessage(),
54+
previous: $exception
55+
);
56+
}
57+
}
58+
59+
/**
60+
* @throws Exception
61+
*/
62+
private function reindexMapping(
63+
ClassDefinition $classDefinition,
64+
bool $skipIfClassNotChanged
65+
): bool {
66+
$mappingProperties = $this->dataObjectIndexHandler->getMappingProperties($classDefinition);
67+
$currentCheckSum = $this->dataObjectIndexHandler->getClassMappingCheckSum($mappingProperties);
68+
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinition->getId());
69+
70+
if ($skipIfClassNotChanged && $storedCheckSum === $currentCheckSum) {
71+
return false;
72+
}
73+
74+
$this->dataObjectIndexHandler
75+
->reindexMapping(
76+
context: $classDefinition,
77+
mappingProperties: $mappingProperties
78+
);
79+
80+
$this->settingsStoreService->storeClassMapping(
81+
classDefinitionId: $classDefinition->getId(),
82+
data: $this->dataObjectIndexHandler->getClassMappingCheckSum(
83+
$mappingProperties
84+
)
85+
);
86+
87+
return true;
88+
}
89+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition;
18+
19+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException;
20+
use Pimcore\Model\DataObject\ClassDefinition;
21+
22+
/**
23+
* @internal
24+
*/
25+
interface ClassDefinitionReindexServiceInterface
26+
{
27+
/**
28+
* @throws ClassDefinitionIndexUpdateFailedException
29+
*/
30+
public function reindexClassDefinition(
31+
ClassDefinition $classDefinition,
32+
bool $skipIfClassNotChanged = false,
33+
bool $enqueueItems = false,
34+
): bool;
35+
}

src/Service/SearchIndex/ReindexService.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex;
1818

1919
use Exception;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\AssetIndexHandler;
22-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
2323
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DocumentIndexHandler;
24-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
2524
use Pimcore\Model\DataObject\ClassDefinition;
2625
use Pimcore\Model\DataObject\ClassDefinition\Listing;
2726

@@ -33,9 +32,8 @@
3332
public function __construct(
3433
private AssetIndexHandler $assetIndexHandler,
3534
private DocumentIndexHandler $documentIndexHandler,
36-
private DataObjectIndexHandler $dataObjectIndexHandler,
3735
private EnqueueServiceInterface $enqueueService,
38-
private SettingsStoreServiceInterface $settingsStoreService,
36+
private ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
3937
) {
4038

4139
}
@@ -85,26 +83,12 @@ public function reindexClassDefinition(
8583
ClassDefinition $classDefinition,
8684
bool $enqueueElements = true
8785
): ReindexService {
88-
$mappingProperties = $this->dataObjectIndexHandler->getMappingProperties($classDefinition);
89-
90-
$this
91-
->dataObjectIndexHandler
92-
->reindexMapping(
93-
context: $classDefinition,
94-
mappingProperties: $mappingProperties
95-
);
96-
97-
$this->settingsStoreService->storeClassMapping(
98-
classDefinitionId: $classDefinition->getId(),
99-
data: $this->dataObjectIndexHandler->getClassMappingCheckSum($mappingProperties)
86+
$this->classDefinitionReindexService->reindexClassDefinition(
87+
$classDefinition,
88+
false,
89+
$enqueueElements
10090
);
10191

102-
if ($enqueueElements) {
103-
$this
104-
->enqueueService
105-
->enqueueByClassDefinition($classDefinition);
106-
}
107-
10892
return $this;
10993
}
11094

0 commit comments

Comments
 (0)