Skip to content

Commit 1c995c1

Browse files
Reverting 36155
1 parent e2ea7bb commit 1c995c1

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Model\Mview;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Sql\Expression;
11+
use Magento\Framework\Mview\View\ChangeLogBatchWalkerInterface;
12+
use Magento\Framework\Mview\View\ChangelogInterface;
13+
14+
/**
15+
* Class BatchIterator
16+
*/
17+
class ChangeLogBatchWalker implements ChangeLogBatchWalkerInterface
18+
{
19+
private const GROUP_CONCAT_MAX_VARIABLE = 'group_concat_max_len';
20+
/** ID is defined as small int. Default size of it is 5 */
21+
private const DEFAULT_ID_SIZE = 5;
22+
23+
/**
24+
* @var ResourceConnection
25+
*/
26+
private $resourceConnection;
27+
28+
/**
29+
* @var array
30+
*/
31+
private $entityTypeCodes;
32+
33+
/**
34+
* @param ResourceConnection $resourceConnection
35+
* @param array $entityTypeCodes
36+
*/
37+
public function __construct(
38+
ResourceConnection $resourceConnection,
39+
array $entityTypeCodes = []
40+
) {
41+
$this->resourceConnection = $resourceConnection;
42+
$this->entityTypeCodes = $entityTypeCodes;
43+
}
44+
45+
/**
46+
* Calculate EAV attributes size
47+
*
48+
* @param ChangelogInterface $changelog
49+
* @return int
50+
* @throws \Exception
51+
*/
52+
private function calculateEavAttributeSize(ChangelogInterface $changelog): int
53+
{
54+
$connection = $this->resourceConnection->getConnection();
55+
56+
if (!isset($this->entityTypeCodes[$changelog->getViewId()])) {
57+
throw new \Exception('Entity type for view was not defined');
58+
}
59+
60+
$select = $connection->select();
61+
$select->from(
62+
$this->resourceConnection->getTableName('eav_attribute'),
63+
new Expression('COUNT(*)')
64+
)
65+
->joinInner(
66+
['type' => $connection->getTableName('eav_entity_type')],
67+
'type.entity_type_id=eav_attribute.entity_type_id'
68+
)
69+
->where('type.entity_type_code = ?', $this->entityTypeCodes[$changelog->getViewId()]);
70+
71+
return (int) $connection->fetchOne($select);
72+
}
73+
74+
/**
75+
* Prepare group max concat
76+
*
77+
* @param int $numberOfAttributes
78+
* @return void
79+
* @throws \Exception
80+
*/
81+
private function setGroupConcatMax(int $numberOfAttributes): void
82+
{
83+
$connection = $this->resourceConnection->getConnection();
84+
$connection->query(sprintf(
85+
'SET SESSION %s=%s',
86+
self::GROUP_CONCAT_MAX_VARIABLE,
87+
$numberOfAttributes * (self::DEFAULT_ID_SIZE + 1)
88+
));
89+
}
90+
91+
/**
92+
* @inheritdoc
93+
* @throws \Exception
94+
*/
95+
public function walk(ChangelogInterface $changelog, int $fromVersionId, int $toVersion, int $batchSize)
96+
{
97+
$connection = $this->resourceConnection->getConnection();
98+
$numberOfAttributes = $this->calculateEavAttributeSize($changelog);
99+
$this->setGroupConcatMax($numberOfAttributes);
100+
$select = $connection->select()->distinct(true)
101+
->where(
102+
'version_id > ?',
103+
(int) $fromVersionId
104+
)
105+
->where(
106+
'version_id <= ?',
107+
$toVersion
108+
)
109+
->group([$changelog->getColumnName(), 'store_id'])
110+
->limit($batchSize);
111+
112+
$columns = [
113+
$changelog->getColumnName(),
114+
'attribute_ids' => new Expression('GROUP_CONCAT(attribute_id)'),
115+
'store_id'
116+
];
117+
$select->from($changelog->getName(), $columns);
118+
return $connection->fetchAll($select);
119+
}
120+
}

0 commit comments

Comments
 (0)