-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcql.lua
More file actions
1422 lines (1266 loc) · 37.1 KB
/
cql.lua
File metadata and controls
1422 lines (1266 loc) · 37.1 KB
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Implement Cassandra's native protocol v2/v3/v4
See:
- v2: https://github.com/apache/cassandra/blob/cassandra-2.2.7/doc/native_protocol_v2.spec
- v3: https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v3.spec
- v4: https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v4.spec
Notes:
- does not implement REGISTER messages
- does not implement SUPPORTED results parsing and OPTIONS messages
- does not implement EVENTS parsing
- does not implement compression
- does not set stream ids for frames
- does not implement no_metadata query flag and ROWS results flag
- does not implement decimal data type
- does not implement parsing of specific error codes
(unavailable/write_timeout/read_timeout/already_exists/etc...)
- v4: does not implement date/time/smallint/tinyint data types
- v4: does not implement custom payloads for custom QueryHandler
--]]
local bit = require 'bit'
local setmetatable = setmetatable
local tonumber = tonumber
local ipairs = ipairs
local pairs = pairs
local type = type
local insert = table.insert
local concat = table.concat
local ldexp = math.ldexp
local frexp = math.frexp
local floor = math.floor
local fmod = math.fmod
local huge = math.huge
local pow = math.pow
local gmatch = string.gmatch
local lower = string.lower
local char = string.char
local byte = string.byte
local gsub = string.gsub
local rep = string.rep
local sub = string.sub
local fmt = string.format
local band = bit.band
local bor = bit.bor
local Buffer = {}
local requests = {}
local frame_reader = {}
local cql_t_unset = {}
local cql_t_null = {}
local cql_types = {
custom = 0x00,
ascii = 0x01,
bigint = 0x02,
blob = 0x03,
boolean = 0x04,
counter = 0x05,
decimal = 0x06,
double = 0x07,
float = 0x08,
int = 0x09,
text = 0x0A,
timestamp = 0x0B,
uuid = 0x0C,
varchar = 0x0D,
varint = 0x0E,
timeuuid = 0x0F,
inet = 0x10,
list = 0x20,
map = 0x21,
set = 0x22,
udt = 0x30,
tuple = 0x31,
}
local consistencies = {
any = 0x0000,
one = 0x0001,
two = 0x0002,
three = 0x0003,
quorum = 0x0004,
all = 0x0005,
local_quorum = 0x0006,
each_quorum = 0x0007,
serial = 0x0008,
local_serial = 0x0009,
local_one = 0x000a,
}
local ERRORS = {
SERVER = 0x0000,
PROTOCOL = 0x000A,
BAD_CREDENTIALS = 0x0100,
UNAVAILABLE_EXCEPTION = 0x1000,
OVERLOADED = 0x1001,
IS_BOOTSTRAPPING = 0x1002,
TRUNCATE_ERROR = 0x1003,
WRITE_TIMEOUT = 0x1100,
READ_TIMEOUT = 0x1200,
READ_FAILURE = 0x1300,
FUNCTION_FAILURE = 0x1400,
WRITE_FAILURE = 0x1500,
SYNTAX_ERROR = 0x2000,
UNAUTHORIZED = 0x2100,
INVALID = 0x2200,
CONFIG_ERROR = 0x2300,
ALREADY_EXISTS = 0x2400,
UNPREPARED = 0x2500,
}
local ERROR_TRANSLATIONS = {
[ERRORS.SERVER] = 'Server error',
[ERRORS.PROTOCOL] = 'Protocol error',
[ERRORS.BAD_CREDENTIALS] = 'Bad credentials',
[ERRORS.UNAVAILABLE_EXCEPTION] = 'Unavailable exception',
[ERRORS.OVERLOADED] = 'Overloaded',
[ERRORS.IS_BOOTSTRAPPING] = 'Is bootstrapping',
[ERRORS.TRUNCATE_ERROR] = 'Truncate error',
[ERRORS.WRITE_TIMEOUT] = 'Write timeout',
[ERRORS.READ_TIMEOUT] = 'Read timeout',
[ERRORS.READ_FAILURE] = 'Read failure',
[ERRORS.FUNCTION_FAILURE] = 'Function failure',
[ERRORS.WRITE_FAILURE] = 'Write failure',
[ERRORS.SYNTAX_ERROR] = 'Syntax error',
[ERRORS.UNAUTHORIZED] = 'Unauthorized',
[ERRORS.INVALID] = 'Invalid',
[ERRORS.CONFIG_ERROR] = 'Config error',
[ERRORS.ALREADY_EXISTS] = 'Already exists',
[ERRORS.UNPREPARED] = 'Unprepared',
}
local OP_CODES = {
ERROR = 0x00,
STARTUP = 0x01,
READY = 0x02,
AUTHENTICATE = 0x03,
OPTIONS = 0x05,
SUPPORTED = 0x06,
QUERY = 0x07,
RESULT = 0x08,
PREPARE = 0x09,
EXECUTE = 0x0A,
REGISTER = 0x0B,
EVENT = 0x0C,
BATCH = 0x0D,
AUTH_CHALLENGE = 0x0E,
AUTH_RESPONSE = 0x0F,
AUTH_SUCCESS = 0x10,
}
local FRAME_FLAGS = {
--COMPRESSION = 0x01,
TRACING = 0x02,
--CUSTOM_PAYLOAD = 0x04,
WARNING = 0x08,
}
local function is_array(t)
if type(t) ~= "table" then return false end
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end
do
-- Buffer
-- @section buffer
Buffer.__index = Buffer
function Buffer.new(version, str)
str = str or ''
local buf = {
version = version,
str = str,
len = #str,
pos = 1,
}
return setmetatable(buf, Buffer)
end
function Buffer:write(bytes)
self.str = self.str..bytes
self.len = self.len + #bytes
self.pos = self.len
end
function Buffer:read(n)
n = n or self.len
if n < 0 then return end
local last_index = n ~= nil and self.pos + n - 1 or -1
local bytes = sub(self.str, self.pos, last_index)
self.pos = self.pos + #bytes
return bytes
end
function Buffer:reset()
self.pos = 1
end
function Buffer:get()
return self.str
end
-- Utils
-- @section utils
local function big_endian_representation(num, bytes)
if num < 0 then
-- 2's complement
num = pow(0x100, bytes) + num
end
local t = {}
while num > 0 do
local rest = fmod(num, 0x100)
insert(t, 1, char(rest))
num = (num-rest) / 0x100
end
local padding = rep('\0', bytes - #t)
return padding..concat(t)
end
local function string_to_number(str, signed)
local number = 0
local exponent = 1
for i = #str, 1, -1 do
number = number + byte(str, i) * exponent
exponent = exponent * 256
end
if signed and number > exponent / 2 then
-- 2's complement
number = number - exponent
end
return number
end
-- Raw types
-- @section raw_types
local function marsh_byte(val)
return char(val)
end
local function unmarsh_byte(buffer)
return byte(buffer:read(1))
end
local function marsh_int(val)
return big_endian_representation(val, 4)
end
local function marsh_unset()
return marsh_int(-2)
end
local function marsh_null()
return marsh_int(-1)
end
local function unmarsh_int(buffer)
return string_to_number(buffer:read(4), true)
end
local function marsh_long(val)
return big_endian_representation(val, 8)
end
local function unmarsh_long(buffer)
return string_to_number(buffer:read(8), true)
end
local function marsh_short(val)
return big_endian_representation(val, 2)
end
local function unmarsh_short(buffer)
return string_to_number(buffer:read(2), true)
end
local function marsh_string(val)
return marsh_short(#val)..val
end
local function unmarsh_string(buffer)
return buffer:read(buffer:read_short())
end
local function marsh_long_string(val)
return marsh_int(#val)..val
end
local function unmarsh_long_string(buffer)
return buffer:read(buffer:read_int())
end
local function marsh_bytes(val)
return marsh_int(#val)..val
end
local function unmarsh_bytes(buffer)
local n_bytes = buffer:read_int()
return buffer:read(n_bytes)
end
local function marsh_short_bytes(val)
return marsh_short(#val)..val
end
local function unmarsh_short_bytes(buffer)
return buffer:read(buffer:read_short())
end
local function marsh_uuid(val)
local repr = {}
local str = gsub(val, '-', '')
for i = 1, #str, 2 do
local byte_str = sub(str, i, i + 1)
repr[#repr+1] = marsh_byte(tonumber(byte_str, 16))
end
return concat(repr)
end
local function unmarsh_uuid(buffer)
local uuid = {}
for i = 1, 16 do
uuid[i] = fmt('%02x', buffer:read_byte())
end
insert(uuid, 5, '-')
insert(uuid, 8, '-')
insert(uuid, 11, '-')
insert(uuid, 14, '-')
return concat(uuid)
end
local function marsh_inet(val)
local t, hexadectets = {}, {}
local ip = gsub(lower(val), '::', ':0000:')
if val:match ':' then
-- ipv6
for hdt in gmatch(ip, '[%x]+') do
-- fill up hexadectets with 0 so all are 4 digits long
hexadectets[#hexadectets + 1] = rep('0', 4 - #hdt)..hdt
end
for i, hdt in ipairs(hexadectets) do
while hdt == '0000' and #hexadectets < 8 do
insert(hexadectets, i + 1, '0000')
end
for j = 1, 4, 2 do
t[#t+1] = marsh_byte(tonumber(sub(hdt, j, j + 1), 16))
end
end
else
-- ipv4
for d in gmatch(val, '(%d+)') do
t[#t+1] = marsh_byte(d)
end
end
return concat(t)
end
local function unmarsh_inet(buffer)
local bytes = buffer:get()
local buf = {}
if #bytes == 16 then
-- ipv6
for i = 1, #bytes, 2 do
buf[#buf+1] = fmt('%02x', byte(bytes, i))..fmt('%02x', byte(bytes, i+1))
end
return concat(buf, ':')
else
-- ipv4
for i = 1, #bytes do
buf[#buf+1] = fmt('%d', byte(bytes, i))
end
return concat(buf, '.')
end
end
local function marsh_string_list(val)
local t = {}
local n = 0
for k, v in pairs(val) do
n = n + 1
t[#t+1] = marsh_string(v)
end
insert(t, 1, marsh_short(n))
return concat(t)
end
local function unmarsh_string_list(buffer)
local list = {}
local n = buffer:read_short()
for _ = 1, n do
list[#list+1] = buffer:read_string()
end
return list
end
local function marsh_string_map(val)
local t = {}
local n = 0
for k, v in pairs(val) do
n = n + 1
t[#t+1] = marsh_string(k)
t[#t+1] = marsh_string(v)
end
insert(t, 1, marsh_short(n))
return concat(t)
end
local function unmarsh_string_map(buffer)
local map = {}
local n = buffer:read_short()
for _ = 1, n do
local key = buffer:read_string()
local value = buffer:read_string()
map[key] = value
end
return map
end
local function marsh_string_multimap(val)
local t = {}
local n = 0
for k, v in pairs(val) do
n = n + 1
t[#t+1] = marsh_string(k)
t[#t+1] = marsh_string_list(v)
end
insert(t, 1, marsh_short(n))
return concat(t)
end
local function unmarsh_string_multimap(buffer)
local multimap = {}
local n = buffer:read_short()
for _ = 1, n do
local key = buffer:read_string()
multimap[key] = buffer:read_string_list()
end
return multimap
end
local function unmarsh_udt_type(buffer)
local udt_ks_name = buffer:read_string()
local udt_name = buffer:read_string()
local n = buffer:read_short()
local fields = {}
for _ = 1, n do
fields[#fields+1] = {
name = buffer:read_string(),
type = buffer:read_options()
}
end
return {
udt_keyspace = udt_ks_name,
udt_name = udt_name,
fields = fields
}
end
local function unmarsh_tuple_type(buffer)
local n = buffer:read_short()
local fields = {}
for _ = 1, n do
fields[#fields+1] = buffer:read_options()
end
return {fields = fields}
end
do
local marshallers = {
byte = {marsh_byte, unmarsh_byte},
int = {marsh_int, unmarsh_int},
long = {marsh_long, unmarsh_long},
short = {marsh_short, unmarsh_short},
string = {marsh_string, unmarsh_string},
long_string = {marsh_long_string, unmarsh_long_string},
bytes = {marsh_bytes, unmarsh_bytes},
short_bytes = {marsh_short_bytes, unmarsh_short_bytes},
uuid = {marsh_uuid, unmarsh_uuid},
inet = {marsh_inet, unmarsh_inet},
string_map = {marsh_string_map, unmarsh_string_map},
string_list = {marsh_string_list, unmarsh_string_list},
string_multimap = {marsh_string_multimap, unmarsh_string_multimap},
udt_type = {nil, unmarsh_udt_type},
tuple_type = {nil, unmarsh_tuple_type}
}
for name, t in pairs(marshallers) do
local marshaller, unmarshaller = t[1], t[2]
if marshaller then -- skip udt/tuple
Buffer['write_'..name] = function(self, ...)
self:write(marshaller(...))
end
end
Buffer['read_'..name] = function(self)
return unmarshaller(self)
end
end
end
-- CQL types
-- @section cql_types
local function marsh_raw(val)
return val
end
local function unmarsh_raw(buffer)
return buffer:read()
end
local function marsh_bigint(val)
local first_byte = val >= 0 and 0 or 0xFF
return char(first_byte, -- only 53 bits from double
floor(val / 0x1000000000000) % 0x100,
floor(val / 0x10000000000) % 0x100,
floor(val / 0x100000000) % 0x100,
floor(val / 0x1000000) % 0x100,
floor(val / 0x10000) % 0x100,
floor(val / 0x100) % 0x100,
val % 0x100)
end
local function unmarsh_bigint(buffer)
local bytes = buffer:read(8)
local b1, b2, b3, b4, b5, b6, b7, b8 = byte(bytes, 1, 8)
if b1 < 0x80 then
return ((((((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4)
* 0x100 + b5) * 0x100 + b6) * 0x100 + b7) * 0x100 + b8
else
return ((((((((b1 - 0xFF) * 0x100 + (b2 - 0xFF)) * 0x100 + (b3 - 0xFF))
* 0x100 + (b4 - 0xFF)) * 0x100 + (b5 - 0xFF)) * 0x100 + (b6 - 0xFF))
* 0x100 + (b7 - 0xFF)) * 0x100 + (b8 - 0xFF)) - 1
end
end
local function marsh_boolean(val)
return marsh_byte(val and 1 or 0)
end
local function unmarsh_boolean(buffer)
return buffer:read_byte() == 1
end
local function marsh_double(val)
local sign = 0
if val < 0.0 then
sign = 0x80
val = -val
end
local mantissa, exponent = frexp(val)
if mantissa ~= mantissa then
return char(0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) -- nan
elseif mantissa == huge then
if sign == 0 then
return char(0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) -- +inf
else
return char(0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) -- -inf
end
elseif mantissa == 0.0 and exponent == 0 then
return char(sign, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) -- zero
end
exponent = exponent + 0x3FE
mantissa = (mantissa * 2.0 - 1.0) * ldexp(0.5, 53)
return char(sign + floor(exponent / 0x10),
(exponent % 0x10) * 0x10 + floor(mantissa / 0x1000000000000),
floor(mantissa / 0x10000000000) % 0x100,
floor(mantissa / 0x100000000) % 0x100,
floor(mantissa / 0x1000000) % 0x100,
floor(mantissa / 0x10000) % 0x100,
floor(mantissa / 0x100) % 0x100,
mantissa % 0x100)
end
local function unmarsh_double(buffer)
local bytes = buffer:read(8)
local b1, b2, b3, b4, b5, b6, b7, b8 = byte(bytes, 1, 8)
local sign = b1 > 0x7F
local exponent = (b1 % 0x80) * 0x10 + floor(b2 / 0x10)
local mantissa = ((((((b2 % 0x10) * 0x100 + b3) * 0x100 + b4) * 0x100 + b5)
* 0x100 + b6) * 0x100 + b7) * 0x100 + b8
if sign then
sign = -1
else
sign = 1
end
if mantissa == 0 and exponent == 0 then
return sign * 0.0
elseif exponent == 0x7FF then
if mantissa == 0 then
return sign * huge
else
return 0.0/0.0
end
end
return sign * ldexp(1.0 + mantissa / 0x10000000000000, exponent - 0x3FF)
end
local function marsh_float(val)
if val == 0 then
return char(0x00, 0x00, 0x00, 0x00)
elseif val ~= val then
return char(0xFF, 0xFF, 0xFF, 0xFF)
end
local sign = 0x00
if val < 0 then
sign = 0x80
val = -val
end
local mantissa, exponent = frexp(val)
exponent = exponent + 0x7F
if exponent <= 0 then
mantissa = ldexp(mantissa, exponent - 1)
exponent = 0
elseif exponent > 0 then
if exponent >= 0xFF then
return char(sign + 0x7F, 0x80, 0x00, 0x00)
elseif exponent == 1 then
exponent = 0
else
mantissa = mantissa * 2 - 1
exponent = exponent - 1
end
end
mantissa = floor(ldexp(mantissa, 23) + 0.5)
return char(sign + floor(exponent / 2),
(exponent % 2) * 0x80 + floor(mantissa / 0x10000),
floor(mantissa / 0x100) % 0x100,
mantissa % 0x100)
end
local function unmarsh_float(buffer)
local bytes = buffer:read(4)
local b1, b2, b3, b4 = byte(bytes, 1, 4)
local exponent = (b1 % 0x80) * 0x02 + floor(b2 / 0x80)
local mantissa = ldexp(((b2 % 0x80) * 0x100 + b3) * 0x100 + b4, -23)
if exponent == 0xFF then
if mantissa > 0 then
return 0 / 0
else
mantissa = huge
exponent = 0x7F
end
elseif exponent > 0 then
mantissa = mantissa + 1
else
exponent = exponent + 1
end
if b1 >= 0x80 then
mantissa = -mantissa
end
return ldexp(mantissa, exponent - 0x7F)
end
-- Nested CQL types
-- @section nested_cql_types
local marsh_cql_value
-- values must be ordered as they are defined in the UDT declaration
local function marsh_udt(val, version)
local repr = {}
for i = 1, #val do
repr[#repr+1] = marsh_cql_value(val[i], version)
end
return concat(repr)
end
local function unmarsh_udt(buffer, __cql_value_type)
local udt = {}
local fields = __cql_value_type.fields -- see unmarsh_udt_type
for n = 1, #fields do
local field = fields[n]
udt[field.name] = buffer:read_cql_value(field.type)
end
return udt
end
local marsh_tuple = marsh_udt
local function unmarsh_tuple(buffer, __cql_value_type)
local tuple = {}
local fields = __cql_value_type.fields -- see unmarsh_tuple_type
for n = 1, #fields do
tuple[#tuple+1] = buffer:read_cql_value(fields[n])
end
return tuple
end
local function marsh_set(val, version)
local repr
if version < 3 then
repr = {marsh_short(#val)}
else
repr = {marsh_int(#val)}
end
for i = 1, #val do
repr[#repr+1] = marsh_cql_value(val[i], version)
end
return concat(repr)
end
local function unmarsh_set(buffer, __cql_type_value)
local set, n = {}
if buffer.version < 3 then
n = buffer:read_short()
else
n = buffer:read_int()
end
for _ = 1, n do
set[#set+1] = buffer:read_cql_value(__cql_type_value)
end
return set
end
local function marsh_map(val, version)
local repr = {}
local size = 0
for k, v in pairs(val) do
repr[#repr+1] = marsh_cql_value(k, version)
repr[#repr+1] = marsh_cql_value(v, version)
size = size + 1
end
if version < 3 then
insert(repr, 1, marsh_short(size))
else
insert(repr, 1, marsh_int(size))
end
return concat(repr)
end
local function unmarsh_map(buffer, __cql_type_value)
local key_t, value_t = __cql_type_value[1], __cql_type_value[2]
local map = {}
local n
if buffer.version < 3 then
n = buffer:read_short()
else
n = buffer:read_int()
end
for _ = 1, n do
local key = buffer:read_cql_value(key_t)
map[key] = buffer:read_cql_value(value_t)
end
return map
end
function Buffer:read_options()
local cql_t, cql_t_val = self:read_short()
if cql_t == cql_types.set or cql_t == cql_types.list then
cql_t_val = self:read_options()
elseif cql_t == cql_types.map then
cql_t_val = {self:read_options(), self:read_options()}
elseif cql_t == cql_types.udt then
cql_t_val = unmarsh_udt_type(self)
elseif cql_t == cql_types.tuple then
cql_t_val = unmarsh_tuple_type(self)
end
return {
__cql_type = cql_t,
__cql_type_value = cql_t_val
}
end
-- CQL Marshalling
-- @section cql_marshalling
local cql_marshallers = {
-- custom = 0x00,
[cql_types.ascii] = marsh_raw,
[cql_types.bigint] = marsh_bigint,
[cql_types.blob] = marsh_raw,
[cql_types.boolean] = marsh_boolean,
[cql_types.counter] = marsh_bigint,
-- decimal 0x06
[cql_types.double] = marsh_double,
[cql_types.float] = marsh_float,
[cql_types.inet] = marsh_inet,
[cql_types.int] = marsh_int,
[cql_types.text] = marsh_raw,
[cql_types.list] = marsh_set,
[cql_types.map] = marsh_map,
[cql_types.set] = marsh_set,
[cql_types.uuid] = marsh_uuid,
[cql_types.timestamp] = marsh_bigint,
[cql_types.varchar] = marsh_raw,
[cql_types.varint] = marsh_int,
[cql_types.timeuuid] = marsh_uuid,
[cql_types.udt] = marsh_udt,
[cql_types.tuple] = marsh_tuple
}
marsh_cql_value = function(val, version)
if val == cql_t_unset then
return marsh_unset()
elseif val == cql_t_null then
return marsh_null()
end
local cql_t
local typ = type(val)
if typ == 'table' then
-- set by cassandra.uuid() or the likes
if val.__cql_type then
cql_t = val.__cql_type
val = val.val
elseif is_array(val) then
cql_t = cql_types.list
else
cql_t = cql_types.map
end
elseif typ == 'number' then
if floor(val) == val then
cql_t = cql_types.int
else
cql_t = cql_types.float
end
elseif typ == 'boolean' then
cql_t = cql_types.boolean
else
cql_t = cql_types.varchar
end
local marshaller = cql_marshallers[cql_t]
if not marshaller then
error('no marshaller for CQL type '..cql_t)
end
return marsh_bytes(marshaller(val, version))
end
function Buffer:write_cql_value(val)
self:write(marsh_cql_value(val, self.version))
end
-- CQL Unmarshalling
-- @section cql_unmarshalling
local cql_unmarshallers = {
-- custom = 0x00,
[cql_types.ascii] = unmarsh_raw,
[cql_types.bigint] = unmarsh_bigint,
[cql_types.blob] = unmarsh_raw,
[cql_types.boolean] = unmarsh_boolean,
[cql_types.counter] = unmarsh_bigint,
-- decimal 0x06
[cql_types.double] = unmarsh_double,
[cql_types.float] = unmarsh_float,
[cql_types.inet] = unmarsh_inet,
[cql_types.int] = unmarsh_int,
[cql_types.text] = unmarsh_raw,
[cql_types.list] = unmarsh_set,
[cql_types.map] = unmarsh_map,
[cql_types.set] = unmarsh_set,
[cql_types.uuid] = unmarsh_uuid,
[cql_types.timestamp] = unmarsh_bigint,
[cql_types.varchar] = unmarsh_raw,
[cql_types.varint] = unmarsh_int,
[cql_types.timeuuid] = unmarsh_uuid,
[cql_types.udt] = unmarsh_udt,
[cql_types.tuple] = unmarsh_tuple
}
-- Read a CQL value with a given CQL type
function Buffer:read_cql_value(t)
local unmarshaller = cql_unmarshallers[t.__cql_type]
if not unmarshaller then
error('no unmarshaller for CQL type '..t.__cql_type)
end
local bytes = self:read_bytes()
if bytes then
local buffer = Buffer.new(self.version, bytes)
return unmarshaller(buffer, t.__cql_type_value)
end
end
end -- do CQL encoding
-- CQL Requests
-- @section cql_requests
do
local CQL_VERSION = '3.0.0'
local QUERY_FLAGS = {
VALUES = 0x01,
--SKIP_METADATA = 0x02,
PAGE_SIZE = 0x04,
WITH_PAGING_STATE = 0x08,
WITH_SERIAL_CONSISTENCY = 0x10,
WITH_DEFAULT_TIMESTAMP = 0x20,
WITH_NAMES_FOR_VALUES = 0x40,
}
local request_mt = {}
request_mt.__index = request_mt
local function new_request(op_code)
return setmetatable({
retries = 0,
header = Buffer.new(),
body = Buffer.new(),
op_code = op_code
}, request_mt)
end
function request_mt:build_frame(version)
local header, body = Buffer.new(version), Buffer.new(version)
self:build_body(body) -- build body (depends on protocol version)
header:write_byte(version)
local flags = 0
if self.opts and self.opts.tracing then
flags = bor(flags, FRAME_FLAGS.TRACING)
end
header:write_byte(flags)
local stream_id = 0
if self.opts and self.opts.stream_id then
stream_id = self.opts.stream_id
end
if version < 3 then
header:write_byte(stream_id)
else
header:write_short(stream_id)
end
header:write_byte(self.op_code)
header:write_int(body.len)
return header:get()..body:get()
end
local default_opts = {consistency = consistencies.one}
local function build_args(body, args, opts)
opts = opts or default_opts
local flags = 0x00
local buf = Buffer.new(body.version)
if args then
flags = bor(flags, QUERY_FLAGS.VALUES)
if body.version >= 3 and opts.named then
flags = bor(flags, QUERY_FLAGS.WITH_NAMES_FOR_VALUES)
local n = 0
local args_buf = Buffer.new(body.version)
for name, val in pairs(args) do
n = n + 1
args_buf:write_string(name)
args_buf:write_cql_value(val)
end
buf:write_short(n)
buf:write(args_buf:get()) -- append args
else
buf:write_short(#args)
for i = 1, #args do
buf:write_cql_value(args[i])
end
end
end
if opts.page_size then
flags = bor(flags, QUERY_FLAGS.PAGE_SIZE)
buf:write_int(opts.page_size)
end
if opts.paging_state then
flags = bor(flags, QUERY_FLAGS.WITH_PAGING_STATE)
buf:write_bytes(opts.paging_state)