Avro/BinaryDecoder.pm

filter: Statementstmt 69.4% 132 / 190 Branchbran 62.5% 20 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.5% 23 / 40 Podpod 3.1% 1 / 32 totaltotal 59.2% 177 / 299 CC 23 SCAR 13.9
CC
23
Cov
68%
skip_block
20.0
decode_map
6.6
skip_record
6.0
CRAP 4.0
« Avro/DataFileReader.pm ↑ Summary Avro/DataFileWriter.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # https://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package Avro::BinaryDecoder;
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Encode();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Error::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Avro::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 our $VERSION = '1.12.2';
28  
29 2 our $complement = ~0x7F;
30 2 ▶unless ($Config{use64bitint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Avro::BinaryDecoder::Config{"use64bitint"}) 0 2 2
31 0 require Math::BigInt;
32 0 $complement = Math::BigInt->new("0b" . ("1" x 57) . ("0" x 7));
33 }
34  
35 =head2 decode(%param)
36  
37 Resolve the given writer and reader_schema to decode the data provided by the
38 reader.
39  
40 =over 4
41  
42 =item * writer_schema
43  
44 The schema that was used to encode the data provided by the C<reader>
45  
46 =item * reader_schema
47  
48 The schema we want to use to decode the data.
49  
50 =item * reader
51  
52 A file handle, or an object implementing a similar interface, like L<IO::File>.
53 Specifically, it must support C<read($buf, $nbytes)> and
54 C<seek($nbytes, $whence)>. These calls will block the decoder if not
55 enough data is available for read.
56  
57 =back
58  
59 =cut
60 1338 ▶sub decode {
Subroutine decode: called CC=2 SCAR=6.9
Pod documented
61 1338 my $class = shift;
62 1338 my %param = @_;
63  
64 my ($writer_schema, $reader_schema, $reader)
65 1338 = @param{qw/writer_schema reader_schema reader/};
66  
67 1338 ▶ my $type = Avro::Schema->match(
Branch: 1 branch
BranchTrueFalseTotal
my $type = "Avro::Schema"->match("writer", $writer_schema, "reader", $reader_schema) or "Avro::Schema::Error::Mismatch"->throw("schema do not match") 1337 1 1338
68 writer => $writer_schema,
69 reader => $reader_schema,
70 ) or throw Avro::Schema::Error::Mismatch 'schema do not match';
71  
72 1337 my $meth = "decode_$type";
73 1337 return $class->$meth($writer_schema, $reader_schema, $reader);
74 }
75  
76 2 ▶sub skip {
Subroutine skip: called CC=2 SCAR=7.0
Pod undocumented
77 2 my $class = shift;
78 2 my ($schema, $reader) = @_;
79 2 ▶ my $type = ref $schema ? $schema->type : $schema;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema ? : 2 0 2
80 2 my $meth = "skip_$type";
81 2 return $class->$meth($schema, $reader);
82 }
83  
84 1 ▶sub decode_null { undef }
Subroutine decode_null: called CC=1 SCAR=0.0
Pod undocumented
85  
86 0 ▶sub skip_boolean { &decode_boolean }
Subroutine skip_boolean: not called CC=1 SCAR=6.9
Pod undocumented
87 0 ▶sub decode_boolean {
Subroutine decode_boolean: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $class = shift;
89 0 my $reader = pop;
90 0 $reader->read(my $bool, 1);
91 0 return unpack 'C', $bool;
92 }
93  
94 0 ▶sub skip_int { &decode_int }
Subroutine skip_int: not called CC=1 SCAR=6.9
Pod undocumented
95 2091 ▶sub decode_int {
Subroutine decode_int: called CC=1 SCAR=0.0
Pod undocumented
96 2091 my $class = shift;
97 2091 my $reader = pop;
98 2091 return zigzag(unsigned_varint($reader));
99 }
100  
101 0 ▶sub skip_long { &decode_long };
Subroutine skip_long: not called CC=1 SCAR=6.9
Pod undocumented
102 2082 ▶sub decode_long {
Subroutine decode_long: called CC=1 SCAR=0.0
Pod undocumented
103 2082 my $class = shift;
104 2082 return decode_int($class, @_);
105 }
106  
107 0 ▶sub skip_float { &decode_float }
Subroutine skip_float: not called CC=1 SCAR=6.9
Pod undocumented
108 0 ▶sub decode_float {
Subroutine decode_float: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $class = shift;
110 0 my $reader = pop;
111 0 $reader->read(my $buf, 4);
112 0 return unpack "f<", $buf;
113 }
114  
115 0 ▶sub skip_double { &decode_double }
Subroutine skip_double: not called CC=1 SCAR=6.9
Pod undocumented
116 0 ▶sub decode_double {
Subroutine decode_double: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my $class = shift;
118 0 my $reader = pop;
119 0 $reader->read(my $buf, 8);
120 0 return unpack "d<", $buf,
121 }
122  
123 2 ▶sub skip_bytes {
Subroutine skip_bytes: called CC=1 SCAR=0.0
Pod undocumented
124 2 my $class = shift;
125 2 my $reader = pop;
126 2 my $size = decode_long($class, undef, undef, $reader);
127 2 $reader->seek($size, 0);
128 2 return;
129 }
130  
131 1192 ▶sub decode_bytes {
Subroutine decode_bytes: called CC=1 SCAR=0.0
Pod undocumented
132 1192 my $class = shift;
133 1192 my $reader = pop;
134 1192 my $size = decode_long($class, undef, undef, $reader);
135 1192 $reader->read(my $buf, $size);
136 1192 return $buf;
137 }
138  
139 2 ▶sub skip_string { &skip_bytes }
Subroutine skip_string: called CC=1 SCAR=0.0
Pod undocumented
140 1174 ▶sub decode_string {
Subroutine decode_string: called CC=1 SCAR=0.0
Pod undocumented
141 1174 my $class = shift;
142 1174 my $reader = pop;
143 1174 my $bytes = decode_bytes($class, undef, undef, $reader);
144 1174 return Encode::decode_utf8($bytes);
145 }
146  
147 0 ▶sub skip_record {
Subroutine skip_record: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my $class = shift;
149 0 my ($schema, $reader) = @_;
150 0 for my $field (@{ $schema->fields }){
151 0 skip($class, $field->{type}, $reader);
152 }
153 }
154  
155 ## 1.3.2 A record is encoded by encoding the values of its fields in the order
156 ## that they are declared. In other words, a record is encoded as just the
157 ## concatenation of the encodings of its fields. Field values are encoded per
158 ## their schema.
159 10 ▶sub decode_record {
Subroutine decode_record: called CC=5 SCAR=16.1
Pod undocumented
160 10 my $class = shift;
161 10 my ($writer_schema, $reader_schema, $reader) = @_;
162 10 my $record;
163  
164 10 my %extra_fields = %{ $reader_schema->fields_as_hash };
165 10 for my $field (@{ $writer_schema->fields }) {
166 27 my $name = $field->{name};
167 27 my $w_field_schema = $field->{type};
168 27 my $r_field_schema = delete $extra_fields{$name};
169  
170 ## 1.3.2 if the writer's record contains a field with a name not
171 ## present in the reader's record, the writer's value for that field
172 ## is ignored.
173 27 ▶ if (! $r_field_schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r_field_schema) 2 25 27
174 2 $class->skip($w_field_schema, $reader);
175 2 next;
176 }
177 my $data = $class->decode(
178 writer_schema => $w_field_schema,
179 reader_schema => $r_field_schema->{type},
180 25 reader => $reader,
181 );
182 25 $record->{ $name } = $data;
183 }
184  
185 10 for my $name (keys %extra_fields) {
186 ## 1.3.2. if the reader's record schema has a field with no default
187 ## value, and writer's schema does not have a field with the same
188 ## name, an error is signalled.
189 2 ▶ unless (exists $extra_fields{$name}->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $extra_fields{$name}{"default"}) 1 1 2
190 1 throw Avro::Schema::Error::Mismatch(
191 "cannot resolve without default"
192 );
193 }
194 ## 1.3.2 ... else the default value is used
195 1 $record->{ $name } = $extra_fields{$name}->{default};
196 }
197 9 return $record;
198 }
199  
200 0 ▶sub skip_enum { &skip_int }
Subroutine skip_enum: not called CC=1 SCAR=6.9
Pod undocumented
201  
202 ## 1.3.2 An enum is encoded by a int, representing the zero-based position of
203 ## the symbol in the schema.
204 5 ▶sub decode_enum {
Subroutine decode_enum: called CC=2 SCAR=6.9
Pod undocumented
205 5 my $class = shift;
206 5 my ($writer_schema, $reader_schema, $reader) = @_;
207 5 my $index = decode_int($class, @_);
208  
209 5 my $w_data = $writer_schema->symbols->[$index];
210 ## 1.3.2 if the writer's symbol is not present in the reader's enum,
211 ## then an error is signalled.
212 5 ▶ throw Avro::Schema::Error::Mismatch("enum unknown")
Branch: 1 branch
BranchTrueFalseTotal
unless $reader_schema->is_data_valid($w_data) 2 3 5
213 unless $reader_schema->is_data_valid($w_data);
214 3 return $w_data;
215 }
216  
217 0 ▶sub skip_block {
Subroutine skip_block: not called CC=4 SCAR=30.0
Pod undocumented
218 0 my $class = shift;
219 0 my ($reader, $block_content) = @_;
220 0 my $block_count = decode_long($class, undef, undef, $reader);
221 0 ▶ while ($block_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count) 0 0 0
222 0 ▶ if ($block_count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count < 0) { } 0 0 0
223 0 $reader->seek($block_count, 0);
224 0 next;
225 }
226 else {
227 0 for (1..$block_count) {
228 0 $block_content->();
229 }
230 }
231 0 $block_count = decode_long($class, undef, undef, $reader);
232 }
233 }
234  
235 0 ▶sub skip_array {
Subroutine skip_array: not called CC=1 SCAR=6.9
Pod undocumented
236 0 my $class = shift;
237 0 my ($schema, $reader) = @_;
238 0 ▶ skip_block($reader, sub { $class->skip($schema->items, $reader) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 }
240  
241 ## 1.3.2 Arrays are encoded as a series of blocks. Each block consists of a
242 ## long count value, followed by that many array items. A block with count zero
243 ## indicates the end of the array. Each item is encoded per the array's item
244 ## schema.
245 ## If a block's count is negative, its absolute value is used, and the count is
246 ## followed immediately by a long block size
247 318 ▶sub decode_array {
Subroutine decode_array: called CC=4 SCAR=14.0
Pod undocumented
248 318 my $class = shift;
249 318 my ($writer_schema, $reader_schema, $reader) = @_;
250 318 my $block_count = decode_long($class, @_);
251 318 my @array;
252 318 my $writer_items = $writer_schema->items;
253 318 my $reader_items = $reader_schema->items;
254 318 ▶ while ($block_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count) 318 318 636
255 318 my $block_size;
256 318 ▶ if ($block_count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count < 0) 0 318 318
257 0 $block_count = -$block_count;
258 0 $block_size = decode_long($class, @_);
259 ## XXX we can skip with $reader_schema?
260 }
261 318 for (1..$block_count) {
262 848 push @array, $class->decode(
263 writer_schema => $writer_items,
264 reader_schema => $reader_items,
265 reader => $reader,
266 );
267 }
268 318 $block_count = decode_long($class, @_);
269 }
270 318 return \@array;
271 }
272  
273 0 ▶sub skip_map {
Subroutine skip_map: not called CC=1 SCAR=6.9
Pod undocumented
274 0 my $class = shift;
275 0 my ($schema, $reader) = @_;
276 0 ▶ skip_block($reader, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 skip_string($class, $reader);
278 0 $class->skip($schema->values, $reader);
279 0 });
280 }
281  
282 ## 1.3.2 Maps are encoded as a series of blocks. Each block consists of a long
283 ## count value, followed by that many key/value pairs. A block with count zero
284 ## indicates the end of the map. Each item is encoded per the map's value
285 ## schema.
286 ##
287 ## If a block's count is negative, its absolute value is used, and the count is
288 ## followed immediately by a long block size indicating the number of bytes in
289 ## the block. This block size permits fast skipping through data, e.g., when
290 ## projecting a record to a subset of its fields.
291 113 ▶sub decode_map {
Subroutine decode_map: called CC=6 SCAR=18.9
Pod undocumented
292 113 my $class = shift;
293 113 my ($writer_schema, $reader_schema, $reader) = @_;
294 113 my %hash;
295  
296 113 my $block_count = decode_long($class, @_);
297 113 my $writer_values = $writer_schema->values;
298 113 my $reader_values = $reader_schema->values;
299 113 ▶ while ($block_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count) 113 113 226
300 113 my $block_size;
301 113 ▶ if ($block_count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count < 0) 0 113 113
302 0 $block_count = -$block_count;
303 0 $block_size = decode_long($class, @_);
304 ## XXX we can skip with $reader_schema?
305 }
306 113 for (1..$block_count) {
307 334 my $key = decode_string($class, @_);
308 334 ▶ unless (defined $key && length $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 0 334 334
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
00334
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
309 0 throw Avro::Schema::Error::Parse("key of map is invalid");
310 }
311 334 $hash{$key} = $class->decode(
312 writer_schema => $writer_values,
313 reader_schema => $reader_values,
314 reader => $reader,
315 );
316 }
317 113 $block_count = decode_long($class, @_);
318 }
319 113 return \%hash;
320 }
321  
322 0 ▶sub skip_union {
Subroutine skip_union: not called CC=2 SCAR=17.9
Pod undocumented
323 0 my $class = shift;
324 0 my ($schema, $reader) = @_;
325 0 my $idx = decode_long($class, undef, undef, $reader);
326 0 ▶ my $union_schema = $schema->schemas->[$idx]
Branch: 1 branch
BranchTrueFalseTotal
my $union_schema = $schema->schemas->[$idx] or "Avro::Schema::Error::Parse"->throw("union union member") 0 0 0
327 or throw Avro::Schema::Error::Parse("union union member");
328 0 $class->skip($union_schema, $reader);
329 }
330  
331 ## 1.3.2 A union is encoded by first writing an int value indicating the
332 ## zero-based position within the union of the schema of its value. The value
333 ## is then encoded per the indicated schema within the union.
334 3 ▶sub decode_union {
Subroutine decode_union: called CC=1 SCAR=0.0
Pod undocumented
335 3 my $class = shift;
336 3 my ($writer_schema, $reader_schema, $reader) = @_;
337 3 my $idx = decode_long($class, @_);
338 3 my $union_schema = $writer_schema->schemas->[$idx];
339 ## XXX TODO: schema resolution
340 # The first schema in the reader's union that matches the selected writer's
341 # union schema is recursively resolved against it. if none match, an error
342 # is signalled.
343 3 return $class->decode(
344 reader_schema => $union_schema,
345 writer_schema => $union_schema,
346 reader => $reader,
347 );
348 }
349  
350 0 ▶sub skip_fixed {
Subroutine skip_fixed: not called CC=1 SCAR=6.9
Pod undocumented
351 0 my $class = shift;
352 0 my ($schema, $reader) = @_;
353 0 $reader->seek($schema->size, 0);
354 }
355  
356 ## 1.3.2 Fixed instances are encoded using the number of bytes declared in the
357 ## schema.
358 14 ▶sub decode_fixed {
Subroutine decode_fixed: called CC=1 SCAR=0.0
Pod undocumented
359 14 my $class = shift;
360 14 my ($writer_schema, $reader_schema, $reader) = @_;
361 14 $reader->read(my $buf, $writer_schema->size);
362 14 return $buf;
363 }
364  
365 2091 ▶sub zigzag {
Subroutine zigzag: called CC=2 SCAR=7.6
Pod undocumented
366 2091 my $int = shift;
367 2091 ▶ if (1 & $int) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 & $int) { } 0 2091 2091
368 ## odd values are encoded negative ints
369 0 return -( 1 + ($int >> 1) );
370 }
371 ## even values are positive natural left shifted one bit
372 else {
373 2091 return $int >> 1;
374 }
375 }
376  
377 2091 ▶sub unsigned_varint {
Subroutine unsigned_varint: called CC=2 SCAR=6.9
Pod undocumented
378 2091 my $reader = shift;
379 2091 my $int = 0;
380 2091 my $more;
381 2091 my $shift = 0;
382 2091 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $more or do { do { $reader->read(my $buf, 1); my $byte = ord $buf; my $value = $byte & 127; $int |= $value << $shift; $shift += 7; $more = $byte & 128 } } 2091 3 2094
383 2094 $reader->read(my $buf, 1);
384 2094 my $byte = ord $buf;
385 2094 my $value = $byte & 0x7F;
386 2094 $int |= $value << $shift;
387 2094 $shift += 7;
388 2094 $more = $byte & 0x80;
389 } until (! $more);
390 2091 return $int;
391 }
392  
393 2 1;
« Avro/DataFileReader.pm ↑ Summary Avro/DataFileWriter.pm »