-
Notifications
You must be signed in to change notification settings - Fork 967
/
ChildCountUpdater.php
131 lines (107 loc) · 4.73 KB
/
ChildCountUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php declare(strict_types=1);
namespace Shopware\Core\Framework\DataAbstractionLayer\Indexing;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\DeadlockException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityDefinitionQueryHelper;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\Uuid\Uuid;
class ChildCountUpdater
{
/**
* @var DefinitionInstanceRegistry
*/
private $registry;
/**
* @var Connection
*/
private $connection;
public function __construct(DefinitionInstanceRegistry $registry, Connection $connection)
{
$this->registry = $registry;
$this->connection = $connection;
}
public function update(string $entity, array $parentIds, Context $context): void
{
$definition = $this->registry->getByEntityName($entity);
if (empty($parentIds)) {
return;
}
try {
// try update all ids with a single sql statement, this works only if no other process writes this table
$this->trySingleUpdate($definition, $parentIds, $context);
} catch (DeadlockException $e) {
// deadlock will appear when another process tries to write the same records
$this->doMultiUpdate($definition, $parentIds, $context);
}
}
private function trySingleUpdate(EntityDefinition $definition, array $parentIds, Context $context): void
{
$entity = $definition->getEntityName();
$versionAware = $definition->isVersionAware();
$sql = sprintf(
'UPDATE #entity# as parent
LEFT JOIN
(
SELECT parent_id, count(id) total
FROM #entity#
%s
GROUP BY parent_id
) child ON parent.id = child.parent_id
SET parent.child_count = IFNULL(child.total, 0)
WHERE parent.id IN (:ids)
%s',
$versionAware ? 'WHERE version_id = :version' : '',
$versionAware ? 'AND parent.version_id = :version' : ''
);
$sql = str_replace(
['#entity#'],
[EntityDefinitionQueryHelper::escape($entity)],
$sql
);
$params = ['ids' => Uuid::fromHexToBytesList($parentIds)];
if ($versionAware) {
$params['version'] = Uuid::fromHexToBytes($context->getVersionId());
}
$this->connection->executeUpdate($sql, $params, ['ids' => Connection::PARAM_STR_ARRAY]);
}
private function doMultiUpdate(EntityDefinition $definition, array $parentIds, Context $context): void
{
$entityName = $definition->getEntityName();
$query = $this->connection->createQueryBuilder();
$query->select([
'LOWER(HEX(parent_id)) as id',
'COUNT(id) as total',
]);
$query->from(EntityDefinitionQueryHelper::escape($entityName), 'parent');
$query->where('parent.parent_id IN (:ids)');
$query->andWhere('parent.parent_id IS NOT NULL');
$query->setParameter('ids', Uuid::fromHexToBytesList($parentIds), Connection::PARAM_STR_ARRAY);
if ($definition->isVersionAware()) {
$query->andWhere('parent.version_id = :versionId');
$query->setParameter('versionId', Uuid::fromHexToBytes($context->getVersionId()));
}
$query->groupBy('parent.parent_id');
$totals = $query->execute()->fetchAll();
$sql = sprintf('UPDATE %s SET child_count = :count WHERE id = :id', EntityDefinitionQueryHelper::escape($entityName));
$params = [];
if ($definition->isVersionAware()) {
$sql = sprintf('UPDATE %s SET child_count = :count WHERE id = :id AND version_id = :versionId', EntityDefinitionQueryHelper::escape($entityName));
$params = ['versionId' => Uuid::fromHexToBytes($context->getVersionId())];
}
$update = new RetryableQuery($this->connection->prepare($sql));
$totals = FetchModeHelper::keyPair($totals);
foreach ($totals as $id => $total) {
$update->execute(array_merge($params, ['id' => Uuid::fromHexToBytes($id), 'count' => (int) $total]));
}
$parentIds = array_flip($parentIds);
$without = array_diff_key($parentIds, $totals);
$without = array_keys($without);
foreach ($without as $id) {
$update->execute(array_merge($params, ['id' => Uuid::fromHexToBytes($id), 'count' => 0]));
}
}
}