blib/lib/Gzip/BinarySearch.pm

filter: Statementstmt 96.8% 154 / 159 Branchbran 92.1% 59 / 64 Conditioncond 76.9% 30 / 39 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 100% 25 / 25 Podpod 100% 8 / 8 totaltotal 89.1% 286 / 321 CC 48 SCAR 18.0
CC
48
Cov
93%
_last_line
10.1
_first_line
9.1
_search_surrounding_lines
9.0
CRAP 6.0
↑ Summary
1 package Gzip::BinarySearch;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Gzip::RandomAccess;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our $VERSION = '0.91';
10  
11 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 our @ISA = qw(Exporter);
14 10 our @EXPORT_OK = qw(fs_column tsv_column);
15 }
16  
17 # When binary searching, assume this is the biggest line we'll find.
18 # We'll try this first, then try a bigger decompression if it fails.
19 10 my $DEFAULT_EST_LINE_LENGTH = 512;
20  
21 # When looking for adjacent identical lines (find_all) extract this
22 # many bytes at a time.
23 10 my $DEFAULT_SURROUNDING_LINES_BLOCKSIZE = 4096;
24  
25 10 my @GRA_ALLOWED_ARGS = qw(file index_file index_span cleanup);
26 10 my %ALLOWED_ARGS = map { $_ => 1 } (
27 @GRA_ALLOWED_ARGS, qw(key_func cmp_func),
28 qw(est_line_length surrounding_lines_blocksize),
29 );
30  
31 10 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod documented
32 10 my ($class, %args) = @_;
33 10 my $gzip = $class->_build_gzip(%args);
34 10 for my $key (keys %args) {
35 20 ▶ $ALLOWED_ARGS{$key} or croak "Invalid argument '$key'";
Branch: 1 branch
BranchTrueFalseTotal
$ALLOWED_ARGS{$key} or croak("Invalid argument '${key}'") 19 1 20
36 }
37  
38 9 ▶ my $key_func = $args{key_func} || fs_column(qr/\s+/, 1);
Condition66%2/3
$args{'key_func'} || fs_column(qr/\s+/, 1)
l!l&&r!l&&!r
270
MC/DC0%0/2
$args{'key_func'} || fs_column(qr/\s+/, 1)
$args{'key_func'} fs_column(qr/\s+/, 1)
Truth table66%2/3
A || B
A= $args{'key_func'}B= fs_column(qr/\s+/, 1)
ABresult
1X1
011
000
39 9 ▶ my $cmp_func = $args{cmp_func} || sub { $main::a cmp $main::b };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'cmp_func'} || sub { $main::a cmp $main::b; }
l!l&&r!l&&!r
180
MC/DC0%0/2
$args{'cmp_func'} || sub { $main::a cmp $main::b; }
$args{'cmp_func'} sub { $main::a cmp $main::b; }
Truth table66%2/3
A || B
A= $args{'cmp_func'}B= sub { $main::a cmp $main::b; }
ABresult
1X1
011
000
40  
41 my $est_line_length = $args{est_line_length}
42 9 ▶ || $DEFAULT_EST_LINE_LENGTH;
Condition66%2/3
$args{'est_line_length'} || $DEFAULT_EST_LINE_LENGTH
l!l&&r!l&&!r
180
MC/DC0%0/2
$args{'est_line_length'} || $DEFAULT_EST_LINE_LENGTH
$args{'est_line_length'} $DEFAULT_EST_LINE_LENGTH
Truth table66%2/3
A || B
A= $args{'est_line_length'}B= $DEFAULT_EST_LINE_LENGTH
ABresult
1X1
011
000
43  
44 my $surrounding_lines_blocksize = $args{surrounding_lines_blocksize}
45 9 ▶ || $DEFAULT_SURROUNDING_LINES_BLOCKSIZE;
Condition66%2/3
$args{'surrounding_lines_blocksize'} || $DEFAULT_SURROUNDING_LINES_BLOCKSIZE
l!l&&r!l&&!r
180
MC/DC0%0/2
$args{'surrounding_lines_blocksize'} || $DEFAULT_SURROUNDING_LINES_BLOCKSIZE
$args{'surrounding_lines_blocksize'} $DEFAULT_SURROUNDING_LINES_BLOCKSIZE
Truth table66%2/3
A || B
A= $args{'surrounding_lines_blocksize'}B= $DEFAULT_SURROUNDING_LINES_BLOCKSIZE
ABresult
1X1
011
000
46  
47 9 bless {
48 gzip => $gzip,
49 max_offset => $gzip->uncompressed_size - 1,
50 est_line_length => $est_line_length,
51 surrounding_lines_blocksize => $surrounding_lines_blocksize,
52 key_func => $key_func,
53 cmp_func => $cmp_func,
54 }, $class;
55 }
56  
57 28 ▶sub find {
Subroutine find: called CC=2 SCAR=6.9
Pod documented
58 28 my ($self, $key) = @_;
59  
60 28 my ($line, $mid) = $self->_find($key);
61 28 ▶ return $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 25 3 28
62 3 return;
63 }
64  
65 8 ▶sub find_all {
Subroutine find_all: called CC=2 SCAR=6.9
Pod documented
66 8 my ($self, $key) = @_;
67  
68 8 my ($line, $mid) = $self->_find($key);
69 8 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 7 1 8
70 7 return $self->_search_surrounding_lines($key, $line, $mid);
71 }
72 1 return;
73 }
74  
75 4 ▶sub gzip { shift->{gzip} }
Subroutine gzip: called CC=1 SCAR=0.0
Pod documented
76 1 ▶sub est_line_length { shift->{est_line_length} }
Subroutine est_line_length: called CC=1 SCAR=0.0
Pod documented
77 1 ▶sub surrounding_lines_blocksize { shift->{surrounding_lines_blocksize} }
Subroutine surrounding_lines_blocksize: called CC=1 SCAR=0.0
Pod documented
78  
79 # Convenience functions
80 38 ▶sub fs_column {
Subroutine fs_column: called CC=4 SCAR=13.9
Pod documented
81 38 my ($field_sep, $column_number) = @_;
82 38 ▶ croak "Invalid column number, should be 1-based"
Branch: 1 branch
BranchTrueFalseTotal
if $column_number < 1 2 36 38
83 if $column_number < 1;
84  
85 36 ▶ if (!ref $field_sep && $field_sep eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $field_sep and $field_sep eq " ") 7 29 36
Condition100%3/3
not ref $field_sep and $field_sep eq " "
!ll&&!rl&&r
19107
MC/DC100%2/2
not ref $field_sep and $field_sep eq " "
not ref $field_sep $field_sep eq " "
Truth table100%3/3
not(A) and B
A= not ref $field_sepB= $field_sep eq " "
ABresult
0X0
100
111
86 # Force Perl to match a space, not \s+
87 7 $field_sep = qr/ /;
88 }
89  
90 305 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 305 chomp;
92 305 my @f = split $field_sep, $_, -1;
93 305 return $f[$column_number - 1];
94 }
95 36 }
96  
97 7 ▶sub tsv_column {
Subroutine tsv_column: called CC=1 SCAR=0.0
Pod documented
98 7 my $column_number = shift;
99 7 return fs_column("\t", $column_number);
100 }
101  
102 # Given a key, binary search for the first line encountered with this key,
103 # and return both the line and its offset in the uncompressed data.
104 # Return nothing if the key was not found.
105 36 ▶sub _find {
Subroutine _find: called CC=4 SCAR=13.9
106 36 my ($self, $key) = @_;
107 36 my ($low, $high, $mid) = (0, $self->{max_offset}, undef);
108  
109 36 ▶ while ($low <= $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low <= $high) 226 4 230
110 226 $mid = int(($low + $high) / 2);
111 226 my $line = $self->_get_line_at($mid);
112 226 my $line_key = $self->_get_key($line);
113 226 my $cmp = $self->_compare_keys($line_key, $key);
114  
115 226 ▶ if ($cmp < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 92 134 226
elsif ($cmp > 0) { } 102 32 134
116 92 $low = $mid + 1;
117 }
118 elsif ($cmp > 0) {
119 102 $high = $mid - 1;
120 }
121 else {
122 32 return ($line, $mid);
123 }
124 }
125  
126 # Key not found
127 4 return;
128 }
129  
130 # Given an offset, return the entire line 'around' that offset.
131 # This includes any trailing newline character.
132 # If the offset is a newline, it will be counted as part of the preceding line.
133 282 ▶sub _get_line_at {
Subroutine _get_line_at: called CC=7 SCAR=20.8
134 282 my ($self, $offset) = @_;
135 282 my $L = $self->{est_line_length};
136  
137 282 my $seek_start = max($offset - $L, 0); # global offset
138 282 my $seek_length = $L * 2;
139 282 my $midpoint = $offset - $seek_start; # relative to extracted $block, not global
140  
141 282 my $line;
142 282 ▶ while (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 282 282 564
143 282 my $block = $self->{gzip}->extract($seek_start, $seek_length);
144  
145 282 my $prev_nl = rindex($block, "\n", $midpoint - 1); # -1 if no match
146 282 my $next_nl = index($block, "\n", $midpoint);
147  
148 282 ▶ if ($prev_nl == -1 && $seek_start > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prev_nl == -1 and $seek_start > 0) { } 0 282 282
elsif ($next_nl == -1 and $seek_start + $seek_length < $self->{'max_offset'}) { } 0 282 282
Condition66%2/3
$prev_nl == -1 and $seek_start > 0
!ll&&!rl&&r
269130
MC/DC0%0/2
$prev_nl == -1 and $seek_start > 0
$prev_nl == -1 $seek_start > 0
Truth table66%2/3
A and B
A= $prev_nl == -1B= $seek_start > 0
ABresult
0X0
100
111
149 0 my $extension = min($L, $seek_start);
150 0 $seek_start -= $extension;
151 0 $seek_length += $extension;
152 0 $midpoint += $extension;
153 }
154 ▶ elsif ($next_nl == -1 && $seek_start + $seek_length < $self->{max_offset}) {
Condition66%2/3
$next_nl == -1 and $seek_start + $seek_length < $self->{"max_offset"}
!ll&&!rl&&r
27930
MC/DC0%0/2
$next_nl == -1 and $seek_start + $seek_length < $self->{"max_offset"}
$next_nl == -1 $seek_start + $seek_length < $self->{"max_offset"}
Truth table66%2/3
A and B
A= $next_nl == -1B= $seek_start + $seek_length < $self->{"max_offset"}
ABresult
0X0
100
111
155 0 $seek_length += $L;
156 }
157 else {
158 282 ▶ $next_nl = length($block) if $next_nl == -1; # no EOF newline - use last char
Branch: 1 branch
BranchTrueFalseTotal
if $next_nl == -1 3 279 282
159 282 $line = substr($block, $prev_nl + 1, $next_nl - $prev_nl);
160 }
161 }
162  
163 282 return $line;
164 }
165  
166 312 ▶sub _get_key {
Subroutine _get_key: called CC=1 SCAR=0.0
167 312 my ($self, $line) = @_;
168 312 local *::_ = \$line;
169 312 return $self->{key_func}->();
170 }
171  
172 312 ▶sub _compare_keys {
Subroutine _compare_keys: called CC=1 SCAR=0.0
173 312 my ($self, $key1, $key2) = @_;
174 312 local (*::a, *::b) = (\$key1, \$key2);
175 312 return $self->{cmp_func}->();
176  
177 }
178  
179 # Given a line and a seekpoint in the uncompressed data, look forwards and backwards
180 # for adjacent lines with the same key. Return matching lines in same order as file.
181 7 ▶sub _search_surrounding_lines {
Subroutine _search_surrounding_lines: called CC=9 SCAR=22.0
182 7 my ($self, $query, $mid_line, $mid) = @_;
183 # Problem description:
184 # when searching for 'zits', the algorithm expands, then shrinks line-by-line
185 # at the end it trims the string to "zings\nzits\n..." and for some reason the
186 # zits offset isn't used to trim it further.
187 # you'll notice the _compare_keys thing which means $result_start is never set
188 # if we're shrinking (?)
189  
190 7 my $block_start = max($mid - length($mid_line) + 1, 0);
191 7 my $block_end = min($mid + length($mid_line) - 1, $self->{max_offset});
192 7 my $block = $self->{gzip}->extract($block_start, $block_end - $block_start);
193  
194 # offsets for the result range, relative to block_start
195 7 my ($result_start, $result_end) = (0, $block_end - $block_start);
196  
197 7 my $shrinking = 0;
198 7 while (1) {
199 47 my ($line, $offset) = $self->_first_line($block, $result_start, $result_end, $block_start == 0);
200 47 ▶ if (!defined $line || $self->_compare_keys($self->_get_key($line), $query) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0) { } 14 33 47
Condition100%3/3
not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0
l!l&&r!l&&!r
11333
MC/DC100%2/2
not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0
not defined $line $self->_compare_keys($self->_get_key($line), $query) == 0
Truth table100%3/3
not(A) or B
A= not defined $lineB= $self->_compare_keys($self->_get_key($line), $query) == 0
ABresult
1X1
011
000
201 14 ▶ last if $shrinking;
Branch: 1 branch
BranchTrueFalseTotal
if $shrinking 6 8 14
202 8 ▶ last if $block_start == 0; # can't expand further
Branch: 1 branch
BranchTrueFalseTotal
if $block_start == 0 1 7 8
203 # Expand left
204 7 my $ex = min($self->{surrounding_lines_blocksize}, $block_start);
205 7 $block_start -= $ex;
206 7 $result_end += $ex;
207 7 my $chunk = $self->{gzip}->extract($block_start, $ex);
208 7 $block = $chunk . $block;
209 }
210 else {
211 # Shrink
212 33 $result_start = $offset + length($line);
213 33 $shrinking = 1;
214 }
215 }
216 7 my ($line, $offset) = $self->_first_line($block, $result_start, $result_end, $block_start == 0);
217 7 $result_start = $offset;
218  
219 7 $shrinking = 0;
220 7 while (1) {
221 45 my ($line, $offset) = $self->_last_line($block, $result_start, $result_end, $result_end == $self->{max_offset});
222 45 ▶ if (!defined $line || $self->_compare_keys($self->_get_key($line), $query) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0) { } 14 31 45
Condition100%3/3
not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0
l!l&&r!l&&!r
5931
MC/DC100%2/2
not defined $line or $self->_compare_keys($self->_get_key($line), $query) == 0
not defined $line $self->_compare_keys($self->_get_key($line), $query) == 0
Truth table100%3/3
not(A) or B
A= not defined $lineB= $self->_compare_keys($self->_get_key($line), $query) == 0
ABresult
1X1
011
000
223 14 ▶ last if $shrinking;
Branch: 1 branch
BranchTrueFalseTotal
if $shrinking 6 8 14
224 8 ▶ last if $block_end == $self->{max_offset};
Branch: 1 branch
BranchTrueFalseTotal
if $block_end == $self->{"max_offset"} 1 7 8
225 # Expand right
226 7 my $ex = min($self->{surrounding_lines_blocksize}, $self->{max_offset} - $block_end);
227 7 my $chunk = $self->{gzip}->extract($block_end, $ex);
228 7 $block_end += $ex;
229 7 $result_end += $ex;
230 7 $block .= $chunk;
231 }
232 else {
233 31 $result_end = $offset - 1;
234 31 $shrinking = 1;
235 }
236 }
237
238 7 my $result = substr($block, $result_start, $result_end - $result_start);
239 7 return map { "$_\n" } split /\n/, $result;
240 }
241  
242 # Get the first complete line from the block, limiting to a range from
243 # $start to $end (inclusive). If $allow_incomplete, we instead return
244 # all data up to the first \n.
245 # Also return the offset of the start of this line, relative to the
246 # entire text block.
247 # Return nothing if we need more data (left/right) to get a full line.
248 68 ▶sub _first_line {
Subroutine _first_line: called CC=9 SCAR=22.1
249 68 my ($self, $block, $start, $end, $allow_incomplete) = @_;
250 68 my $nl = index($block, "\n", $start);
251 68 ▶ $nl = -1 if $nl > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $nl > $end 0 68 68
252 68 ▶ if ($allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_incomplete) { } 52 16 68
253 52 ▶ $nl = $end if $nl == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nl == -1 3 49 52
254 52 return (substr($block, $start, $nl - $start + 1), $start);
255 }
256 else {
257 16 ▶ return if $nl == -1; # more data needed
Branch: 1 branch
BranchTrueFalseTotal
if $nl == -1 2 14 16
258 14 my $nl2 = index($block, "\n", $nl + 1);
259 14 ▶ die if $nl2 < $start && $nl2 != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nl2 < $start and $nl2 != -1 0 14 14
Condition66%2/3
$nl2 < $start and $nl2 != -1
!ll&&!rl&&r
1220
MC/DC0%0/2
$nl2 < $start and $nl2 != -1
$nl2 < $start $nl2 != -1
Truth table66%2/3
A and B
A= $nl2 < $startB= $nl2 != -1
ABresult
0X0
100
111
260 14 ▶ return if $nl2 == -1 || $nl2 > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $nl2 == -1 or $nl2 > $end 3 11 14
Condition100%3/3
$nl2 == -1 or $nl2 > $end
l!l&&r!l&&!r
2111
MC/DC100%2/2
$nl2 == -1 or $nl2 > $end
$nl2 == -1 $nl2 > $end
Truth table100%3/3
A or B
A= $nl2 == -1B= $nl2 > $end
ABresult
1X1
011
000
261 11 return (substr($block, $nl + 1, $nl2 - $nl), $nl + 1);
262 }
263 }
264  
265 # Same deal as above, but for the last line. $allow_incomplete
266 # returns all data from the last \n onwards, and ignores a
267 # trailing newline at the end of the range.
268 59 ▶sub _last_line {
Subroutine _last_line: called CC=10 SCAR=23.1
269 59 my ($self, $block, $start, $end, $allow_incomplete) = @_;
270 59 my $nl = rindex($block, "\n", $end);
271 59 ▶ $nl = -1 if $nl < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $nl < $start 4 55 59
272 59 ▶ if ($allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_incomplete) { } 12 47 59
273 12 ▶ $nl = rindex($block, "\n", $nl - 1) if $nl == $end; # ignore trailing newline
Branch: 1 branch
BranchTrueFalseTotal
if $nl == $end 2 10 12
274 12 ▶ $nl = $start - 1 if $nl == -1; # pretend there's one
Branch: 1 branch
BranchTrueFalseTotal
if $nl == -1 2 10 12
275 12 return (substr($block, $nl + 1, $end - $nl), $nl + 1);
276 }
277 else {
278 47 ▶ return if $nl == -1; # more data needed
Branch: 1 branch
BranchTrueFalseTotal
if $nl == -1 3 44 47
279 44 my $nl2 = rindex($block, "\n", $nl - 1);
280 44 ▶ die if $nl2 > $end && $nl2 != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nl2 > $end and $nl2 != -1 0 44 44
Condition33%1/3
$nl2 > $end and $nl2 != -1
!ll&&!rl&&r
4400
MC/DC0%0/2
$nl2 > $end and $nl2 != -1
$nl2 > $end $nl2 != -1
Truth table33%1/3
A and B
A= $nl2 > $endB= $nl2 != -1
ABresult
0X0
100
111
281 44 ▶ return if $nl2 == -1 || $nl2 < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $nl2 == -1 or $nl2 < $start 6 38 44
Condition100%3/3
$nl2 == -1 or $nl2 < $start
l!l&&r!l&&!r
2438
MC/DC100%2/2
$nl2 == -1 or $nl2 < $start
$nl2 == -1 $nl2 < $start
Truth table100%3/3
A or B
A= $nl2 == -1B= $nl2 < $start
ABresult
1X1
011
000
282 38 return (substr($block, $nl2 + 1, $nl - $nl2), $nl2 + 1);
283 }
284 }
285  
286 # Build and return the Gzip::RandomAccess object.
287 10 ▶sub _build_gzip {
Subroutine _build_gzip: called CC=3 SCAR=11.0
288 10 my ($class, %args) = @_;
289  
290 10 my %gra_args;
291 10 for my $key (@GRA_ALLOWED_ARGS) {
292 40 ▶ $gra_args{$key} = $args{$key} if exists $args{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 15 25 40
293 }
294  
295 10 return Gzip::RandomAccess->new(%gra_args);
296 }
297  
298 __END__
↑ Summary