BatchChunker/LoopState.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 78.5% 11 / 14 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.8% 23 / 24 Podpod n/a totaltotal 90.4% 123 / 136 CC 11 SCAR 7.9
CC
11
Cov
92%
_decrease_multiplier
5.1
_increase_multiplier
4.3
__ANON__
2.0
CRAP 2.2
« BatchChunker.pm ↑ Summary
1 package DBIx::BatchChunker::LoopState;
2  
3 10 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Loop state object for DBIx::BatchChunker
5 10 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 our $VERSION = 'v1.0.3'; # VERSION
7  
8 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Types::Standard qw( InstanceOf ArrayRef HashRef Int Str Num Maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Types::Numbers qw( UnsignedInt PositiveNum PositiveOrZeroNum FloatSafeNum );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use Math::BigInt upgrade => 'Math::BigFloat';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Don't export the above, but don't conflict with StrictConstructor, either
20 10 ▶use namespace::clean -except => [qw< new meta >];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #pod =encoding utf8
23 #pod
24 #pod =head1 SYNOPSIS
25 #pod
26 #pod sub chunk_method {
27 #pod my ($bc, $rs) = @_;
28 #pod
29 #pod my $loop_state = $bc->loop_state;
30 #pod # introspect stuff
31 #pod }
32 #pod
33 #pod =head1 DESCRIPTION
34 #pod
35 #pod This is the loop state object used during BatchChunker runs. It only exists within the
36 #pod BatchChunker execution loop, and would generally only be accessible through the coderef
37 #pod or method referenced within that loop.
38 #pod
39 #pod This is a quasi-private object and its API may be subject to change, but the module is
40 #pod in a pretty stable state at this point. While permissions are available to write to
41 #pod the attributes, it is highly recommended to not do so unless you know exactly what you
42 #pod doing. These are mostly available for introspection of loop progress.
43 #pod
44 #pod =head1 ATTRIBUTES
45 #pod
46 #pod =head2 batch_chunker
47 #pod
48 #pod Reference back to the parent L<DBIx::BatchChunker> object.
49 #pod
50 #pod =cut
51  
52 10 has batch_chunker => (
53 is => 'ro',
54 isa => InstanceOf['DBIx::BatchChunker'],
55 required => 1,
56 weak_ref => 1,
57 );
58  
59 #pod =head2 progress_bar
60 #pod
61 #pod The progress bar being used in the loop. This may be different than
62 #pod L<DBIx::BatchChunker/progress_bar>, since it could be auto-generated.
63 #pod
64 #pod If you're trying to access the progress bar for debug or display purposes, it's best to
65 #pod use this attribute:
66 #pod
67 #pod my $progress_bar = $bc->loop_state->progress_bar;
68 #pod $progress_bar->message('Found something here');
69 #pod
70 #pod =cut
71  
72 10 has progress_bar => (
73 is => 'rw',
74 isa => InstanceOf['Term::ProgressBar'],
75 required => 1,
76 );
77  
78 #pod =head2 total_timer
79 #pod
80 #pod Epoch timer for the start of the entire operation.
81 #pod
82 #pod =cut
83  
84 has total_timer => (
85 is => 'ro',
86 isa => PositiveNum,
87 lazy => 0, # must be set on creation
88 36 ▶ default => sub { time() },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 10 );
90  
91 #pod =head2 chunk_timer
92 #pod
93 #pod Epoch timer for the start of each chunk.
94 #pod
95 #pod =for Pod::Coverage timer
96 #pod
97 #pod =cut
98  
99 has chunk_timer => (
100 is => 'rw',
101 isa => PositiveNum,
102 36 ▶ default => sub { time() },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 10 );
104  
105 # Backwards-compatibility
106 10 *timer = \&chunk_timer;
107  
108 1463 ▶sub _mark_chunk_timer { shift->chunk_timer(time); }
Subroutine _mark_chunk_timer: called CC=1 SCAR=0.0
109  
110 #pod =head2 start
111 #pod
112 #pod The real start ID that the loop is currently on. May continue to exist within iterations
113 #pod if chunk resizing is trying to find a valid range. Otherwise, this value will become
114 #pod undef when a chunk is finally processed.
115 #pod
116 #pod =cut
117  
118 has start => (
119 is => 'rw',
120 isa => Maybe[UnsignedInt],
121 lazy => 1,
122 35 ▶ default => sub { shift->batch_chunker->min_id },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 10 );
124  
125 #pod =head2 end
126 #pod
127 #pod The real end ID that the loop is currently looking at. This is always redefined at the
128 #pod beginning of the loop.
129 #pod
130 #pod =cut
131  
132 has end => (
133 is => 'rw',
134 isa => UnsignedInt,
135 lazy => 1,
136 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 my $self = shift;
138 0 my $bc = $self->batch_chunker;
139 0 $self->start + $bc->chunk_size * $bc->id_increment - 1;
140 },
141 10 );
142  
143 #pod =head2 prev_end
144 #pod
145 #pod Last "processed" value of L</end>. This also includes skipped blocks. Used in L</start>
146 #pod calculations and to determine if the end of the loop has been reached.
147 #pod
148 #pod Initially C<undef>, if no blocks have been processed yet.
149 #pod
150 #pod =cut
151  
152 10 has prev_end => (
153 is => 'rw',
154 isa => Maybe[UnsignedInt],
155 lazy => 1,
156 default => undef,
157 );
158  
159 #pod =head2 last_range
160 #pod
161 #pod A hashref of min/max values used for the bisecting of one block, measured in chunk
162 #pod multipliers. Cleared out after a block has been processed or skipped.
163 #pod
164 #pod =cut
165  
166 has last_range => (
167 is => 'rw',
168 isa => HashRef,
169 36 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
170 10 );
171  
172 #pod =head2 last_timings
173 #pod
174 #pod An arrayref of hashrefs, containing data for the previous 5 runs. This data is used for
175 #pod runtime targeting.
176 #pod
177 #pod =cut
178  
179 has last_timings => (
180 is => 'rw',
181 isa => ArrayRef,
182 36 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 10 );
184  
185 8 ▶sub _reset_last_timings { shift->last_timings([]) }
Subroutine _reset_last_timings: called CC=1 SCAR=0.0
186  
187 #pod =head2 multiplier_range
188 #pod
189 #pod The range (in units of L</chunk_size> times L<DBIx::BatchChunker/id_increment>) between
190 #pod the start and end IDs. This starts at 1 (at the beginning of the loop), but may expand
191 #pod or shrink depending on chunk count checks. Resets after block processing.
192 #pod
193 #pod =cut
194  
195 has multiplier_range => (
196 is => 'rw',
197 isa => FloatSafeNum,
198 lazy => 1,
199 35 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
200 35 ▶ shift->batch_chunker->_use_bignums ? Math::BigFloat->new(0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->batch_chunker->_use_bignums ? : 2 33 35
201 },
202 10 );
203  
204 #pod =head2 multiplier_step
205 #pod
206 #pod Determines how fast L</multiplier_range> increases, so that chunk resizing happens at an
207 #pod accelerated pace. Speeds or slows depending on what kind of limits the chunk count
208 #pod checks are hitting. Resets after block processing.
209 #pod
210 #pod =cut
211  
212 has multiplier_step => (
213 is => 'rw',
214 isa => FloatSafeNum,
215 lazy => 1,
216 35 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
217 35 ▶ shift->batch_chunker->_use_bignums ? Math::BigFloat->new(1) : 1;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->batch_chunker->_use_bignums ? : 2 33 35
218 },
219 10 );
220  
221 76 ▶sub _increase_multiplier {
Subroutine _increase_multiplier: called CC=4 SCAR=14.6
222 76 my $ls = shift;
223 76 my $lr = $ls->last_range;
224  
225 76 ▶ $lr->{min} = $ls->multiplier_range if !defined $lr->{min} || $ls->multiplier_range > $lr->{min};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lr->{"min"} or $ls->multiplier_range > $lr->{"min"} 76 0 76
Condition66%2/3
not defined $lr->{"min"} or $ls->multiplier_range > $lr->{"min"}
l!l&&r!l&&!r
56200
MC/DC0%0/2
not defined $lr->{"min"} or $ls->multiplier_range > $lr->{"min"}
not defined $lr->{"min"} $ls->multiplier_range > $lr->{"min"}
Truth table66%2/3
not(A) or B
A= not defined $lr->{"min"}B= $ls->multiplier_range > $lr->{"min"}
ABresult
1X1
011
000
226  
227 # If we have a min/max range, bisect down the middle, which will now be higher than the
228 # previous minimum. If not, keep accelerating the stepping.
229 $ls->multiplier_step(
230 76 ▶ defined $lr->{max} ? ($lr->{max} - $lr->{min}) / 2 : $ls->multiplier_step * 2
Branch: 1 branch
BranchTrueFalseTotal
defined $lr->{'max'} ? : 0 76 76
231 );
232 }
233  
234 58 ▶sub _decrease_multiplier {
Subroutine _decrease_multiplier: called CC=5 SCAR=16.2
235 58 my $ls = shift;
236 58 my $lr = $ls->last_range;
237  
238 58 ▶ $lr->{max} = $ls->multiplier_range if !defined $lr->{max} || $ls->multiplier_range < $lr->{max};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lr->{"max"} or $ls->multiplier_range < $lr->{"max"} 58 0 58
Condition66%2/3
not defined $lr->{"max"} or $ls->multiplier_range < $lr->{"max"}
l!l&&r!l&&!r
48100
MC/DC0%0/2
not defined $lr->{"max"} or $ls->multiplier_range < $lr->{"max"}
not defined $lr->{"max"} $ls->multiplier_range < $lr->{"max"}
Truth table66%2/3
not(A) or B
A= not defined $lr->{"max"}B= $ls->multiplier_range < $lr->{"max"}
ABresult
1X1
011
000
239  
240 # If we have a min/max range, bisect down the middle. If not, walk back to the previous range
241 # and decelerate the stepping, which should bring it to a halfway point from this range and
242 # last.
243 58 ▶ $ls->multiplier_range( $lr->{min} || ($ls->multiplier_range - $ls->multiplier_step) );
Condition100%3/3
$lr->{'min'} || $ls->multiplier_range - $ls->multiplier_step
l!l&&r!l&&!r
7483
MC/DC100%2/2
$lr->{'min'} || $ls->multiplier_range - $ls->multiplier_step
$lr->{'min'} $ls->multiplier_range - $ls->multiplier_step
Truth table100%3/3
A || B
A= $lr->{'min'}B= $ls->multiplier_range - $ls->multiplier_step
ABresult
1X1
011
000
244 $ls->multiplier_step(
245 58 ▶ defined $lr->{min} ? ($lr->{max} - $lr->{min}) / 2 : $ls->multiplier_step / 2
Branch: 1 branch
BranchTrueFalseTotal
defined $lr->{'min'} ? : 7 51 58
246 );
247 }
248  
249 #pod =head2 checked_count
250 #pod
251 #pod A check counter to make sure the chunk resizing isn't taking too long. After ten checks,
252 #pod it will give up, assuming the block is safe to process.
253 #pod
254 #pod =cut
255  
256 10 has checked_count => (
257 is => 'rw',
258 isa => Int,
259 default => 0,
260 );
261  
262 #pod =head2 chunk_size
263 #pod
264 #pod The I<current> chunk size, which might be adjusted by runtime targeting.
265 #pod
266 #pod =cut
267  
268 has chunk_size => (
269 is => 'rw',
270 isa => UnsignedInt,
271 lazy => 1,
272 35 ▶ default => sub { shift->batch_chunker->chunk_size },
Subroutine __ANON__: called CC=1 SCAR=0.0
273 10 );
274  
275 #pod =head2 chunk_count
276 #pod
277 #pod Records the results of the C<COUNT(*)> query for chunk resizing.
278 #pod
279 #pod =cut
280  
281 10 has chunk_count => (
282 is => 'rw',
283 isa => Maybe[UnsignedInt],
284 default => undef,
285 );
286  
287 #pod =head2 prev_check
288 #pod
289 #pod A short string recording what happened during the last chunk resizing check. Exists
290 #pod purely for debugging purposes.
291 #pod
292 #pod =cut
293  
294 10 has prev_check => (
295 is => 'rw',
296 isa => Str,
297 default => '',
298 );
299  
300 #pod =head2 prev_runtime
301 #pod
302 #pod The number of seconds the previously processed chunk took to run, not including sleep
303 #pod time.
304 #pod
305 #pod =cut
306  
307 10 has prev_runtime => (
308 is => 'rw',
309 isa => Maybe[PositiveOrZeroNum],
310 default => undef,
311 );
312  
313 605 ▶sub _reset_chunk_state {
Subroutine _reset_chunk_state: called CC=2 SCAR=6.9
314 605 my $ls = shift;
315 605 $ls->start (undef);
316 605 $ls->prev_end($ls->end);
317 605 $ls->_mark_chunk_timer;
318  
319 605 $ls->last_range ({});
320 605 $ls->multiplier_range(0);
321 605 $ls->multiplier_step (1);
322 605 $ls->checked_count (0);
323  
324 605 ▶ if ($ls->batch_chunker->_use_bignums) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ls->batch_chunker->_use_bignums) 92 513 605
325 92 $ls->multiplier_range( Math::BigFloat->new(0) );
326 92 $ls->multiplier_step ( Math::BigFloat->new(1) );
327 }
328 }
329  
330 10 1;
331  
332 __END__
« BatchChunker.pm ↑ Summary