3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Catalog \Model \Indexer \Category \Product \Action ;
7
10
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Model \Config ;
13
+ use Magento \Catalog \Model \Indexer \Category \Product \AbstractAction ;
8
14
use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
15
+ use Magento \Framework \App \ObjectManager ;
16
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
9
17
use Magento \Framework \DB \Query \Generator as QueryGenerator ;
10
18
use Magento \Framework \App \ResourceConnection ;
19
+ use Magento \Framework \DB \Select ;
20
+ use Magento \Framework \EntityManager \MetadataPool ;
21
+ use Magento \Framework \Indexer \BatchProviderInterface ;
22
+ use Magento \Framework \Indexer \BatchSizeManagementInterface ;
11
23
use Magento \Indexer \Model \ProcessManager ;
24
+ use Magento \Store \Model \Store ;
25
+ use Magento \Store \Model \StoreManagerInterface ;
12
26
13
27
/**
14
28
* Class Full reindex action
15
29
*
16
- * @package Magento\Catalog\Model\Indexer\Category\Product\Action
17
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
31
*/
19
- class Full extends \ Magento \ Catalog \ Model \ Indexer \ Category \ Product \ AbstractAction
32
+ class Full extends AbstractAction
20
33
{
21
34
/**
22
- * @var \Magento\Framework\Indexer\ BatchSizeManagementInterface
35
+ * @var BatchSizeManagementInterface
23
36
*/
24
37
private $ batchSizeManagement ;
25
38
26
39
/**
27
- * @var \Magento\Framework\Indexer\ BatchProviderInterface
40
+ * @var BatchProviderInterface
28
41
*/
29
42
private $ batchProvider ;
30
43
31
44
/**
32
- * @var \Magento\Framework\EntityManager\ MetadataPool
45
+ * @var MetadataPool
33
46
*/
34
47
protected $ metadataPool ;
35
48
@@ -52,25 +65,25 @@ class Full extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
52
65
53
66
/**
54
67
* @param ResourceConnection $resource
55
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
56
- * @param \Magento\Catalog\Model\ Config $config
68
+ * @param StoreManagerInterface $storeManager
69
+ * @param Config $config
57
70
* @param QueryGenerator|null $queryGenerator
58
- * @param \Magento\Framework\Indexer\ BatchSizeManagementInterface|null $batchSizeManagement
59
- * @param \Magento\Framework\Indexer\ BatchProviderInterface|null $batchProvider
60
- * @param \Magento\Framework\EntityManager\ MetadataPool|null $metadataPool
71
+ * @param BatchSizeManagementInterface|null $batchSizeManagement
72
+ * @param BatchProviderInterface|null $batchProvider
73
+ * @param MetadataPool|null $metadataPool
61
74
* @param int|null $batchRowsCount
62
75
* @param ActiveTableSwitcher|null $activeTableSwitcher
63
76
* @param ProcessManager $processManager
64
77
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
65
78
*/
66
79
public function __construct (
67
- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
68
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
69
- \ Magento \ Catalog \ Model \ Config $ config ,
80
+ ResourceConnection $ resource ,
81
+ StoreManagerInterface $ storeManager ,
82
+ Config $ config ,
70
83
QueryGenerator $ queryGenerator = null ,
71
- \ Magento \ Framework \ Indexer \ BatchSizeManagementInterface $ batchSizeManagement = null ,
72
- \ Magento \ Framework \ Indexer \ BatchProviderInterface $ batchProvider = null ,
73
- \ Magento \ Framework \ EntityManager \ MetadataPool $ metadataPool = null ,
84
+ BatchSizeManagementInterface $ batchSizeManagement = null ,
85
+ BatchProviderInterface $ batchProvider = null ,
86
+ MetadataPool $ metadataPool = null ,
74
87
$ batchRowsCount = null ,
75
88
ActiveTableSwitcher $ activeTableSwitcher = null ,
76
89
ProcessManager $ processManager = null
@@ -81,49 +94,55 @@ public function __construct(
81
94
$ config ,
82
95
$ queryGenerator
83
96
);
84
- $ objectManager = \ Magento \ Framework \ App \ ObjectManager::getInstance ();
97
+ $ objectManager = ObjectManager::getInstance ();
85
98
$ this ->batchSizeManagement = $ batchSizeManagement ?: $ objectManager ->get (
86
- \ Magento \ Framework \ Indexer \ BatchSizeManagementInterface::class
99
+ BatchSizeManagementInterface::class
87
100
);
88
101
$ this ->batchProvider = $ batchProvider ?: $ objectManager ->get (
89
- \ Magento \ Framework \ Indexer \ BatchProviderInterface::class
102
+ BatchProviderInterface::class
90
103
);
91
104
$ this ->metadataPool = $ metadataPool ?: $ objectManager ->get (
92
- \ Magento \ Framework \ EntityManager \ MetadataPool::class
105
+ MetadataPool::class
93
106
);
94
107
$ this ->batchRowsCount = $ batchRowsCount ;
95
108
$ this ->activeTableSwitcher = $ activeTableSwitcher ?: $ objectManager ->get (ActiveTableSwitcher::class);
96
109
$ this ->processManager = $ processManager ?: $ objectManager ->get (ProcessManager::class);
97
110
}
98
111
99
112
/**
113
+ * Create the store tables
114
+ *
100
115
* @return void
101
116
*/
102
- private function createTables ()
117
+ private function createTables (): void
103
118
{
104
119
foreach ($ this ->storeManager ->getStores () as $ store ) {
105
- $ this ->tableMaintainer ->createTablesForStore ($ store ->getId ());
120
+ $ this ->tableMaintainer ->createTablesForStore (( int ) $ store ->getId ());
106
121
}
107
122
}
108
123
109
124
/**
125
+ * Truncates the replica tables
126
+ *
110
127
* @return void
111
128
*/
112
- private function clearReplicaTables ()
129
+ private function clearReplicaTables (): void
113
130
{
114
131
foreach ($ this ->storeManager ->getStores () as $ store ) {
115
- $ this ->connection ->truncateTable ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()));
132
+ $ this ->connection ->truncateTable ($ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ()));
116
133
}
117
134
}
118
135
119
136
/**
137
+ * Switches the active table
138
+ *
120
139
* @return void
121
140
*/
122
- private function switchTables ()
141
+ private function switchTables (): void
123
142
{
124
143
$ tablesToSwitch = [];
125
144
foreach ($ this ->storeManager ->getStores () as $ store ) {
126
- $ tablesToSwitch [] = $ this ->tableMaintainer ->getMainTable ($ store ->getId ());
145
+ $ tablesToSwitch [] = $ this ->tableMaintainer ->getMainTable (( int ) $ store ->getId ());
127
146
}
128
147
$ this ->activeTableSwitcher ->switchTable ($ this ->connection , $ tablesToSwitch );
129
148
}
@@ -133,12 +152,13 @@ private function switchTables()
133
152
*
134
153
* @return $this
135
154
*/
136
- public function execute ()
155
+ public function execute (): self
137
156
{
138
157
$ this ->createTables ();
139
158
$ this ->clearReplicaTables ();
140
159
$ this ->reindex ();
141
160
$ this ->switchTables ();
161
+
142
162
return $ this ;
143
163
}
144
164
@@ -147,7 +167,7 @@ public function execute()
147
167
*
148
168
* @return void
149
169
*/
150
- protected function reindex ()
170
+ protected function reindex (): void
151
171
{
152
172
$ userFunctions = [];
153
173
@@ -165,9 +185,9 @@ protected function reindex()
165
185
/**
166
186
* Execute indexation by store
167
187
*
168
- * @param \Magento\Store\Model\ Store $store
188
+ * @param Store $store
169
189
*/
170
- private function reindexStore ($ store )
190
+ private function reindexStore ($ store ): void
171
191
{
172
192
$ this ->reindexRootCategory ($ store );
173
193
$ this ->reindexAnchorCategories ($ store );
@@ -177,31 +197,31 @@ private function reindexStore($store)
177
197
/**
178
198
* Publish data from tmp to replica table
179
199
*
180
- * @param \Magento\Store\Model\ Store $store
200
+ * @param Store $store
181
201
* @return void
182
202
*/
183
- private function publishData ($ store )
203
+ private function publishData ($ store ): void
184
204
{
185
- $ select = $ this ->connection ->select ()->from ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()));
205
+ $ select = $ this ->connection ->select ()->from ($ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()));
186
206
$ columns = array_keys (
187
- $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()))
207
+ $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ()))
188
208
);
189
- $ tableName = $ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ());
209
+ $ tableName = $ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ());
190
210
191
211
$ this ->connection ->query (
192
212
$ this ->connection ->insertFromSelect (
193
213
$ select ,
194
214
$ tableName ,
195
215
$ columns ,
196
- \ Magento \ Framework \ DB \ Adapter \ AdapterInterface::INSERT_ON_DUPLICATE
216
+ AdapterInterface::INSERT_ON_DUPLICATE
197
217
)
198
218
);
199
219
}
200
220
201
221
/**
202
- * { @inheritdoc}
222
+ * @inheritdoc
203
223
*/
204
- protected function reindexRootCategory (\ Magento \ Store \ Model \ Store $ store )
224
+ protected function reindexRootCategory (Store $ store ): void
205
225
{
206
226
if ($ this ->isIndexRootCategoryNeeded ()) {
207
227
$ this ->reindexCategoriesBySelect ($ this ->getAllProducts ($ store ), 'cp.entity_id IN (?) ' , $ store );
@@ -211,62 +231,64 @@ protected function reindexRootCategory(\Magento\Store\Model\Store $store)
211
231
/**
212
232
* Reindex products of anchor categories
213
233
*
214
- * @param \Magento\Store\Model\ Store $store
234
+ * @param Store $store
215
235
* @return void
216
236
*/
217
- protected function reindexAnchorCategories (\ Magento \ Store \ Model \ Store $ store )
237
+ protected function reindexAnchorCategories (Store $ store ): void
218
238
{
219
239
$ this ->reindexCategoriesBySelect ($ this ->getAnchorCategoriesSelect ($ store ), 'ccp.product_id IN (?) ' , $ store );
220
240
}
221
241
222
242
/**
223
243
* Reindex products of non anchor categories
224
244
*
225
- * @param \Magento\Store\Model\ Store $store
245
+ * @param Store $store
226
246
* @return void
227
247
*/
228
- protected function reindexNonAnchorCategories (\ Magento \ Store \ Model \ Store $ store )
248
+ protected function reindexNonAnchorCategories (Store $ store ): void
229
249
{
230
250
$ this ->reindexCategoriesBySelect ($ this ->getNonAnchorCategoriesSelect ($ store ), 'ccp.product_id IN (?) ' , $ store );
231
251
}
232
252
233
253
/**
234
254
* Reindex categories using given SQL select and condition.
235
255
*
236
- * @param \Magento\Framework\DB\ Select $basicSelect
256
+ * @param Select $basicSelect
237
257
* @param string $whereCondition
238
- * @param \Magento\Store\Model\ Store $store
258
+ * @param Store $store
239
259
* @return void
240
260
*/
241
- private function reindexCategoriesBySelect (\ Magento \ Framework \ DB \ Select $ basicSelect , $ whereCondition , $ store )
261
+ private function reindexCategoriesBySelect (Select $ basicSelect , $ whereCondition , $ store ): void
242
262
{
243
- $ this ->tableMaintainer ->createMainTmpTable ($ store ->getId ());
263
+ $ this ->tableMaintainer ->createMainTmpTable (( int ) $ store ->getId ());
244
264
245
- $ entityMetadata = $ this ->metadataPool ->getMetadata (\ Magento \ Catalog \ Api \ Data \ ProductInterface::class);
265
+ $ entityMetadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
246
266
$ columns = array_keys (
247
- $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()))
267
+ $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()))
248
268
);
249
269
$ this ->batchSizeManagement ->ensureBatchSize ($ this ->connection , $ this ->batchRowsCount );
250
- $ batches = $ this ->batchProvider ->getBatches (
251
- $ this ->connection ,
252
- $ entityMetadata ->getEntityTable (),
270
+
271
+ $ select = $ this ->connection ->select ();
272
+ $ select ->distinct (true );
273
+ $ select ->from (['e ' => $ entityMetadata ->getEntityTable ()], $ entityMetadata ->getIdentifierField ());
274
+
275
+ $ batchQueries = $ this ->prepareSelectsByRange (
276
+ $ select ,
253
277
$ entityMetadata ->getIdentifierField (),
254
- $ this ->batchRowsCount
278
+ ( int ) $ this ->batchRowsCount
255
279
);
256
- foreach ($ batches as $ batch ) {
257
- $ this ->connection ->delete ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()));
280
+
281
+ foreach ($ batchQueries as $ query ) {
282
+ $ this ->connection ->delete ($ this ->tableMaintainer ->getMainTmpTable ((int )$ store ->getId ()));
283
+ $ entityIds = $ this ->connection ->fetchCol ($ query );
258
284
$ resultSelect = clone $ basicSelect ;
259
- $ select = $ this ->connection ->select ();
260
- $ select ->distinct (true );
261
- $ select ->from (['e ' => $ entityMetadata ->getEntityTable ()], $ entityMetadata ->getIdentifierField ());
262
- $ entityIds = $ this ->batchProvider ->getBatchIds ($ this ->connection , $ select , $ batch );
263
285
$ resultSelect ->where ($ whereCondition , $ entityIds );
264
286
$ this ->connection ->query (
265
287
$ this ->connection ->insertFromSelect (
266
288
$ resultSelect ,
267
- $ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()),
289
+ $ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()),
268
290
$ columns ,
269
- \ Magento \ Framework \ DB \ Adapter \ AdapterInterface::INSERT_ON_DUPLICATE
291
+ AdapterInterface::INSERT_ON_DUPLICATE
270
292
)
271
293
);
272
294
$ this ->publishData ($ store );
0 commit comments