Skip to content

Commit 048d2e6

Browse files
committed
MAGETWO-58876: [BP][Cloud] Mass actions are slow and consume excessive memory when merchandizing for 2.1.x
1 parent 75a7b9b commit 048d2e6

File tree

2 files changed

+383
-12
lines changed

2 files changed

+383
-12
lines changed

app/code/Magento/Ui/Component/MassAction/Filter.php

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66
namespace Magento\Ui\Component\MassAction;
77

8+
use Magento\Framework\Data\Collection;
89
use Magento\Framework\Api\FilterBuilder;
910
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\View\Element\UiComponentFactory;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\View\Element\UiComponentInterface;
1314
use Magento\Framework\Data\Collection\AbstractDb;
15+
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
1416

1517
/**
1618
* Class Filter
@@ -43,6 +45,11 @@ class Filter
4345
*/
4446
protected $filterBuilder;
4547

48+
/**
49+
* @var DataProviderInterface
50+
*/
51+
private $dataProvider;
52+
4653
/**
4754
* @param UiComponentFactory $factory
4855
* @param RequestInterface $request
@@ -70,27 +77,39 @@ public function getComponent()
7077
if (!isset($this->components[$namespace])) {
7178
$this->components[$namespace] = $this->factory->create($namespace);
7279
}
80+
7381
return $this->components[$namespace];
7482
}
7583

7684
/**
85+
* Adds filters to collection using DataProvider filter results
86+
*
7787
* @param AbstractDb $collection
7888
* @return AbstractDb
7989
* @throws LocalizedException
8090
*/
8191
public function getCollection(AbstractDb $collection)
8292
{
83-
$component = $this->getComponent();
84-
$this->prepareComponent($component);
85-
$dataProvider = $component->getContext()->getDataProvider();
86-
$dataProvider->setLimit(0, false);
87-
$ids = [];
88-
foreach ($dataProvider->getSearchResult()->getItems() as $document) {
89-
$ids[] = $document->getId();
93+
$selected = $this->request->getParam(static::SELECTED_PARAM);
94+
$excluded = $this->request->getParam(static::EXCLUDED_PARAM);
95+
96+
$isExcludedIdsValid = (is_array($excluded) && !empty($excluded));
97+
$isSelectedIdsValid = (is_array($selected) && !empty($selected));
98+
99+
if ('false' !== $excluded) {
100+
if (!$isExcludedIdsValid && !$isSelectedIdsValid) {
101+
throw new LocalizedException(__('Please select item(s).'));
102+
}
103+
}
104+
$idsArray = $this->getFilterIds();
105+
if (!empty($idsArray)) {
106+
$collection->addFieldToFilter(
107+
$collection->getIdFieldName(),
108+
['in' => $idsArray]
109+
);
90110
}
91111

92-
$collection->addFieldToFilter($collection->getIdFieldName(), ['in' => $ids]);
93-
return $this->applySelection($collection);
112+
return $collection;
94113
}
95114

96115
/**
@@ -103,12 +122,12 @@ public function applySelectionOnTargetProvider()
103122
{
104123
$selected = $this->request->getParam(static::SELECTED_PARAM);
105124
$excluded = $this->request->getParam(static::EXCLUDED_PARAM);
125+
106126
if ('false' === $excluded) {
107127
return;
108128
}
109-
$component = $this->getComponent();
110-
$this->prepareComponent($component);
111-
$dataProvider = $component->getContext()->getDataProvider();
129+
130+
$dataProvider = $this->getDataProvider();
112131
try {
113132
if (is_array($excluded) && !empty($excluded)) {
114133
$this->filterBuilder->setConditionType('nin')
@@ -127,6 +146,8 @@ public function applySelectionOnTargetProvider()
127146
}
128147

129148
/**
149+
* Applies selection to collection from POST parameters
150+
*
130151
* @param AbstractDb $collection
131152
* @return AbstractDb
132153
* @throws LocalizedException
@@ -151,6 +172,7 @@ protected function applySelection(AbstractDb $collection)
151172
} catch (\Exception $e) {
152173
throw new LocalizedException(__($e->getMessage()));
153174
}
175+
154176
return $collection;
155177
}
156178

@@ -176,6 +198,51 @@ public function prepareComponent(UiComponentInterface $component)
176198
public function getComponentRefererUrl()
177199
{
178200
$data = $this->getComponent()->getContext()->getDataProvider()->getConfigData();
201+
179202
return (isset($data['referer_url'])) ? $data['referer_url'] : null;
180203
}
204+
205+
/**
206+
* Get data provider
207+
*
208+
* @return DataProviderInterface
209+
*/
210+
private function getDataProvider()
211+
{
212+
if (!$this->dataProvider) {
213+
$component = $this->getComponent();
214+
$this->prepareComponent($component);
215+
$this->dataProvider = $component->getContext()->getDataProvider();
216+
$this->dataProvider->setLimit(0, false);
217+
}
218+
219+
return $this->dataProvider;
220+
}
221+
222+
/**
223+
* Get filter ids as array
224+
*
225+
* @return int[]
226+
*/
227+
private function getFilterIds()
228+
{
229+
$ids = [];
230+
$this->applySelectionOnTargetProvider();
231+
232+
/** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
233+
$searchResult = $this->getDataProvider()->getSearchResult();
234+
235+
if ($searchResult) {
236+
if ($searchResult instanceof Collection
237+
|| method_exists($searchResult, 'getAllIds')) {
238+
$ids = $searchResult->getAllIds();
239+
} else {
240+
foreach ($searchResult->getItems() as $document) {
241+
$ids[] = $document->getId();
242+
}
243+
}
244+
}
245+
246+
return $ids;
247+
}
181248
}

0 commit comments

Comments
 (0)