-
Notifications
You must be signed in to change notification settings - Fork 64
/
primitive.go
419 lines (322 loc) · 7.54 KB
/
primitive.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package zed
import (
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"math/bits"
"net/netip"
"github.com/brimdata/zed/pkg/nano"
"github.com/brimdata/zed/zcode"
"github.com/x448/float16"
)
type TypeOfBool struct{}
func AppendBool(zb zcode.Bytes, b bool) zcode.Bytes {
if b {
return append(zb, 1)
}
return append(zb, 0)
}
func EncodeBool(b bool) zcode.Bytes {
return AppendBool(nil, b)
}
func DecodeBool(zv zcode.Bytes) bool {
return zv != nil && zv[0] != 0
}
func (t *TypeOfBool) ID() int {
return IDBool
}
func (t *TypeOfBool) Kind() Kind {
return PrimitiveKind
}
type TypeOfBytes struct{}
func EncodeBytes(b []byte) zcode.Bytes {
return zcode.Bytes(b)
}
func DecodeBytes(zv zcode.Bytes) []byte {
return []byte(zv)
}
func (t *TypeOfBytes) ID() int {
return IDBytes
}
func (t *TypeOfBytes) Kind() Kind {
return PrimitiveKind
}
func (t *TypeOfBytes) Format(zv zcode.Bytes) string {
return "0x" + hex.EncodeToString(zv)
}
type TypeOfDuration struct{}
func EncodeDuration(d nano.Duration) zcode.Bytes {
return EncodeInt(int64(d))
}
func AppendDuration(bytes zcode.Bytes, d nano.Duration) zcode.Bytes {
return AppendInt(bytes, int64(d))
}
func DecodeDuration(zv zcode.Bytes) nano.Duration {
return nano.Duration(DecodeInt(zv))
}
func (t *TypeOfDuration) ID() int {
return IDDuration
}
func (t *TypeOfDuration) Kind() Kind {
return PrimitiveKind
}
func DecodeFloat(zb zcode.Bytes) float64 {
if zb == nil {
return 0
}
switch len(zb) {
case 2:
bits := binary.LittleEndian.Uint16(zb)
return float64(float16.Frombits(bits).Float32())
case 4:
bits := binary.LittleEndian.Uint32(zb)
return float64(math.Float32frombits(bits))
case 8:
bits := binary.LittleEndian.Uint64(zb)
return math.Float64frombits(bits)
}
panic("float encoding is neither 4 nor 8 bytes")
}
type TypeOfFloat16 struct{}
func AppendFloat16(zb zcode.Bytes, f float32) zcode.Bytes {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, float16.Fromfloat32(f).Bits())
return append(zb, buf...)
}
func EncodeFloat16(d float32) zcode.Bytes {
var b [2]byte
return AppendFloat16(b[:0], d)
}
func DecodeFloat16(zb zcode.Bytes) float32 {
if zb == nil {
return 0
}
return float16.Frombits(binary.LittleEndian.Uint16(zb)).Float32()
}
func (t *TypeOfFloat16) ID() int {
return IDFloat16
}
func (t *TypeOfFloat16) Kind() Kind {
return PrimitiveKind
}
type TypeOfFloat32 struct{}
func AppendFloat32(zb zcode.Bytes, f float32) zcode.Bytes {
return binary.LittleEndian.AppendUint32(zb, math.Float32bits(f))
}
func EncodeFloat32(d float32) zcode.Bytes {
var b [4]byte
return AppendFloat32(b[:0], d)
}
func DecodeFloat32(zb zcode.Bytes) float32 {
if zb == nil {
return 0
}
return math.Float32frombits(binary.LittleEndian.Uint32(zb))
}
func (t *TypeOfFloat32) ID() int {
return IDFloat32
}
func (t *TypeOfFloat32) Kind() Kind {
return PrimitiveKind
}
type TypeOfFloat64 struct{}
func AppendFloat64(zb zcode.Bytes, d float64) zcode.Bytes {
return binary.LittleEndian.AppendUint64(zb, math.Float64bits(d))
}
func EncodeFloat64(d float64) zcode.Bytes {
var b [8]byte
return AppendFloat64(b[:0], d)
}
func DecodeFloat64(zv zcode.Bytes) float64 {
if zv == nil {
return 0
}
return math.Float64frombits(binary.LittleEndian.Uint64(zv))
}
func (t *TypeOfFloat64) ID() int {
return IDFloat64
}
func (t *TypeOfFloat64) Kind() Kind {
return PrimitiveKind
}
func EncodeInt(i int64) zcode.Bytes {
var b [8]byte
n := zcode.EncodeCountedVarint(b[:], i)
return b[:n]
}
func AppendInt(bytes zcode.Bytes, i int64) zcode.Bytes {
return zcode.AppendCountedVarint(bytes, i)
}
func EncodeUint(i uint64) zcode.Bytes {
var b [8]byte
n := zcode.EncodeCountedUvarint(b[:], i)
return b[:n]
}
func AppendUint(bytes zcode.Bytes, i uint64) zcode.Bytes {
return zcode.AppendCountedUvarint(bytes, i)
}
func DecodeInt(zv zcode.Bytes) int64 {
return zcode.DecodeCountedVarint(zv)
}
func DecodeUint(zv zcode.Bytes) uint64 {
return zcode.DecodeCountedUvarint(zv)
}
type TypeOfInt8 struct{}
func (t *TypeOfInt8) ID() int {
return IDInt8
}
func (t *TypeOfInt8) Kind() Kind {
return PrimitiveKind
}
type TypeOfUint8 struct{}
func (t *TypeOfUint8) ID() int {
return IDUint8
}
func (t *TypeOfUint8) Kind() Kind {
return PrimitiveKind
}
type TypeOfInt16 struct{}
func (t *TypeOfInt16) ID() int {
return IDInt16
}
func (t *TypeOfInt16) Kind() Kind {
return PrimitiveKind
}
type TypeOfUint16 struct{}
func (t *TypeOfUint16) ID() int {
return IDUint16
}
func (t *TypeOfUint16) Kind() Kind {
return PrimitiveKind
}
type TypeOfInt32 struct{}
func (t *TypeOfInt32) ID() int {
return IDInt32
}
func (t *TypeOfInt32) Kind() Kind {
return PrimitiveKind
}
type TypeOfUint32 struct{}
func (t *TypeOfUint32) ID() int {
return IDUint32
}
func (t *TypeOfUint32) Kind() Kind {
return PrimitiveKind
}
type TypeOfInt64 struct{}
func (t *TypeOfInt64) ID() int {
return IDInt64
}
func (t *TypeOfInt64) Kind() Kind {
return PrimitiveKind
}
type TypeOfUint64 struct{}
func (t *TypeOfUint64) ID() int {
return IDUint64
}
func (t *TypeOfUint64) Kind() Kind {
return PrimitiveKind
}
type TypeOfIP struct{}
func AppendIP(zb zcode.Bytes, a netip.Addr) zcode.Bytes {
return append(zb, a.AsSlice()...)
}
func EncodeIP(a netip.Addr) zcode.Bytes {
return AppendIP(nil, a)
}
func DecodeIP(zv zcode.Bytes) netip.Addr {
var a netip.Addr
if err := a.UnmarshalBinary(zv); err != nil {
panic(fmt.Errorf("failure trying to decode IP address: %w", err))
}
return a
}
func (t *TypeOfIP) ID() int {
return IDIP
}
func (t *TypeOfIP) Kind() Kind {
return PrimitiveKind
}
type TypeOfNet struct{}
var ones = [16]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
func AppendNet(zb zcode.Bytes, p netip.Prefix) zcode.Bytes {
// Mask for canonical form.
p = p.Masked()
zb = append(zb, p.Addr().AsSlice()...)
length := p.Addr().BitLen() / 8
onesAddr, ok := netip.AddrFromSlice(ones[:length])
if !ok {
panic(fmt.Sprintf("bad slice length %d for %s", length, p))
}
mask := netip.PrefixFrom(onesAddr, p.Bits()).Masked()
return append(zb, mask.Addr().AsSlice()...)
}
func EncodeNet(p netip.Prefix) zcode.Bytes {
return AppendNet(nil, p)
}
func DecodeNet(zv zcode.Bytes) netip.Prefix {
if zv == nil {
return netip.Prefix{}
}
a, ok := netip.AddrFromSlice(zv[:len(zv)/2])
if !ok {
panic("failure trying to decode IP subnet that is not 8 or 32 bytes long")
}
return netip.PrefixFrom(a, LeadingOnes(zv[len(zv)/2:]))
}
// LeadingOnes returns the number of leading one bits in b.
func LeadingOnes(b []byte) int {
var n int
for ; len(b) > 0; b = b[1:] {
n += bits.LeadingZeros8(b[0] ^ 0xff)
if b[0] != 0xff {
break
}
}
return n
}
func (t *TypeOfNet) ID() int {
return IDNet
}
func (t *TypeOfNet) Kind() Kind {
return PrimitiveKind
}
type TypeOfNull struct{}
func (t *TypeOfNull) ID() int {
return IDNull
}
func (t *TypeOfNull) Kind() Kind {
return PrimitiveKind
}
type TypeOfString struct{}
func EncodeString(s string) zcode.Bytes {
return zcode.Bytes(s)
}
func DecodeString(zv zcode.Bytes) string {
return string(zv)
}
func (t *TypeOfString) ID() int {
return IDString
}
func (t *TypeOfString) Kind() Kind {
return PrimitiveKind
}
type TypeOfTime struct{}
func EncodeTime(t nano.Ts) zcode.Bytes {
var b [8]byte
n := zcode.EncodeCountedVarint(b[:], int64(t))
return b[:n]
}
func AppendTime(bytes zcode.Bytes, t nano.Ts) zcode.Bytes {
return AppendInt(bytes, int64(t))
}
func DecodeTime(zv zcode.Bytes) nano.Ts {
return nano.Ts(zcode.DecodeCountedVarint(zv))
}
func (t *TypeOfTime) ID() int {
return IDTime
}
func (t *TypeOfTime) Kind() Kind {
return PrimitiveKind
}