/
BinaryEncoder.pm
267 lines (226 loc) · 6.86 KB
/
BinaryEncoder.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package Avro::BinaryEncoder;
use strict;
use warnings;
use Config;
use Encode();
use Error::Simple;
our $complement = ~0x7F;
unless ($Config{use64bitint}) {
require Math::BigInt;
$complement = Math::BigInt->new("0b" . ("1" x 57) . ("0" x 7));
}
=head2 encode(%param)
Encodes the given C<data> according to the given C<schema>, and pass it
to the C<emit_cb>
Params are:
=over 4
=item * data
The data to encode (can be any perl data structure, but it should match
schema)
=item * schema
The schema to use to encode C<data>
=item * emit_cb($byte_ref)
The callback that will be invoked with the a reference to the encoded data
in parameters.
=back
=cut
sub encode {
my $class = shift;
my %param = @_;
my ($schema, $data, $cb) = @param{qw/schema data emit_cb/};
## a schema can also be just a string
my $type = ref $schema ? $schema->type : $schema;
## might want to profile and optimize this
my $meth = "encode_$type";
$class->$meth($schema, $data, $cb);
return;
}
sub encode_null {
$_[3]->(\'');
}
sub encode_boolean {
my $class = shift;
my ($schema, $data, $cb) = @_;
$cb->( $data ? \0x1 : \0x0 );
}
sub encode_int {
my $class = shift;
my ($schema, $data, $cb) = @_;
my @count = unpack "W*", $data;
if (scalar @count > 4) {
throw Avro::BinaryEncoder::Error("int should be 32bits");
}
my $enc = unsigned_varint(zigzag($data));
$cb->(\$enc);
}
sub encode_long {
my $class = shift;
my ($schema, $data, $cb) = @_;
my @count = unpack "W*", $data;
if (scalar @count > 8) {
throw Avro::BinaryEncoder::Error("int should be 64bits");
}
my $enc = unsigned_varint(zigzag($data));
$cb->(\$enc);
}
sub encode_float {
my $class = shift;
my ($schema, $data, $cb) = @_;
my $enc = pack "f<", $data;
$cb->(\$enc);
}
sub encode_double {
my $class = shift;
my ($schema, $data, $cb) = @_;
my $enc = pack "d<", $data;
$cb->(\$enc);
}
sub encode_bytes {
my $class = shift;
my ($schema, $data, $cb) = @_;
encode_long($class, undef, bytes::length($data), $cb);
$cb->(\$data);
}
sub encode_string {
my $class = shift;
my ($schema, $data, $cb) = @_;
my $bytes = Encode::encode_utf8($data);
encode_long($class, undef, bytes::length($bytes), $cb);
$cb->(\$bytes);
}
## 1.3.2 A record is encoded by encoding the values of its fields in the order
## that they are declared. In other words, a record is encoded as just the
## concatenation of the encodings of its fields. Field values are encoded per
## their schema.
sub encode_record {
my $class = shift;
my ($schema, $data, $cb) = @_;
for my $field (@{ $schema->fields }) {
$class->encode(
schema => $field->{type},
data => $data->{ $field->{name} },
emit_cb => $cb,
);
}
}
## 1.3.2 An enum is encoded by a int, representing the zero-based position of
## the symbol in the schema.
sub encode_enum {
my $class = shift;
my ($schema, $data, $cb) = @_;
my $symbols = $schema->symbols_as_hash;
my $pos = $symbols->{ $data };
throw Avro::BinaryEncoder::Error("Cannot find enum $data")
unless defined $pos;
$class->encode_int(undef, $pos, $cb);
}
## 1.3.2 Arrays are encoded as a series of blocks. Each block consists of a
## long count value, followed by that many array items. A block with count zero
## indicates the end of the array. Each item is encoded per the array's item
## schema.
## If a block's count is negative, its absolute value is used, and the count is
## followed immediately by a long block size
## maybe here it would be worth configuring what a typical block size should be
sub encode_array {
my $class = shift;
my ($schema, $data, $cb) = @_;
## FIXME: multiple blocks
if (@$data) {
$class->encode_long(undef, scalar @$data, $cb);
for (@$data) {
$class->encode(
schema => $schema->items,
data => $_,
emit_cb => $cb,
);
}
}
## end of the only block
$class->encode_long(undef, 0, $cb);
}
## 1.3.2 Maps are encoded as a series of blocks. Each block consists of a long
## count value, followed by that many key/value pairs. A block with count zero
## indicates the end of the map. Each item is encoded per the map's value
## schema.
##
## (TODO)
## If a block's count is negative, its absolute value is used, and the count is
## followed immediately by a long block size indicating the number of bytes in
## the block. This block size permits fast skipping through data, e.g., when
## projecting a record to a subset of its fields.
sub encode_map {
my $class = shift;
my ($schema, $data, $cb) = @_;
my @keys = keys %$data;
if (@keys) {
$class->encode_long(undef, scalar @keys, $cb);
for (@keys) {
## the key
$class->encode_string(undef, $_, $cb);
## the value
$class->encode(
schema => $schema->values->{$_},
data => $data->{$_},
emit_cb => $cb,
);
}
}
## end of the only block
$class->encode_long(undef, 0, $cb);
}
## 1.3.2 A union is encoded by first writing a long value indicating the
## zero-based position within the union of the schema of its value. The value
## is then encoded per the indicated schema within the union.
sub encode_union {
my $class = shift;
my ($schema, $data, $cb) = @_;
my $idx = 0;
my $elected_schema;
for my $inner_schema (@{$schema->schemas}) {
if ($inner_schema->is_data_valid($data)) {
$elected_schema = $inner_schema;
last;
}
$idx++;
}
unless ($elected_schema) {
throw Avro::BinaryEncoder::Error("union cannot validate the data");
}
$class->encode_long(undef, $idx, $cb);
$class->encode(
schema => $elected_schema,
data => $data,
emit_cb => $cb,
);
}
## 1.3.2 Fixed instances are encoded using the number of bytes declared in the
## schema.
sub encode_fixed {
my $class = shift;
my ($schema, $data, $cb) = @_;
if (bytes::length $data != $schema->size) {
my $s1 = bytes::length $data;
my $s2 = $schema->size;
throw Avro::BinaryEncoder::Error("Fixed size doesn't match $s1!=$s2");
}
$class->encode_bytes(undef, $data, $cb);
}
sub zigzag {
use warnings FATAL => 'numeric';
if ( $_[0] >= 0 ) {
return $_[0] << 1;
}
return (($_[0] << 1) ^ -1) | 0x1;
}
sub unsigned_varint {
my @bytes;
while ($_[0] & $complement ) { # mask with continuation bit
push @bytes, ($_[0] & 0x7F) | 0x80; # out and set continuation bit
$_[0] >>= 7; # next please
}
push @bytes, $_[0]; # last byte
return pack "W*", @bytes; ## TODO C
}
package Avro::BinaryEncoder::Error;
use parent 'Error::Simple';
1;