Skip to content

Commit 177b9dc

Browse files
author
Igor Miniailo
committed
MAGETWO-38814: Refactor MySQL search indexer to use indexer interface
1 parent cbfab58 commit 177b9dc

File tree

8 files changed

+61
-224
lines changed

8 files changed

+61
-224
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/Full.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,14 @@ class Full
9999
protected $storeManager;
100100

101101
/**
102-
* @var \Magento\CatalogSearch\Model\Resource\EngineProvider
102+
* @var \Magento\CatalogSearch\Model\Resource\Engine
103103
*/
104-
protected $engineProvider;
104+
protected $engine;
105+
106+
/**
107+
* @var \Magento\Framework\IndexerInterface
108+
*/
109+
protected $indexHandler;
105110

106111
/**
107112
* @var \Magento\Framework\Stdlib\DateTime
@@ -145,7 +150,8 @@ class Full
145150
* @param \Magento\Framework\Search\Request\Config $searchRequestConfig
146151
* @param \Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus
147152
* @param \Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $productAttributeCollectionFactory
148-
* @param \Magento\CatalogSearch\Model\Resource\EngineProvider $engineProvider
153+
* @param \Magento\CatalogSearch\Model\Resource\Engine $engine
154+
* @param \Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory $indexHandlerFactory
149155
* @param \Magento\Framework\Event\ManagerInterface $eventManager
150156
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
151157
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -163,7 +169,8 @@ public function __construct(
163169
\Magento\Framework\Search\Request\Config $searchRequestConfig,
164170
\Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus,
165171
\Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $productAttributeCollectionFactory,
166-
\Magento\CatalogSearch\Model\Resource\EngineProvider $engineProvider,
172+
\Magento\CatalogSearch\Model\Resource\Engine $engine,
173+
\Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory $indexHandlerFactory,
167174
\Magento\Framework\Event\ManagerInterface $eventManager,
168175
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
169176
\Magento\Store\Model\StoreManagerInterface $storeManager,
@@ -182,7 +189,8 @@ public function __construct(
182189
$this->eventManager = $eventManager;
183190
$this->scopeConfig = $scopeConfig;
184191
$this->storeManager = $storeManager;
185-
$this->engineProvider = $engineProvider;
192+
$this->engine = $engine;
193+
$this->indexHandler = $indexHandlerFactory->create();
186194
$this->dateTime = $dateTime;
187195
$this->localeResolver = $localeResolver;
188196
$this->localeDate = $localeDate;
@@ -245,11 +253,10 @@ protected function getWriteAdapter()
245253
protected function rebuildIndex($productIds = null)
246254
{
247255
$storeIds = array_keys($this->storeManager->getStores());
248-
$engine = $this->getEngineProvider();
249256
foreach ($storeIds as $storeId) {
250257
$dimension = $this->dimensionFactory->create(['name' => self::SCOPE_FIELD_NAME, 'value' => $storeId]);
251-
$engine->deleteIndex([$dimension], $this->getIterator($productIds));
252-
$engine->saveIndex(
258+
$this->indexHandler->deleteIndex([$dimension], $this->getIterator($productIds));
259+
$this->indexHandler->saveIndex(
253260
[$dimension],
254261
$this->rebuildStoreIndex($storeId, $productIds)
255262
);
@@ -308,7 +315,7 @@ public function rebuildStoreIndex($storeId, $productIds = null)
308315
$visibility = $this->getSearchableAttribute('visibility');
309316
$status = $this->getSearchableAttribute('status');
310317
$statusIds = $this->catalogProductStatus->getVisibleStatusIds();
311-
$allowedVisibility = $this->getEngineProvider()->getAllowedVisibility();
318+
$allowedVisibility = $this->engine->getAllowedVisibility();
312319

313320
$lastProductId = 0;
314321
while (true) {
@@ -431,7 +438,7 @@ protected function getSearchableProducts(
431438
protected function cleanIndex($storeId)
432439
{
433440
$dimension = $this->dimensionFactory->create(['name' => self::SCOPE_FIELD_NAME, 'value' => $storeId]);
434-
$this->getEngineProvider()->cleanIndex([$dimension]);
441+
$this->indexHandler->cleanIndex([$dimension]);
435442
}
436443

437444
/**
@@ -444,7 +451,7 @@ protected function cleanIndex($storeId)
444451
protected function deleteIndex($storeId = null, $productIds = null)
445452
{
446453
$dimension = $this->dimensionFactory->create(['name' => self::SCOPE_FIELD_NAME, 'value' => $storeId]);
447-
$this->getEngineProvider()->deleteIndex([$dimension], $this->getIterator($productIds));
454+
$this->indexHandler->deleteIndex([$dimension], $this->getIterator($productIds));
448455
}
449456

450457
/**
@@ -476,7 +483,7 @@ protected function getSearchableAttributes($backendType = null)
476483

477484
$this->eventManager->dispatch(
478485
'catelogsearch_searchable_attributes_load_after',
479-
['engine' => $this->getEngineProvider(), 'attributes' => $attributes]
486+
['engine' => $this->engine, 'attributes' => $attributes]
480487
);
481488

482489
$entity = $this->getEavConfig()->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getEntity();
@@ -722,7 +729,7 @@ protected function prepareProductIndex($indexData, $productData, $storeId)
722729
$index['options'] = $data;
723730
}
724731

725-
return $this->getEngineProvider()->prepareEntityIndex($index, $this->separator);
732+
return $this->engine->prepareEntityIndex($index, $this->separator);
726733
}
727734

728735
/**
@@ -736,11 +743,11 @@ protected function prepareProductIndex($indexData, $productData, $storeId)
736743
protected function getAttributeValue($attributeId, $valueId, $storeId)
737744
{
738745
$attribute = $this->getSearchableAttribute($attributeId);
739-
$value = $this->getEngineProvider()->processAttributeValue($attribute, $valueId);
746+
$value = $this->engine->processAttributeValue($attribute, $valueId);
740747

741748
if ($attribute->getIsSearchable()
742749
&& $attribute->usesSource()
743-
&& $this->getEngineProvider()->allowAdvancedIndex()
750+
&& $this->engine->allowAdvancedIndex()
744751
) {
745752
$attribute->setStoreId($storeId);
746753
$valueText = $attribute->getSource()->getIndexOptionText($valueId);
@@ -803,12 +810,4 @@ protected function getIterator(array $data)
803810
yield $key => $value;
804811
}
805812
}
806-
807-
/**
808-
* @return \Magento\CatalogSearch\Model\Resource\EngineInterface
809-
*/
810-
private function getEngineProvider()
811-
{
812-
return $this->engineProvider->get();
813-
}
814813
}

app/code/Magento/CatalogSearch/Model/Indexer/IndexerHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer;
77

8-
use Magento\Catalog\Model\Product;
98
use Magento\Eav\Model\Config;
109
use Magento\Framework\App\Resource;
1110
use Magento\Framework\DB\Adapter\AdapterInterface;

app/code/Magento/CatalogSearch/Model/Indexer/IndexerHandlerFactory.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer;
77

8-
use Magento\CatalogSearch\Model\Resource\EngineProvider;
98
use Magento\Framework\App\Config\ScopeConfigInterface;
10-
use Magento\Framework\App\Resource;
119
use Magento\Framework\IndexerInterface;
1210
use Magento\Framework\ObjectManagerInterface;
1311
use Magento\Store\Model\ScopeInterface;
@@ -33,38 +31,58 @@ class IndexerHandlerFactory
3331
*/
3432
private $scopeConfig;
3533

34+
/**
35+
* Configuration path by which current indexer handler stored
36+
*
37+
* @var string
38+
*/
39+
private $configPath;
40+
3641
/**
3742
* Factory constructor
3843
*
3944
* @param ObjectManagerInterface $objectManager
4045
* @param ScopeConfigInterface $scopeConfig
46+
* @param string $configPath
4147
* @param string[] $handlers
4248
*/
4349
public function __construct(
4450
ObjectManagerInterface $objectManager,
4551
ScopeConfigInterface $scopeConfig,
52+
$configPath,
4653
array $handlers = []
4754
) {
4855
$this->_objectManager = $objectManager;
49-
$this->handlers = $handlers;
5056
$this->scopeConfig = $scopeConfig;
57+
$this->configPath = $configPath;
58+
$this->handlers = $handlers;
5159
}
5260

5361
/**
54-
* Create class instance with specified parameters
62+
* Create indexer handler
5563
*
5664
* @param array $data
5765
* @return IndexerInterface
5866
*/
5967
public function create(array $data = [])
6068
{
61-
$currentEngine = $this->scopeConfig->getValue(EngineProvider::CONFIG_ENGINE_PATH, ScopeInterface::SCOPE_STORE);
62-
$object = $this->_objectManager->create($this->handlers[$currentEngine], $data);
69+
$currentHandler = $this->scopeConfig->getValue($this->configPath, ScopeInterface::SCOPE_STORE);
70+
if (!isset($this->handlers[$currentHandler])) {
71+
throw new \LogicException(
72+
'There is no such indexer handler: ' . $currentHandler
73+
);
74+
}
75+
$indexer = $this->_objectManager->create($this->handlers[$currentHandler], $data);
6376

64-
if (!$object instanceof IndexerInterface) {
65-
throw new \InvalidArgumentException($object . ' doesn\'t implement ' . IndexerInterface::class);
77+
if (!$indexer instanceof IndexerInterface) {
78+
throw new \InvalidArgumentException($indexer . ' doesn\'t implement ' . IndexerInterface::class);
6679
}
6780

68-
return $object;
81+
if ($indexer && !$indexer->isAvailable()) {
82+
throw new \LogicException(
83+
'Indexer handler is not available: ' . $indexer
84+
);
85+
}
86+
return $indexer;
6987
}
7088
}

app/code/Magento/CatalogSearch/Model/Resource/Engine.php

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Resource;
77

8-
use Magento\Framework\Exception\LocalizedException;
9-
use Magento\Framework\Search\Request\Dimension;
10-
118
/**
129
* CatalogSearch Fulltext Index Engine resource model
1310
*
@@ -29,69 +26,23 @@ class Engine implements EngineInterface
2926
*/
3027
protected $catalogProductVisibility;
3128

32-
/**
33-
* Array of product collection factory names
34-
*
35-
* @var array
36-
*/
37-
protected $productFactoryNames;
38-
3929
/**
4030
* @var \Magento\Search\Model\ScopeResolver\IndexScopeResolver
4131
*/
4232
private $indexScopeResolver;
4333

44-
/**
45-
* @var \Magento\Framework\DB\Adapter\AdapterInterface
46-
*/
47-
protected $connection;
48-
49-
/**
50-
* @var string
51-
*/
52-
protected $tableName;
53-
5434
/**
5535
* Construct
5636
*
5737
* @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
5838
* @param \Magento\Search\Model\ScopeResolver\IndexScopeResolver $indexScopeResolver
59-
* @param \Magento\Framework\App\Resource $resource
60-
* @param string $tableName
6139
*/
6240
public function __construct(
6341
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
64-
\Magento\Search\Model\ScopeResolver\IndexScopeResolver $indexScopeResolver,
65-
\Magento\Framework\App\Resource $resource,
66-
$tableName = 'catalogsearch_fulltext'
42+
\Magento\Search\Model\ScopeResolver\IndexScopeResolver $indexScopeResolver
6743
) {
6844
$this->catalogProductVisibility = $catalogProductVisibility;
6945
$this->indexScopeResolver = $indexScopeResolver;
70-
$this->connection = $resource->getConnection(\Magento\Framework\App\Resource::DEFAULT_WRITE_RESOURCE);
71-
$this->tableName = $resource->getTableName($tableName);
72-
}
73-
74-
/**
75-
* @inheritdoc
76-
*/
77-
public function saveIndex($dimensions, \Traversable $documents)
78-
{
79-
$data = [];
80-
foreach ($documents as $entityId => $productAttributes) {
81-
foreach ($productAttributes as $attributeId => $indexValue) {
82-
$data[] = [
83-
'product_id' => (int)$entityId,
84-
'attribute_id' =>(int)$attributeId,
85-
'data_index' => $indexValue
86-
];
87-
}
88-
}
89-
90-
if ($data) {
91-
$this->connection->insertOnDuplicate($this->resolveTableName($dimensions), $data, ['data_index']);
92-
}
93-
94-
return $this;
9546
}
9647

9748
/**
@@ -154,48 +105,6 @@ public function processAttributeValue($attribute, $value)
154105
}
155106
}
156107

157-
/**
158-
* @param Dimension[] $dimensions
159-
* @return string
160-
* @throws LocalizedException
161-
*/
162-
private function resolveTableName($dimensions)
163-
{
164-
if (empty($this->tableName)) {
165-
throw new LocalizedException(new \Magento\Framework\Phrase('Empty main table name'));
166-
}
167-
168-
return $this->indexScopeResolver->resolve($this->tableName, $dimensions);
169-
}
170-
171-
172-
/**
173-
* @inheritdoc
174-
*/
175-
public function deleteIndex($dimensions, \Traversable $documents)
176-
{
177-
$where = [];
178-
$entityIds = iterator_to_array($documents);
179-
if ($entityIds !== null) {
180-
$where[] = $this->connection
181-
->quoteInto('product_id IN (?)', $entityIds);
182-
}
183-
184-
$this->connection
185-
->delete($this->resolveTableName($dimensions), $where);
186-
187-
return $this;
188-
}
189-
190-
/**
191-
* @inheritdoc
192-
*/
193-
public function cleanIndex($dimensions)
194-
{
195-
$this->connection->delete($this->resolveTableName($dimensions));
196-
return $this;
197-
}
198-
199108
/**
200109
* Prepare index array as a string glued by separator
201110
* Support 2 level array gluing

app/code/Magento/CatalogSearch/Model/Resource/EngineInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
*/
1010
namespace Magento\CatalogSearch\Model\Resource;
1111

12-
interface EngineInterface extends \Magento\Framework\IndexerInterface
12+
interface EngineInterface
1313
{
14+
/**
15+
* Configuration path by which current indexer handler stored
16+
*/
17+
const CONFIG_ENGINE_PATH = 'catalog/search/engine';
18+
1419
/**
1520
* Retrieve allowed visibility values for current engine
1621
*

0 commit comments

Comments
 (0)