forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 59
/
response.go
360 lines (337 loc) · 8.73 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
package tarantool
import (
"fmt"
"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
)
type Response struct {
RequestId uint32
Code uint32
// Error contains an error message.
Error string
// Data contains deserialized data for untyped requests.
Data []interface{}
// Pos contains a position descriptor of last selected tuple.
Pos []byte
MetaData []ColumnMetaData
SQLInfo SQLInfo
buf smallBuf
}
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 (resp *Response) smallInt(d *msgpack.Decoder) (i int, err error) {
b, err := resp.buf.ReadByte()
if err != nil {
return
}
if b <= 127 {
return int(b), nil
}
resp.buf.UnreadByte()
return d.DecodeInt()
}
func (resp *Response) decodeHeader(d *msgpack.Decoder) (err error) {
var l int
d.Reset(&resp.buf)
if l, err = d.DecodeMapLen(); err != nil {
return
}
for ; l > 0; l-- {
var cd int
if cd, err = resp.smallInt(d); err != nil {
return
}
switch iproto.Key(cd) {
case iproto.IPROTO_SYNC:
var rid uint64
if rid, err = d.DecodeUint64(); err != nil {
return
}
resp.RequestId = uint32(rid)
case iproto.IPROTO_REQUEST_TYPE:
var rcode uint64
if rcode, err = d.DecodeUint64(); err != nil {
return
}
resp.Code = uint32(rcode)
default:
if err = d.Skip(); err != nil {
return
}
}
}
return nil
}
func (resp *Response) decodeBody() (err error) {
if resp.buf.Len() > 2 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
var l, larr int
var stmtID, bindCount uint64
var serverProtocolInfo ProtocolInfo
var feature ProtocolFeature
var errorExtendedInfo *BoxError = nil
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 = resp.smallInt(d); err != nil {
return err
}
switch iproto.Key(cd) {
case iproto.IPROTO_DATA:
var res interface{}
var ok bool
if res, err = d.DecodeInterface(); err != nil {
return err
}
if resp.Data, ok = res.([]interface{}); !ok {
return fmt.Errorf("result is not array: %v", res)
}
case iproto.IPROTO_ERROR:
if errorExtendedInfo, err = decodeBoxError(d); err != nil {
return err
}
case iproto.IPROTO_ERROR_24:
if resp.Error, err = d.DecodeString(); err != nil {
return err
}
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
}
case iproto.IPROTO_STMT_ID:
if stmtID, err = d.DecodeUint64(); err != nil {
return err
}
case iproto.IPROTO_BIND_COUNT:
if bindCount, err = d.DecodeUint64(); err != nil {
return err
}
case iproto.IPROTO_VERSION:
if err = d.Decode(&serverProtocolInfo.Version); err != nil {
return err
}
case iproto.IPROTO_FEATURES:
if larr, err = d.DecodeArrayLen(); err != nil {
return err
}
serverProtocolInfo.Features = make([]ProtocolFeature, larr)
for i := 0; i < larr; i++ {
if err = d.Decode(&feature); err != nil {
return err
}
serverProtocolInfo.Features[i] = feature
}
case iproto.IPROTO_AUTH_TYPE:
var auth string
if auth, err = d.DecodeString(); err != nil {
return err
}
found := false
for _, a := range [...]Auth{ChapSha1Auth, PapSha256Auth} {
if auth == a.String() {
serverProtocolInfo.Auth = a
found = true
}
}
if !found {
return fmt.Errorf("unknown auth type %s", auth)
}
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 stmtID != 0 {
stmt := &Prepared{
StatementID: PreparedID(stmtID),
ParamCount: bindCount,
MetaData: resp.MetaData,
}
resp.Data = []interface{}{stmt}
}
// Tarantool may send only version >= 1
if serverProtocolInfo.Version != ProtocolVersion(0) || serverProtocolInfo.Features != nil {
if serverProtocolInfo.Version == ProtocolVersion(0) {
return fmt.Errorf("no protocol version provided in Id response")
}
if serverProtocolInfo.Features == nil {
return fmt.Errorf("no features provided in Id response")
}
resp.Data = []interface{}{serverProtocolInfo}
}
if resp.Code != OkCode && resp.Code != PushCode {
resp.Code &^= uint32(iproto.IPROTO_TYPE_ERROR)
err = Error{iproto.Error(resp.Code), resp.Error, errorExtendedInfo}
}
}
return
}
func (resp *Response) decodeBodyTyped(res interface{}) (err error) {
if resp.buf.Len() > 0 {
offset := resp.buf.Offset()
defer resp.buf.Seek(offset)
var errorExtendedInfo *BoxError = nil
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 = resp.smallInt(d); err != nil {
return err
}
switch iproto.Key(cd) {
case iproto.IPROTO_DATA:
if err = d.Decode(res); err != nil {
return err
}
case iproto.IPROTO_ERROR:
if errorExtendedInfo, err = decodeBoxError(d); err != nil {
return err
}
case iproto.IPROTO_ERROR_24:
if resp.Error, err = d.DecodeString(); err != nil {
return err
}
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
}
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 resp.Code != OkCode && resp.Code != PushCode {
resp.Code &^= uint32(iproto.IPROTO_TYPE_ERROR)
err = Error{iproto.Error(resp.Code), resp.Error, errorExtendedInfo}
}
}
return
}
// String implements Stringer interface.
func (resp *Response) String() (str string) {
if resp.Code == OkCode {
return fmt.Sprintf("<%d OK %v>", resp.RequestId, resp.Data)
}
return fmt.Sprintf("<%d ERR 0x%x %s>", resp.RequestId, resp.Code, resp.Error)
}
// Tuples converts result of Eval and Call to same format
// as other actions returns (i.e. array of arrays).
func (resp *Response) Tuples() (res [][]interface{}) {
res = make([][]interface{}, len(resp.Data))
for i, t := range resp.Data {
switch t := t.(type) {
case []interface{}:
res[i] = t
default:
res[i] = []interface{}{t}
}
}
return res
}