forked from sv/kdbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
387 lines (371 loc) · 9.67 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
package kdb
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io"
"reflect"
"time"
"unsafe"
"github.com/nu7hatch/gouuid"
)
var typeSize = []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 = []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([]time.Duration{}),
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, 12:
return make([]time.Duration, veclen)
case 7:
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
}
// Decode deserialises data from src in q ipc format.
func Decode(src *bufio.Reader) (data *K, msgtype ReqType, e error) {
var header ipcHeader
e = binary.Read(src, binary.LittleEndian, &header)
if e != nil {
return nil, -1, errors.New("Failed to read message header:" + e.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)
_, e = io.ReadFull(src, compressed)
if e != nil {
return nil, header.RequestType, errors.New("Decode:readcompressed error - " + e.Error())
}
var uncompressed = Uncompress(compressed)
var buf = bufio.NewReader(bytes.NewReader(uncompressed[8:]))
data, e = readData(buf, order)
return data, header.RequestType, e
}
data, e = readData(src, order)
return data, header.RequestType, e
}
func readData(r *bufio.Reader, order binary.ByteOrder) (kobj *K, err error) {
var msgtype int8
err = binary.Read(r, order, &msgtype)
if 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, -KC:
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, -KD, -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 -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 ts time.Duration
binary.Read(r, order, &ts)
return &K{msgtype, NONE, qEpoch.Add(ts)}, nil
case -KM:
var m Month
binary.Read(r, order, &m)
return &K{msgtype, NONE, m}, nil
case -KN:
var span time.Duration
binary.Read(r, order, &span)
return &K{msgtype, NONE, span}, nil
case KB, UU, KG, KH, KI, KJ, KE, KF, KC, KP, KM, KD, KN, KU, KV, KT, KZ:
var vecattr Attr
err = binary.Read(r, order, &vecattr)
if err != nil {
return nil, errors.New("readData: Failed to read vecattr:" + err.Error())
}
var veclen uint32
err = binary.Read(r, order, &veclen)
if err != nil {
return nil, errors.New("Reading vector length failed -> " + err.Error())
}
var arr interface{}
if msgtype >= KB && msgtype <= KT {
bytedata := make([]byte, int(veclen)*typeSize[msgtype])
_, err = io.ReadFull(r, bytedata)
if err != nil {
return nil, errors.New("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))
err = binary.Read(r, order, arr)
}
if err != nil {
return nil, errors.New("Error during conversion -> " + err.Error())
}
if msgtype == KC {
return &K{msgtype, vecattr, string(arr.([]byte))}, nil
}
if msgtype == KP {
arr := arr.([]time.Duration)
var timearr = make([]time.Time, veclen)
for i := 0; i < int(veclen); i++ {
timearr[i] = qEpoch.Add(arr[i])
}
return &K{msgtype, vecattr, timearr}, nil
}
if msgtype == KD {
arr := arr.([]int32)
var timearr = make([]time.Time, veclen)
for i := 0; i < int(veclen); i++ {
d := time.Duration(arr[i]) * 24 * time.Hour
timearr[i] = qEpoch.Add(d)
}
return &K{msgtype, vecattr, timearr}, nil
}
if msgtype == 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
}
if msgtype == 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
}
if msgtype == 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 {
arr := arr.([]int32)
var timearr = make([]Time, veclen)
for i := 0; i < int(veclen); i++ {
timearr[i] = Time(qEpoch.Add(time.Duration(arr[i]) * time.Millisecond))
}
return &K{msgtype, vecattr, timearr}, nil
}
return &K{msgtype, vecattr, arr}, nil
case K0:
var vecattr Attr
err = binary.Read(r, order, &vecattr)
if err != nil {
return nil, errors.New("readData: Failed to read vecattr ->" + err.Error())
}
var veclen uint32
err = binary.Read(r, order, &veclen)
if err != nil {
return nil, errors.New("Reading vector length failed -> " + 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
err = binary.Read(r, order, &vecattr)
if err != nil {
return nil, errors.New("readData: Failed to read vecattr ->" + err.Error())
}
var veclen uint32
err = binary.Read(r, order, &veclen)
if err != nil {
return nil, errors.New("Reading vector length failed -> " + 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
}
res := NewDict(dk, dv)
if msgtype == SD {
res.Attr = SORTED
}
return res, nil
case XT:
var vecattr Attr
err = binary.Read(r, order, &vecattr)
if err != nil {
return nil, errors.New("readData: Failed to read vecattr" + 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
err = binary.Read(r, order, &primitiveidx)
if err != nil {
return nil, err
}
return &K{msgtype, NONE, primitiveidx}, nil
case KPROJ, KCOMP:
var n uint32
err = binary.Read(r, order, &n)
if err != nil {
return nil, err
}
var res = make([]*K, n)
for i := 0; i < len(res); i++ {
res[i], err = readData(r, order)
if err != nil {
return nil, err
}
}
return &K{msgtype, NONE, res}, nil
case KEACH, KOVER, KSCAN, KPRIOR, KEACHRIGHT, KEACHLEFT:
res, err := readData(r, order)
if err != nil {
return nil, err
}
return &K{msgtype, NONE, res}, nil
case KDYNLOAD:
// 112 - dynamic load
return nil, errors.New("type is unsupported")
case KERR:
line, err := r.ReadSlice(0)
if err != nil {
return nil, err
}
errmsg := string(line[:len(line)-1])
return nil, errors.New(errmsg)
}
return nil, ErrBadMsg
}