forked from mstump/golang-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
498 lines (433 loc) · 12.6 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
package cassandra
// #cgo LDFLAGS: -L/usr/local/lib -lcassandra
// #cgo CFLAGS: -I/usr/local/include
// #include <stdlib.h>
// #include <cassandra.h>
import "C"
import (
"encoding/hex"
"fmt"
"math"
"math/big"
"strconv"
"strings"
"time"
)
// A CassType represents a specific Cassandra data type.
// Collection types (list, set, map, tuple, UDTs) have subtypes
// specifying the data type of their elements.
type CassType struct {
primary int
subtypes []CassType
}
// Predefined CassTypes for all known Cassandra data types.
var (
CUnknown = newCassType(CASS_VALUE_TYPE_UNKNOWN)
CAscii = newCassType(CASS_VALUE_TYPE_ASCII)
CBigInt = newCassType(CASS_VALUE_TYPE_BIGINT)
CBlob = newCassType(CASS_VALUE_TYPE_BLOB)
CBoolean = newCassType(CASS_VALUE_TYPE_BOOLEAN)
CDecimal = newCassType(CASS_VALUE_TYPE_DECIMAL)
CDouble = newCassType(CASS_VALUE_TYPE_DOUBLE)
CFloat = newCassType(CASS_VALUE_TYPE_FLOAT)
CInt = newCassType(CASS_VALUE_TYPE_INT)
CText = newCassType(CASS_VALUE_TYPE_TEXT)
CTimestamp = newCassType(CASS_VALUE_TYPE_TIMESTAMP)
CUuid = newCassType(CASS_VALUE_TYPE_UUID)
CVarchar = newCassType(CASS_VALUE_TYPE_VARCHAR)
CVarint = newCassType(CASS_VALUE_TYPE_VARINT)
CTimeuuid = newCassType(CASS_VALUE_TYPE_TIMEUUID)
CInet = newCassType(CASS_VALUE_TYPE_INET)
CDate = newCassType(CASS_VALUE_TYPE_DATE)
CTime = newCassType(CASS_VALUE_TYPE_TIME)
CSmallInt = newCassType(CASS_VALUE_TYPE_SMALL_INT)
CTinyInt = newCassType(CASS_VALUE_TYPE_TINY_INT)
// collections
CList = newCassType(CASS_VALUE_TYPE_LIST)
CSet = newCassType(CASS_VALUE_TYPE_SET)
CMap = newCassType(CASS_VALUE_TYPE_MAP)
CTuple = newCassType(CASS_VALUE_TYPE_TUPLE)
CUdt = newCassType(CASS_VALUE_TYPE_UDT)
)
// Specialize a collection type (list, set, map, tuple) with the
// type(s) of its elements
func (ct CassType) Specialize(subTypes ...CassType) CassType {
return CassType{primary: ct.primary, subtypes: subTypes}
}
func (ct CassType) String() string {
switch ct.primary {
case CASS_VALUE_TYPE_LIST:
if len(ct.subtypes) > 0 {
return fmt.Sprintf("list<%s>", ct.subtypes[0].String())
}
return "list<?>"
case CASS_VALUE_TYPE_SET:
if len(ct.subtypes) > 0 {
return fmt.Sprintf("set<%s>", ct.subtypes[0].String())
}
return "set<?>"
case CASS_VALUE_TYPE_MAP:
if len(ct.subtypes) > 1 {
return fmt.Sprintf("map<%s, %s>", ct.subtypes[0].String(),
ct.subtypes[1].String())
}
return "map<?, ?>"
case CASS_VALUE_TYPE_TUPLE:
names := make([]string, len(ct.subtypes))
for i, st := range ct.subtypes {
names[i] = st.String()
}
return fmt.Sprintf("tuple<%s>", strings.Join(names, ", "))
case CASS_VALUE_TYPE_ASCII:
return "ascii"
case CASS_VALUE_TYPE_TEXT:
return "text"
case CASS_VALUE_TYPE_VARCHAR:
return "varchar"
case CASS_VALUE_TYPE_BIGINT:
return "bigint"
case CASS_VALUE_TYPE_INT:
return "int"
case CASS_VALUE_TYPE_SMALL_INT:
return "smallint"
case CASS_VALUE_TYPE_TINY_INT:
return "tinyint"
case CASS_VALUE_TYPE_VARINT:
return "varint"
case CASS_VALUE_TYPE_BLOB:
return "blob"
case CASS_VALUE_TYPE_BOOLEAN:
return "boolean"
case CASS_VALUE_TYPE_COUNTER:
return "counter"
case CASS_VALUE_TYPE_DECIMAL:
return "decimal"
case CASS_VALUE_TYPE_DOUBLE:
return "double"
case CASS_VALUE_TYPE_FLOAT:
return "float"
case CASS_VALUE_TYPE_TIMESTAMP:
return "timestamp"
case CASS_VALUE_TYPE_DATE:
return "date"
case CASS_VALUE_TYPE_TIME:
return "time"
case CASS_VALUE_TYPE_UUID:
return "uuid"
case CASS_VALUE_TYPE_TIMEUUID:
return "timeuuid"
case CASS_VALUE_TYPE_INET:
return "inet"
case CASS_VALUE_TYPE_UDT:
return "udt"
case CASS_VALUE_TYPE_CUSTOM:
return "custom"
default:
return "UNKNOWN"
}
}
var Epoch = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
// Cassandra `timestamp` represents a date plus time,
// encoded as 8 bytes since epoch
type Timestamp struct {
secondsSinceEpoch int64
}
func NewTimestampFromTime(t time.Time) Timestamp {
return Timestamp{int64(t.Sub(Epoch).Seconds())}
}
func NewTimestamp(secondsFromEpoch int64) Timestamp {
return Timestamp{secondsFromEpoch}
}
func (t Timestamp) Time() time.Time {
return Epoch.Add(time.Duration(t.secondsSinceEpoch) * time.Second)
}
func (t Timestamp) String() string {
return t.Time().String()
}
func (t Timestamp) NativeString() string {
// FIXME: should return the value as represented in CQL
return fmt.Sprintf("%d", t.secondsSinceEpoch)
}
func (t Timestamp) Raw() int64 {
return t.secondsSinceEpoch
}
// Cassandra Date is a 32-bit unsigned integer representing
// the number of days with Epoch (1970-1-1) at the center of the range
type Date struct {
days uint32
}
// Create a new Date.
func NewDate(year int, month time.Month, day int) Date {
date := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
d := date.Sub(Epoch)
var days int64 = int64(d.Hours()) / 24
var udays = uint32(days + math.MaxInt32 + 1)
return Date{udays}
}
var datePatterns = []string{
"2006-01-02",
"2006-01-2",
"2006-1-02",
"2006-1-2",
}
// Creates a new Date value from the string representation.
// The accepted formats are:
// * yyyy-mm-dd
// * yyyy-m-d
// * yyyy-mm-d
// * yyyy-m-dd
// If the value cannot be parsed to a valid date, this function
// return a non-nil error
func ParseDate(s string) (Date, error) {
var t time.Time
var err error
for _, p := range datePatterns {
t, err = time.Parse(p, s)
if err == nil {
break
}
}
if err != nil {
return Date{}, err
}
return NewDate(t.Year(), t.Month(), t.Day()), nil
}
// Only the year, month, day part are set in the returned time.Time
func (d Date) Time() time.Time {
var v int64 = int64(d.days) - math.MaxInt32 - 1
return Epoch.Add(time.Duration(v*24) * time.Hour)
}
func (d Date) Raw() uint32 {
return d.days
}
func (d Date) String() string {
return d.Time().Format("2006-01-02")
}
// Returns a representation that can be used directly in CQL
func (d Date) NativeString() string {
return fmt.Sprintf("'%d'", d.days)
}
// Cassandra `time` type represents a time of day
// with no date (and no notion of time zone)
type Time int64
var nanosInADay int64 = 24 * int64(time.Hour)
func NewTime(hours, minutes, seconds, nanos uint) (Time, error) {
var nanotime int64 = int64(hours)*int64(time.Hour) +
int64(minutes)*int64(time.Minute) +
int64(seconds)*int64(time.Second) +
int64(nanos)
if nanotime < 0 || nanotime > nanosInADay {
return 0, fmt.Errorf("Time value must be bigger than 0 and less than the number of nanoseconds in a day (%d)", nanosInADay)
}
return Time(nanotime), nil
}
func ParseTime(str string) (Time, error) {
parts := strings.Split(str, ".")
hms := strings.Split(parts[0], ":")
if len(hms) != 3 {
return 0, fmt.Errorf("Time must be in format hh:mm:ss.nnnnnnnnn")
}
var nanotime int64
n, err := strconv.ParseInt(hms[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("Time must be in format hh:mm:ss.nnnnnnnnn")
}
nanotime += n * int64(time.Hour)
n, err = strconv.ParseInt(hms[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("Time must be in format hh:mm:ss.nnnnnnnnn")
}
nanotime += n * int64(time.Minute)
n, err = strconv.ParseInt(hms[2], 10, 64)
if err != nil {
return 0, fmt.Errorf("Time must be in format hh:mm:ss.nnnnnnnnn")
}
nanotime += n * int64(time.Second)
if len(parts) > 1 {
padded := parts[1] + strings.Repeat("0", (9-len(parts[1])))
fmt.Printf(padded)
n, err = strconv.ParseInt(padded, 10, 64)
if err != nil {
return 0, fmt.Errorf("Time must be in format hh:mm:ss.nnnnnnnnn")
}
nanotime += n
}
return Time(nanotime), nil
}
func (t Time) Hours() uint {
return uint(math.Floor(float64(t) / float64(time.Hour)))
}
func (t Time) Minutes() uint {
minutes := math.Floor(float64(t) / float64(time.Minute))
return uint(math.Remainder(minutes, 60))
}
func (t Time) Seconds() uint {
seconds := math.Floor(float64(t) / float64(time.Second))
return uint(math.Remainder(seconds, 60))
}
func (t Time) Nanoseconds() uint {
return uint(math.Remainder(float64(t), float64(time.Second)))
}
func (t Time) Raw() int64 {
return int64(t)
}
func (t Time) String() string {
return fmt.Sprintf("%02d:%02d:%02d.%d", t.Hours(), t.Minutes(), t.Seconds(),
t.Nanoseconds())
}
func (t Time) NativeString() string {
return fmt.Sprintf("'%s'", t.String())
}
// Used to represent both a Cassandra `uuid` (UUID v4)
// and `timeuuid` (UUID v1).
//
// Inspired by:
// * https://github.com/satori/go.uuid
// * https://github.com/pborman/uuid
type UUID [16]byte
func ParseUUID(s string) (uuid UUID, err error) {
if len(s) != 36 {
return uuid, fmt.Errorf("Invalid UUID format: %s", s)
}
for _, idx := range []int{8, 13, 18, 23} {
if s[idx] != '-' {
return uuid, fmt.Errorf("Invalid UUID format: %s", s)
}
}
b := []byte(s)
u := uuid[:]
_, err = hex.Decode(u[0:4], b[0:8])
_, err = hex.Decode(u[4:6], b[9:13])
_, err = hex.Decode(u[6:8], b[14:18])
_, err = hex.Decode(u[8:10], b[19:23])
_, err = hex.Decode(u[10:], b[24:])
return
}
func (u UUID) Version() uint {
return uint(u[6] >> 4)
}
func (u UUID) String() string {
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
u[:4], u[4:6], u[6:8], u[8:10], u[10:])
}
func (u UUID) NativeString() string {
return u.String()
}
func (u UUID) Raw() [16]byte {
return [16]byte(u)
}
// Utility method allowing to mark an array, slice, or map as a value
// to be written as Cassandra `set` from simple statements (in which
// automatic conversions wouldn't recognize those types as potential
// sets without incurring a significant penalty)
func Set(val interface{}) setmarker {
return setmarker{val}
}
type setmarker struct {
value interface{}
}
// A Decimal type corresponding to the Cassandra decimal data type.
// The internal representation of the decimal is an arbitrary precision
// integer unscaled balue and a 32-bit integer scale. Thus the value
// is equal to (unscaled * 10 ^ (-scale))
type Decimal struct {
Value *big.Int
Scale int32
}
func ParseDecimal(val string) (*Decimal, error) {
if val == "" {
return nil, fmt.Errorf("val must non empty")
}
parts := strings.Split(val, ".")
if len(parts) > 2 {
return nil, fmt.Errorf("%s is not a valid decimal (too many .)", val)
}
scale := 0
if len(parts) == 2 {
scale = len(parts[1])
}
if scale < math.MinInt32 || scale > math.MaxInt32 {
return nil, fmt.Errorf("%s is not a valid decimal (fractional part too large)", val)
}
bigint := big.NewInt(0)
bigint, ok := bigint.SetString(strings.Join(parts, ""), 10)
if !ok {
return nil, fmt.Errorf("val is not a valid decimal (%s)", val)
}
return &Decimal{bigint, int32(scale)}, nil
}
func NewDecimal(val int64, scale int32) *Decimal {
return &Decimal{big.NewInt(val), scale}
}
func (d Decimal) String() string {
s := d.Value.String()
pos := len(s) - int(d.Scale)
return fmt.Sprintf("%s.%s", s[0:pos], s[pos:])
}
func (d Decimal) NativeString() string {
return d.String()
}
// A Tuple type corresponding to the Cassandra tuple data type.
type Tuple struct {
kind CassType
values []interface{}
}
func NewTuple(t CassType, args ...interface{}) *Tuple {
tuple := new(Tuple)
tuple.kind = t
tuple.values = make([]interface{}, len(t.subtypes))
if len(args) > 0 {
copy(tuple.values, args)
}
return tuple
}
func (tuple Tuple) Kind() CassType {
return tuple.kind
}
func (tuple Tuple) Values() []interface{} {
return tuple.values
}
func (tuple Tuple) Len() int {
return len(tuple.kind.subtypes)
}
// Sets the value at the given index and returns the same
// pointer to the Tuple so multiple Set operations can be chained.
// This method panics if the provided index falls outside the length
// of the tuple
func (tuple *Tuple) Set(index int, value interface{}) *Tuple {
if index < 0 || index >= tuple.Len() {
panic(fmt.Sprintf("Cannot set value at index %d in a tuple %s which has only %d values",
index, tuple.kind.String(), tuple.Len()))
}
tuple.values[index] = value
return tuple
}
func (tuple *Tuple) SetValues(values ...interface{}) error {
if tuple.Len() < len(values) {
return fmt.Errorf("Cannot set %d values in a tuple %s which has only %d values",
len(values), tuple.kind.String(), tuple.Len())
}
copy(tuple.values, values)
return nil
}
func (tuple Tuple) Get(index int) interface{} {
if index < 0 || index >= tuple.Len() {
panic(fmt.Sprintf("Tuple %s has only %d values", tuple.kind.String(), tuple.Len()))
}
return tuple.values[index]
}
func (tuple Tuple) String() string {
names := make([]string, tuple.Len())
for i, st := range tuple.kind.subtypes {
names[i] = "%v " + st.String()
}
format := "tuple<" + strings.Join(names, ", ") + ">"
return fmt.Sprintf(format, tuple.values...)
}
func (tuple Tuple) NativeString() string {
if tuple.Len() == 0 {
return "()"
}
format := "(" + strings.Repeat("%v, ", tuple.Len()-1) + "%v)"
return fmt.Sprintf(format, tuple.values...)
}