This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
decode.go
426 lines (409 loc) · 10.7 KB
/
decode.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
420
421
422
423
424
425
426
package kdb
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io"
"reflect"
"time"
"unsafe"
"github.com/nu7hatch/gouuid"
"fmt"
)
var typeSize = map[int8]int{
1: 1, 4: 1, 10: 1,
2: 16,
5: 2,
6: 4, 8: 4, 13: 4, 14: 4, 17: 4, 18: 4, 19: 4,
7: 8, 9: 8, 12: 8, 15: 8, 16: 8}
var typeReflect = map[int8]reflect.Type{
1: reflect.TypeOf([]bool{}),
2: reflect.TypeOf([]uuid.UUID{}),
4: reflect.TypeOf([]byte{}),
5: reflect.TypeOf([]int16{}),
6: reflect.TypeOf([]int32{}),
7: reflect.TypeOf([]int64{}),
8: reflect.TypeOf([]float32{}),
9: reflect.TypeOf([]float64{}),
10: reflect.TypeOf([]byte{}),
12: reflect.TypeOf([]int64{}),
13: reflect.TypeOf([]Month{}),
14: reflect.TypeOf([]int32{}),
15: reflect.TypeOf([]float64{}),
16: reflect.TypeOf([]time.Duration{}),
17: reflect.TypeOf([]int32{}),
18: reflect.TypeOf([]int32{}),
19: reflect.TypeOf([]int32{})}
func makeArray(vectype int8, veclen int) interface{} {
switch vectype {
case 1:
return make([]bool, veclen)
case 4, 10:
return make([]byte, veclen)
case 2:
return make([]uuid.UUID, veclen)
case 5:
return make([]int16, veclen)
case 6, 14, 17, 18, 19:
return make([]int32, veclen)
case 13:
return make([]Month, veclen)
case 16:
return make([]time.Duration, veclen)
case 7, 12:
return make([]int64, veclen)
case 8:
return make([]float32, veclen)
case 9, 15:
return make([]float64, veclen)
case 11:
return make([]string, veclen)
}
return nil
}
func (h *ipcHeader) getByteOrder() binary.ByteOrder {
var order binary.ByteOrder
order = binary.LittleEndian
if h.ByteOrder == 0x00 {
order = binary.BigEndian
}
return order
}
func (h *ipcHeader) ok() bool {
return h.ByteOrder == 0x01 && h.RequestType < 3 && h.Compressed < 0x02 && h.MsgSize > 9
}
// Ucompress byte array compressed with Q IPC compression
func Uncompress(b []byte) (dst []byte) {
if len(b) < 4+1 {
return b
}
n, r, f, s := int32(0), int32(0), int32(0), int32(8)
p := int32(s)
i := int16(0)
var usize int32
binary.Read(bytes.NewReader(b[0:4]), binary.LittleEndian, &usize)
dst = make([]byte, usize)
d := int32(4)
aa := make([]int32, 256)
for int(s) < len(dst) {
if i == 0 {
f = 0xff & int32(b[d])
d++
i = 1
}
if (f & int32(i)) != 0 {
r = aa[0xff&int32(b[d])]
d++
dst[s] = dst[r]
s++
r++
dst[s] = dst[r]
s++
r++
n = 0xff & int32(b[d])
d++
for m := int32(0); m < n; m++ {
dst[s+m] = dst[r+m]
}
} else {
dst[s] = b[d]
s++
d++
}
for p < s-1 {
aa[(0xff&int32(dst[p]))^(0xff&int32(dst[p+1]))] = p
p++
}
if (f & int32(i)) != 0 {
s += n
p = s
}
i *= 2
if i == 256 {
i = 0
}
}
return dst
}
// Decodes data from src in q ipc format.
func Decode(src *bufio.Reader) (*K, int, error) {
var header ipcHeader
err := binary.Read(src, binary.LittleEndian, &header)
if err != nil {
return nil, -1, errors.New("Failed to read message header:" + err.Error())
}
if !header.ok() {
return nil, -1, errors.New("header is invalid")
}
// try to buffer entire message in one go
src.Peek(int(header.MsgSize - 8))
var order = header.getByteOrder()
if header.Compressed == 0x01 {
compressed := make([]byte, header.MsgSize-8)
_, err = io.ReadFull(src, compressed)
if err != nil {
return nil, int(header.RequestType), errors.New("Decode:readcompressed error - " + err.Error())
}
var uncompressed = Uncompress(compressed)
var buf = bufio.NewReader(bytes.NewReader(uncompressed[8:]))
data, err := readData(buf, order)
return data, int(header.RequestType), err
}
data, err := readData(src, order)
return data, int(header.RequestType), err
}
func readData(r *bufio.Reader, order binary.ByteOrder) (*K, error) {
var msgtype int8
if err := binary.Read(r, order, &msgtype); err != nil {
return nil, err
}
switch msgtype {
case -KB:
var b byte
binary.Read(r, order, &b)
return &K{msgtype, NONE, b != 0x0}, nil
case -UU:
var u uuid.UUID
binary.Read(r, order, &u)
return &K{msgtype, NONE, u}, nil
case -KG:
var b byte
binary.Read(r, order, &b)
return &K{msgtype, NONE, b}, nil
case -KH:
var sh int16
binary.Read(r, order, &sh)
return &K{msgtype, NONE, sh}, nil
case -KI, -KU, -KV:
var i int32
binary.Read(r, order, &i)
return &K{msgtype, NONE, i}, nil
case -KJ:
var j int64
binary.Read(r, order, &j)
return &K{msgtype, NONE, j}, nil
case -KE:
var e float32
binary.Read(r, order, &e)
return &K{msgtype, NONE, e}, nil
case -KF, -KZ:
var f float64
binary.Read(r, order, &f)
return &K{msgtype, NONE, f}, nil
case -KC:
var c byte
binary.Read(r, order, &c)
return &K{msgtype, NONE, c}, nil // should be rune?
case -KS:
line, err := r.ReadBytes(0)
if err != nil {
return nil, err
}
str := string(line[:len(line)-1])
return &K{msgtype, NONE, str}, nil
case -KP:
var nanos int64
binary.Read(r, order, &nanos)
return Timestamp(qEpoch.Add(time.Duration(nanos))), nil
case -KM:
var m Month
binary.Read(r, order, &m)
return &K{msgtype, NONE, m}, nil
case -KD:
var days int32
binary.Read(r, order, &days)
return Date(time.Unix((int64(days)*86400)+qEpoch.Unix(), 0)), nil
case -KN:
var span time.Duration
binary.Read(r, order, &span)
return &K{msgtype, NONE, span}, nil
case -KT:
var millis int32
binary.Read(r, order, &millis)
return Time(time.Unix(0, int64(millis)*int64(time.Millisecond))), nil
case KB, UU, KG, KH, KI, KJ, KE, KF, KC, KP, KM, KD, KN, KU, KV, KT, KZ:
var vecattr Attr
if err := binary.Read(r, order, &vecattr); err != nil {
return nil, errors.New("failed to read vector attr: " + err.Error())
}
var veclen uint32
if err := binary.Read(r, order, &veclen); err != nil {
return nil, errors.New("failed to read vector length: " + err.Error())
}
var arr interface{}
if msgtype >= KB && msgtype <= KT {
bytedata := make([]byte, int(veclen)*typeSize[msgtype])
if _, err := io.ReadFull(r, bytedata); err != nil {
return nil, errors.New("failed to read. not enough data: " + err.Error())
}
head := (*reflect.SliceHeader)(unsafe.Pointer(&bytedata))
head.Len = int(veclen)
head.Cap = int(veclen)
arr = reflect.Indirect(reflect.NewAt(typeReflect[msgtype], unsafe.Pointer(&bytedata))).Interface()
} else {
arr = makeArray(msgtype, int(veclen))
if err := binary.Read(r, order, arr); err != nil {
return nil, errors.New("error during conversion: " + err.Error())
}
}
switch msgtype {
case KC:
return String(string(arr.([]byte))), nil
case KP:
var vec = make([]time.Time, veclen)
for i, nanos := range arr.([]int64) {
vec[i] = qEpoch.Add(time.Duration(nanos))
}
return TimestampV(vec), nil
case KD:
var vec = make([]time.Time, veclen)
for i, days := range arr.([]int32) {
vec[i] = time.Unix((int64(days)*86400)+qEpoch.Unix(), 0)
}
return DateV(vec), nil
case KZ:
arr := arr.([]float64)
var timearr = make([]time.Time, veclen)
for i := 0; i < int(veclen); i++ {
d := time.Duration(86400000*arr[i]) * time.Millisecond
timearr[i] = qEpoch.Add(d)
}
return &K{msgtype, vecattr, timearr}, nil
case KU:
arr := arr.([]int32)
var timearr = make([]Minute, veclen)
for i := 0; i < int(veclen); i++ {
d := time.Duration(arr[i]) * time.Minute
timearr[i] = Minute(time.Time{}.Add(d))
}
return &K{msgtype, vecattr, timearr}, nil
case KV:
arr := arr.([]int32)
var timearr = make([]Second, veclen)
for i := 0; i < int(veclen); i++ {
d := time.Duration(arr[i]) * time.Second
timearr[i] = Second(time.Time{}.Add(d))
}
return &K{msgtype, vecattr, timearr}, nil
}
if msgtype == KT {
var vec = make([]time.Time, veclen)
for i, millis := range arr.([]int32) {
vec[i] = time.Unix(0, int64(millis)*int64(time.Millisecond))
}
return TimeV(vec), nil
}
return &K{msgtype, vecattr, arr}, nil
case K0:
var vecattr Attr
if err := binary.Read(r, order, &vecattr); err != nil {
return nil, errors.New("failed to read vector attr: " + err.Error())
}
var veclen uint32
if err := binary.Read(r, order, &veclen); err != nil {
return nil, errors.New("failed to read vector length: " + err.Error())
}
var arr = make([]*K, veclen)
for i := 0; i < int(veclen); i++ {
v, err := readData(r, order)
if err != nil {
return nil, err
}
arr[i] = v
}
return &K{msgtype, vecattr, arr}, nil
case KS:
var vecattr Attr
if err := binary.Read(r, order, &vecattr); err != nil {
return nil, errors.New("failed to read vector attr: " + err.Error())
}
var veclen uint32
if err := binary.Read(r, order, &veclen); err != nil {
return nil, errors.New("failed to read vector length: " + err.Error())
}
var arr = makeArray(msgtype, int(veclen)).([]string)
for i := 0; i < int(veclen); i++ {
line, err := r.ReadSlice(0)
if err != nil {
return nil, err
}
arr[i] = string(line[:len(line)-1])
}
return &K{msgtype, vecattr, arr}, nil
case XD, SD:
dk, err := readData(r, order)
if err != nil {
return nil, err
}
dv, err := readData(r, order)
if err != nil {
return nil, err
}
return NewDict(dk, dv), nil
case XT:
var vecattr Attr
if err := binary.Read(r, order, &vecattr); err != nil {
return nil, errors.New("failed to read vector attr: " + err.Error())
}
d, err := readData(r, order)
if err != nil {
return nil, err
}
if d.Type != XD {
return nil, errors.New("expected dict")
}
dict := d.Data.(Dict)
colNames := dict.Key.Data.([]string)
colValues := dict.Value.Data.([]*K)
return &K{msgtype, vecattr, Table{colNames, colValues}}, nil
case KFUNC:
var f Function
line, err := r.ReadSlice(0)
if err != nil {
return nil, err
}
f.Namespace = string(line[:len(line)-1])
b, err := readData(r, order)
if err != nil {
return nil, err
}
if b.Type != KC {
return nil, errors.New("expected string")
}
f.Body = b.Data.(string)
return &K{msgtype, NONE, f}, nil
case KFUNCUP, KFUNCBP, KFUNCTR:
var primitiveidx byte
if err := binary.Read(r, order, &primitiveidx); err != nil {
return nil, err
}
return &K{msgtype, NONE, primitiveidx}, nil
case KPROJ, KCOMP:
var n uint32
if err := binary.Read(r, order, &n); err != nil {
return nil, err
}
var res = make([]interface{}, n)
for i := 0; i < int(n); i++ {
var err error
if res[i], err = readData(r, order); err != nil {
return nil, err
}
}
return &K{msgtype, NONE, res}, nil
case KEACH, KOVER, KSCAN, KPRIOR, KEACHRIGHT, KEACHLEFT:
return readData(r, order)
case KDYNLOAD:
// 112 - dynamic load
return nil, NewUnsupportedTypeError(fmt.Sprintf("Unsupported type: %d", msgtype))
case KERR:
line, err := r.ReadSlice(0)
if err != nil {
return nil, err
}
errmsg := string(line[:len(line)-1])
return nil, NewRuntimeError(errmsg)
}
return nil, NewUnsupportedTypeError(fmt.Sprintf("Unsupported type: %d", msgtype))
}