Skip to content

Commit 4fef4b4

Browse files
committed
Mview patch update
1 parent f472536 commit 4fef4b4

File tree

8 files changed

+239
-26
lines changed

8 files changed

+239
-26
lines changed

lib/internal/Magento/Framework/Mview/Config/Converter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function convert($source)
2828
$data['view_id'] = $viewId;
2929
$data['action_class'] = $this->getAttributeValue($viewNode, 'class');
3030
$data['group'] = $this->getAttributeValue($viewNode, 'group');
31+
$data['store_scope'] = $this->getAttributeValue($viewNode, 'store_scope');
32+
$data['attribute_scope'] = $this->getAttributeValue($viewNode, 'attribute_scope');
3133
$data['subscriptions'] = [];
3234

3335
/** @var $childNode \DOMNode */

lib/internal/Magento/Framework/Mview/View.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use InvalidArgumentException;
1212
use Magento\Framework\DataObject;
13+
use Magento\Framework\Mview\View\ChangeLogBatchIteratorInterface;
1314
use Magento\Framework\Mview\View\ChangelogTableNotExistsException;
1415
use Magento\Framework\Mview\View\SubscriptionFactory;
1516
use Exception;
@@ -67,6 +68,11 @@ class View extends DataObject implements ViewInterface
6768
*/
6869
private $changelogBatchSize;
6970

71+
/**
72+
* @var ChangeLogBatchIteratorInterface[]
73+
*/
74+
private $strategies;
75+
7076
/**
7177
* @param ConfigInterface $config
7278
* @param ActionFactory $actionFactory
@@ -75,6 +81,7 @@ class View extends DataObject implements ViewInterface
7581
* @param SubscriptionFactory $subscriptionFactory
7682
* @param array $data
7783
* @param array $changelogBatchSize
84+
* @param array $strategies
7885
*/
7986
public function __construct(
8087
ConfigInterface $config,
@@ -83,7 +90,8 @@ public function __construct(
8390
View\ChangelogInterface $changelog,
8491
SubscriptionFactory $subscriptionFactory,
8592
array $data = [],
86-
array $changelogBatchSize = []
93+
array $changelogBatchSize = [],
94+
array $strategies = []
8795
) {
8896
$this->config = $config;
8997
$this->actionFactory = $actionFactory;
@@ -92,6 +100,7 @@ public function __construct(
92100
$this->subscriptionFactory = $subscriptionFactory;
93101
$this->changelogBatchSize = $changelogBatchSize;
94102
parent::__construct($data);
103+
$this->strategies = $strategies;
95104
}
96105

97106
/**
@@ -196,7 +205,7 @@ public function load($viewId)
196205
*/
197206
public function subscribe()
198207
{
199-
if ($this->getState()->getMode() !== View\StateInterface::MODE_ENABLED) {
208+
//if ($this->getState()->getMode() !== View\StateInterface::MODE_ENABLED) {
200209
// Create changelog table
201210
$this->getChangelog()->create();
202211

@@ -206,7 +215,7 @@ public function subscribe()
206215

207216
// Update view state
208217
$this->getState()->setMode(View\StateInterface::MODE_ENABLED)->save();
209-
}
218+
// }
210219

211220
return $this;
212221
}
@@ -240,7 +249,7 @@ public function unsubscribe()
240249
*/
241250
public function update()
242251
{
243-
if (!$this->isIdle() || !$this->isEnabled()) {
252+
if (!$this->isEnabled()) {
244253
return;
245254
}
246255

@@ -256,7 +265,7 @@ public function update()
256265
try {
257266
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
258267

259-
$this->executeAction($action, $lastVersionId, $currentVersionId);
268+
$this->executeAction($action, 0, 1);
260269

261270
$this->getState()->loadByView($this->getId());
262271
$statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
@@ -297,13 +306,24 @@ private function executeAction(ActionInterface $action, int $lastVersionId, int
297306

298307
$vsFrom = $lastVersionId;
299308
while ($vsFrom < $currentVersionId) {
300-
$ids = $this->getBatchOfIds($vsFrom, $currentVersionId);
301-
// We run the actual indexer in batches.
302-
// Chunked AFTER loading to avoid duplicates in separate chunks.
303-
$chunks = array_chunk($ids, $batchSize);
304-
foreach ($chunks as $ids) {
309+
if (isset($this->strategies[$this->changelog->getViewId()])) {
310+
$changelogData = [
311+
'name' => $this->changelog->getName(),
312+
'column_name' => $this->changelog->getColumnName(),
313+
'view_id' => $this->changelog->getViewId()
314+
];
315+
$ids = $this->strategies[$this->changelog->getViewId()]->walk($changelogData, $vsFrom, $batchSize);
305316
$action->execute($ids);
317+
} else {
318+
$ids = $this->getBatchOfIds($vsFrom, $currentVersionId);
319+
// We run the actual indexer in batches.
320+
// Chunked AFTER loading to avoid duplicates in separate chunks.
321+
$chunks = array_chunk($ids, $batchSize);
322+
foreach ($chunks as $ids) {
323+
$action->execute($ids);
324+
}
306325
}
326+
307327
}
308328
}
309329

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Mview\View;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Sql\Expression;
11+
use Magento\Framework\Mview\Config;
12+
use Magento\Framework\Phrase;
13+
14+
/**
15+
* Interface \Magento\Framework\Mview\View\ChangeLogBatchIterator
16+
*
17+
*/
18+
class ChangeLogBatchIterator implements ChangeLogBatchIteratorInterface
19+
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @var Config
27+
*/
28+
private $mviewConfig;
29+
30+
/**
31+
* ChangeLogBatchIterator constructor.
32+
* @param ResourceConnection $resourceConnection
33+
* @param Config $mviewConfig
34+
*/
35+
public function __construct(
36+
ResourceConnection $resourceConnection,
37+
Config $mviewConfig
38+
) {
39+
$this->resourceConnection = $resourceConnection;
40+
$this->mviewConfig = $mviewConfig;
41+
}
42+
43+
/**
44+
* Walk through batches
45+
*
46+
* @param array $changeLogData
47+
* @param $fromVersionId
48+
* @param int $batchSize
49+
* @return mixed
50+
* @throws ChangelogTableNotExistsException
51+
*/
52+
public function walk(array $changeLogData, $fromVersionId, int $batchSize)
53+
{
54+
$configuration = $this->mviewConfig->getView($changeLogData['view_id']);
55+
$connection = $this->resourceConnection->getConnection();
56+
$changelogTableName = $this->resourceConnection->getTableName($changeLogData['name']);
57+
if (!$connection->isTableExists($changelogTableName)) {
58+
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
59+
}
60+
$columns = [$changeLogData['column_name']];
61+
$select = $connection->select()->distinct(true)
62+
->where(
63+
'version_id > ?',
64+
(int)$fromVersionId
65+
)
66+
->group([$changeLogData['column_name'], 'store_id'])
67+
->limit($batchSize);
68+
69+
$columns = [
70+
$changeLogData['column_name'],
71+
'attribute_ids' => new Expression('GROUP_CONCAT(attribute_id)'),
72+
'store_id'
73+
];
74+
75+
$select->from($changelogTableName, $columns);
76+
return $connection->fetchAll($select);
77+
}
78+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Mview\View;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Sql\Expression;
11+
use Magento\Framework\Mview\Config;
12+
use Magento\Framework\Phrase;
13+
14+
/**
15+
* Interface \Magento\Framework\Mview\View\ChangeLogBatchIteratorInterface
16+
*
17+
*/
18+
interface ChangeLogBatchIteratorInterface
19+
{
20+
/**
21+
* Walk through batches
22+
*
23+
* @param array $changeLogData
24+
* @param $fromVersionId
25+
* @param int $batchSize
26+
* @return mixed
27+
* @throws ChangelogTableNotExistsException
28+
*/
29+
public function walk(array $changeLogData, $fromVersionId, int $batchSize);
30+
}

lib/internal/Magento/Framework/Mview/View/Changelog.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
namespace Magento\Framework\Mview\View;
88

99
use Magento\Framework\DB\Adapter\ConnectionException;
10+
use Magento\Framework\DB\Sql\Expression;
1011
use Magento\Framework\Exception\RuntimeException;
12+
use Magento\Framework\Mview\Config;
1113
use Magento\Framework\Phrase;
1214

1315
/**
@@ -25,6 +27,11 @@ class Changelog implements ChangelogInterface
2527
*/
2628
const COLUMN_NAME = 'entity_id';
2729

30+
/**
31+
* Version ID column name
32+
*/
33+
const VERSION_ID_COLUMN_NAME = 'version_id';
34+
2835
/**
2936
* Database connection
3037
*
@@ -44,15 +51,24 @@ class Changelog implements ChangelogInterface
4451
*/
4552
protected $resource;
4653

54+
/**
55+
* @var Config
56+
*/
57+
private $mviewConfig;
58+
4759
/**
4860
* @param \Magento\Framework\App\ResourceConnection $resource
61+
* @param Config $mviewConfig
4962
* @throws ConnectionException
5063
*/
51-
public function __construct(\Magento\Framework\App\ResourceConnection $resource)
52-
{
64+
public function __construct(
65+
\Magento\Framework\App\ResourceConnection $resource,
66+
Config $mviewConfig
67+
) {
5368
$this->connection = $resource->getConnection();
5469
$this->resource = $resource;
5570
$this->checkConnection();
71+
$this->mviewConfig = $mviewConfig;
5672
}
5773

5874
/**
@@ -78,12 +94,13 @@ protected function checkConnection()
7894
*/
7995
public function create()
8096
{
97+
$config = $this->mviewConfig->getView($this->getViewId());
8198
$changelogTableName = $this->resource->getTableName($this->getName());
8299
if (!$this->connection->isTableExists($changelogTableName)) {
83100
$table = $this->connection->newTable(
84101
$changelogTableName
85102
)->addColumn(
86-
'version_id',
103+
self::VERSION_ID_COLUMN_NAME,
87104
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
88105
null,
89106
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
@@ -95,6 +112,25 @@ public function create()
95112
['unsigned' => true, 'nullable' => false, 'default' => '0'],
96113
'Entity ID'
97114
);
115+
if ($config[self::ATTRIBUTE_SCOPE_SUPPORT]) {
116+
$table->addColumn(
117+
self::ATTRIBUTE_COLUMN,
118+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
119+
null,
120+
['unsigned' => true, 'nullable' => false, 'default' => '0'],
121+
'Attribute ID'
122+
);
123+
}
124+
if ($config[self::STORE_SCOPE_SUPPORT]) {
125+
$table->addColumn(
126+
self::STORE_COLUMN,
127+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
128+
null,
129+
['unsigned' => true, 'nullable' => false, 'default' => '0'],
130+
'Store ID'
131+
);
132+
}
133+
98134
$this->connection->createTable($table);
99135
}
100136
}
@@ -139,7 +175,7 @@ public function clear($versionId)
139175
*
140176
* @param int $fromVersionId
141177
* @param int $toVersionId
142-
* @return int[]
178+
* @return array
143179
* @throws ChangelogTableNotExistsException
144180
*/
145181
public function getList($fromVersionId, $toVersionId)

lib/internal/Magento/Framework/Mview/View/ChangelogInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
interface ChangelogInterface
1313
{
14+
const ATTRIBUTE_SCOPE_SUPPORT = 'attribute_scope';
15+
const STORE_SCOPE_SUPPORT = 'store_scope';
16+
const ATTRIBUTE_COLUMN = 'attribute_id';
17+
const STORE_COLUMN = 'store_id';
18+
1419
/**
1520
* Create changelog table
1621
*

0 commit comments

Comments
 (0)