forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
/
response.go
705 lines (643 loc) · 16.6 KB
/
response.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
package tarantool
import (
"fmt"
"io"
"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
)
// Response is an interface with operations for the server responses.
type Response interface {
// Header returns a response header.
Header() Header
// Decode decodes a response.
Decode() ([]interface{}, error)
// DecodeTyped decodes a response into a given container res.
DecodeTyped(res interface{}) error
}
type baseResponse struct {
// header is a response header.
header Header
// data contains deserialized data for untyped requests.
data []interface{}
buf smallBuf
// Was the Decode() func called for this response.
decoded bool
// Was the DecodeTyped() func called for this response.
decodedTyped bool
err error
}
func createBaseResponse(header Header, body io.Reader) (baseResponse, error) {
if body == nil {
return baseResponse{header: header}, nil
}
if buf, ok := body.(*smallBuf); ok {
return baseResponse{header: header, buf: *buf}, nil
}
data, err := io.ReadAll(body)
if err != nil {
return baseResponse{}, err
}
return baseResponse{header: header, buf: smallBuf{b: data}}, nil
}
// DecodeBaseResponse parse response header and body.
func DecodeBaseResponse(header Header, body io.Reader) (Response, error) {
resp, err := createBaseResponse(header, body)
return &resp, err
}
// SelectResponse is used for the select requests.
// It might contain a position descriptor of the last selected tuple.
//
// You need to cast to SelectResponse a response from SelectRequest.
type SelectResponse struct {
baseResponse
// pos contains a position descriptor of last selected tuple.
pos []byte
}
// PrepareResponse is used for the prepare requests.
// It might contain meta-data and sql info.
//
// Be careful: now this is an alias for `ExecuteResponse`,
// but it could be changed in the future.
// You need to cast to PrepareResponse a response from PrepareRequest.
type PrepareResponse ExecuteResponse
// ExecuteResponse is used for the execute requests.
// It might contain meta-data and sql info.
//
// You need to cast to ExecuteResponse a response from ExecuteRequest.
type ExecuteResponse struct {
baseResponse
metaData []ColumnMetaData
sqlInfo SQLInfo
}
type ColumnMetaData struct {
FieldName string
FieldType string
FieldCollation string
FieldIsNullable bool
FieldIsAutoincrement bool
FieldSpan string
}
type SQLInfo struct {
AffectedCount uint64
InfoAutoincrementIds []uint64
}
func (meta *ColumnMetaData) DecodeMsgpack(d *msgpack.Decoder) error {
var err error
var l int
if l, err = d.DecodeMapLen(); err != nil {
return err
}
if l == 0 {
return fmt.Errorf("map len doesn't match: %d", l)
}
for i := 0; i < l; i++ {
var mk uint64
var mv interface{}
if mk, err = d.DecodeUint64(); err != nil {
return fmt.Errorf("failed to decode meta data")
}
if mv, err = d.DecodeInterface(); err != nil {
return fmt.Errorf("failed to decode meta data")
}
switch iproto.MetadataKey(mk) {
case iproto.IPROTO_FIELD_NAME:
meta.FieldName = mv.(string)
case iproto.IPROTO_FIELD_TYPE:
meta.FieldType = mv.(string)
case iproto.IPROTO_FIELD_COLL:
meta.FieldCollation = mv.(string)
case iproto.IPROTO_FIELD_IS_NULLABLE:
meta.FieldIsNullable = mv.(bool)
case iproto.IPROTO_FIELD_IS_AUTOINCREMENT:
meta.FieldIsAutoincrement = mv.(bool)
case iproto.IPROTO_FIELD_SPAN:
meta.FieldSpan = mv.(string)
default:
return fmt.Errorf("failed to decode meta data")
}
}
return nil
}
func (info *SQLInfo) DecodeMsgpack(d *msgpack.Decoder) error {
var err error
var l int
if l, err = d.DecodeMapLen(); err != nil {
return err
}
if l == 0 {
return fmt.Errorf("map len doesn't match")
}
for i := 0; i < l; i++ {
var mk uint64
if mk, err = d.DecodeUint64(); err != nil {
return fmt.Errorf("failed to decode meta data")
}
switch iproto.SqlInfoKey(mk) {
case iproto.SQL_INFO_ROW_COUNT:
if info.AffectedCount, err = d.DecodeUint64(); err != nil {
return fmt.Errorf("failed to decode meta data")
}
case iproto.SQL_INFO_AUTOINCREMENT_IDS:
if err = d.Decode(&info.InfoAutoincrementIds); err != nil {
return fmt.Errorf("failed to decode meta data")
}
default:
return fmt.Errorf("failed to decode meta data")
}
}
return nil
}
func smallInt(d *msgpack.Decoder, buf *smallBuf) (i int, err error) {
b, err := buf.ReadByte()
if err != nil {
return
}
if b <= 127 {
return int(b), nil
}
buf.UnreadByte()
return d.DecodeInt()
}
func decodeHeader(d *msgpack.Decoder, buf *smallBuf) (Header, iproto.Type, error) {
var l int
var code int
var err error
d.Reset(buf)
if l, err = d.DecodeMapLen(); err != nil {
return Header{}, 0, err
}
decodedHeader := Header{Error: ErrorNo}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, buf); err != nil {
return Header{}, 0, err
}
switch iproto.Key(cd) {
case iproto.IPROTO_SYNC:
var rid uint64
if rid, err = d.DecodeUint64(); err != nil {
return Header{}, 0, err
}
decodedHeader.RequestId = uint32(rid)
case iproto.IPROTO_REQUEST_TYPE:
if code, err = d.DecodeInt(); err != nil {
return Header{}, 0, err
}
if code&int(iproto.IPROTO_TYPE_ERROR) != 0 {
decodedHeader.Error = iproto.Error(code &^ int(iproto.IPROTO_TYPE_ERROR))
} else {
decodedHeader.Error = ErrorNo
}
default:
if err = d.Skip(); err != nil {
return Header{}, 0, err
}
}
}
return decodedHeader, iproto.Type(code), nil
}
type decodeInfo struct {
stmtID uint64
bindCount uint64
serverProtocolInfo ProtocolInfo
errorExtendedInfo *BoxError
decodedError string
}
func (info *decodeInfo) parseData(resp *baseResponse) error {
if info.stmtID != 0 {
stmt := &Prepared{
StatementID: PreparedID(info.stmtID),
ParamCount: info.bindCount,
}
resp.data = []interface{}{stmt}
return nil
}
// Tarantool may send only version >= 1.
if info.serverProtocolInfo.Version != ProtocolVersion(0) ||
info.serverProtocolInfo.Features != nil {
if info.serverProtocolInfo.Version == ProtocolVersion(0) {
return fmt.Errorf("no protocol version provided in Id response")
}
if info.serverProtocolInfo.Features == nil {
return fmt.Errorf("no features provided in Id response")
}
resp.data = []interface{}{info.serverProtocolInfo}
return nil
}
return nil
}
func decodeCommonField(d *msgpack.Decoder, cd int, data *[]interface{},
info *decodeInfo) (bool, error) {
var feature iproto.Feature
var err error
switch iproto.Key(cd) {
case iproto.IPROTO_DATA:
var res interface{}
var ok bool
if res, err = d.DecodeInterface(); err != nil {
return false, err
}
if *data, ok = res.([]interface{}); !ok {
return false, fmt.Errorf("result is not array: %v", res)
}
case iproto.IPROTO_ERROR:
if info.errorExtendedInfo, err = decodeBoxError(d); err != nil {
return false, err
}
case iproto.IPROTO_ERROR_24:
if info.decodedError, err = d.DecodeString(); err != nil {
return false, err
}
case iproto.IPROTO_STMT_ID:
if info.stmtID, err = d.DecodeUint64(); err != nil {
return false, err
}
case iproto.IPROTO_BIND_COUNT:
if info.bindCount, err = d.DecodeUint64(); err != nil {
return false, err
}
case iproto.IPROTO_VERSION:
if err = d.Decode(&info.serverProtocolInfo.Version); err != nil {
return false, err
}
case iproto.IPROTO_FEATURES:
var larr int
if larr, err = d.DecodeArrayLen(); err != nil {
return false, err
}
info.serverProtocolInfo.Features = make([]iproto.Feature, larr)
for i := 0; i < larr; i++ {
if err = d.Decode(&feature); err != nil {
return false, err
}
info.serverProtocolInfo.Features[i] = feature
}
case iproto.IPROTO_AUTH_TYPE:
var auth string
if auth, err = d.DecodeString(); err != nil {
return false, err
}
found := false
for _, a := range [...]Auth{ChapSha1Auth, PapSha256Auth} {
if auth == a.String() {
info.serverProtocolInfo.Auth = a
found = true
}
}
if !found {
return false, fmt.Errorf("unknown auth type %s", auth)
}
default:
return false, nil
}
return true, nil
}
func (resp *baseResponse) Decode() ([]interface{}, error) {
if resp.decoded {
return resp.data, resp.err
}
resp.decoded = true
var err error
if resp.buf.Len() > 2 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
var l int
info := &decodeInfo{}
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
resp.err = err
return nil, resp.err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
resp.err = err
return nil, resp.err
}
decoded, err := decodeCommonField(d, cd, &resp.data, info)
if err != nil {
resp.err = err
return nil, resp.err
}
if !decoded {
if err = d.Skip(); err != nil {
resp.err = err
return nil, resp.err
}
}
}
err = info.parseData(resp)
if err != nil {
resp.err = err
return nil, resp.err
}
if info.decodedError != "" {
resp.err = Error{resp.header.Error, info.decodedError,
info.errorExtendedInfo}
}
}
return resp.data, resp.err
}
func (resp *SelectResponse) Decode() ([]interface{}, error) {
if resp.decoded {
return resp.data, resp.err
}
resp.decoded = true
var err error
if resp.buf.Len() > 2 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
var l int
info := &decodeInfo{}
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
resp.err = err
return nil, resp.err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
resp.err = err
return nil, resp.err
}
decoded, err := decodeCommonField(d, cd, &resp.data, info)
if err != nil {
resp.err = err
return nil, err
}
if !decoded {
switch iproto.Key(cd) {
case iproto.IPROTO_POSITION:
if resp.pos, err = d.DecodeBytes(); err != nil {
resp.err = err
return nil, fmt.Errorf("unable to decode a position: %w", resp.err)
}
default:
if err = d.Skip(); err != nil {
resp.err = err
return nil, resp.err
}
}
}
}
err = info.parseData(&resp.baseResponse)
if err != nil {
resp.err = err
return nil, resp.err
}
if info.decodedError != "" {
resp.err = Error{resp.header.Error, info.decodedError,
info.errorExtendedInfo}
}
}
return resp.data, resp.err
}
func (resp *ExecuteResponse) Decode() ([]interface{}, error) {
if resp.decoded {
return resp.data, resp.err
}
resp.decoded = true
var err error
if resp.buf.Len() > 2 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
var l int
info := &decodeInfo{}
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
resp.err = err
return nil, resp.err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
resp.err = err
return nil, resp.err
}
decoded, err := decodeCommonField(d, cd, &resp.data, info)
if err != nil {
resp.err = err
return nil, resp.err
}
if !decoded {
switch iproto.Key(cd) {
case iproto.IPROTO_SQL_INFO:
if err = d.Decode(&resp.sqlInfo); err != nil {
resp.err = err
return nil, resp.err
}
case iproto.IPROTO_METADATA:
if err = d.Decode(&resp.metaData); err != nil {
resp.err = err
return nil, resp.err
}
default:
if err = d.Skip(); err != nil {
resp.err = err
return nil, resp.err
}
}
}
}
err = info.parseData(&resp.baseResponse)
if err != nil {
resp.err = err
return nil, resp.err
}
if info.decodedError != "" {
resp.err = Error{resp.header.Error, info.decodedError,
info.errorExtendedInfo}
}
}
return resp.data, resp.err
}
func decodeTypedCommonField(d *msgpack.Decoder, res interface{}, cd int,
info *decodeInfo) (bool, error) {
var err error
switch iproto.Key(cd) {
case iproto.IPROTO_DATA:
if err = d.Decode(res); err != nil {
return false, err
}
case iproto.IPROTO_ERROR:
if info.errorExtendedInfo, err = decodeBoxError(d); err != nil {
return false, err
}
case iproto.IPROTO_ERROR_24:
if info.decodedError, err = d.DecodeString(); err != nil {
return false, err
}
default:
return false, nil
}
return true, nil
}
func (resp *baseResponse) DecodeTyped(res interface{}) error {
resp.decodedTyped = true
var err error
if resp.buf.Len() > 0 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
info := &decodeInfo{}
var l int
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
return err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
return err
}
decoded, err := decodeTypedCommonField(d, res, cd, info)
if err != nil {
return err
}
if !decoded {
if err = d.Skip(); err != nil {
return err
}
}
}
if info.decodedError != "" {
err = Error{resp.header.Error, info.decodedError, info.errorExtendedInfo}
}
}
return err
}
func (resp *SelectResponse) DecodeTyped(res interface{}) error {
resp.decodedTyped = true
var err error
if resp.buf.Len() > 0 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
info := &decodeInfo{}
var l int
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
return err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
return err
}
decoded, err := decodeTypedCommonField(d, res, cd, info)
if err != nil {
return err
}
if !decoded {
switch iproto.Key(cd) {
case iproto.IPROTO_POSITION:
if resp.pos, err = d.DecodeBytes(); err != nil {
return fmt.Errorf("unable to decode a position: %w", err)
}
default:
if err = d.Skip(); err != nil {
return err
}
}
}
}
if info.decodedError != "" {
err = Error{resp.header.Error, info.decodedError, info.errorExtendedInfo}
}
}
return err
}
func (resp *ExecuteResponse) DecodeTyped(res interface{}) error {
resp.decodedTyped = true
var err error
if resp.buf.Len() > 0 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
info := &decodeInfo{}
var l int
d := msgpack.NewDecoder(&resp.buf)
d.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
if l, err = d.DecodeMapLen(); err != nil {
return err
}
for ; l > 0; l-- {
var cd int
if cd, err = smallInt(d, &resp.buf); err != nil {
return err
}
decoded, err := decodeTypedCommonField(d, res, cd, info)
if err != nil {
return err
}
if !decoded {
switch iproto.Key(cd) {
case iproto.IPROTO_SQL_INFO:
if err = d.Decode(&resp.sqlInfo); err != nil {
return err
}
case iproto.IPROTO_METADATA:
if err = d.Decode(&resp.metaData); err != nil {
return err
}
default:
if err = d.Skip(); err != nil {
return err
}
}
}
}
if info.decodedError != "" {
err = Error{resp.header.Error, info.decodedError, info.errorExtendedInfo}
}
}
return err
}
func (resp *baseResponse) Header() Header {
return resp.header
}
// Pos returns a position descriptor of the last selected tuple for the SelectResponse.
// If the response was not decoded, this method will call Decode().
func (resp *SelectResponse) Pos() ([]byte, error) {
if !resp.decoded && !resp.decodedTyped {
resp.Decode()
}
return resp.pos, resp.err
}
// MetaData returns ExecuteResponse meta-data.
// If the response was not decoded, this method will call Decode().
func (resp *ExecuteResponse) MetaData() ([]ColumnMetaData, error) {
if !resp.decoded && !resp.decodedTyped {
resp.Decode()
}
return resp.metaData, resp.err
}
// SQLInfo returns ExecuteResponse sql info.
// If the response was not decoded, this method will call Decode().
func (resp *ExecuteResponse) SQLInfo() (SQLInfo, error) {
if !resp.decoded && !resp.decodedTyped {
resp.Decode()
}
return resp.sqlInfo, resp.err
}
// String implements Stringer interface.
func (resp *baseResponse) String() (str string) {
if resp.header.Error == ErrorNo {
return fmt.Sprintf("<%d OK %v>", resp.header.RequestId, resp.data)
}
return fmt.Sprintf("<%d ERR %s %v>", resp.header.RequestId, resp.header.Error, resp.err)
}