| 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::BinaryEncoder; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Encode(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use Error::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use Regexp::Common qw(number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use JSON::PP; # For is_bool | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | our $VERSION = '1.12.2'; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # Private function deleted below, should be a lexical sub | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | sub _bigint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bigint: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | require Math::BigInt; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | my $val = Math::BigInt->new(shift); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | $Config{use64bitint} | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | ? 0 + $val->bstr() # numify() loses precision | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | : $val; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Avro type limits | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # Private constants deleted below | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use constant INT_MAX => 0x7FFF_FFFF; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use constant INT_MIN => -0x8000_0000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use constant LONG_MAX => _bigint('0x7FFF_FFFF_FFFF_FFFF'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | use constant LONG_MIN => _bigint('-0x8000_0000_0000_0000'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head2 encode(%param) | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | Encodes the given C<data> according to the given C<schema>, and pass it | ||||||||||||||||||||||||||||||||||||||||||
| 50 | to the C<emit_cb> | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | Params are: | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =item * data | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | The data to encode (can be any perl data structure, but it should match | ||||||||||||||||||||||||||||||||||||||||||
| 59 | schema) | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =item * schema | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | The schema to use to encode C<data> | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =item * emit_cb($byte_ref) | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | The callback that will be invoked with the a reference to the encoded data | ||||||||||||||||||||||||||||||||||||||||||
| 68 | in parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1326 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1326 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 1326 | my %param = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 1326 | my ($schema, $data, $cb) = @param{qw/schema data emit_cb/}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | ## a schema can also be just a string | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 1326 | ▶ | my $type = ref $schema ? $schema->type : $schema; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | ## might want to profile and optimize this | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 1326 | my $meth = "encode_$type"; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 1326 | $class->$meth($schema, $data, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 1326 | return; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 5 | ▶ | sub encode_null { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_null: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 5 | $_[3]->(\''); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 49 | ▶ | sub encode_boolean { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_boolean: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 49 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 49 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 49 | ▶ | throw Avro::BinaryEncoder::Error( "<UNDEF> is not a valid boolean value") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | unless defined $data; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 48 | ▶ | if ( my $type = ref $data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | throw Avro::BinaryEncoder::Error("cannot encode a '$type' reference as boolean") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | unless $type eq 'JSON::PP::Boolean'; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 43 | ▶ | throw Avro::BinaryEncoder::Error( "'$data' is not a valid boolean value") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$data eq "" or JSON::PP::is_bool($data) or $data =~ /^(?:true|t|false|f|yes|y|no|n|0|1)$/i
$data eq "" or JSON::PP::is_bool($data)
MC/DC100%3/3
$data eq "" or JSON::PP::is_bool($data) or $data =~ /^(?:true|t|false|f|yes|y|no|n|0|1)$/i
$data eq ""
JSON::PP::is_bool($data)
$data =~ /^(?:true|t|false|f|yes|y|no|n|0|1)$/i
Truth table100%4/4
A or B or C
A= $data eq ""B= JSON::PP::is_bool($data)C= $data =~ /^(?:true|t|false|f|yes|y|no|n|0|1)$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | unless $data eq '' # For Perl versions without builtin::is_bool | ||||||||||||||||||||||||||||||||||||||||||
| 106 | || JSON::PP::is_bool($data) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | || $data =~ /^(?:true|t|false|f|yes|y|no|n|0|1)$/i; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # Some values might be false but evaluate to "truthy" for Perl, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # like the string 'false'. For these known exceptions, which | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # would otherwise be false, we read a false value. For everything | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # else, we rely on their value evaluated in a boolean context. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 44 | ▶ | my $true = $data =~ /^(?:no|n|false|f)$/i ? 0 : !!$data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 44 | ▶ | $cb->( $true ? \"\x1" : \"\x0" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 36 | ▶ | sub encode_int { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_int: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 36 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 36 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 36 | ▶ | if ($data !~ /^$RE{num}{int}$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | throw Avro::BinaryEncoder::Error("cannot convert '$data' to integer"); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 34 | ▶ | if ($data > INT_MAX || $data < INT_MIN) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data > 2147483647 or $data < '-2147483648'
MC/DC100%2/2
$data > 2147483647 or $data < '-2147483648'
$data > 2147483647
$data < '-2147483648'
Truth table100%3/3
A or B
A= $data > 2147483647B= $data < '-2147483648'
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | throw Avro::BinaryEncoder::Error("data ($data) out of range for Avro 'int'"); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 31 | my $enc = unsigned_varint(zigzag($data)); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 31 | $cb->(\$enc); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 2080 | ▶ | sub encode_long { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_long: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2080 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 2080 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 2080 | ▶ | if ($data !~ /^$RE{num}{int}$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | throw Avro::BinaryEncoder::Error("cannot convert '$data' to long integer"); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 2078 | ▶ | if ($data > LONG_MAX || $data < LONG_MIN) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data > '9223372036854775807' or $data < '-9223372036854775808'
MC/DC50%1/2
$data > '9223372036854775807' or $data < '-9223372036854775808'
$data > '9223372036854775807'
$data < '-9223372036854775808'
Truth table66%2/3
A or B
A= $data > '9223372036854775807'B= $data < '-9223372036854775808'
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | throw Avro::BinaryEncoder::Error("data ($data) out of range for Avro 'long'"); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 2077 | my $enc = unsigned_varint(zigzag($data)); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 2077 | $cb->(\$enc); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub encode_float { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_float: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $enc = pack "f<", $data; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $cb->(\$enc); | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub encode_double { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_double: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $enc = pack "d<", $data; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $cb->(\$enc); | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | ▶ | sub encode_bytes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_bytes: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 14 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 14 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | ▶ | throw Avro::BinaryEncoder::Error("Invalid data given for 'bytes': Contains values >255") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | unless utf8::downgrade($data, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | encode_long($class, undef, length($data), $cb); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 14 | $cb->(\$data); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 1170 | ▶ | sub encode_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1170 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 1170 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 1170 | my $bytes = Encode::encode_utf8($data); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 1170 | encode_long($class, undef, length($bytes), $cb); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 1170 | $cb->(\$bytes); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | ## 1.3.2 A record is encoded by encoding the values of its fields in the order | ||||||||||||||||||||||||||||||||||||||||||
| 178 | ## that they are declared. In other words, a record is encoded as just the | ||||||||||||||||||||||||||||||||||||||||||
| 179 | ## concatenation of the encodings of its fields. Field values are encoded per | ||||||||||||||||||||||||||||||||||||||||||
| 180 | ## their schema. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | ▶ | sub encode_record { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 8 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 8 | for my $field (@{ $schema->fields }) { | |||||||||||||||||||||||||||||||||||||||||
| 185 | $class->encode( | ||||||||||||||||||||||||||||||||||||||||||
| 186 | schema => $field->{type}, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | data => $data->{ $field->{name} }, | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 20 | emit_cb => $cb, | |||||||||||||||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | ## 1.3.2 An enum is encoded by a int, representing the zero-based position of | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ## the symbol in the schema. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | ▶ | sub encode_enum { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_enum: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 5 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 5 | my $symbols = $schema->symbols_as_hash; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 5 | my $pos = $symbols->{ $data }; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | ▶ | throw Avro::BinaryEncoder::Error("Cannot find enum $data") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | unless defined $pos; | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 5 | $class->encode_int(undef, $pos, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | ## 1.3.2 Arrays are encoded as a series of blocks. Each block consists of a | ||||||||||||||||||||||||||||||||||||||||||
| 206 | ## long count value, followed by that many array items. A block with count zero | ||||||||||||||||||||||||||||||||||||||||||
| 207 | ## indicates the end of the array. Each item is encoded per the array's item | ||||||||||||||||||||||||||||||||||||||||||
| 208 | ## schema. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | ## If a block's count is negative, its absolute value is used, and the count is | ||||||||||||||||||||||||||||||||||||||||||
| 210 | ## followed immediately by a long block size | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | ## maybe here it would be worth configuring what a typical block size should be | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 318 | ▶ | sub encode_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_array: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 318 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 318 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | ## FIXME: multiple blocks | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 318 | ▶ | if (@$data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 318 | $class->encode_long(undef, scalar @$data, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 318 | for (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 221 | 848 | $class->encode( | |||||||||||||||||||||||||||||||||||||||||
| 222 | schema => $schema->items, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | data => $_, | ||||||||||||||||||||||||||||||||||||||||||
| 224 | emit_cb => $cb, | ||||||||||||||||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | ## end of the only block | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 318 | $class->encode_long(undef, 0, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | ## 1.3.2 Maps are encoded as a series of blocks. Each block consists of a long | ||||||||||||||||||||||||||||||||||||||||||
| 234 | ## count value, followed by that many key/value pairs. A block with count zero | ||||||||||||||||||||||||||||||||||||||||||
| 235 | ## indicates the end of the map. Each item is encoded per the map's value | ||||||||||||||||||||||||||||||||||||||||||
| 236 | ## schema. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 238 | ## (TODO) | ||||||||||||||||||||||||||||||||||||||||||
| 239 | ## If a block's count is negative, its absolute value is used, and the count is | ||||||||||||||||||||||||||||||||||||||||||
| 240 | ## followed immediately by a long block size indicating the number of bytes in | ||||||||||||||||||||||||||||||||||||||||||
| 241 | ## the block. This block size permits fast skipping through data, e.g., when | ||||||||||||||||||||||||||||||||||||||||||
| 242 | ## projecting a record to a subset of its fields. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 111 | ▶ | sub encode_map { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_map: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 111 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 111 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 111 | my @keys = keys %$data; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 111 | ▶ | if (@keys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 111 | $class->encode_long(undef, scalar @keys, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 250 | 111 | for (@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 251 | ## the key | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 330 | $class->encode_string(undef, $_, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | ## the value | ||||||||||||||||||||||||||||||||||||||||||
| 255 | $class->encode( | ||||||||||||||||||||||||||||||||||||||||||
| 256 | schema => $schema->values, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 330 | data => $data->{$_}, | |||||||||||||||||||||||||||||||||||||||||
| 258 | emit_cb => $cb, | ||||||||||||||||||||||||||||||||||||||||||
| 259 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | ## end of the only block | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 111 | $class->encode_long(undef, 0, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | ## 1.3.2 A union is encoded by first writing an int value indicating the | ||||||||||||||||||||||||||||||||||||||||||
| 267 | ## zero-based position within the union of the schema of its value. The value | ||||||||||||||||||||||||||||||||||||||||||
| 268 | ## is then encoded per the indicated schema within the union. | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | sub encode_union { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_union: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 4 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 4 | my $idx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | my $elected_schema; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 4 | for my $inner_schema (@{$schema->schemas}) { | |||||||||||||||||||||||||||||||||||||||||
| 275 | 8 | ▶ | if ($inner_schema->is_data_valid($data)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 4 | $elected_schema = $inner_schema; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 4 | $idx++; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 4 | ▶ | unless ($elected_schema) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | throw Avro::BinaryEncoder::Error("union cannot validate the data"); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 4 | $class->encode_long(undef, $idx, $cb); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 4 | $class->encode( | |||||||||||||||||||||||||||||||||||||||||
| 286 | schema => $elected_schema, | ||||||||||||||||||||||||||||||||||||||||||
| 287 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 288 | emit_cb => $cb, | ||||||||||||||||||||||||||||||||||||||||||
| 289 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | ## 1.3.2 Fixed instances are encoded using the number of bytes declared in the | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ## schema. | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 10 | ▶ | sub encode_fixed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_fixed: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 10 | my ($schema, $data, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 10 | ▶ | throw Avro::BinaryEncoder::Error("Invalid data given for 'fixed': Contains values >255") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | unless utf8::downgrade($data, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 10 | my $length = length $data; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 10 | my $size = $schema->size; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 10 | ▶ | throw Avro::BinaryEncoder::Error("Fixed size doesn't match $length!=$size") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | unless $length == $size; | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 10 | $cb->(\$data); | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 2108 | ▶ | sub zigzag { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
zigzag: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | use warnings FATAL => 'numeric'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 2108 | ▶ | if ( $_[0] >= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 2094 | return $_[0] << 1; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 14 | return (($_[0] << 1) ^ -1) | 0x1; | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 2108 | ▶ | sub unsigned_varint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unsigned_varint: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 2108 | my @bytes; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 2108 | ▶ | while ($_[0] > 0x7F) { # while more than 7 bits to encode | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 65 | push @bytes, ($_[0] & 0x7F) | 0x80; # append continuation bit and 7 data bits | |||||||||||||||||||||||||||||||||||||||||
| 322 | 65 | $_[0] >>= 7; # get next 7 bits | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 2108 | push @bytes, $_[0]; # last byte | |||||||||||||||||||||||||||||||||||||||||
| 325 | 2108 | return pack "C*", @bytes; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | # Delete private symbols to avoid adding them to the API | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 3 | ▶ | delete $Avro::BinaryEncoder::{$_} for '_bigint', <{INT,LONG}_{MIN,MAX}>; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | package Avro::BinaryEncoder::Error; | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 3 | ▶ | use parent 'Error::Simple'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||