forked from mvo5/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
455 lines (397 loc) · 8.97 KB
/
frame.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
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"net"
)
const (
protoRequest byte = 0x02
protoResponse byte = 0x82
opError byte = 0x00
opStartup byte = 0x01
opReady byte = 0x02
opAuthenticate byte = 0x03
opCredentials byte = 0x04
opOptions byte = 0x05
opSupported byte = 0x06
opQuery byte = 0x07
opResult byte = 0x08
opPrepare byte = 0x09
opExecute byte = 0x0A
opRegister byte = 0x0B
opEvent byte = 0x0C
opBatch byte = 0x0D
opAuthChallenge byte = 0x0E
opAuthResponse byte = 0x0F
opAuthSuccess byte = 0x10
resultKindVoid = 1
resultKindRows = 2
resultKindKeyspace = 3
resultKindPrepared = 4
resultKindSchemaChanged = 5
flagQueryValues uint8 = 1
flagCompress uint8 = 1
flagTrace uint8 = 2
flagPageSize uint8 = 4
flagPageState uint8 = 8
flagHasMore uint8 = 2
errServer = 0x0000
errProtocol = 0x000A
errCredentials = 0x0100
errUnavailable = 0x1000
errOverloaded = 0x1001
errBootstrapping = 0x1002
errTruncate = 0x1003
errWriteTimeout = 0x1100
errReadTimeout = 0x1200
errSyntax = 0x2000
errUnauthorized = 0x2100
errInvalid = 0x2200
errConfig = 0x2300
errAlreadyExists = 0x2400
errUnprepared = 0x2500
headerSize = 8
)
type frame []byte
func (f *frame) writeInt(v int32) {
p := f.grow(4)
(*f)[p] = byte(v >> 24)
(*f)[p+1] = byte(v >> 16)
(*f)[p+2] = byte(v >> 8)
(*f)[p+3] = byte(v)
}
func (f *frame) writeShort(v uint16) {
p := f.grow(2)
(*f)[p] = byte(v >> 8)
(*f)[p+1] = byte(v)
}
func (f *frame) writeString(v string) {
f.writeShort(uint16(len(v)))
p := f.grow(len(v))
copy((*f)[p:], v)
}
func (f *frame) writeLongString(v string) {
f.writeInt(int32(len(v)))
p := f.grow(len(v))
copy((*f)[p:], v)
}
func (f *frame) writeUUID() {
}
func (f *frame) writeStringList(v []string) {
f.writeShort(uint16(len(v)))
for i := range v {
f.writeString(v[i])
}
}
func (f *frame) writeByte(v byte) {
p := f.grow(1)
(*f)[p] = v
}
func (f *frame) writeBytes(v []byte) {
if v == nil {
f.writeInt(-1)
return
}
f.writeInt(int32(len(v)))
p := f.grow(len(v))
copy((*f)[p:], v)
}
func (f *frame) writeShortBytes(v []byte) {
f.writeShort(uint16(len(v)))
p := f.grow(len(v))
copy((*f)[p:], v)
}
func (f *frame) writeInet(ip net.IP, port int) {
p := f.grow(1 + len(ip))
(*f)[p] = byte(len(ip))
copy((*f)[p+1:], ip)
f.writeInt(int32(port))
}
func (f *frame) writeStringMap(v map[string]string) {
f.writeShort(uint16(len(v)))
for key, value := range v {
f.writeString(key)
f.writeString(value)
}
}
func (f *frame) writeStringMultimap(v map[string][]string) {
f.writeShort(uint16(len(v)))
for key, values := range v {
f.writeString(key)
f.writeStringList(values)
}
}
func (f *frame) setHeader(version, flags, stream, opcode uint8) {
(*f)[0] = version
(*f)[1] = flags
(*f)[2] = stream
(*f)[3] = opcode
}
func (f *frame) setLength(length int) {
(*f)[4] = byte(length >> 24)
(*f)[5] = byte(length >> 16)
(*f)[6] = byte(length >> 8)
(*f)[7] = byte(length)
}
func (f *frame) Length() int {
return int((*f)[4])<<24 | int((*f)[5])<<16 | int((*f)[6])<<8 | int((*f)[7])
}
func (f *frame) grow(n int) int {
if len(*f)+n >= cap(*f) {
buf := make(frame, len(*f), len(*f)*2+n)
copy(buf, *f)
*f = buf
}
p := len(*f)
*f = (*f)[:p+n]
return p
}
func (f *frame) skipHeader() {
*f = (*f)[headerSize:]
}
func (f *frame) readInt() int {
if len(*f) < 4 {
panic(ErrProtocol)
}
v := uint32((*f)[0])<<24 | uint32((*f)[1])<<16 | uint32((*f)[2])<<8 | uint32((*f)[3])
*f = (*f)[4:]
return int(int32(v))
}
func (f *frame) readShort() uint16 {
if len(*f) < 2 {
panic(ErrProtocol)
}
v := uint16((*f)[0])<<8 | uint16((*f)[1])
*f = (*f)[2:]
return v
}
func (f *frame) readString() string {
n := int(f.readShort())
if len(*f) < n {
panic(ErrProtocol)
}
v := string((*f)[:n])
*f = (*f)[n:]
return v
}
func (f *frame) readLongString() string {
n := f.readInt()
if len(*f) < n {
panic(ErrProtocol)
}
v := string((*f)[:n])
*f = (*f)[n:]
return v
}
func (f *frame) readBytes() []byte {
n := f.readInt()
if n < 0 {
return nil
}
if len(*f) < n {
panic(ErrProtocol)
}
v := (*f)[:n]
*f = (*f)[n:]
return v
}
func (f *frame) readShortBytes() []byte {
n := int(f.readShort())
if len(*f) < n {
panic(ErrProtocol)
}
v := (*f)[:n]
*f = (*f)[n:]
return v
}
func (f *frame) readTypeInfo() *TypeInfo {
x := f.readShort()
typ := &TypeInfo{Type: Type(x)}
switch typ.Type {
case TypeCustom:
typ.Custom = f.readString()
case TypeMap:
typ.Key = f.readTypeInfo()
fallthrough
case TypeList, TypeSet:
typ.Elem = f.readTypeInfo()
}
return typ
}
func (f *frame) readMetaData() ([]ColumnInfo, []byte) {
flags := f.readInt()
numColumns := f.readInt()
var pageState []byte
if flags&2 != 0 {
pageState = f.readBytes()
}
globalKeyspace := ""
globalTable := ""
if flags&1 != 0 {
globalKeyspace = f.readString()
globalTable = f.readString()
}
columns := make([]ColumnInfo, numColumns)
for i := 0; i < numColumns; i++ {
columns[i].Keyspace = globalKeyspace
columns[i].Table = globalTable
if flags&1 == 0 {
columns[i].Keyspace = f.readString()
columns[i].Table = f.readString()
}
columns[i].Name = f.readString()
columns[i].TypeInfo = f.readTypeInfo()
}
return columns, pageState
}
func (f *frame) writeConsistency(c Consistency) {
f.writeShort(consistencyCodes[c])
}
func (f frame) encodeFrame(version uint8, dst frame) (frame, error) {
return f, nil
}
var consistencyCodes = []uint16{
Any: 0x0000,
One: 0x0001,
Two: 0x0002,
Three: 0x0003,
Quorum: 0x0004,
All: 0x0005,
LocalQuorum: 0x0006,
EachQuorum: 0x0007,
Serial: 0x0008,
LocalSerial: 0x0009,
}
type readyFrame struct{}
type authenticateFrame struct{}
type supportedFrame struct{}
type resultVoidFrame struct{}
type resultRowsFrame struct {
Columns []ColumnInfo
Rows [][][]byte
PagingState []byte
}
type resultKeyspaceFrame struct {
Keyspace string
}
type resultPreparedFrame struct {
PreparedId []byte
Values []ColumnInfo
}
type errorFrame struct {
Code int
Message string
}
func (e errorFrame) Error() string {
return e.Message
}
type operation interface {
encodeFrame(version uint8, dst frame) (frame, error)
}
type authenticationFrame struct {
Username string
Password string
}
func (op *authenticationFrame) encodeFrame(version uint8, f frame) (frame, error) {
if f == nil {
f = make(frame, headerSize, defaultFrameSize)
}
f.setHeader(version, 0, 0, opCredentials)
auth := make(map[string]string)
auth["username"] = op.Username
auth["password"] = op.Password
f.writeStringMap(auth)
return f, nil
}
type startupFrame struct {
CQLVersion string
Compression string
}
func (op *startupFrame) encodeFrame(version uint8, f frame) (frame, error) {
if f == nil {
f = make(frame, headerSize, defaultFrameSize)
}
f.setHeader(version, 0, 0, opStartup)
f.writeShort(1)
f.writeString("CQL_VERSION")
f.writeString(op.CQLVersion)
if op.Compression != "" {
f[headerSize+1] += 1
f.writeString("COMPRESSION")
f.writeString(op.Compression)
}
return f, nil
}
type queryFrame struct {
Stmt string
Prepared []byte
Cons Consistency
Values [][]byte
PageSize int
PageState []byte
}
func (op *queryFrame) encodeFrame(version uint8, f frame) (frame, error) {
if version == 1 && (op.PageSize != 0 || len(op.PageState) > 0 ||
(len(op.Values) > 0 && len(op.Prepared) == 0)) {
return nil, ErrUnsupported
}
if f == nil {
f = make(frame, headerSize, defaultFrameSize)
}
if len(op.Prepared) > 0 {
f.setHeader(version, 0, 0, opExecute)
f.writeShortBytes(op.Prepared)
} else {
f.setHeader(version, 0, 0, opQuery)
f.writeLongString(op.Stmt)
}
if version >= 2 {
f.writeConsistency(op.Cons)
flagPos := len(f)
f.writeByte(0)
if len(op.Values) > 0 {
f[flagPos] |= flagQueryValues
f.writeShort(uint16(len(op.Values)))
for _, value := range op.Values {
f.writeBytes(value)
}
}
if op.PageSize > 0 {
f[flagPos] |= flagPageSize
f.writeInt(int32(op.PageSize))
}
if len(op.PageState) > 0 {
f[flagPos] |= flagPageState
f.writeBytes(op.PageState)
}
} else if version == 1 {
if len(op.Prepared) > 0 {
f.writeShort(uint16(len(op.Values)))
for _, value := range op.Values {
f.writeBytes(value)
}
}
f.writeConsistency(op.Cons)
}
return f, nil
}
type prepareFrame struct {
Stmt string
}
func (op *prepareFrame) encodeFrame(version uint8, f frame) (frame, error) {
if f == nil {
f = make(frame, headerSize, defaultFrameSize)
}
f.setHeader(version, 0, 0, opPrepare)
f.writeLongString(op.Stmt)
return f, nil
}
type optionsFrame struct{}
func (op *optionsFrame) encodeFrame(version uint8, f frame) (frame, error) {
if f == nil {
f = make(frame, headerSize, defaultFrameSize)
}
f.setHeader(version, 0, 0, opOptions)
return f, nil
}