Skip to content

Commit

Permalink
Merge remote-tracking branch 'm2/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
maximgubar committed Jan 3, 2017
2 parents d880330 + 6868558 commit dbc7675
Show file tree
Hide file tree
Showing 43 changed files with 1,361 additions and 143 deletions.
56 changes: 47 additions & 9 deletions app/code/Magento/CatalogSearch/Model/Indexer/Fulltext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,68 @@
namespace Magento\CatalogSearch\Model\Indexer;

use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\FullFactory;
use Magento\CatalogSearch\Model\Indexer\Scope\State;
use Magento\CatalogSearch\Model\ResourceModel\Fulltext as FulltextResource;
use \Magento\Framework\Search\Request\Config as SearchRequestConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Search\Request\Config as SearchRequestConfig;
use Magento\Framework\Search\Request\DimensionFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Provide functionality for Fulltext Search indexing
*/
class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{
/**
* Indexer ID in configuration
*/
const INDEXER_ID = 'catalogsearch_fulltext';

/** @var array index structure */
/**
* @var array index structure
*/
protected $data;

/**
* @var IndexerHandlerFactory
*/
private $indexerHandlerFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var DimensionFactory
* @var \Magento\Framework\Search\Request\DimensionFactory
*/
private $dimensionFactory;

/**
* @var Full
* @var \Magento\CatalogSearch\Model\Indexer\Fulltext\Action\Full
*/
private $fullAction;

/**
* @var FulltextResource
*/
private $fulltextResource;

/**
* @var SearchRequestConfig
* @var \Magento\Framework\Search\Request\Config
*/
private $searchRequestConfig;

/**
* @var IndexSwitcherInterface
*/
private $indexSwitcher;

/**
* @var \Magento\CatalogSearch\Model\Indexer\Scope\State
*/
private $indexScopeState;

/**
* @param FullFactory $fullActionFactory
* @param IndexerHandlerFactory $indexerHandlerFactory
Expand All @@ -54,6 +76,8 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
* @param FulltextResource $fulltextResource
* @param SearchRequestConfig $searchRequestConfig
* @param array $data
* @param IndexSwitcherInterface $indexSwitcher
* @param Scope\State $indexScopeState
*/
public function __construct(
FullFactory $fullActionFactory,
Expand All @@ -62,7 +86,9 @@ public function __construct(
DimensionFactory $dimensionFactory,
FulltextResource $fulltextResource,
SearchRequestConfig $searchRequestConfig,
array $data
array $data,
IndexSwitcherInterface $indexSwitcher = null,
State $indexScopeState = null
) {
$this->fullAction = $fullActionFactory->create(['data' => $data]);
$this->indexerHandlerFactory = $indexerHandlerFactory;
Expand All @@ -71,6 +97,14 @@ public function __construct(
$this->fulltextResource = $fulltextResource;
$this->searchRequestConfig = $searchRequestConfig;
$this->data = $data;
if (null === $indexSwitcher) {
$indexSwitcher = ObjectManager::getInstance()->get(IndexSwitcherInterface::class);
}
if (null === $indexScopeState) {
$indexScopeState = ObjectManager::getInstance()->get(State::class);
}
$this->indexSwitcher = $indexSwitcher;
$this->indexScopeState = $indexScopeState;
}

/**
Expand Down Expand Up @@ -106,10 +140,14 @@ public function executeFull()
'data' => $this->data
]);
foreach ($storeIds as $storeId) {
$dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]);
$saveHandler->cleanIndex([$dimension]);
$saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId));
$dimensions = [$this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId])];
$this->indexScopeState->useTemporaryIndex();

$saveHandler->cleanIndex($dimensions);
$saveHandler->saveIndex($dimensions, $this->fullAction->rebuildStoreIndex($storeId));

$this->indexSwitcher->switchIndex($dimensions);
$this->indexScopeState->useRegularIndex();
}
$this->fulltextResource->resetSearchResults();
$this->searchRequestConfig->reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@
use Magento\Framework\Search\Request\Dimension;
use Magento\Framework\Indexer\IndexStructureInterface;
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;

class IndexStructure implements IndexStructureInterface
{
/**
* @var Resource
*/
private $resource;

/**
* @var IndexScopeResolver
*/
private $indexScopeResolver;

/**
* @param ResourceConnection $resource
* @param IndexScopeResolver $indexScopeResolver
* @param IndexScopeResolverInterface $indexScopeResolver
*/
public function __construct(
ResourceConnection $resource,
IndexScopeResolver $indexScopeResolver
IndexScopeResolverInterface $indexScopeResolver
) {
$this->resource = $resource;
$this->indexScopeResolver = $indexScopeResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Model\Indexer;

/**
* Provides a functionality to replace main index with its temporary representation
*/
interface IndexSwitcherInterface
{
/**
* Switch current index with temporary index
*
* It will drop current index table and rename temporary index table to the current index table.
*
* @param array $dimensions
* @return void
*/
public function switchIndex(array $dimensions);
}
100 changes: 100 additions & 0 deletions app/code/Magento/CatalogSearch/Model/Indexer/IndexSwitcherProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogSearch\Model\Indexer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Proxy for adapter-specific index switcher
*/
class IndexSwitcherProxy implements IndexSwitcherInterface
{
/**
* Object Manager instance
*
* @var ObjectManagerInterface
*/
private $objectManager = null;

/**
* Instance name to create
*
* @var string
*/
private $handlers;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Configuration path by which current indexer handler stored
*
* @var string
*/
private $configPath;

/**
* Factory constructor
*
* @param ObjectManagerInterface $objectManager
* @param ScopeConfigInterface $scopeConfig
* @param string $configPath
* @param string[] $handlers
*/
public function __construct(
ObjectManagerInterface $objectManager,
ScopeConfigInterface $scopeConfig,
$configPath,
array $handlers = []
) {
$this->objectManager = $objectManager;
$this->scopeConfig = $scopeConfig;
$this->configPath = $configPath;
$this->handlers = $handlers;
}

/**
* {@inheritDoc}
*
* As index switcher is an optional part of the search SPI, it may be not defined by a search engine.
* It is especially reasonable for search engines with pre-defined indexes declaration (like old SOLR and Sphinx)
* which cannot create temporary indexes on the fly.
* That's the reason why this method do nothing for the case
* when switcher is not defined for a specific search engine.
*/
public function switchIndex(array $dimensions)
{
$currentHandler = $this->scopeConfig->getValue($this->configPath, ScopeInterface::SCOPE_STORE);
if (!isset($this->handlers[$currentHandler])) {
return;
}
$this->create($currentHandler)->switchIndex($dimensions);
}

/**
* Create indexer handler
*
* @param string $handler
* @return IndexSwitcherInterface
*/
private function create($handler)
{
$indexSwitcher = $this->objectManager->create($this->handlers[$handler]);

if (!$indexSwitcher instanceof IndexSwitcherInterface) {
throw new \InvalidArgumentException(
$handler . ' index switcher doesn\'t implement ' . IndexSwitcherInterface::class
);
}

return $indexSwitcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
use Magento\Framework\Indexer\IndexStructureInterface;
use Magento\Framework\Search\Request\Dimension;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;

class IndexerHandler implements IndexerInterface
{
Expand Down Expand Up @@ -62,7 +60,7 @@ class IndexerHandler implements IndexerInterface
* @param ResourceConnection $resource
* @param Config $eavConfig
* @param Batch $batch
* @param \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver $indexScopeResolver
* @param IndexScopeResolverInterface $indexScopeResolver
* @param array $data
* @param int $batchSize
*/
Expand All @@ -71,7 +69,7 @@ public function __construct(
ResourceConnection $resource,
Config $eavConfig,
Batch $batch,
IndexScopeResolver $indexScopeResolver,
IndexScopeResolverInterface $indexScopeResolver,
array $data,
$batchSize = 100
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Model\Indexer\Scope;

use Magento\CatalogSearch\Model\Indexer\IndexSwitcherInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;

/**
* Provides a functionality to replace main index with its temporary representation
*/
class IndexSwitcher implements IndexSwitcherInterface
{
/**
* @var Resource
*/
private $resource;

/**
* @var ScopeProxy
*/
private $resolver;

/**
* @var State
*/
private $state;

/**
* @param ResourceConnection $resource
* @param IndexScopeResolverInterface $indexScopeResolver
* @param State $state
*/
public function __construct(
ResourceConnection $resource,
IndexScopeResolverInterface $indexScopeResolver,
State $state
) {
$this->resource = $resource;
$this->resolver = $indexScopeResolver;
$this->state = $state;
}

/**
* {@inheritdoc}
* @throws IndexTableNotExistException
*/
public function switchIndex(array $dimensions)
{
if (State::USE_TEMPORARY_INDEX === $this->state->getState()) {
$index = \Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID;

$temporalIndexTable = $this->resolver->resolve($index, $dimensions);
if (!$this->resource->getConnection()->isTableExists($temporalIndexTable)) {
throw new IndexTableNotExistException(
__(
"Temporary table for index $index doesn't exist,"
. " which is inconsistent with state of scope resolver"
)
);
}

$this->state->useRegularIndex();
$tableName = $this->resolver->resolve($index, $dimensions);
if ($this->resource->getConnection()->isTableExists($tableName)) {
$this->resource->getConnection()->dropTable($tableName);
}

$this->resource->getConnection()->renameTable($temporalIndexTable, $tableName);
$this->state->useTemporaryIndex();
}
}
}
Loading

0 comments on commit dbc7675

Please sign in to comment.