-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathBatchIterator.php
192 lines (173 loc) · 4.09 KB
/
BatchIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Copyright © 2013-2017 Magento, Inc. 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;
}
}