Skip to content

Commit 9fb3f5f

Browse files
committed
MAGE-1122 Track product ids already reindexed in a single process
1 parent 8f8a1bb commit 9fb3f5f

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

Service/Product/MissingPriceIndexHandler.php

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class MissingPriceIndexHandler
1818
public const PRICE_INDEX_TABLE_ALIAS = 'price_index';
1919
public const MAIN_TABLE_ALIAS = 'e';
2020

21+
protected array $_indexedProducts = [];
22+
2123
protected IndexerInterface $indexer;
2224
public function __construct(
2325
protected CollectionFactory $productCollectionFactory,
@@ -41,7 +43,7 @@ public function refreshPriceIndex(array|ProductCollection $products): array
4143
return [];
4244
}
4345

44-
$this->logger->log(__("Pricing records missing for %1 product(s)", count($reindexIds)));
46+
$this->logger->log(__("Pricing records missing or invalid for %1 product(s)", count($reindexIds)));
4547
$this->logger->log(__("Reindexing product ID(s): %1", implode(', ', $reindexIds)));
4648

4749
$this->indexer->reindexList($reindexIds);
@@ -67,13 +69,18 @@ protected function getProductIdsToReindex(array|ProductCollection $products): ar
6769

6870
$state = $this->indexer->getState()->getStatus();
6971
if ($state === StateInterface::STATUS_INVALID) {
70-
return $productIds; //all records must be reindexed
72+
return $this->filterProductIdsNotYetProcessed($productIds);
73+
}
74+
75+
$productIds = $this->filterProductIdsMissingPricing($productIds);
76+
if (empty($productIds)) {
77+
return [];
7178
}
7279

73-
return $this->filterProductIds($productIds);
80+
return $this->filterProductIdsNotYetProcessed($productIds);
7481
}
7582

76-
protected function filterProductIds(array $productIds): array
83+
protected function filterProductIdsMissingPricing(array $productIds): array
7784
{
7885
$collection = $this->productCollectionFactory->create();
7986

@@ -92,6 +99,20 @@ protected function filterProductIds(array $productIds): array
9299
return $collection->getAllIds();
93100
}
94101

102+
protected function filterProductIdsNotYetProcessed(array $productIds): array {
103+
$pendingProcessing = array_fill_keys($productIds, true);
104+
105+
$notProcessed = array_diff_key($pendingProcessing, $this->_indexedProducts);
106+
107+
if (empty($notProcessed)) {
108+
return [];
109+
}
110+
111+
$this->_indexedProducts += $notProcessed;
112+
113+
return array_keys($notProcessed);
114+
}
115+
95116
/**
96117
* Expand the query for product ids from the collection regardless of price index status
97118
* @throws \Zend_Db_Select_Exception
@@ -122,30 +143,6 @@ protected function expandPricingJoin(array &$joins, string $priceIndexJoin): voi
122143
$modifyJoin['joinType'] = Zend_Db_Select::LEFT_JOIN;
123144
}
124145

125-
/** Collection based approach - more memory intensive */
126-
protected function getProductIdsFromCollectionOrig(ProductCollection $collection): array
127-
{
128-
$expandedCollection = clone $collection;
129-
130-
$select = $expandedCollection->getSelect();
131-
132-
$joins = $select->getPart(Zend_Db_Select::FROM);
133-
134-
$priceIndexJoin = $this->getPriceIndexJoinAlias($joins);
135-
136-
if (!$priceIndexJoin) {
137-
// nothing to do here - keep calm and carry on
138-
return [];
139-
}
140-
141-
$modifyJoin = &$joins[$priceIndexJoin];
142-
$modifyJoin['joinType'] = Zend_Db_Select::LEFT_JOIN;
143-
144-
$this->rebuildJoins($select, $joins);
145-
146-
return $expandedCollection->getAllIds();
147-
}
148-
149146
protected function rebuildJoins(Select $select, array $joins): void
150147
{
151148
$select->reset(Zend_Db_Select::COLUMNS);
@@ -174,15 +171,6 @@ protected function rebuildJoins(Select $select, array $joins): void
174171
}
175172
}
176173

177-
private function inspectJoins(array $joins): void {
178-
foreach ($joins as $alias => $joinData) {
179-
echo "Table Alias: $alias\n";
180-
echo "Table Name: {$joinData['tableName']}\n";
181-
echo "Join Type: {$joinData['joinType']}\n";
182-
echo "Join Condition: " . ($joinData['joinCondition'] ?? 'N/A') . "\n\n";
183-
}
184-
}
185-
186174
/**
187175
* @param array<string, array> $joins
188176
* @return string

0 commit comments

Comments
 (0)