Skip to content

Commit b961223

Browse files
authored
MDEE-974: Product Feed index taking long time to execute (#469)
1 parent 107fb44 commit b961223

File tree

1 file changed

+60
-20
lines changed

1 file changed

+60
-20
lines changed

CatalogDataExporter/Model/Provider/Products.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,37 @@ public function execute(
116116
}
117117

118118
$connection = $this->resourceConnection->getConnection();
119-
$itemN = 0;
120119
foreach ($queryArguments as $scopeId => $productData) {
120+
$storeViewItemN = [];
121121
$cursor = $connection->query(
122122
$this->productMainQuery->getQuery(\array_keys($productData), $scopeId ?: null)
123123
);
124124

125125
while ($row = $cursor->fetch()) {
126-
$itemN++;
127-
$mappedProducts[$row['storeViewCode']][$row['productId']] = $row;
128-
$attributesData[$row['storeViewCode']][$row['productId']] = $productData[$row['productId']];
129-
if ($itemN % $metadata->getBatchSize() == 0) {
130-
$this->processProducts($mappedProducts, $attributesData, $dataProcessorCallback, $metadata);
131-
$mappedProducts = [];
132-
$attributesData = [];
126+
$storeViewCode = $row['storeViewCode'];
127+
$productId = $row['productId'];
128+
129+
if (!isset($storeViewItemN[$storeViewCode])) {
130+
$storeViewItemN[$storeViewCode] = 0;
131+
}
132+
$storeViewItemN[$storeViewCode]++;
133+
134+
$mappedProducts[$storeViewCode][$productId] = $row;
135+
$attributesData[$storeViewCode][$productId] = $productData[$productId];
136+
137+
if ($storeViewItemN[$storeViewCode] % $metadata->getBatchSize() == 0
138+
|| count($mappedProducts) % $metadata->getBatchSize() == 0) {
139+
$this->processProducts(
140+
$mappedProducts,
141+
$attributesData,
142+
$dataProcessorCallback,
143+
$storeViewCode
144+
);
145+
unset($mappedProducts[$storeViewCode], $attributesData[$storeViewCode]);
133146
}
134147
}
135148
}
136-
if ($itemN === 0) {
149+
if (empty($storeViewItemN)) {
137150
$productsIds = \implode(',', \array_unique(\array_column($arguments, 'productId')));
138151
$scopes = \implode(',', \array_unique(\array_column($arguments, 'scopeId')));
139152
$this->logger->info(
@@ -145,7 +158,7 @@ public function execute(
145158
)
146159
);
147160
} else {
148-
$this->processProducts($mappedProducts, $attributesData, $dataProcessorCallback, $metadata);
161+
$this->processProducts($mappedProducts, $attributesData, $dataProcessorCallback);
149162
}
150163
}
151164

@@ -166,25 +179,28 @@ public function get(array $values) : array
166179
* @param array $mappedProducts
167180
* @param array $attributesData
168181
* @param callable $dataProcessorCallback
169-
* @param FeedIndexMetadata $metadata
182+
* @param string|null $storeViewCode
170183
* @return void
171184
* @throws UnableRetrieveData
172185
*/
173186
private function processProducts(
174187
array $mappedProducts,
175188
array $attributesData,
176189
callable $dataProcessorCallback,
177-
FeedIndexMetadata $metadata
190+
string $storeViewCode = null
178191
): void {
179192
$output = [];
180-
181-
foreach ($mappedProducts as $storeCode => $products) {
182-
$output[] = \array_map(function ($row) {
183-
return $this->formatter->format($row);
184-
}, \array_replace_recursive(
185-
$products,
186-
$this->entityEavAttributesResolver->resolve($attributesData[$storeCode], $storeCode)
187-
));
193+
if (null === $storeViewCode) {
194+
foreach ($mappedProducts as $mappedStoreViewCode => $products) {
195+
$this->formatOutput($products, $attributesData[$mappedStoreViewCode], $output, $mappedStoreViewCode);
196+
}
197+
} else {
198+
$this->formatOutput(
199+
$mappedProducts[$storeViewCode],
200+
$attributesData[$storeViewCode],
201+
$output,
202+
$storeViewCode
203+
);
188204
}
189205

190206
$errorEntityIds = [];
@@ -205,4 +221,28 @@ private function processProducts(
205221

206222
$dataProcessorCallback($this->get(\array_merge(...$output)));
207223
}
224+
225+
/**
226+
* Format output
227+
*
228+
* @param array $products
229+
* @param array $attributesData
230+
* @param array $output
231+
* @param string $storeViewCode
232+
* @return void
233+
* @throws UnableRetrieveData
234+
*/
235+
private function formatOutput(
236+
array $products,
237+
array $attributesData,
238+
array &$output,
239+
string $storeViewCode
240+
): void {
241+
$output[] = \array_map(function ($row) {
242+
return $this->formatter->format($row);
243+
}, \array_replace_recursive(
244+
$products,
245+
$this->entityEavAttributesResolver->resolve($attributesData, $storeViewCode)
246+
));
247+
}
208248
}

0 commit comments

Comments
 (0)