Skip to content

Commit d8e9a01

Browse files
committed
MAGE-1122 Replace collection based approach for lower memory usage
1 parent 82fe828 commit d8e9a01

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

Helper/Data.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ public function rebuildStoreProductIndex(int $storeId, array $productIds): void
372372
return;
373373
}
374374

375-
$this->missingPriceIndexHandler->refreshPriceIndex($productIds);
376-
377375
$this->startEmulation($storeId);
378376
$this->logger->start('Indexing');
379377
try {

Service/Product/MissingPriceIndexHandler.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Algolia\AlgoliaSearch\Service\Product;
44

5-
use Magento\Framework\DB\Select;
65
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
76
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
8-
use Magento\Framework\Indexer\IndexerRegistry;
7+
use Magento\Framework\App\ResourceConnection;
8+
use Magento\Framework\DB\Select;
99
use Magento\Framework\Indexer\IndexerInterface;
10+
use Magento\Framework\Indexer\IndexerRegistry;
11+
use Magento\Framework\Indexer\StateInterface;
1012
use Zend_Db_Select;
1113

1214
class MissingPriceIndexHandler
@@ -18,6 +20,7 @@ class MissingPriceIndexHandler
1820
protected IndexerInterface $indexer;
1921
public function __construct(
2022
protected CollectionFactory $productCollectionFactory,
23+
protected ResourceConnection $resourceConnection,
2124
IndexerRegistry $indexerRegistry
2225
)
2326
{
@@ -27,6 +30,7 @@ public function __construct(
2730
/**
2831
* @param string[]|ProductCollection $products
2932
* @return string[] Array of product IDs that were reindexed by this repair operation
33+
* @throws \Zend_Db_Select_Exception
3034
*/
3135
public function refreshPriceIndex(array|ProductCollection $products): array
3236
{
@@ -43,15 +47,20 @@ public function refreshPriceIndex(array|ProductCollection $products): array
4347
/**
4448
* @param string[]|ProductCollection $products
4549
* @return string[]
50+
* @throws \Zend_Db_Select_Exception
4651
*/
4752
protected function getProductIdsToReindex(array|ProductCollection $products): array
4853
{
4954
$productIds = $products instanceof ProductCollection
50-
? $this->getExpandedProductCollectionIds($products)
55+
? $this->getProductIdsFromCollection($products)
5156
: $products;
5257

58+
if (empty($productIds)) {
59+
return [];
60+
}
61+
5362
$state = $this->indexer->getState()->getStatus();
54-
if ($state === \Magento\Framework\Indexer\StateInterface::STATUS_INVALID) {
63+
if ($state === StateInterface::STATUS_INVALID) {
5564
return $productIds;
5665
}
5766

@@ -77,7 +86,38 @@ protected function filterProductIds(array $productIds): array
7786
return $collection->getAllIds();
7887
}
7988

80-
protected function getExpandedProductCollectionIds(ProductCollection $collection): array
89+
/**
90+
* Expand the query for product ids from the collection regardless of price index status
91+
* @throws \Zend_Db_Select_Exception
92+
* @return string[] An array of indices to be evaluated - array will be empty if no price index join found
93+
*/
94+
protected function getProductIdsFromCollection(ProductCollection $collection): array
95+
{
96+
97+
$select = clone $collection->getSelect();
98+
// TODO: Log this exception - pending DiagnosticsLogger
99+
$joins = $select->getPart(Zend_Db_Select::FROM);
100+
$priceIndexJoin = $this->getPriceIndexJoinAlias($joins);
101+
102+
if (!$priceIndexJoin) {
103+
// no price index on query - keep calm and carry on
104+
return [];
105+
}
106+
107+
$this->expandPricingJoin($joins, $priceIndexJoin);
108+
$this->rebuildJoins($select, $joins);
109+
110+
return $this->resourceConnection->getConnection()->fetchCol($select);
111+
}
112+
113+
protected function expandPricingJoin(array &$joins, string $priceIndexJoin): void
114+
{
115+
$modifyJoin = &$joins[$priceIndexJoin];
116+
$modifyJoin['joinType'] = Zend_Db_Select::LEFT_JOIN;
117+
}
118+
119+
/** Collection based approach - more memory intensive */
120+
protected function getProductIdsFromCollectionOrig(ProductCollection $collection): array
81121
{
82122
$expandedCollection = clone $collection;
83123

@@ -102,10 +142,14 @@ protected function getExpandedProductCollectionIds(ProductCollection $collection
102142

103143
protected function rebuildJoins(Select $select, array $joins): void
104144
{
145+
$select->reset(Zend_Db_Select::COLUMNS);
105146
$select->reset(Zend_Db_Select::FROM);
106147
foreach ($joins as $alias => $joinData) {
107148
if ($joinData['joinType'] === Zend_Db_Select::FROM) {
108-
$select->from([$alias => $joinData['tableName']]);
149+
$select->from(
150+
[$alias => $joinData['tableName']],
151+
'entity_id'
152+
);
109153
} elseif ($joinData['joinType'] === Zend_Db_Select::LEFT_JOIN) {
110154
$select->joinLeft(
111155
[$alias => $joinData['tableName']],
@@ -121,7 +165,6 @@ protected function rebuildJoins(Select $select, array $joins): void
121165
$joinData['schema']
122166
);
123167
}
124-
$sql = $select->__toString();
125168
}
126169
}
127170

0 commit comments

Comments
 (0)