-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
231 lines (194 loc) · 4.6 KB
/
encoder.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
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
package sbt
import (
"encoding/binary"
"math"
"time"
)
type RowSerializerBase struct {
buffer []byte
counter int
}
// newRowSerializerBase
func newRowSerializerBase(buffer []byte) RowSerializerBase {
return RowSerializerBase{buffer: buffer}
}
// Bytes returns the byte slice of the serializer.
func (s *RowSerializerBase) Bytes() []byte {
return s.buffer
}
// Reset resets the encoder.
func (s *RowSerializerBase) Reset(buffer []byte) {
s.buffer = buffer
s.counter = 0
}
// Encoder is passed to Row.Encode as the encoding context and helper.
type Encoder struct {
RowSerializerBase
}
// NewEncoder
func NewEncoder(buffer []byte) *Encoder {
return &Encoder{newRowSerializerBase(buffer)}
}
// EncodeTime
func (e *Encoder) EncodeTime(t time.Time) {
e.EncodeInt64(t.UnixNano())
}
// EncodeStringPadded
func (e *Encoder) EncodeStringPadded(s string, size int) {
copy(e.buffer[e.counter:], StringToBytePadded(s, size))
e.counter += size
}
// EncodeBytesPadded
func (e *Encoder) EncodeBytesPadded(b []byte, size int) {
copy(e.buffer[e.counter:], b)
e.counter += size
}
// EncodeUInt8
func (e *Encoder) EncodeUInt8(v uint8) {
e.buffer[e.counter] = v
e.counter++
}
// EncodeUInt16
func (e *Encoder) EncodeUInt16(v uint16) {
binary.LittleEndian.PutUint16(e.buffer[e.counter:], v)
e.counter += 2
}
// EncodeUInt32
func (e *Encoder) EncodeUInt32(v uint32) {
binary.LittleEndian.PutUint32(e.buffer[e.counter:], v)
e.counter += 4
}
// EncodeUInt64
func (e *Encoder) EncodeUInt64(v uint64) {
binary.LittleEndian.PutUint64(e.buffer[e.counter:], v)
e.counter += 8
}
// EncodeInt8
func (e *Encoder) EncodeInt8(v int8) {
e.buffer[e.counter] = byte(v)
e.counter++
}
// EncodeInt16
func (e *Encoder) EncodeInt16(v int16) {
binary.LittleEndian.PutUint16(e.buffer[e.counter:], uint16(v))
e.counter += 2
}
// EncodeInt32
func (e *Encoder) EncodeInt32(v int32) {
binary.LittleEndian.PutUint32(e.buffer[e.counter:], uint32(v))
e.counter += 4
}
// EncodeInt64
func (e *Encoder) EncodeInt64(v int64) {
binary.LittleEndian.PutUint64(e.buffer[e.counter:], uint64(v))
e.counter += 8
}
// EncodeFloat32
func (e *Encoder) EncodeFloat32(v float32) {
binary.LittleEndian.PutUint32(e.buffer[e.counter:], math.Float32bits(v))
e.counter += 4
}
// EncodeFloat64
func (e *Encoder) EncodeFloat64(v float64) {
binary.LittleEndian.PutUint64(e.buffer[e.counter:], math.Float64bits(v))
e.counter += 8
}
// EncodeBool
func (e *Encoder) EncodeBool(v bool) {
if v {
e.buffer[e.counter] = 1
} else {
e.buffer[e.counter] = 0
}
e.counter++
}
// Decoder is passed to Row.Decode as the encoding context and helper.
type Decoder struct {
RowSerializerBase
}
// NewDecoder
func NewDecoder(buffer []byte) *Decoder {
return &Decoder{newRowSerializerBase(buffer)}
}
// DecodeTime
func (d *Decoder) DecodeTime() time.Time {
return time.Unix(0, d.DecodeInt64())
}
// DecodeStringPadded
func (d *Decoder) DecodeStringPadded(size int) string {
s := ByteToStringPadded(d.buffer[d.counter : d.counter+size])
d.counter += size
return s
}
// DecodeBytes
func (d *Decoder) DecodeBytes(size int) []byte {
b := d.buffer[d.counter : d.counter+size]
d.counter += size
return b
}
// DecodeUInt8
func (d *Decoder) DecodeUInt8() uint8 {
v := d.buffer[d.counter]
d.counter++
return v
}
// DecodeUInt16
func (d *Decoder) DecodeUInt16() uint16 {
v := binary.LittleEndian.Uint16(d.buffer[d.counter:])
d.counter += 2
return v
}
// DecodeUInt32
func (d *Decoder) DecodeUInt32() uint32 {
v := binary.LittleEndian.Uint32(d.buffer[d.counter:])
d.counter += 4
return v
}
// DecodeUInt64
func (d *Decoder) DecodeUInt64() uint64 {
v := binary.LittleEndian.Uint64(d.buffer[d.counter:])
d.counter += 8
return v
}
// DecodeInt8
func (d *Decoder) DecodeInt8() int8 {
v := d.buffer[d.counter]
d.counter++
return int8(v)
}
// DecodeInt16
func (d *Decoder) DecodeInt16() int16 {
v := binary.LittleEndian.Uint16(d.buffer[d.counter:])
d.counter += 2
return int16(v)
}
// DecodeInt32
func (d *Decoder) DecodeInt32() int32 {
v := binary.LittleEndian.Uint32(d.buffer[d.counter:])
d.counter += 4
return int32(v)
}
// DecodeInt64
func (d *Decoder) DecodeInt64() int64 {
v := binary.LittleEndian.Uint64(d.buffer[d.counter:])
d.counter += 8
return int64(v)
}
// DecodeFloat32
func (d *Decoder) DecodeFloat32() float32 {
v := binary.LittleEndian.Uint32(d.buffer[d.counter:])
d.counter += 4
return math.Float32frombits(v)
}
// DecodeFloat64
func (d *Decoder) DecodeFloat64() float64 {
v := binary.LittleEndian.Uint64(d.buffer[d.counter:])
d.counter += 8
return math.Float64frombits(v)
}
// DecodeBool
func (d *Decoder) DecodeBool() bool {
v := d.buffer[d.counter]
d.counter++
return v != 0
}