/
Encode.pm
187 lines (149 loc) · 5.47 KB
/
Encode.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
use v6;
use JSON::Tiny;
use Avro::Auxiliary;
use Avro::Schema;
use experimental :pack;
#use trace;
package Avro{
#======================================
# Exceptions
#======================================
class X::Avro::EncodeFail is Avro::AvroException {
has Avro::Schema $.schema;
has Mu $.data;
method message { "Failed to encode "~$!data~" as "~$!schema.type() }
}
#== Role ==============================
# * Encoder
#======================================
role Encoder {
method encode(Avro::Schema,Mu:D --> Blob) { * };
}
#== Class =============================
# * BinaryEncoder
#======================================
class BinaryEncoder does Encoder {
has Int $!blocksize;
# constructor
submethod BUILD( :$blocksize = 250 ) {
$!blocksize = $blocksize;
}
# int8 template "*" doesn't work as I expect it too
sub template(int $length) {
((1..$length).map:{ "C" }).join(" ");
}
# integers and longs are encode as variable sized zigzag numbers
sub encode_long(int $l){
my @var_int = to_varint(to_zigzag($l));
pack(template(@var_int.elems()),@var_int);
}
multi submethod encode_schema(Avro::Record $schema, Associative:D $hash) {
my BlobStream $stream = BlobStream.new();
for $schema.fields.list -> $field {
my $data = $hash{$field.name};
$stream.append(self.encode_schema($field.type,$data));
}
$stream.blob
}
# encode an array in blocks of max size $!blocksize
multi submethod encode_schema(Avro::Array $schema, Positional:D $arr) {
my BlobStream $stream = BlobStream.new();
my @copy = $arr.clone();
my Int $iterations = ($arr.elems() div $!blocksize);
my Int $leftover = $arr.elems() mod $!blocksize;
my @blocks = (1..$iterations).map:{ $!blocksize };
push(@blocks, $leftover) if ($leftover > 0);
for @blocks -> $size {
$stream.append(encode_long($size));
for 1..$size {
$stream.append(self.encode_schema($schema.items,@copy.shift));
}
}
$stream.append(encode_long(0));
$stream.blob
}
# todo set maps with negative counts ?
multi submethod encode_schema(Avro::Map $schema, Associative:D $hash) {
my BlobStream $stream = BlobStream.new();
my Avro::Schema $keyschema = Avro::String.new();
my @kv = $hash.kv;
my Int $iterations = ($hash.elems() div $!blocksize);
my Int $leftover = $hash.elems() mod $!blocksize;
my @blocks = (1..$iterations).map:{ $!blocksize };
push(@blocks, $leftover) if ($leftover > 0);
for @blocks -> $size {
$stream.append(encode_long($size));
for (1..$size) -> $i {
$stream.append(self.encode_schema($keyschema,@kv.shift));
$stream.append(self.encode_schema($schema.values,@kv.shift));
}
}
$stream.append(encode_long(0));
$stream.blob
}
multi submethod encode_schema(Avro::Enum $schema, Str:D $str) {
my Int $result = $schema.sym.first({ ($^a eq $str) }, :k);
if $result.defined {
return encode_long($result);
} else {
X::Avro::EncodeFail.new(:schema($schema),:data($str)).throw()
}
}
multi submethod encode_schema(Avro::Union $schema, Mu $data) {
my Avro::Schema $type = $schema.find_type($data);
my Int $index = $schema.types.first({ ($^a ~~ $type) }, :k);
my BlobStream $stream = BlobStream.new(:blob(encode_long($index)));
$stream.append(self.encode_schema($type,$data));
$stream.blob
}
multi submethod encode_schema(Avro::Fixed $schema, Str:D $str) {
X::Avro::EncodeFail.new(:schema($schema),:data($str)).throw()
unless $schema.size == $str.codes();
pack(template($schema.size),$str.ords())
}
multi submethod encode_schema(Avro::Null $schema, Any:U $any) {
# (pack("C",0)); --> misinterpretation
BlobStream.new().blob
}
multi submethod encode_schema(Avro::String $schema, Str:D $str) {
my Blob $encoding = $str.encode();
my BlobStream $stream = BlobStream.new(:blob(encode_long($encoding.elems())));
$stream.append($encoding);
$stream.blob
}
multi submethod encode_schema(Avro::Bytes $schema, Str:D $str) {
my BlobStream $stream = BlobStream.new(:blob(encode_long($str.codes())));
$stream.append( pack(template($str.codes()),$str.ords()) );
$stream.blob
}
multi submethod encode_schema(Avro::Boolean $schema, Bool:D $bool) {
my $buf = (pack("C",$bool));
return $buf;
}
multi submethod encode_schema(Avro::Integer $schema, Int:D $int) {
encode_long($int)
}
multi submethod encode_schema(Avro::Long $schema, Int:D $long) {
encode_long($long)
}
multi submethod encode_schema(Avro::Float $schema, Rat:D $float) {
my @arr = int_to_bytes(to_floatbits($float),4);
(pack(template(4),@arr))
}
multi submethod encode_schema(Avro::Double $schema, Rat:D $double) {
my @arr = int_to_bytes(to_doublebits($double),8);
(pack(template(8),@arr))
}
method encode(Avro::Schema $schema, Mu $data) {
try {
return self.encode_schema($schema,$data);
}
CATCH { default { say $_; X::Avro::EncodeFail.new(:schema($schema),:data($data)).throw() }}
};
};
#== Class =============================
# * JSONEncoder
#======================================
class JSONEncoder does Encoder {
};
}