IO/TypedBytes.pm

filter: Statementstmt 28.5% 32 / 112 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 33.3% 10 / 30 Podpod 100% 5 / 5 totaltotal 23.9% 47 / 196 CC 27 SCAR 24.6
CC
27
Cov
21%
decode_hive
30.0
__ANON__
30.0
encode
20.0
CRAP 11.7
« IO/SequenceFile/Text.pm ↑ Summary IO/SequenceFile/BytesWriteable.pm »
1 package Hadoop::IO::TypedBytes;
2 1 $Hadoop::IO::TypedBytes::VERSION = '0.003';
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @VERSION = 0.01;
9  
10 1 our @EXPORT_OK = qw(
11 decode
12 decode_hive
13 encode
14 encode_hive
15 make_reader
16 );
17  
18  
19 6 ▶sub _primitive {
Subroutine _primitive: called CC=1 SCAR=3.1
20 6 my ($name, $code, $size, $format) = @_;
21  
22 return {
23 name => $name,
24 code => $code,
25 0 ▶ enc => sub { pack $format, shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ dec => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
27 0 my $read = shift;
28 0 ▶ return unpack $format, $read->($size) // die "EOF while decoding $name";
Condition0%0/2
&$read($size) // die("EOF while decoding $name")
l!l
00
MC/DC0%0/1
&$read($size) // die("EOF while decoding $name")
&$read($size)
Truth table0%0/2
A
A= &$read($size)
Aresult
11
00
29 },
30 }
31 6 }
32  
33 2 ▶sub _prefixed {
Subroutine _prefixed: called CC=1 SCAR=3.9
34 2 my ($name, $code) = @_;
35  
36 return {
37 name => $name,
38 code => $code,
39 0 ▶ enc => sub { my $d = shift; pack("L>", length $d) . $d },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 ▶ dec => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
41 0 my $read = shift;
42 0 ▶ my $len = unpack "L>", $read->(4) // die "EOF while decoding $name";
Condition0%0/2
&$read(4) // die("EOF while decoding $name")
l!l
00
MC/DC0%0/1
&$read(4) // die("EOF while decoding $name")
&$read(4)
Truth table0%0/2
A
A= &$read(4)
Aresult
11
00
43 0 return $read->($len);
44 }
45 }
46 2 }
47  
48 1 ▶sub _array {
Subroutine _array: called CC=1 SCAR=5.1
49 1 my ($name, $code) = @_;
50  
51 return {
52 name => $name,
53 code => $code,
54 0 ▶ enc => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
55 0 my $vec = shift;
56 0 my $buf = pack("L>", scalar @$vec);
57 0 $buf .= encode($_) foreach (@$vec);
58 0 return $buf;
59 },
60  
61 0 ▶ dec => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
62 0 my $read = shift;
63 0 ▶ my $len = unpack "L>", $read->(4) // die "EOF while decoding $name";
Condition0%0/2
&$read(4) // die("EOF while decoding $name")
l!l
00
MC/DC0%0/1
&$read(4) // die("EOF while decoding $name")
&$read(4)
Truth table0%0/2
A
A= &$read(4)
Aresult
11
00
64 0 my @vec;
65 0 ▶ while ($len-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len-- > 0) 0 0 0
66 0 push @vec, _decode($read, $name);
67 }
68 0 return \@vec;
69 },
70 }
71 1 }
72  
73 1 ▶sub _struct {
Subroutine _struct: called CC=1 SCAR=5.3
74 1 my ($name, $code) = @_;
75  
76 return {
77 name => $name,
78 code => $code,
79 0 ▶ enc => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
80 0 my $map = shift;
81 0 my $buf = pack("L>", scalar keys %$map);
82 0 $buf .= encode([ $_, $map->{$_} ]) foreach keys %$map;
83 0 return $buf;
84 },
85  
86 0 ▶ dec => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
87 0 my $read = shift;
88 0 ▶ my $len = unpack "L>", $read->(4) // die "EOF while decoding $name";
Condition0%0/2
&$read(4) // die("EOF while decoding $name")
l!l
00
MC/DC0%0/1
&$read(4) // die("EOF while decoding $name")
&$read(4)
Truth table0%0/2
A
A= &$read(4)
Aresult
11
00
89 0 my %map;
90 0 ▶ while ($len-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len-- > 0) 0 0 0
91 0 my ($k, $v) = @{_decode($read, $name)};
92 0 $map{$k} = $v;
93 }
94 0 return \%map;
95 },
96 }
97 1 }
98  
99 1 ▶sub _list {
Subroutine _list: called CC=1 SCAR=4.6
100 1 my ($name, $code) = @_;
101 return {
102 name => $name,
103 code => $code,
104 0 ▶ enc => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
105 0 my $lst = shift;
106 0 my $buf = "";
107 0 $buf .= encode($_) foreach @$lst;
108 0 return $buf . "\xff";
109 },
110 0 ▶ dec => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 my $read = shift;
112 0 my (@lst, @val);
113 0 ▶ push @lst, @val while @val = _decode($read, $name);
Branch: 1 branch
BranchTrueFalseTotal
@val = _decode($read, $name) and do { push @lst, @val } 0 0 0
114 0 return \@lst;
115 }
116 }
117 1 }
118  
119 2 ▶sub _empty {
Subroutine _empty: called CC=1 SCAR=1.2
120 2 my ($name, $code) = @_;
121 return {
122 name => $name,
123 code => $code,
124 0 ▶ enc => sub { "" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ dec => sub { () },
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 2 };
127 }
128  
129 1 my @TYPES = (
130 _prefixed(bytes => 0),
131 _primitive(byte => 1, 1, "c"),
132 _primitive(bool => 2, 1, "c"),
133 _primitive(int => 3, 4, "l>"),
134 _primitive(long => 4, 8, "q>"),
135 _primitive(float => 5, 4, "f>"),
136 _primitive(double => 6, 8, "d>"),
137 _prefixed(string => 7),
138 _array(array => 8),
139 _list(list => 9),
140 _struct(struct => 10),
141 _empty(end_of_record => 177), # hive specific
142 _empty(empty => 255),
143 );
144  
145 1 my %type_by_name = map { $_->{name}, $_ } @TYPES;
146 1 my %type_by_code = map { $_->{code}, $_ } @TYPES;
147  
148  
149 0 ▶sub encode {
Subroutine encode: not called CC=4 SCAR=30.0
Pod documented
150 0 my ($val) = @_;
151 0 ▶ if (ref $val eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH') { } 0 0 0
elsif (ref $val) { } 0 0 0
152 0 return _encode_as($val, "array");
153 }
154 elsif (ref $val eq "HASH") {
155 0 return _encode_as($val, "struct");
156 }
157 elsif (ref $val) {
158 0 die "unsupported value reference $val";
159 }
160 else {
161 0 return _encode_as($val, "string");
162 }
163 }
164  
165 0 ▶sub _encode_as {
Subroutine _encode_as: not called CC=2 SCAR=17.9
166 0 my ($val, $type) = @_;
167  
168 0 ▶ my $codec = $type_by_name{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $codec = $type_by_name{$type} or die "unsupported type name '${type}'" 0 0 0
169 or die "unsupported type name '$type'";
170  
171 0 my $code = pack "c", $codec->{code};
172 0 return $code . $codec->{enc}->($val);
173 }
174  
175  
176 0 ▶sub decode {
Subroutine decode: not called CC=1 SCAR=6.9
Pod documented
177 0 my $read = shift;
178 0 return _decode($read);
179 }
180  
181 0 ▶sub _decode {
Subroutine _decode: not called CC=4 SCAR=30.0
182 0 my ($read, $inner) = @_;
183  
184 0 my $mark = $read->(1);
185 0 ▶ unless (defined $mark) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mark) 0 0 0
186 0 ▶ if ($inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner) { } 0 0 0
187 0 die "EOF while decoding $inner";
188 } else {
189 0 return ();
190 }
191 }
192  
193 0 my $code = unpack "C", $mark;
194 0 ▶ my $codec = $type_by_code{$code}
Branch: 1 branch
BranchTrueFalseTotal
my $codec = $type_by_code{$code} or die "unsupported type code '${code}'" 0 0 0
195 or die "unsupported type code '$code'";
196  
197 0 return $codec->{dec}->($read);
198 }
199  
200  
201 0 ▶sub encode_hive {
Subroutine encode_hive: not called CC=2 SCAR=17.9
Pod documented
202 0 my $buf;
203 0 $buf .= encode($_) foreach (@_);
204 0 $buf .= _encode_as(undef, "end_of_record");
205 0 return $buf;
206 }
207  
208  
209 0 ▶sub decode_hive {
Subroutine decode_hive: not called CC=5 SCAR=34.0
Pod documented
210 0 my $read = shift;
211 my $codec = $type_by_name{list}
212 0 ▶ or die "missing 'list' type codec";
Branch: 1 branch
BranchTrueFalseTotal
my $codec = $type_by_name{"list"} or die "missing 'list' type codec" 0 0 0
213  
214 0 my $inner;
215 0 my (@lst, @val);
216  
217 # If decode returns empty list at first read, this means either EOF at row
218 # boundary or empty row (row starting with row terminator). The latter is
219 # impossible because Hive requires at least one input expr in the transform
220 # clause, so it must be EOF.
221  
222 0 my $first = 1;
223 0 ▶ push @lst, @val while @val = _decode($read, $first ? $first = undef : "hive record");
Branch: 2 branches
BranchTrueFalseTotal
@val = _decode($read, $first ? $first = undef : "hive record") and do { push @lst, @val } 0 0 0
$first ? : 0 0 0
224  
225 0 ▶ return @lst ? \@lst : undef;
Branch: 1 branch
BranchTrueFalseTotal
@lst ? : 0 0 0
226 }
227  
228  
229 0 ▶sub make_reader {
Subroutine make_reader: not called CC=1 SCAR=6.9
Pod documented
230 0 my $fh = shift;
231  
232 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
233 0 my $len = shift;
234 0 ▶ return "" if $len <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 0 0 0
235  
236 0 ▶ my $num = $fh->read(my $buf, $len) // die $!;
Condition0%0/2
$fh->read(my $buf, $len) // die($!)
l!l
00
MC/DC0%0/1
$fh->read(my $buf, $len) // die($!)
$fh->read(my $buf, $len)
Truth table0%0/2
A
A= $fh->read(my $buf, $len)
Aresult
11
00
237  
238 0 ▶ return undef if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 0 0 0
239  
240 0 ▶ die "EOF in the middle of value (want $len bytes, got $num)" if $num < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $num < $len 0 0 0
241  
242 0 return $buf;
243 }
244 0 }
245  
246  
247 1 1;
248  
249 __END__
« IO/SequenceFile/Text.pm ↑ Summary IO/SequenceFile/BytesWriteable.pm »