-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from magento-fearless-kiwis/MAGETWO-55589-bat…
…ch-size Fixed issues: - MAGETWO-55589: Wrong algorithm for calculation batch size on category indexing
- Loading branch information
Showing
6 changed files
with
714 additions
and
43 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
192 changes: 192 additions & 0 deletions
192
lib/internal/Magento/Framework/DB/Query/BatchIterator.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,192 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\DB\Query; | ||
|
||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* Query batch iterator | ||
*/ | ||
class BatchIterator implements \Iterator | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $batchSize; | ||
|
||
/** | ||
* @var Select | ||
*/ | ||
private $select; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $minValue = 0; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $correlationName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $rangeField; | ||
|
||
/** | ||
* @var Select | ||
*/ | ||
private $currentSelect; | ||
|
||
/** | ||
* @var AdapterInterface | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $iteration = 0; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $rangeFieldAlias; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $isValid = true; | ||
|
||
/** | ||
* Initialize dependencies. | ||
* | ||
* @param Select $select | ||
* @param int $batchSize | ||
* @param string $correlationName | ||
* @param string $rangeField | ||
* @param string $rangeFieldAlias | ||
*/ | ||
public function __construct( | ||
Select $select, | ||
$batchSize, | ||
$correlationName, | ||
$rangeField, | ||
$rangeFieldAlias | ||
) { | ||
$this->batchSize = $batchSize; | ||
$this->select = $select; | ||
$this->correlationName = $correlationName; | ||
$this->rangeField = $rangeField; | ||
$this->rangeFieldAlias = $rangeFieldAlias; | ||
$this->connection = $select->getConnection(); | ||
} | ||
|
||
/** | ||
* @return Select | ||
*/ | ||
public function current() | ||
{ | ||
if (null == $this->currentSelect) { | ||
$this->currentSelect = $this->initSelectObject(); | ||
$itemsCount = $this->calculateBatchSize($this->currentSelect); | ||
$this->isValid = $itemsCount > 0; | ||
} | ||
return $this->currentSelect; | ||
} | ||
|
||
/** | ||
* @return Select | ||
*/ | ||
public function next() | ||
{ | ||
if (null == $this->currentSelect) { | ||
$this->current(); | ||
} | ||
$select = $this->initSelectObject(); | ||
$itemsCountInSelect = $this->calculateBatchSize($select); | ||
$this->isValid = $itemsCountInSelect > 0; | ||
if ($this->isValid) { | ||
$this->iteration++; | ||
$this->currentSelect = $select; | ||
} else { | ||
$this->currentSelect = null; | ||
} | ||
return $this->currentSelect; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function key() | ||
{ | ||
return $this->iteration; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function valid() | ||
{ | ||
return $this->isValid; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function rewind() | ||
{ | ||
$this->minValue = 0; | ||
$this->currentSelect = null; | ||
$this->iteration = 0; | ||
$this->isValid = true; | ||
} | ||
|
||
/** | ||
* Calculate batch size for select. | ||
* | ||
* @param Select $select | ||
* @return int | ||
*/ | ||
private function calculateBatchSize(Select $select) | ||
{ | ||
$wrapperSelect = $this->connection->select(); | ||
$wrapperSelect->from( | ||
$select, | ||
[ | ||
new \Zend_Db_Expr('MAX(' . $this->rangeFieldAlias . ') as max'), | ||
new \Zend_Db_Expr('COUNT(*) as cnt') | ||
] | ||
); | ||
$row = $this->connection->fetchRow($wrapperSelect); | ||
$this->minValue = $row['max']; | ||
return intval($row['cnt']); | ||
} | ||
|
||
/** | ||
* Initialize select object. | ||
* | ||
* @return \Magento\Framework\DB\Select | ||
*/ | ||
private function initSelectObject() | ||
{ | ||
$object = clone $this->select; | ||
$object->where( | ||
$this->connection->quoteIdentifier($this->correlationName) | ||
. '.' . $this->connection->quoteIdentifier($this->rangeField) | ||
. ' > ?', | ||
$this->minValue | ||
); | ||
$object->limit($this->batchSize); | ||
/** | ||
* Reset sort order section from origin select object | ||
*/ | ||
$object->order($this->correlationName . '.' . $this->rangeField . ' ' . \Magento\Framework\DB\Select::SQL_ASC); | ||
return $object; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
lib/internal/Magento/Framework/DB/Query/BatchIteratorFactory.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,51 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\DB\Query; | ||
|
||
/** | ||
* Factory class for @see \Magento\Framework\DB\Query\BatchIterator | ||
*/ | ||
class BatchIteratorFactory | ||
{ | ||
/** | ||
* Object Manager instance | ||
* | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
private $objectManager = null; | ||
|
||
/** | ||
* Instance name to create | ||
* | ||
* @var string | ||
*/ | ||
private $instanceName = null; | ||
|
||
/** | ||
* Factory constructor | ||
* | ||
* @param \Magento\Framework\ObjectManagerInterface $objectManager | ||
* @param string $instanceName | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\ObjectManagerInterface $objectManager, | ||
$instanceName = \Magento\Framework\DB\Query\BatchIterator::class | ||
) { | ||
$this->objectManager = $objectManager; | ||
$this->instanceName = $instanceName; | ||
} | ||
|
||
/** | ||
* Create class instance with specified parameters | ||
* | ||
* @param array $data | ||
* @return \Magento\Framework\DB\Query\BatchIterator | ||
*/ | ||
public function create(array $data = []) | ||
{ | ||
return $this->objectManager->create($this->instanceName, $data); | ||
} | ||
} |
Oops, something went wrong.