forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
338 lines (318 loc) · 7.58 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
package tarantool
import (
"fmt"
)
type Response struct {
RequestId uint32
Code uint32
Error string // error message
// Data contains deserialized data for untyped requests
Data []interface{}
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 *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 mk {
case KeyFieldName:
meta.FieldName = mv.(string)
case KeyFieldType:
meta.FieldType = mv.(string)
case KeyFieldColl:
meta.FieldCollation = mv.(string)
case KeyFieldIsNullable:
meta.FieldIsNullable = mv.(bool)
case KeyIsAutoincrement:
meta.FieldIsAutoincrement = mv.(bool)
case KeyFieldSpan:
meta.FieldSpan = mv.(string)
default:
return fmt.Errorf("failed to decode meta data")
}
}
return nil
}
func (info *SQLInfo) DecodeMsgpack(d *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 mk {
case KeySQLInfoRowCount:
if info.AffectedCount, err = d.DecodeUint64(); err != nil {
return fmt.Errorf("failed to decode meta data")
}
case KeySQLInfoAutoincrementIds:
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 *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 *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 cd {
case KeySync:
var rid uint64
if rid, err = d.DecodeUint64(); err != nil {
return
}
resp.RequestId = uint32(rid)
case KeyCode:
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 := newDecoder(&resp.buf)
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 cd {
case KeyData:
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 KeyError:
if errorExtendedInfo, err = decodeBoxError(d); err != nil {
return err
}
case KeyError24:
if resp.Error, err = d.DecodeString(); err != nil {
return err
}
case KeySQLInfo:
if err = d.Decode(&resp.SQLInfo); err != nil {
return err
}
case KeyMetaData:
if err = d.Decode(&resp.MetaData); err != nil {
return err
}
case KeyStmtID:
if stmtID, err = d.DecodeUint64(); err != nil {
return err
}
case KeyBindCount:
if bindCount, err = d.DecodeUint64(); err != nil {
return err
}
case KeyVersion:
if err = d.Decode(&serverProtocolInfo.Version); err != nil {
return err
}
case KeyFeatures:
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 KeyAuthType:
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)
}
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 &^= ErrorCodeBit
err = 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 := newDecoder(&resp.buf)
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 cd {
case KeyData:
if err = d.Decode(res); err != nil {
return err
}
case KeyError:
if errorExtendedInfo, err = decodeBoxError(d); err != nil {
return err
}
case KeyError24:
if resp.Error, err = d.DecodeString(); err != nil {
return err
}
case KeySQLInfo:
if err = d.Decode(&resp.SQLInfo); err != nil {
return err
}
case KeyMetaData:
if err = d.Decode(&resp.MetaData); err != nil {
return err
}
default:
if err = d.Skip(); err != nil {
return err
}
}
}
if resp.Code != OkCode && resp.Code != PushCode {
resp.Code &^= ErrorCodeBit
err = 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
}