This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
types.go
659 lines (599 loc) · 17 KB
/
types.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
package gossie
import (
"fmt"
enc "encoding/binary"
"strings"
"strconv"
"os"
"bytes"
)
/*
to do:
ReversedType
IntegerType
DecimalType
don't assume int is int32 (tho it's prob ok)
uints, support them?
maybe add ascii/utf8 types support UUIDType, string native support?
maybe something better for DateType, instead of just int64 conv (go v1)?
maybe some more (un)marshalings?
more error checking, pass along all strconv errors
*/
const (
_ = iota
UnknownType
BytesType
AsciiType
UTF8Type
LongType
IntegerType
DecimalType
UUIDType
BooleanType
FloatType
DoubleType
DateType
CounterColumnType
CompositeType
)
var (
ErrorUnsupportedMarshaling = os.NewError("Cannot marshal value")
ErrorUnsupportedNilMarshaling = os.NewError("Cannot marshal nil")
ErrorUnsupportedUnmarshaling = os.NewError("Cannot unmarshal value")
ErrorUnsupportedNativeTypeUnmarshaling = os.NewError("Cannot unmarshal to native type")
ErrorUnsupportedCassandraTypeUnmarshaling = os.NewError("Cannot unmarshal from Cassandra type")
ErrorCassandraTypeSerializationUnmarshaling = os.NewError("Cassandra serialization is wrong for the type, cannot unmarshal")
)
type TypeDesc int
type UUID [16]byte
func (value UUID) String() string {
var r []string
var s int
for _, size := range [5]int{4, 2, 2, 2, 6} {
var v int64
for i := 0; i < size; i++ {
v = v << 8
v = v | int64(value[s+i])
}
r = append(r, fmt.Sprintf("%0*x", size*2, v))
s += size
}
return strings.Join(r, "-")
}
func NewUUID(value string) (UUID, os.Error) {
var r []byte
var ru UUID
if len(value) != 36 {
return ru, ErrorUnsupportedMarshaling
}
ints := strings.Split(value, "-")
if len(ints) != 5 {
return ru, ErrorUnsupportedMarshaling
}
for i, size := range [5]int{4, 2, 2, 2, 6} {
t, err := strconv.Btoi64(ints[i], 16)
if err != nil {
return ru, ErrorUnsupportedMarshaling
}
b, _ := marshalInt(t, size, BytesType)
r = append(r, b...)
}
unmarshalUUID(r, BytesType, &ru)
return ru, nil
}
func Marshal(value interface{}, typeDesc TypeDesc) ([]byte, os.Error) {
// plain nil case
if value == nil {
return nil, ErrorUnsupportedNilMarshaling
}
// dereference in case we got a pointer, check for nil too
var dvalue interface{}
switch v := value.(type) {
case *[]byte:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *bool:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *int8:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *int16:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *int:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *int32:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *int64:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *string:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *UUID:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *float32:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
case *float64:
if v == nil {
return nil, ErrorUnsupportedNilMarshaling
}
dvalue = *v
default:
dvalue = v
}
switch v := dvalue.(type) {
case []byte:
return v, nil
case bool:
return marshalBool(v, typeDesc)
case int8:
return marshalInt(int64(v), 1, typeDesc)
case int16:
return marshalInt(int64(v), 2, typeDesc)
case int:
return marshalInt(int64(v), 4, typeDesc)
case int32:
return marshalInt(int64(v), 4, typeDesc)
case int64:
return marshalInt(v, 8, typeDesc)
case string:
return marshalString(v, typeDesc)
case UUID:
return marshalUUID(v, typeDesc)
case float32:
return marshalFloat32(v, typeDesc)
case float64:
return marshalFloat64(v, typeDesc)
}
return nil, ErrorUnsupportedMarshaling
}
func marshalBool(value bool, typeDesc TypeDesc) ([]byte, os.Error) {
switch typeDesc {
case BytesType, BooleanType:
b := make([]byte, 1)
if value {
b[0] = 1
}
return b, nil
case AsciiType, UTF8Type:
b := make([]byte, 1)
if value {
b[0] = '1'
} else {
b[0] = '0'
}
return b, nil
case LongType:
b := make([]byte, 8)
if value {
b[7] = 1
}
return b, nil
}
return nil, ErrorUnsupportedMarshaling
}
func marshalInt(value int64, size int, typeDesc TypeDesc) ([]byte, os.Error) {
switch typeDesc {
case LongType:
b := make([]byte, 8)
enc.BigEndian.PutUint64(b, uint64(value))
return b, nil
case BytesType:
b := make([]byte, 8)
enc.BigEndian.PutUint64(b, uint64(value))
return b[len(b)-size:], nil
case DateType:
if size != 8 {
return nil, ErrorUnsupportedMarshaling
}
b := make([]byte, 8)
enc.BigEndian.PutUint64(b, uint64(value))
return b, nil
case AsciiType, UTF8Type:
return marshalString(strconv.Itoa64(value), UTF8Type)
}
return nil, ErrorUnsupportedMarshaling
}
func marshalString(value string, typeDesc TypeDesc) ([]byte, os.Error) {
// let cassandra check the ascii-ness of the []byte
switch typeDesc {
case BytesType, AsciiType, UTF8Type:
return []byte(value), nil
case LongType:
i, err := strconv.Atoi64(value)
if err != nil {
return nil, err
}
return marshalInt(i, 8, LongType)
}
return nil, ErrorUnsupportedMarshaling
}
func marshalUUID(value UUID, typeDesc TypeDesc) ([]byte, os.Error) {
switch typeDesc {
case BytesType, UUIDType:
return []byte(value[:]), nil
}
return nil, ErrorUnsupportedMarshaling
}
func marshalFloat32(value float32, typeDesc TypeDesc) ([]byte, os.Error) {
switch typeDesc {
case BytesType, FloatType:
var b []byte
buf := bytes.NewBuffer(b)
enc.Write(buf, enc.BigEndian, value)
return buf.Bytes(), nil
/*
case DoubleType:
var valueD float64 = float64(value)
var b []byte
buf := bytes.NewBuffer(b)
enc.Write(buf, enc.BigEndian, valueD)
return buf.Bytes(), nil
*/
}
return nil, ErrorUnsupportedMarshaling
}
func marshalFloat64(value float64, typeDesc TypeDesc) ([]byte, os.Error) {
switch typeDesc {
case BytesType, DoubleType:
var b []byte
buf := bytes.NewBuffer(b)
enc.Write(buf, enc.BigEndian, value)
return buf.Bytes(), nil
/*
case FloatType:
var valueF float32 = float32(value)
var b []byte
buf := bytes.NewBuffer(b)
enc.Write(buf, enc.BigEndian, valueF)
return buf.Bytes(), nil
*/
}
return nil, ErrorUnsupportedMarshaling
}
func Unmarshal(b []byte, typeDesc TypeDesc, value interface{}) os.Error {
switch v := value.(type) {
case *[]byte:
*v = b
return nil
case *bool:
return unmarshalBool(b, typeDesc, v)
case *string:
return unmarshalString(b, typeDesc, v)
case *int8:
return unmarshalInt8(b, typeDesc, v)
case *int16:
return unmarshalInt16(b, typeDesc, v)
case *int:
var vt int32
err := unmarshalInt32(b, typeDesc, &vt)
if err == nil {
*v = int(vt)
}
return err
case *int32:
return unmarshalInt32(b, typeDesc, v)
case *int64:
return unmarshalInt64(b, typeDesc, v)
case *UUID:
return unmarshalUUID(b, typeDesc, v)
case *float32:
return unmarshalFloat32(b, typeDesc, v)
case *float64:
return unmarshalFloat64(b, typeDesc, v)
}
return ErrorUnsupportedNativeTypeUnmarshaling
}
func unmarshalBool(b []byte, typeDesc TypeDesc, value *bool) os.Error {
switch typeDesc {
case BytesType, BooleanType:
if len(b) < 1 {
return ErrorCassandraTypeSerializationUnmarshaling
}
if b[0] == 0 {
*value = false
} else {
*value = true
}
return nil
case AsciiType, UTF8Type:
if len(b) < 1 {
return ErrorCassandraTypeSerializationUnmarshaling
}
if b[0] == '0' {
*value = false
} else {
*value = true
}
return nil
case LongType:
if len(b) != 8 {
return ErrorCassandraTypeSerializationUnmarshaling
}
if b[7] == 0 {
*value = false
} else {
*value = true
}
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalInt64(b []byte, typeDesc TypeDesc, value *int64) os.Error {
switch typeDesc {
case LongType, BytesType, DateType, CounterColumnType:
if len(b) != 8 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int64(enc.BigEndian.Uint64(b))
return nil
case AsciiType, UTF8Type:
var r string
err := unmarshalString(b, AsciiType, &r)
if err != nil {
return err
}
*value, err = strconv.Atoi64(r)
if err != nil {
return err
}
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalInt32(b []byte, typeDesc TypeDesc, value *int32) os.Error {
switch typeDesc {
case LongType:
if len(b) != 8 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int32(enc.BigEndian.Uint64(b))
return nil
case BytesType:
if len(b) != 4 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int32(enc.BigEndian.Uint32(b))
return nil
case AsciiType, UTF8Type:
var r string
err := unmarshalString(b, AsciiType, &r)
if err != nil {
return err
}
i, err := strconv.Atoi(r)
if err != nil {
return err
}
*value = int32(i)
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalInt16(b []byte, typeDesc TypeDesc, value *int16) os.Error {
switch typeDesc {
case LongType:
if len(b) != 8 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int16(enc.BigEndian.Uint64(b))
return nil
case BytesType:
if len(b) != 2 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int16(enc.BigEndian.Uint16(b))
return nil
case AsciiType, UTF8Type:
var r string
err := unmarshalString(b, AsciiType, &r)
if err != nil {
return err
}
i, err := strconv.Atoi(r)
if err != nil {
return err
}
*value = int16(i)
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalInt8(b []byte, typeDesc TypeDesc, value *int8) os.Error {
switch typeDesc {
case LongType:
if len(b) != 8 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int8(b[7])
return nil
case BytesType:
if len(b) != 1 {
return ErrorCassandraTypeSerializationUnmarshaling
}
*value = int8(b[0])
return nil
case AsciiType, UTF8Type:
var r string
err := unmarshalString(b, AsciiType, &r)
if err != nil {
return err
}
i, err := strconv.Atoi(r)
if err != nil {
return err
}
*value = int8(i)
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalString(b []byte, typeDesc TypeDesc, value *string) os.Error {
switch typeDesc {
case BytesType, AsciiType, UTF8Type:
*value = string(b)
return nil
case LongType:
var i int64
err := unmarshalInt64(b, LongType, &i)
if err != nil {
return err
}
*value = strconv.Itoa64(i)
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalUUID(b []byte, typeDesc TypeDesc, value *UUID) os.Error {
switch typeDesc {
case BytesType, UUIDType:
if len(b) != 16 {
return ErrorCassandraTypeSerializationUnmarshaling
}
copy((*value)[:], b)
return nil
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalFloat32(b []byte, typeDesc TypeDesc, value *float32) os.Error {
switch typeDesc {
case BytesType, FloatType:
buf := bytes.NewBuffer(b)
enc.Read(buf, enc.BigEndian, value)
return nil
/*
case DoubleType:
var valueD float64
buf := bytes.NewBuffer(b)
enc.Read(buf, enc.BigEndian, &valueD)
*value = float32(valueD)
return nil
*/
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
func unmarshalFloat64(b []byte, typeDesc TypeDesc, value *float64) os.Error {
switch typeDesc {
case BytesType, DoubleType:
buf := bytes.NewBuffer(b)
enc.Read(buf, enc.BigEndian, value)
return nil
/*
case FloatType:
var valueF float32
buf := bytes.NewBuffer(b)
enc.Read(buf, enc.BigEndian, &valueF)
*value = float64(valueF)
return nil
*/
}
return ErrorUnsupportedCassandraTypeUnmarshaling
}
type TypeClass struct {
Desc TypeDesc
Components []TypeClass
}
func parseTypeDesc(cassType string) TypeDesc {
switch cassType {
case "BytesType", "org.apache.cassandra.db.marshal.BytesType":
return BytesType
case "AsciiType", "org.apache.cassandra.db.marshal.AsciiType":
return AsciiType
case "UTF8Type", "org.apache.cassandra.db.marshal.UTF8Type":
return UTF8Type
case "LongType", "org.apache.cassandra.db.marshal.LongType":
return LongType
case "IntegerType", "org.apache.cassandra.db.marshal.IntegerType":
return IntegerType
case "DecimalType", "org.apache.cassandra.db.marshal.DecimalType":
return DecimalType
case "UUIDType", "org.apache.cassandra.db.marshal.UUIDType":
return UUIDType
case "BooleanType", "org.apache.cassandra.db.marshal.BooleanType":
return BooleanType
case "FloatType", "org.apache.cassandra.db.marshal.FloatType":
return FloatType
case "DoubleType", "org.apache.cassandra.db.marshal.DoubleType":
return DoubleType
case "DateType", "org.apache.cassandra.db.marshal.DateType":
return DateType
case "CounterColumnType", "org.apache.cassandra.db.marshal.CounterColumnType":
return CounterColumnType
}
return BytesType
}
func parseTypeClass(cassType string) TypeClass {
r := TypeClass{Desc: BytesType}
// check for composite and parse it
if strings.HasPrefix(cassType, "org.apache.cassandra.db.marshal.CompositeType(") {
r.Desc = CompositeType
componentsString := cassType[strings.Index(cassType, "(")+1 : len(cassType)-1]
componentsSlice := strings.Split(componentsString, ",")
var components []TypeClass
for _, component := range componentsSlice {
components = append(components, parseTypeClass(component))
}
r.Components = components
return r
}
r.Desc = parseTypeDesc(cassType)
return r
}
func packComposite(current, component []byte, comparator, sliceStart, inclusive bool) []byte {
var eoc byte = 0
if comparator {
if inclusive {
if sliceStart {
eoc = 0xff
} else {
eoc = 0x01
}
} else {
if sliceStart {
eoc = 0x01
} else {
eoc = 0xff
}
}
}
r := make([]byte, 2)
enc.BigEndian.PutUint16(r, uint16(len(component)))
r = append(current, r...)
r = append(r, component...)
return append(r, eoc)
}
func unpackComposite(composite []byte) [][]byte {
components := make([][]byte, 0)
for len(composite) > 0 {
l := enc.BigEndian.Uint16(composite[:2])
components = append(components, composite[2:2+l])
composite = composite[3+l:]
}
return components
}