Skip to content

Commit 7ba4e48

Browse files
Merge remote-tracking branch '36155/fixed-duplicated-ids-to-index-by-mview-changelog' into comm_voted_v3
2 parents dc76865 + 0293171 commit 7ba4e48

File tree

16 files changed

+854
-218
lines changed

16 files changed

+854
-218
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Model\Mview\ChangeLogBatchWalker;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Select;
11+
use Magento\Framework\Mview\View\ChangeLogBatchWalker\IdsFetcherInterface;
12+
13+
class IdsFetcher implements IdsFetcherInterface
14+
{
15+
/**
16+
* @var \Magento\Framework\App\ResourceConnection
17+
*/
18+
private ResourceConnection $resourceConnection;
19+
20+
/**
21+
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
22+
*/
23+
public function __construct(
24+
ResourceConnection $resourceConnection
25+
) {
26+
$this->resourceConnection = $resourceConnection;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function fetch(Select $select): array
33+
{
34+
return $this->resourceConnection->getConnection()->fetchAll($select);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\Eav\Model\Mview;
7+
namespace Magento\Eav\Model\Mview\ChangeLogBatchWalker;
88

99
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Select;
1011
use Magento\Framework\DB\Sql\Expression;
11-
use Magento\Framework\Mview\View\ChangeLogBatchWalkerInterface;
12+
use Magento\Framework\Mview\View\ChangeLogBatchWalker\IdsSelectBuilderInterface;
1213
use Magento\Framework\Mview\View\ChangelogInterface;
1314

14-
/**
15-
* Class BatchIterator
16-
*/
17-
class ChangeLogBatchWalker implements ChangeLogBatchWalkerInterface
15+
class IdsSelectBuilder implements IdsSelectBuilderInterface
1816
{
1917
private const GROUP_CONCAT_MAX_VARIABLE = 'group_concat_max_len';
2018
/** ID is defined as small int. Default size of it is 5 */
2119
private const DEFAULT_ID_SIZE = 5;
22-
2320
/**
24-
* @var ResourceConnection
21+
* @var \Magento\Framework\App\ResourceConnection
2522
*/
26-
private $resourceConnection;
27-
23+
private ResourceConnection $resourceConnection;
2824
/**
2925
* @var array
3026
*/
31-
private $entityTypeCodes;
27+
private array $entityTypeCodes;
3228

3329
/**
34-
* @param ResourceConnection $resourceConnection
30+
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
3531
* @param array $entityTypeCodes
3632
*/
3733
public function __construct(
3834
ResourceConnection $resourceConnection,
39-
array $entityTypeCodes = []
35+
array $entityTypeCodes = []
4036
) {
4137
$this->resourceConnection = $resourceConnection;
4238
$this->entityTypeCodes = $entityTypeCodes;
4339
}
4440

41+
/**
42+
* @inheritdoc
43+
*/
44+
public function build(ChangelogInterface $changelog): Select
45+
{
46+
$numberOfAttributes = $this->calculateEavAttributeSize($changelog);
47+
$this->setGroupConcatMax($numberOfAttributes);
48+
49+
$changelogTableName = $this->resourceConnection->getTableName($changelog->getName());
50+
51+
$connection = $this->resourceConnection->getConnection();
52+
53+
$columns = [
54+
$changelog->getColumnName(),
55+
'attribute_ids' => new Expression('GROUP_CONCAT(attribute_id)'),
56+
'store_id'
57+
];
58+
59+
return $connection->select()
60+
->from($changelogTableName, $columns)
61+
->group([$changelog->getColumnName(), 'store_id']);
62+
}
63+
4564
/**
4665
* Calculate EAV attributes size
4766
*
@@ -54,7 +73,7 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
5473
$connection = $this->resourceConnection->getConnection();
5574

5675
if (!isset($this->entityTypeCodes[$changelog->getViewId()])) {
57-
throw new \Exception('Entity type for view was not defined');
76+
throw new \InvalidArgumentException('Entity type for view was not defined');
5877
}
5978

6079
$select = $connection->select();
@@ -63,12 +82,12 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
6382
new Expression('COUNT(*)')
6483
)
6584
->joinInner(
66-
['type' => $connection->getTableName('eav_entity_type')],
85+
['type' => $connection->getTableName('eav_entity_type')],
6786
'type.entity_type_id=eav_attribute.entity_type_id'
6887
)
6988
->where('type.entity_type_code = ?', $this->entityTypeCodes[$changelog->getViewId()]);
7089

71-
return (int) $connection->fetchOne($select);
90+
return (int)$connection->fetchOne($select);
7291
}
7392

7493
/**
@@ -87,34 +106,4 @@ private function setGroupConcatMax(int $numberOfAttributes): void
87106
$numberOfAttributes * (self::DEFAULT_ID_SIZE + 1)
88107
));
89108
}
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-
}
120109
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Model\Mview\ChangeLogBatchWalker;
8+
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
use Magento\Framework\DB\Ddl\Table;
11+
use Magento\Framework\Mview\View\ChangeLogBatchWalker\IdsTableBuilder as BaseIdsTableBuilder;
12+
use Magento\Framework\Mview\View\ChangelogInterface;
13+
14+
class IdsTableBuilder extends BaseIdsTableBuilder
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function build(ChangelogInterface $changelog): Table
20+
{
21+
$table = parent::build($changelog);
22+
$table->addColumn(
23+
'attribute_ids',
24+
Table::TYPE_TEXT,
25+
null,
26+
['unsigned' => true, 'nullable' => false],
27+
'Attribute IDs'
28+
);
29+
$table->addColumn(
30+
'store_id',
31+
Table::TYPE_INTEGER,
32+
null,
33+
['unsigned' => true, 'nullable' => false],
34+
'Store ID'
35+
);
36+
$table->addIndex(
37+
self::INDEX_NAME_UNIQUE,
38+
[
39+
$changelog->getColumnName(),
40+
'attribute_ids',
41+
'store_id'
42+
],
43+
[
44+
'type' => AdapterInterface::INDEX_TYPE_UNIQUE
45+
]
46+
);
47+
48+
return $table;
49+
}
50+
}

app/code/Magento/Eav/etc/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,16 @@
221221
<argument name="cache" xsi:type="object">configured_eav_cache</argument>
222222
</arguments>
223223
</type>
224+
<virtualType name="Magento\Eav\Model\Mview\ChangeLogBatchWalker" type="Magento\Framework\Mview\View\ChangeLogBatchWalker">
225+
<arguments>
226+
<argument name="idsContext" xsi:type="object">Magento\Eav\Model\Mview\ChangeLogBatchWalker\IdsContext</argument>
227+
</arguments>
228+
</virtualType>
229+
<virtualType name="Magento\Eav\Model\Mview\ChangeLogBatchWalker\IdsContext" type="Magento\Framework\Mview\View\ChangeLogBatchWalker\IdsContext">
230+
<arguments>
231+
<argument name="tableBuilder" xsi:type="object">Magento\Eav\Model\Mview\ChangeLogBatchWalker\IdsTableBuilder</argument>
232+
<argument name="selectBuilder" xsi:type="object">Magento\Eav\Model\Mview\ChangeLogBatchWalker\IdsSelectBuilder</argument>
233+
<argument name="fetcher" xsi:type="object">Magento\Eav\Model\Mview\ChangeLogBatchWalker\IdsFetcher</argument>
234+
</arguments>
235+
</virtualType>
224236
</config>

0 commit comments

Comments
 (0)