Skip to content

Commit 22fee4f

Browse files
author
Oleksii Korshenko
authored
Merge pull request #333 from magento-fearless-kiwis/MAGETWO-55589-batch-size
Fixed issues: - MAGETWO-55589: Wrong algorithm for calculation batch size on category indexing
2 parents 70b8638 + 8263ddb commit 22fee4f

File tree

6 files changed

+714
-43
lines changed

6 files changed

+714
-43
lines changed

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Framework\Phrase;
2727
use Magento\Framework\Stdlib\DateTime;
2828
use Magento\Framework\Stdlib\StringUtils;
29+
use Magento\Framework\DB\Query\Generator as QueryGenerator;
2930

3031
/**
3132
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
@@ -199,6 +200,11 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface
199200
*/
200201
private $exceptionMap;
201202

203+
/**
204+
* @var QueryGenerator
205+
*/
206+
private $queryGenerator;
207+
202208
/**
203209
* @param StringUtils $string
204210
* @param DateTime $dateTime
@@ -3362,57 +3368,32 @@ public function insertFromSelect(Select $select, $table, array $fields = [], $mo
33623368
* @param int $stepCount
33633369
* @return \Magento\Framework\DB\Select[]
33643370
* @throws LocalizedException
3371+
* @deprecated
33653372
*/
33663373
public function selectsByRange($rangeField, \Magento\Framework\DB\Select $select, $stepCount = 100)
33673374
{
3368-
$fromSelect = $select->getPart(\Magento\Framework\DB\Select::FROM);
3369-
if (empty($fromSelect)) {
3370-
throw new LocalizedException(
3371-
new \Magento\Framework\Phrase('Select object must have correct "FROM" part')
3372-
);
3373-
}
3374-
3375-
$tableName = [];
3376-
$correlationName = '';
3377-
foreach ($fromSelect as $correlationName => $formPart) {
3378-
if ($formPart['joinType'] == \Magento\Framework\DB\Select::FROM) {
3379-
$tableName = $formPart['tableName'];
3380-
break;
3381-
}
3382-
}
3383-
3384-
$selectRange = $this->select()
3385-
->from(
3386-
$tableName,
3387-
[
3388-
new \Zend_Db_Expr('MIN(' . $this->quoteIdentifier($rangeField) . ') AS min'),
3389-
new \Zend_Db_Expr('MAX(' . $this->quoteIdentifier($rangeField) . ') AS max'),
3390-
]
3391-
);
3392-
3393-
$rangeResult = $this->fetchRow($selectRange);
3394-
$min = $rangeResult['min'];
3395-
$max = $rangeResult['max'];
3396-
3375+
$iterator = $this->getQueryGenerator()->generate($rangeField, $select, $stepCount);
33973376
$queries = [];
3398-
while ($min <= $max) {
3399-
$partialSelect = clone $select;
3400-
$partialSelect->where(
3401-
$this->quoteIdentifier($correlationName) . '.'
3402-
. $this->quoteIdentifier($rangeField) . ' >= ?',
3403-
$min
3404-
)
3405-
->where(
3406-
$this->quoteIdentifier($correlationName) . '.'
3407-
. $this->quoteIdentifier($rangeField) . ' < ?',
3408-
$min + $stepCount
3409-
);
3410-
$queries[] = $partialSelect;
3411-
$min += $stepCount;
3377+
foreach ($iterator as $query) {
3378+
$queries[] = $query;
34123379
}
34133380
return $queries;
34143381
}
34153382

3383+
/**
3384+
* Get query generator
3385+
*
3386+
* @return QueryGenerator
3387+
* @deprecated
3388+
*/
3389+
private function getQueryGenerator()
3390+
{
3391+
if ($this->queryGenerator === null) {
3392+
$this->queryGenerator = \Magento\Framework\App\ObjectManager::getInstance()->create(QueryGenerator::class);
3393+
}
3394+
return $this->queryGenerator;
3395+
}
3396+
34163397
/**
34173398
* Get update table query using select object for join and update
34183399
*
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\DB\Query;
7+
8+
use Magento\Framework\DB\Adapter\AdapterInterface;
9+
use Magento\Framework\DB\Select;
10+
11+
/**
12+
* Query batch iterator
13+
*/
14+
class BatchIterator implements \Iterator
15+
{
16+
/**
17+
* @var int
18+
*/
19+
private $batchSize;
20+
21+
/**
22+
* @var Select
23+
*/
24+
private $select;
25+
26+
/**
27+
* @var int
28+
*/
29+
private $minValue = 0;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $correlationName;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $rangeField;
40+
41+
/**
42+
* @var Select
43+
*/
44+
private $currentSelect;
45+
46+
/**
47+
* @var AdapterInterface
48+
*/
49+
private $connection;
50+
51+
/**
52+
* @var int
53+
*/
54+
private $iteration = 0;
55+
56+
/**
57+
* @var string
58+
*/
59+
private $rangeFieldAlias;
60+
61+
/**
62+
* @var bool
63+
*/
64+
private $isValid = true;
65+
66+
/**
67+
* Initialize dependencies.
68+
*
69+
* @param Select $select
70+
* @param int $batchSize
71+
* @param string $correlationName
72+
* @param string $rangeField
73+
* @param string $rangeFieldAlias
74+
*/
75+
public function __construct(
76+
Select $select,
77+
$batchSize,
78+
$correlationName,
79+
$rangeField,
80+
$rangeFieldAlias
81+
) {
82+
$this->batchSize = $batchSize;
83+
$this->select = $select;
84+
$this->correlationName = $correlationName;
85+
$this->rangeField = $rangeField;
86+
$this->rangeFieldAlias = $rangeFieldAlias;
87+
$this->connection = $select->getConnection();
88+
}
89+
90+
/**
91+
* @return Select
92+
*/
93+
public function current()
94+
{
95+
if (null == $this->currentSelect) {
96+
$this->currentSelect = $this->initSelectObject();
97+
$itemsCount = $this->calculateBatchSize($this->currentSelect);
98+
$this->isValid = $itemsCount > 0;
99+
}
100+
return $this->currentSelect;
101+
}
102+
103+
/**
104+
* @return Select
105+
*/
106+
public function next()
107+
{
108+
if (null == $this->currentSelect) {
109+
$this->current();
110+
}
111+
$select = $this->initSelectObject();
112+
$itemsCountInSelect = $this->calculateBatchSize($select);
113+
$this->isValid = $itemsCountInSelect > 0;
114+
if ($this->isValid) {
115+
$this->iteration++;
116+
$this->currentSelect = $select;
117+
} else {
118+
$this->currentSelect = null;
119+
}
120+
return $this->currentSelect;
121+
}
122+
123+
/**
124+
* @return int
125+
*/
126+
public function key()
127+
{
128+
return $this->iteration;
129+
}
130+
131+
/**
132+
* @return bool
133+
*/
134+
public function valid()
135+
{
136+
return $this->isValid;
137+
}
138+
139+
/**
140+
* @return void
141+
*/
142+
public function rewind()
143+
{
144+
$this->minValue = 0;
145+
$this->currentSelect = null;
146+
$this->iteration = 0;
147+
$this->isValid = true;
148+
}
149+
150+
/**
151+
* Calculate batch size for select.
152+
*
153+
* @param Select $select
154+
* @return int
155+
*/
156+
private function calculateBatchSize(Select $select)
157+
{
158+
$wrapperSelect = $this->connection->select();
159+
$wrapperSelect->from(
160+
$select,
161+
[
162+
new \Zend_Db_Expr('MAX(' . $this->rangeFieldAlias . ') as max'),
163+
new \Zend_Db_Expr('COUNT(*) as cnt')
164+
]
165+
);
166+
$row = $this->connection->fetchRow($wrapperSelect);
167+
$this->minValue = $row['max'];
168+
return intval($row['cnt']);
169+
}
170+
171+
/**
172+
* Initialize select object.
173+
*
174+
* @return \Magento\Framework\DB\Select
175+
*/
176+
private function initSelectObject()
177+
{
178+
$object = clone $this->select;
179+
$object->where(
180+
$this->connection->quoteIdentifier($this->correlationName)
181+
. '.' . $this->connection->quoteIdentifier($this->rangeField)
182+
. ' > ?',
183+
$this->minValue
184+
);
185+
$object->limit($this->batchSize);
186+
/**
187+
* Reset sort order section from origin select object
188+
*/
189+
$object->order($this->correlationName . '.' . $this->rangeField . ' ' . \Magento\Framework\DB\Select::SQL_ASC);
190+
return $object;
191+
}
192+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\DB\Query;
7+
8+
/**
9+
* Factory class for @see \Magento\Framework\DB\Query\BatchIterator
10+
*/
11+
class BatchIteratorFactory
12+
{
13+
/**
14+
* Object Manager instance
15+
*
16+
* @var \Magento\Framework\ObjectManagerInterface
17+
*/
18+
private $objectManager = null;
19+
20+
/**
21+
* Instance name to create
22+
*
23+
* @var string
24+
*/
25+
private $instanceName = null;
26+
27+
/**
28+
* Factory constructor
29+
*
30+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
31+
* @param string $instanceName
32+
*/
33+
public function __construct(
34+
\Magento\Framework\ObjectManagerInterface $objectManager,
35+
$instanceName = \Magento\Framework\DB\Query\BatchIterator::class
36+
) {
37+
$this->objectManager = $objectManager;
38+
$this->instanceName = $instanceName;
39+
}
40+
41+
/**
42+
* Create class instance with specified parameters
43+
*
44+
* @param array $data
45+
* @return \Magento\Framework\DB\Query\BatchIterator
46+
*/
47+
public function create(array $data = [])
48+
{
49+
return $this->objectManager->create($this->instanceName, $data);
50+
}
51+
}

0 commit comments

Comments
 (0)