-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Avoid collections during full reindex
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Model/ResourceModel/Magento/Product/ProductBatchFetcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
Check failure on line 1 in Model/ResourceModel/Magento/Product/ProductBatchFetcher.php GitHub Actions / Code Sniffer
|
||
|
||
namespace Nosto\Tagging\Model\ResourceModel\Magento\Product; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Catalog\Model\Product\Visibility as ProductVisibility; | ||
use Magento\Catalog\Model\Product\Attribute\Source\Status; | ||
use Nosto\Tagging\Helper\Account as NostoAccountHelper; | ||
use Nosto\Tagging\Helper\Data as NostoDataHelper; | ||
use Nosto\Tagging\Logger\Logger as NostoLogger; | ||
use Nosto\Tagging\Model\Service\AbstractService; | ||
|
||
class ProductBatchFetcher extends AbstractService | ||
{ | ||
public const BATCH_SIZE = 500; | ||
public const BENCHMARK_SYNC_NAME = 'nosto_product_queue_insert'; | ||
public const BENCHMARK_SYNC_BREAKPOINT = 1; | ||
|
||
/** @var ResourceConnection */ | ||
protected $resourceConnection; | ||
|
||
public function __construct( | ||
ResourceConnection $resourceConnection, | ||
NostoLogger $logger, | ||
NostoDataHelper $nostoDataHelper, | ||
NostoAccountHelper $nostoAccountHelper, | ||
) { | ||
parent::__construct($nostoDataHelper, $nostoAccountHelper, $logger); | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
public function fetchProductIdBatches() | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $connection->getTableName('catalog_product_entity'); | ||
$visibilityTable = $connection->getTableName('catalog_product_entity_int'); | ||
$statusTable = $connection->getTableName('catalog_product_entity_int'); | ||
|
||
$offset = 0; | ||
$this->startBenchmark(self::BENCHMARK_SYNC_NAME, self::BENCHMARK_SYNC_BREAKPOINT); | ||
$visibility = $this->getAttributeId('visibility'); | ||
$status = $this->getAttributeId('status'); | ||
do { | ||
$this->checkMemoryConsumption('indexer by ID product sync'); | ||
$query = $connection->select() | ||
->from(['e' => $tableName], ['entity_id']) | ||
->join(['visibility' => $visibilityTable], 'e.entity_id = visibility.entity_id', []) | ||
->join(['status' => $statusTable], 'e.entity_id = status.entity_id', []) | ||
->where('visibility.attribute_id = ?', $visibility) | ||
->where('status.attribute_id = ?', $status) // @TODO: abstract those like in the collections | ||
->where('visibility.value != ?', ProductVisibility::VISIBILITY_NOT_VISIBLE) | ||
->where('status.value = ?', Status::STATUS_ENABLED) | ||
->limit(self::BATCH_SIZE, $offset); | ||
|
||
$results = $connection->fetchAll($query); | ||
|
||
if (count($results) === 0) { | ||
break; | ||
} | ||
|
||
$productIdsBatch = []; | ||
foreach ($results as $row) { | ||
$productIdsBatch[] = (int) $row['entity_id']; | ||
} | ||
|
||
yield $productIdsBatch; | ||
$this->tickBenchmark(self::BENCHMARK_SYNC_NAME, true); | ||
$offset += self::BATCH_SIZE; | ||
} while (count($results) > 0); | ||
} | ||
|
||
/** | ||
* @param string $attributeCode | ||
* @return string | ||
*/ | ||
protected function getAttributeId(string $attributeCode): string | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$attributeTable = $connection->getTableName('eav_attribute'); | ||
|
||
$query = $connection->select() | ||
->from($attributeTable, 'attribute_id') | ||
->where('attribute_code = ?', $attributeCode) | ||
->where('entity_type_id = ?', $this->getEntityTypeId('catalog_product')); | ||
|
||
return $connection->fetchOne($query); | ||
} | ||
|
||
/** | ||
* @param string $entityTypeCode | ||
* @return string | ||
*/ | ||
protected function getEntityTypeId(string $entityTypeCode): string | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$entityTypeTable = $connection->getTableName('eav_entity_type'); | ||
|
||
$query = $connection->select() | ||
->from($entityTypeTable, 'entity_type_id') | ||
->where('entity_type_code = ?', $entityTypeCode); | ||
|
||
return $connection->fetchOne($query); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters