forked from alecthomas/go_serialization_benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structdef_avro.go
191 lines (171 loc) · 4.3 KB
/
structdef_avro.go
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
package goserbench
import (
"bytes"
"fmt"
"time"
goavro2 "github.com/linkedin/goavro"
goavro "gopkg.in/linkedin/goavro.v1"
)
type AvroA struct {
NeedGeneratingCodeFalse
record *goavro.Record
codec goavro.Codec
}
type Avro2Txt struct {
NeedGeneratingCodeFalse
codec *goavro2.Codec
}
type Avro2Bin struct {
NeedGeneratingCodeFalse
codec *goavro2.Codec
}
var (
avroSchemaJSON = `
{
"type": "record",
"name": "AvroA",
"doc:": "Schema for encoding/decoding sample message",
"namespace": "com.example",
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "birthday",
"type": "long"
},
{
"name": "phone",
"type": "string"
},
{
"name": "siblings",
"type": "int"
},
{
"name": "spouse",
"type": "boolean"
},
{
"name": "money",
"type": "double"
}
]
}
`
)
func NewAvroA() Serializer {
rec, err := goavro.NewRecord(goavro.RecordSchema(avroSchemaJSON))
if err != nil {
fmt.Printf("Avro record initialization error: %v\n", err)
return nil
}
codec, err := goavro.NewCodec(avroSchemaJSON)
if err != nil {
fmt.Printf("Avro codec initialization error: %v\n", err)
return nil
}
return &AvroA{record: rec, codec: codec}
}
func (a *AvroA) Marshal(o interface{}) ([]byte, error) {
object := o.(*A)
a.record.Set("name", object.Name)
a.record.Set("birthday", int64(object.BirthDay.UnixNano()))
a.record.Set("phone", object.Phone)
a.record.Set("siblings", int32(object.Siblings))
a.record.Set("spouse", object.Spouse)
a.record.Set("money", object.Money)
b := new(bytes.Buffer)
if err := a.codec.Encode(b, a.record); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (a *AvroA) Unmarshal(d []byte, o interface{}) error {
object := o.(*A)
b := bytes.NewBuffer(d)
i, err := a.codec.Decode(b)
if err != nil {
fmt.Printf("Avro decoding error: %v\n", err)
return err
}
rec := i.(*goavro.Record)
temp, _ := rec.Get("name")
object.Name = temp.(string)
temp, _ = rec.Get("birthday")
object.BirthDay = time.Unix(0, temp.(int64))
temp, _ = rec.Get("phone")
object.Phone = temp.(string)
temp, _ = rec.Get("siblings")
object.Siblings = int(temp.(int32))
temp, _ = rec.Get("spouse")
object.Spouse = temp.(bool)
temp, _ = rec.Get("money")
object.Money = temp.(float64)
return nil
}
func (a *AvroA) String() string {
return "GoAvro"
}
func avroMarshal(o interface{}, marshalFunc func([]byte, interface{}) ([]byte, error)) ([]byte, error) {
object := o.(*A)
return marshalFunc(nil, map[string]interface{}{
"name": object.Name,
"birthday": int64(object.BirthDay.UnixNano()),
"phone": object.Phone,
"siblings": int32(object.Siblings),
"spouse": object.Spouse,
"money": object.Money,
})
}
func avroUnmarshal(d []byte, o interface{}, unmarshalFunc func([]byte) (interface{}, []byte, error)) error {
object := o.(*A)
r, _, err := unmarshalFunc(d)
if err != nil {
fmt.Printf("Avro2 decoding error: %v\n", err)
return err
}
m := r.(map[string]interface{})
object.Name = m["name"].(string)
object.BirthDay = time.Unix(0, m["birthday"].(int64))
object.Phone = m["phone"].(string)
object.Siblings = int(m["siblings"].(int32))
object.Spouse = m["spouse"].(bool)
object.Money = m["money"].(float64)
return nil
}
func NewAvro2Txt() Serializer {
codec, err := goavro2.NewCodec(avroSchemaJSON)
if err != nil {
fmt.Printf("Avro2Txt codec initialization error: %v\n", err)
return nil
}
return &Avro2Txt{codec: codec}
}
func (a *Avro2Txt) Marshal(o interface{}) ([]byte, error) {
return avroMarshal(o, a.codec.TextualFromNative)
}
func (a *Avro2Txt) Unmarshal(d []byte, o interface{}) error {
return avroUnmarshal(d, o, a.codec.NativeFromTextual)
}
func (a *Avro2Txt) String() string {
return "GoAvro2Text"
}
func NewAvro2Bin() Serializer {
codec, err := goavro2.NewCodec(avroSchemaJSON)
if err != nil {
fmt.Printf("Avro2Bin codec initialization error: %v\n", err)
return nil
}
return &Avro2Bin{codec: codec}
}
func (a *Avro2Bin) Marshal(o interface{}) ([]byte, error) {
return avroMarshal(o, a.codec.BinaryFromNative)
}
func (a *Avro2Bin) Unmarshal(d []byte, o interface{}) error {
return avroUnmarshal(d, o, a.codec.NativeFromBinary)
}
func (a *Avro2Bin) String() string {
return "GoAvro2Binary"
}