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