1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of YaEtl |
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
6
|
|
|
* This source file is licensed under the MIT license which you will |
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Extractors; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ExtractorBatchLimitAbstract |
14
|
|
|
*/ |
15
|
|
|
abstract class ExtractorBatchLimitAbstract extends ExtractorLimitAbstract implements ExtractorBatchLimitInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The current offset |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $offset = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The start offset |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $startOffset = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Number of records to fetch at once |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $batchSize = 1337; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* makes sure that offset + batchSize does not exceed limit |
40
|
|
|
* by setting $this->batchSize to 0 when going beyond $this->limit |
41
|
|
|
* |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
|
public function enforceBatchSize(): ExtractorBatchLimitInterface |
45
|
|
|
{ |
46
|
|
|
if ($this->limit && ($this->numRecords + (int) $this->batchSize > $this->limit)) { |
47
|
|
|
$this->batchSize = max(0, $this->limit - $this->numRecords); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* can be used to set a specific offset prior to start the scan |
55
|
|
|
* |
56
|
|
|
* @param int $offset |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
|
|
public function setOffset(int $offset): ExtractorBatchLimitInterface |
61
|
|
|
{ |
62
|
|
|
$this->startOffset = max(0, $offset); |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get query offset |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getOffset(): int |
73
|
|
|
{ |
74
|
|
|
return (int) $this->offset; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set batch size |
79
|
|
|
* |
80
|
|
|
* @param int $batchSize |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
|
|
public function setBatchSize(int $batchSize): ExtractorBatchLimitInterface |
85
|
|
|
{ |
86
|
|
|
$this->batchSize = max(1, (int) $batchSize); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get batch size |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getBatchSize(): int |
97
|
|
|
{ |
98
|
|
|
return $this->batchSize; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adds limit to offset, to be invoked |
103
|
|
|
* each time extract() is executed |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function incrementOffset(): self |
108
|
|
|
{ |
109
|
|
|
$this->offset += (int) $this->batchSize; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return ExtractorAbstract |
116
|
|
|
*/ |
117
|
|
|
public function bootNumExtracts(): ExtractorAbstract |
118
|
|
|
{ |
119
|
|
|
// reset pagination each time we trigger |
120
|
|
|
// an extract (one or more time per flow) |
121
|
|
|
$this->offset = $this->startOffset ?: 0; |
122
|
|
|
|
123
|
|
|
return parent::bootNumExtracts(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|