JSONRest/Scanner.pm

filter: Statementstmt 25.1% 34 / 135 Branchbran 7.1% 4 / 56 Conditioncond 7.5% 5 / 66 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 54.5% 6 / 11 Podpod 100% 2 / 2 totaltotal 16.5% 51 / 308 CC 53 SCAR 61.4
CC
53
Cov
17%
get_next_batch
930.0
_filter_rows_beyond_last_key
110.0
new
20.0
CRAP 464.2
↑ Summary JSONRest.pm »
1 package HBase::JSONRest::Scanner;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # new
11 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
12 0 my $class = shift;
13 0 my $params = shift;
14  
15 die "HBase handle required!"
16 0 ▶ unless ($params->{hbase} and (ref $params->{hbase}));
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"hbase"} and ref $params->{"hbase"} 0 0 0
Condition0%0/3
$params->{"hbase"} and ref $params->{"hbase"}
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{"hbase"} and ref $params->{"hbase"}
$params->{"hbase"} ref $params->{"hbase"}
Truth table0%0/3
A and B
A= $params->{"hbase"}B= ref $params->{"hbase"}
ABresult
0X0
100
111
17  
18 0 my $hbase = $params->{hbase};
19  
20 0 ▶ my $limit = $params->{atatime} || 1;
Condition0%0/2
$params->{'atatime'} || 1
l!l
00
MC/DC0%0/1
$params->{'atatime'} || 1
$params->{'atatime'}
Truth table0%0/2
A
A= $params->{'atatime'}
Aresult
11
00
21  
22 my $self = {
23 hbase => $hbase,
24  
25 table => $params->{table},
26  
27 startrow => $params->{startrow},
28  
29 endrow => $params->{endrow},
30  
31 starttime => $params->{starttime}, # server's default is 0
32  
33 endtime => $params->{endtime}, # server's default is Long.MAX_VALUE
34  
35 maxversions => $params->{maxversions}, # server's default is 1
36  
37 prefix => $params->{prefix},
38  
39 0 limit => $limit,
40  
41 last_key_from_previous_batch => undef,
42  
43 batch_no => 0,
44  
45 EOF => 0,
46 };
47  
48 0 return bless $self, $class;
49 }
50  
51 # get_next_batch
52 0 ▶sub get_next_batch {
Subroutine get_next_batch: not called CC=30 SCAR=68.4
Pod documented
53  
54 0 my $self = shift;
55  
56 0 $self->{_last_batch_time_start} = time;
57  
58 0 my $table = $self->{table};
59 0 my $prefix = $self->{prefix};
60 0 my $limit = $self->{limit};
61 0 my $hbase = $self->{hbase};
62  
63 0 my $last_key_from_previous_batch;
64  
65 # Three ways of scanning are supported:
66 #
67 # I. Provide a prefix and scan all rows with that prefix
68 # II. Provide startrow and endrow. Scan is inclusive for
69 # startrow and exclusive for endrow.
70 # III. Provide just startrow - scan entire table, batch by batch.
71 #
72 # All of these are converted to startrow and end_condition under
73 # the hood. Difference is only in user API.
74  
75 # First Batch
76 0 ▶ if ($self->{batch_no} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'batch_no'} == 0) { } 0 0 0
77  
78 # Case I:
79 0 ▶ if ((defined $prefix) && !$self->{startrow} && !$self->{endrow}) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $prefix and not $self->{'startrow'} and not $self->{'endrow'}) { } 0 0 0
elsif (not defined $prefix and $self->{'startrow'} and $self->{'endrow'}) { } 0 0 0
elsif (not defined $prefix and $self->{'startrow'} and not $self->{'endrow'}) { } 0 0 0
elsif (defined $prefix and $self->{'startrow'} || $self->{'endrow'}) { } 0 0 0
elsif (not defined $prefix || $self->{'startrow'}) { } 0 0 0
Condition0%0/6
defined $prefix and not $self->{"startrow"} and not $self->{"endrow"}
!ll&&!rl&&r
000
defined $prefix and not $self->{"startrow"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $prefix and not $self->{"startrow"} and not $self->{"endrow"}
defined $prefix not $self->{"startrow"} not $self->{"endrow"}
Truth table0%0/4
A and not(B) and not(C)
A= defined $prefixB= not $self->{"startrow"}C= not $self->{"endrow"}
ABCresult
0XX0
10X0
1100
1111
80  
81 0 my $first_row = $self->_get_first_row_of_prefix();
82  
83 # no rows for specified prefix
84 0 ▶ return undef if (!$first_row && !$first_row->{row});
Branch: 1 branch
BranchTrueFalseTotal
unless $first_row or $first_row->{"row"} 0 0 0
Condition0%0/3
$first_row or $first_row->{"row"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$first_row or $first_row->{"row"}
$first_row $first_row->{"row"}
Truth table0%0/3
A or B
A= $first_rowB= $first_row->{"row"}
ABresult
1X1
011
000
85  
86 0 $self->{startrow} = $first_row->{row};
87 0 $self->{end_condition_type} = 'PREFIX';
88 }
89 # Case II:
90 # case no prefix, startrow exists, endrow exists
91 ▶ elsif ((!defined $prefix) && $self->{startrow} && $self->{endrow}){
Condition0%0/6
not defined $prefix and $self->{"startrow"} and $self->{"endrow"}
!ll&&!rl&&r
000
not defined $prefix and $self->{"startrow"}
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $prefix and $self->{"startrow"} and $self->{"endrow"}
not defined $prefix $self->{"startrow"} $self->{"endrow"}
Truth table0%0/4
not(A) and B and C
A= not defined $prefixB= $self->{"startrow"}C= $self->{"endrow"}
ABCresult
0XX0
10X0
1100
1111
92 # $self->{startrow} allready assigned
93 0 $self->{end_condition_type} = 'ENDROW';
94 }
95 # Case III:
96 # only firs_key specified, scan untill the end of the table
97 ▶ elsif ((!defined $prefix) && $self->{startrow} && !$self->{endrow}){
Condition0%0/6
not defined $prefix and $self->{"startrow"} and not $self->{"endrow"}
!ll&&!rl&&r
000
not defined $prefix and $self->{"startrow"}
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $prefix and $self->{"startrow"} and not $self->{"endrow"}
not defined $prefix $self->{"startrow"} not $self->{"endrow"}
Truth table0%0/4
not(A) and B and not(C)
A= not defined $prefixB= $self->{"startrow"}C= not $self->{"endrow"}
ABCresult
0XX0
10X0
1100
1111
98 # $self->{startrow} allready assigned
99 0 $self->{end_condition_type} = 'NONE';
100 }
101 # Forbiden cases:
102 # case prefix and startrow/endrow
103 ▶ elsif ((defined $prefix) && ($self->{startrow} || $self->{endrow})){
Condition0%0/6
defined $prefix and $self->{"startrow"} || $self->{"endrow"}
!ll&&!rl&&r
000
$self->{"startrow"} or $self->{"endrow"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $prefix and $self->{"startrow"} || $self->{"endrow"}
defined $prefix $self->{"startrow"} $self->{"endrow"}
Truth table0%0/4
A and B or C
A= defined $prefixB= $self->{"startrow"}C= $self->{"endrow"}
ABCresult
0XX0
1000
11X1
1011
104 0 die "Can not use prefix and startrow/endrow at the same time!";
105 }
106 # case no params
107 ▶ elsif ((!defined $prefix) && !$self->{startrow}) {
Condition0%0/3
defined $prefix || $self->{'startrow'}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $prefix || $self->{'startrow'}
defined $prefix $self->{'startrow'}
Truth table0%0/3
A || B
A= defined $prefixB= $self->{'startrow'}
ABresult
1X1
011
000
108 0 die "Must specify either prefix or startrow!";
109 }
110 else {
111 0 die "Unknown query case!";
112 }
113  
114 # SCAN FOR FIRST BATCH
115 my $rows = $self->_scan_raw({
116 table => $self->{table},
117 startrow => $self->{startrow}, # <- inclusive
118 starttime => $self->{starttime},
119 endtime => $self->{endtime},
120 maxversions=> $self->{maxversions},
121 0 limit => $limit,
122 });
123 0 $self->{last_batch_time} = time - $self->{_last_batch_time_start};
124 0 $self->{batch_no}++;
125  
126 0 ▶ if (!$hbase->{last_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hbase->{'last_error'}) { } 0 0 0
127  
128 0 ▶ if ($rows && @$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows and @$rows) { } 0 0 0
Condition0%0/3
$rows and @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table0%0/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
129  
130 0 $self->_filter_rows_beyond_last_key($rows);
131  
132 # return what is left, if something is left after filter
133 0 ▶ if ($rows && @$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows and @$rows) { } 0 0 0
Condition0%0/3
$rows and @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table0%0/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
134 0 $self->{last_key_from_previous_batch} = $rows->[-1]->{row};
135 0 return $rows;
136 }
137 else {
138 0 $self->{last_key_from_previous_batch} = undef;
139 0 $self->{EOF} = 1;
140 0 return [];
141 }
142 }
143 else {
144 0 $self->{last_key_from_previous_batch} = undef;
145 0 $self->{EOF} = 1;
146 0 return [];
147 }
148 }
149 else {
150 0 die "Error while trying to get the first key of a prefix!" . Dumper($hbase->{last_error});
151 }
152 }
153 # Next Batch
154 else {
155 # no more records, last batch was empty or it was the last batch
156 0 ▶ if (!$self->{last_key_from_previous_batch} || $self->{EOF}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"last_key_from_previous_batch"} or $self->{"EOF"}) 0 0 0
Condition0%0/3
not $self->{"last_key_from_previous_batch"} or $self->{"EOF"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"last_key_from_previous_batch"} or $self->{"EOF"}
not $self->{"last_key_from_previous_batch"} $self->{"EOF"}
Truth table0%0/3
not(A) or B
A= not $self->{"last_key_from_previous_batch"}B= $self->{"EOF"}
ABresult
1X1
011
000
157 0 return undef;
158 }
159  
160 0 $last_key_from_previous_batch = $self->{last_key_from_previous_batch};
161 0 $self->{last_key_from_previous_batch} = undef;
162  
163 # Use last row from previous batch as start row for the next scan, but
164 # make an exclude-start-row scan type.
165  
166 my $next_batch = $self->_scan_raw({
167 table => $table,
168 startrow => $last_key_from_previous_batch,
169 exclude_startrow_from_result => 1,
170 starttime => $self->{starttime},
171 endtime => $self->{endtime},
172 maxversions=> $self->{maxversions},
173 0 limit => $limit,
174 });
175  
176 0 $self->{last_batch_time} = time - $self->{_last_batch_time_start};
177 0 $self->{batch_no}++;
178  
179 0 ▶ if (!$hbase->{last_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hbase->{'last_error'}) { } 0 0 0
180  
181 0 ▶ if ($next_batch && @$next_batch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_batch and @$next_batch) { } 0 0 0
Condition0%0/3
$next_batch and @$next_batch
!ll&&!rl&&r
000
MC/DC0%0/2
$next_batch and @$next_batch
$next_batch @$next_batch
Truth table0%0/3
A and B
A= $next_batchB= @$next_batch
ABresult
0X0
100
111
182  
183 0 $self->_filter_rows_beyond_last_key($next_batch);
184  
185 # return what is left, if something is left after filter
186 0 ▶ if ($next_batch && @$next_batch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_batch and @$next_batch) { } 0 0 0
Condition0%0/3
$next_batch and @$next_batch
!ll&&!rl&&r
000
MC/DC0%0/2
$next_batch and @$next_batch
$next_batch @$next_batch
Truth table0%0/3
A and B
A= $next_batchB= @$next_batch
ABresult
0X0
100
111
187 0 $self->{last_key_from_previous_batch} = $next_batch->[-1]->{row};
188 0 return $next_batch;
189 }
190 else {
191 0 $self->{last_key_from_previous_batch} = undef;
192 0 $self->{EOF} = 1;
193 0 return [];
194 }
195 }
196 else {
197 0 $self->{last_key_from_previous_batch} = undef;
198 0 $self->{EOF} = 1;
199 0 return [];
200 }
201 }
202 else {
203 die "Scanner error while trying to get next batch!"
204 0 . Dumper($hbase->{last_error});
205 }
206 }
207 }
208  
209 # _get_first_row_of_prefix
210 0 ▶sub _get_first_row_of_prefix {
Subroutine _get_first_row_of_prefix: not called CC=3 SCAR=24.8
211 0 my $self = shift;
212  
213 0 my $prefix = $self->{prefix};
214 0 my $hbase = $self->{hbase};
215 0 my $table = $self->{table};
216  
217 # use prefix as the first row with limit 1 - returns the first row with given prefix
218 0 my $rows = $self->_scan_raw({
219 table => $table,
220 startrow => $prefix,
221 limit => 1,
222 });
223  
224 0 ▶ die "Should be only one first row!"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$rows > 1 0 0 0
225 if ( scalar @$rows > 1);
226  
227 0 ▶ return undef unless $rows->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $rows->[0] 0 0 0
228  
229 0 my $first_row = $rows->[0];
230  
231 0 return $first_row;
232 }
233  
234 # _scan_raw (uses passed paremeters instead of instance parameters)
235 0 ▶sub _scan_raw {
Subroutine _scan_raw: not called CC=1 SCAR=6.9
236 0 my $self = shift;
237 0 my $params = shift;
238  
239 0 my $hbase = $self->{hbase};
240 0 $hbase->{last_error} = undef;
241  
242 0 my $scan_uri = _build_scan_uri($params);
243  
244 0 my $rows = $hbase->_get_tiny($scan_uri);
245  
246 0 return $rows;
247 }
248  
249 1 ▶sub _build_scan_uri {
Subroutine _build_scan_uri: called CC=10 SCAR=24.8
250 1 my $params = shift;
251  
252 #
253 # request parameters:
254 #
255 # 1. startrow - The start row for the scan.
256 # 2. endrow - The end row for the scan.
257 # 4. starttime, endtime - To only retrieve columns within a specific range of version timestamps, both start and end time must be specified.
258 # 5. maxversions - To limit the number of versions of each column to be returned.
259 # 6. limit - The number of rows to return in the scan operation.
260  
261 1 my $table = $params->{table};
262 1 ▶ my $limit = $params->{limit} || 1;
Condition50%1/2
$params->{'limit'} || 1
l!l
10
MC/DC0%0/1
$params->{'limit'} || 1
$params->{'limit'}
Truth table50%1/2
A
A= $params->{'limit'}
Aresult
11
00
263  
264 # optional
265 1 ▶ my $startrow = $params->{startrow} || "";
Condition50%1/2
$params->{'startrow'} || ''
l!l
10
MC/DC0%0/1
$params->{'startrow'} || ''
$params->{'startrow'}
Truth table50%1/2
A
A= $params->{'startrow'}
Aresult
11
00
266 1 ▶ my $endrow = $params->{endrow} || "";
Condition50%1/2
$params->{'endrow'} || ''
l!l
01
MC/DC0%0/1
$params->{'endrow'} || ''
$params->{'endrow'}
Truth table50%1/2
A
A= $params->{'endrow'}
Aresult
11
00
267 1 my $starttime = $params->{starttime}; # server's default is 0
268 1 my $endtime = $params->{endtime}; # server's default is Long.MAX_VALUE
269 1 my $maxversions = $params->{maxversions}; # server's default is 1
270  
271 # not supported yet:
272 1 ▶ my $columns = $params->{columns} || "";
Condition50%1/2
$params->{'columns'} || ''
l!l
01
MC/DC0%0/1
$params->{'columns'} || ''
$params->{'columns'}
Truth table50%1/2
A
A= $params->{'columns'}
Aresult
11
00
273  
274 # option to do scans with exclusion of first row. Usefull when
275 # scanning for the next batch based on the last key from previous
276 # batch. By default this option is false.
277 1 ▶ my $exclude_startrow = $params->{exclude_startrow_from_result} || 0;
Condition50%1/2
$params->{'exclude_startrow_from_result'} || 0
l!l
01
MC/DC0%0/1
$params->{'exclude_startrow_from_result'} || 0
$params->{'exclude_startrow_from_result'}
Truth table50%1/2
A
A= $params->{'exclude_startrow_from_result'}
Aresult
11
00
278  
279 1 my $uri;
280  
281 1 ▶ if ($exclude_startrow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_startrow) { } 0 1 1
282 0 $startrow = uri_escape($startrow) . uri_escape(chr(0));
283 }
284 else {
285 1 $startrow = uri_escape($startrow);
286 }
287 1 $uri
288 = "/"
289 . uri_escape($table)
290 . "/"
291 . '*?'
292 . "startrow=" . $startrow
293 . "&limit=" . $limit
294 ;
295  
296 1 ▶ $uri .= "&starttime=" . $starttime if defined $starttime;
Branch: 1 branch
BranchTrueFalseTotal
if defined $starttime 0 1 1
297 1 ▶ $uri .= "&endtime=" . $endtime if defined $endtime;
Branch: 1 branch
BranchTrueFalseTotal
if defined $endtime 0 1 1
298 1 ▶ $uri .= "&maxversions=" . $maxversions if defined $maxversions;
Branch: 1 branch
BranchTrueFalseTotal
if defined $maxversions 0 1 1
299  
300 1 return $uri;
301 }
302  
303 0 ▶sub _filter_rows_beyond_last_key {
Subroutine _filter_rows_beyond_last_key: not called CC=10 SCAR=47.0
304 0 my $self = shift;
305 0 my $rows = shift;
306  
307 0 my $last_retrieved_row = $rows->[-1]->{row};
308  
309 0 ▶ if ($self->{end_condition_type} eq 'PREFIX') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'end_condition_type'} eq 'PREFIX') { } 0 0 0
elsif ($self->{'end_condition_type'} eq 'ENDROW') { } 0 0 0
elsif ($self->{'end_condition_type'} eq 'NONE') { } 0 0 0
310 0 my $prefix_end = $self->{prefix} . chr(255);
311 0 ▶ if ($last_retrieved_row gt $prefix_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_retrieved_row gt $prefix_end) 0 0 0
312 # need to filter out surpluss of rows
313 0 @$rows = grep { $_->{row} le $prefix_end } @$rows;
314 # also mark EOF
315 0 $self->{EOF} = 1;
316 0 ▶ if ($rows && @$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows and @$rows) 0 0 0
Condition0%0/3
$rows and @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table0%0/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
317 0 my $last_retrieved_valid_row = $rows->[-1]->{row};
318 0 $self->{last_key_from_previous_batch} = $last_retrieved_valid_row;
319 }
320 0 return;
321 }
322 }
323 elsif ($self->{end_condition_type} eq 'ENDROW') {
324 0 ▶ if ($last_retrieved_row ge $self->{endrow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_retrieved_row ge $self->{"endrow"}) 0 0 0
325 # need to filter out surpluss of rows
326 0 @$rows = grep { $_->{row} lt $self->{endrow} } @$rows;
327 # also mark EOF
328 0 $self->{EOF} = 1;
329 0 ▶ if ($rows && @$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows and @$rows) 0 0 0
Condition0%0/3
$rows and @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table0%0/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
330 0 my $last_retrieved_valid_row = $rows->[-1]->{row};
331 0 $self->{last_key_from_previous_batch} = $last_retrieved_valid_row;
332 }
333 0 return;
334 }
335 }
336 elsif ($self->{end_condition_type} eq 'NONE') {
337 0 return;
338 }
339 else {
340 0 die 'Unknown end_condition_type!';
341 }
342 }
343  
344 2 1;
345  
346 __END__
↑ Summary JSONRest.pm »