From fc95a5e129e43c828405064b012e9c5a361b7675 Mon Sep 17 00:00:00 2001 From: Thiago Lima Date: Mon, 30 Apr 2018 13:31:10 +0200 Subject: [PATCH 1/2] #14918 improve Indexer Batch Provider --- .../Framework/Indexer/BatchProvider.php | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/lib/internal/Magento/Framework/Indexer/BatchProvider.php b/lib/internal/Magento/Framework/Indexer/BatchProvider.php index 9f8a1b1ace401..b3326c6bb56ea 100644 --- a/lib/internal/Magento/Framework/Indexer/BatchProvider.php +++ b/lib/internal/Magento/Framework/Indexer/BatchProvider.php @@ -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. @@ -24,22 +25,15 @@ 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]; } } @@ -47,18 +41,11 @@ public function getBatches(AdapterInterface $adapter, $tableName, $linkField, $b * @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); } } From 29fab6ee3ca13135eb7737b67caaa51a8c892911 Mon Sep 17 00:00:00 2001 From: Thiago Lima Date: Tue, 1 May 2018 13:21:10 +0200 Subject: [PATCH 2/2] #14918 refactor unit test --- .../Indexer/Test/Unit/BatchProviderTest.php | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Indexer/Test/Unit/BatchProviderTest.php b/lib/internal/Magento/Framework/Indexer/Test/Unit/BatchProviderTest.php index 06d9f58c2f2ec..f22eaaa59ed71 100644 --- a/lib/internal/Magento/Framework/Indexer/Test/Unit/BatchProviderTest.php +++ b/lib/internal/Magento/Framework/Indexer/Test/Unit/BatchProviderTest.php @@ -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] + ] + ] ]; } @@ -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]) ); } }