Skip to content

#14918 improve Indexer Batch Provider #14919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions lib/internal/Magento/Framework/Indexer/BatchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
namespace Magento\Framework\Indexer;

use \Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;

/**
* Generator of consecutive entity ID ranges that must be handled as a batch.
Expand All @@ -24,41 +25,27 @@ public function getBatches(AdapterInterface $adapter, $tableName, $linkField, $b
$adapter->select()->from(
['entity' => $tableName],
[
'max_value' => new \Zend_Db_Expr('MAX(entity.' . $linkField . ')')
'max_value' => new \Zend_Db_Expr('COUNT(*)')
]
)
);

/** @var int $truncatedBatchSize size of the last batch that is smaller than expected batch size */
$truncatedBatchSize = $maxLinkFieldValue % $batchSize;
/** @var int $fullBatchCount count of the batches that have expected batch size */
$fullBatchCount = ($maxLinkFieldValue - $truncatedBatchSize) / $batchSize;
$fullBatchCount = ceil($maxLinkFieldValue / $batchSize);

for ($batchIndex = 0; $batchIndex < $fullBatchCount; $batchIndex ++) {
yield ['from' => $batchIndex * $batchSize + 1, 'to' => ($batchIndex + 1) * $batchSize];
}
// return the last batch if it has smaller size
if ($truncatedBatchSize > 0) {
yield ['from' => $fullBatchCount * $batchSize + 1, 'to' => $maxLinkFieldValue];
yield ['limit' => $batchSize, 'offset' => $batchIndex * $batchSize];
}
}

/**
* @inheritdoc
*/
public function getBatchIds(
\Magento\Framework\DB\Adapter\AdapterInterface $connection,
\Magento\Framework\DB\Select $select,
AdapterInterface $connection,
Select $select,
array $batch
) {
$betweenCondition = sprintf(
'(%s BETWEEN %s AND %s)',
'entity_id',
$connection->quote($batch['from']),
$connection->quote($batch['to'])
);

$ids = $connection->fetchCol($select->where($betweenCondition));
$ids = $connection->fetchCol($select->order('entity_id')->limit($batch['limit'], $batch['offset']));
return array_map('intval', $ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,42 @@ public function testGetBatches($batchSize, $maxLinkFieldValue, $expectedResult)
public function getBatchesDataProvider()
{
return [
[200, 600, [['from' => 1, 'to' => 200], ['from' => 201, 'to' => 400], ['from' => 401, 'to' => 600]]],
[200, 555, [['from' => 1, 'to' => 200], ['from' => 201, 'to' => 400], ['from' => 401, 'to' => 555]]],
[200, 10, [['from' => 1, 'to' => 10]]],
[200, 0, []],
[
100,
200,
[
['limit' => 100, 'offset' => 0],
['limit' => 100, 'offset' => 100]
]

],
[
30,
66,
[
['limit' => 30, 'offset' => 0],
['limit' => 30, 'offset' => 30],
['limit' => 30, 'offset' => 60]
]
],
[
200,
50,
[
['limit' => 200, 'offset' => 0],
['limit' => 200, 'offset' => 50],
['limit' => 200, 'offset' => 100],
['limit' => 200, 'offset' => 150],
['limit' => 200, 'offset' => 200]
]
],
[
100,
100,
[
['limit' => 100, 'offset' => 0]
]
]
];
}

Expand All @@ -63,12 +95,12 @@ public function testGetBatchIds()
$selectMock = $this->createMock(Select::class);
$adapterMock = $this->createMock(AdapterInterface::class);

$selectMock->expects($this->once())->method('where')->with('(entity_id BETWEEN 10 AND 100)')->willReturnSelf();
$adapterMock->expects($this->atLeastOnce())->method('quote')->willReturnArgument(0);
$selectMock->expects($this->once())->method('order')->with('entity_id')->willReturnSelf();
$selectMock->expects($this->once())->method('limit')->with(100, 0)->willReturnSelf();
$adapterMock->expects($this->once())->method('fetchCol')->with($selectMock, [])->willReturn([1, 2, 3]);
$this->assertEquals(
[1, 2, 3],
$this->model->getBatchIds($adapterMock, $selectMock, ['from' => 10, 'to' => 100])
$this->model->getBatchIds($adapterMock, $selectMock, ['limit' => 100, 'offset' => 0])
);
}
}