Skip to content

Commit 585906b

Browse files
committed
MAGETWO-90563: [Forwardport] Implement segmentation for Category Product Indexer
1 parent 0a2e9d6 commit 585906b

File tree

4 files changed

+54
-45
lines changed

4 files changed

+54
-45
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Catalog\Model\Indexer\Category\Product;
108

119
use Magento\Catalog\Api\Data\ProductInterface;
@@ -16,7 +14,6 @@
1614
use Magento\Framework\DB\Select;
1715
use Magento\Framework\EntityManager\MetadataPool;
1816
use Magento\Store\Model\Store;
19-
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
2017

2118
/**
2219
* Class AbstractAction

app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Rows.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,32 @@ public function execute(array $entityIds = [], $useTempTable = false)
3636
/**
3737
* Return array of all category root IDs + tree root ID
3838
*
39-
* @return int[]
39+
* @param \Magento\Store\Model\Store $store
40+
* @return int
4041
*/
41-
protected function getRootCategoryIds()
42+
private function getRootCategoryId($store)
4243
{
43-
$rootIds = [\Magento\Catalog\Model\Category::TREE_ROOT_ID];
44-
foreach ($this->storeManager->getStores() as $store) {
45-
if ($this->getPathFromCategoryId($store->getRootCategoryId())) {
46-
$rootIds[] = $store->getRootCategoryId();
47-
}
44+
$rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
45+
if ($this->getPathFromCategoryId($store->getRootCategoryId())) {
46+
$rootId = $store->getRootCategoryId();
4847
}
49-
return $rootIds;
48+
return $rootId;
5049
}
5150

5251
/**
5352
* Remove index entries before reindexation
5453
*
5554
* @return void
5655
*/
57-
protected function removeEntries()
56+
private function removeEntries()
5857
{
59-
$removalCategoryIds = array_diff($this->limitationByCategories, $this->getRootCategoryIds());
60-
$this->connection->delete($this->getMainTable(), ['category_id IN (?)' => $removalCategoryIds]);
58+
foreach ($this->storeManager->getStores() as $store) {
59+
$removalCategoryIds = array_diff($this->limitationByCategories, [$this->getRootCategoryId($store)]);
60+
$this->connection->delete(
61+
$this->getIndexTable($store->getId()),
62+
['category_id IN (?)' => $removalCategoryIds]
63+
);
64+
}
6165
}
6266

6367
/**

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
namespace Magento\Catalog\Model\Indexer\Product\Category\Action;
77

88
use Magento\Catalog\Model\Category;
9+
use Magento\Catalog\Model\Config;
910
use Magento\Catalog\Model\Product;
1011
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\DB\Query\Generator as QueryGenerator;
14+
use Magento\Framework\EntityManager\MetadataPool;
15+
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
1116
use Magento\Framework\Indexer\CacheContext;
17+
use Magento\Store\Model\StoreManagerInterface;
1218

1319
/**
14-
* Reindex products categories.
1520
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1621
*/
1722
class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction
@@ -29,32 +34,31 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
2934
private $cacheContext;
3035

3136
/**
32-
* @var \Magento\Framework\Event\ManagerInterface|null
37+
* @var EventManagerInterface|null
3338
*/
3439
private $eventManager;
3540

3641
/**
37-
* @param \Magento\Framework\App\ResourceConnection $resource
38-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
39-
* @param \Magento\Catalog\Model\Config $config
40-
* @param \Magento\Framework\DB\Query\Generator|null $queryGenerator
41-
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
42+
* @param ResourceConnection $resource
43+
* @param StoreManagerInterface $storeManager
44+
* @param Config $config
45+
* @param QueryGenerator|null $queryGenerator
46+
* @param MetadataPool|null $metadataPool
4247
* @param CacheContext|null $cacheContext
43-
* @param \Magento\Framework\Event\ManagerInterface|null $eventManager
48+
* @param EventManagerInterface|null $eventManager
4449
*/
4550
public function __construct(
46-
\Magento\Framework\App\ResourceConnection $resource,
47-
\Magento\Store\Model\StoreManagerInterface $storeManager,
48-
\Magento\Catalog\Model\Config $config,
49-
\Magento\Framework\DB\Query\Generator $queryGenerator = null,
50-
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
51+
ResourceConnection $resource,
52+
StoreManagerInterface $storeManager,
53+
Config $config,
54+
QueryGenerator $queryGenerator = null,
55+
MetadataPool $metadataPool = null,
5156
CacheContext $cacheContext = null,
52-
\Magento\Framework\Event\ManagerInterface $eventManager = null
57+
EventManagerInterface $eventManager = null
5358
) {
5459
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool);
5560
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
56-
$this->eventManager = $eventManager ?:
57-
ObjectManager::getInstance()->get(\Magento\Framework\Event\ManagerInterface::class);
61+
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManagerInterface::class);
5862
}
5963

6064
/**
@@ -152,10 +156,12 @@ private function registerCategories(array $categoryIds)
152156
*/
153157
protected function removeEntries()
154158
{
155-
$this->connection->delete(
156-
$this->getMainTable(),
157-
['product_id IN (?)' => $this->limitationByProducts]
158-
);
159+
foreach ($this->storeManager->getStores() as $store) {
160+
$this->connection->delete(
161+
$this->getIndexTable($store->getId()),
162+
['product_id IN (?)' => $this->limitationByProducts]
163+
);
164+
};
159165
}
160166

161167
/**
@@ -167,7 +173,6 @@ protected function removeEntries()
167173
protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
168174
{
169175
$select = parent::getNonAnchorCategoriesSelect($store);
170-
171176
return $select->where('ccp.product_id IN (?) OR relation.child_id IN (?)', $this->limitationByProducts);
172177
}
173178

@@ -206,19 +211,24 @@ protected function isRangingNeeded()
206211
}
207212

208213
/**
209-
* Returns a list of category ids which are assigned to product ids in the index.
214+
* Returns a list of category ids which are assigned to product ids in the index
210215
*
211-
* @param array $productIds
212216
* @return \Magento\Framework\Indexer\CacheContext
213217
*/
214218
private function getCategoryIdsFromIndex(array $productIds)
215219
{
216-
$categoryIds = $this->connection->fetchCol(
217-
$this->connection->select()
218-
->from($this->getMainTable(), ['category_id'])
219-
->where('product_id IN (?)', $productIds)
220-
->distinct()
221-
);
220+
$categoryIds = [];
221+
foreach ($this->storeManager->getStores() as $store) {
222+
$categoryIds = array_merge(
223+
$categoryIds,
224+
$this->connection->fetchCol(
225+
$this->connection->select()
226+
->from($this->getIndexTable($store->getId()), ['category_id'])
227+
->where('product_id IN (?)', $productIds)
228+
->distinct()
229+
)
230+
);
231+
};
222232
$parentCategories = $categoryIds;
223233
foreach ($categoryIds as $categoryId) {
224234
$parentIds = explode('/', $this->getPathFromCategoryId($categoryId));

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Catalog\Model\ResourceModel\Product;
108

119
use Magento\Catalog\Api\Data\ProductInterface;

0 commit comments

Comments
 (0)