IndexSwitcher.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Indexer\Scope;
  7. use Magento\CatalogSearch\Model\Indexer\IndexSwitcherInterface;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\Search\Request\IndexScopeResolverInterface;
  10. /**
  11. * Provides a functionality to replace main index with its temporary representation
  12. *
  13. * @deprecated 101.0.0
  14. * @see \Magento\ElasticSearch
  15. */
  16. class IndexSwitcher implements IndexSwitcherInterface
  17. {
  18. /**
  19. * @var Resource
  20. */
  21. private $resource;
  22. /**
  23. * @var ScopeProxy
  24. */
  25. private $resolver;
  26. /**
  27. * @var State
  28. */
  29. private $state;
  30. /**
  31. * @param ResourceConnection $resource
  32. * @param IndexScopeResolverInterface $indexScopeResolver
  33. * @param State $state
  34. */
  35. public function __construct(
  36. ResourceConnection $resource,
  37. IndexScopeResolverInterface $indexScopeResolver,
  38. State $state
  39. ) {
  40. $this->resource = $resource;
  41. $this->resolver = $indexScopeResolver;
  42. $this->state = $state;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. * @throws IndexTableNotExistException
  47. */
  48. public function switchIndex(array $dimensions)
  49. {
  50. if (State::USE_TEMPORARY_INDEX === $this->state->getState()) {
  51. $index = \Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID;
  52. $temporalIndexTable = $this->resolver->resolve($index, $dimensions);
  53. if (!$this->resource->getConnection()->isTableExists($temporalIndexTable)) {
  54. throw new IndexTableNotExistException(
  55. __(
  56. "Temporary table for index $index doesn't exist,"
  57. . " which is inconsistent with state of scope resolver"
  58. )
  59. );
  60. }
  61. $this->state->useRegularIndex();
  62. $tableName = $this->resolver->resolve($index, $dimensions);
  63. if ($this->resource->getConnection()->isTableExists($tableName)) {
  64. $this->resource->getConnection()->dropTable($tableName);
  65. }
  66. $this->resource->getConnection()->renameTable($temporalIndexTable, $tableName);
  67. $this->state->useTemporaryIndex();
  68. }
  69. }
  70. }