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
use std::io::Cursor;
use {IntoBytes, FromBytes, FromCursor};
use types::*;
use types::rows::Row;
pub enum ResultKind {
Void,
Rows,
SetKeyspace,
Prepared,
SchemaChange
}
impl IntoBytes for ResultKind {
fn into_cbytes(&self) -> Vec<u8> {
return match *self {
ResultKind::Void => to_int(0x0001),
ResultKind::Rows => to_int(0x0002),
ResultKind::SetKeyspace => to_int(0x0003),
ResultKind::Prepared => to_int(0x0004),
ResultKind::SchemaChange => to_int(0x0005)
}
}
}
impl FromBytes for ResultKind {
fn from_bytes(bytes: Vec<u8>) -> ResultKind {
return match from_bytes(bytes.clone()) {
0x0001 => ResultKind::Void,
0x0002 => ResultKind::Rows,
0x0003 => ResultKind::SetKeyspace,
0x0004 => ResultKind::Prepared,
0x0005 => ResultKind::SchemaChange,
_ => unreachable!()
};
}
}
impl FromCursor for ResultKind {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> ResultKind {
return ResultKind::from_bytes(cursor_next_value(&mut cursor, INT_LEN as u64));
}
}
#[derive(Debug)]
pub enum ResResultBody {
Void(BodyResResultVoid),
Rows(BodyResResultRows),
SetKeyspace(BodyResResultSetKeyspace),
Prepared(BodyResResultPrepared),
SchemaChange(BodyResResultSchemaChange)
}
impl ResResultBody {
pub fn parse_body(mut cursor: &mut Cursor<Vec<u8>>, result_kind: ResultKind) -> ResResultBody {
return match result_kind {
ResultKind::Void => ResResultBody::Void(BodyResResultVoid::from_cursor(&mut cursor)),
ResultKind::Rows => ResResultBody::Rows(BodyResResultRows::from_cursor(&mut cursor)),
ResultKind::SetKeyspace => ResResultBody::SetKeyspace(BodyResResultSetKeyspace::from_cursor(&mut cursor)),
ResultKind::Prepared => ResResultBody::Prepared(BodyResResultPrepared::from_cursor(&mut cursor)),
ResultKind::SchemaChange => ResResultBody::SchemaChange(BodyResResultSchemaChange::from_cursor(&mut cursor))
};
}
pub fn parse_body_from_cursor(mut cursor: &mut Cursor<Vec<u8>>, result_kind: ResultKind) -> ResResultBody {
return match result_kind {
ResultKind::Void => ResResultBody::Void(BodyResResultVoid::from_cursor(&mut cursor)),
ResultKind::Rows => ResResultBody::Rows(BodyResResultRows::from_cursor(&mut cursor)),
ResultKind::SetKeyspace => ResResultBody::SetKeyspace(BodyResResultSetKeyspace::from_cursor(&mut cursor)),
_ => unimplemented!()
};
}
pub fn into_rows(self) -> Option<Vec<Row>> {
return match self {
ResResultBody::Rows(rows_body) => Some(Row::from_frame_body(rows_body)),
_ => None
}
}
}
impl FromCursor for ResResultBody {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> ResResultBody {
let result_kind = ResultKind::from_cursor(&mut cursor);
return ResResultBody::parse_body(&mut cursor, result_kind);
}
}
#[derive(Debug)]
pub struct BodyResResultVoid {}
impl BodyResResultVoid {
pub fn new() -> BodyResResultVoid {
return BodyResResultVoid {};
}
}
impl FromBytes for BodyResResultVoid {
fn from_bytes(_bytes: Vec<u8>) -> BodyResResultVoid {
return BodyResResultVoid::new();
}
}
impl FromCursor for BodyResResultVoid {
fn from_cursor(mut _cursor: &mut Cursor<Vec<u8>>) -> BodyResResultVoid {
return BodyResResultVoid::new();
}
}
#[derive(Debug)]
pub struct BodyResResultSetKeyspace {
pub body: CString
}
impl BodyResResultSetKeyspace {
pub fn new(body: CString) -> BodyResResultSetKeyspace {
return BodyResResultSetKeyspace {
body: body
}
}
}
impl FromCursor for BodyResResultSetKeyspace {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> BodyResResultSetKeyspace {
return BodyResResultSetKeyspace::new(CString::from_cursor(&mut cursor));
}
}
#[derive(Debug)]
pub struct BodyResResultRows {
pub metadata: RowsMetadata,
pub rows_count: CInt,
pub rows_content: Vec<Vec<CBytes>>
}
impl BodyResResultRows {
fn get_rows_content(mut cursor: &mut Cursor<Vec<u8>>,
rows_count: i32,
columns_count: i32) -> Vec<Vec<CBytes>> {
return (0..rows_count).map(|_| {
return (0..columns_count).map(|_| CBytes::from_cursor(&mut cursor) as CBytes).collect();
}).collect();
}
pub fn nth_row_val_types(&self, n: usize) -> Vec<(CBytes, ColType)> {
let col_types = self.metadata.col_specs
.iter()
.map(|col_spec| col_spec.col_type.id.clone());
return self.rows_content[n].iter()
.map(|cbyte| cbyte.clone())
.zip(col_types)
.collect();
}
}
impl FromCursor for BodyResResultRows {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> BodyResResultRows{
let metadata = RowsMetadata::from_cursor(&mut cursor);
let rows_count = CInt::from_cursor(&mut cursor);
let rows_content: Vec<Vec<CBytes>> = BodyResResultRows::get_rows_content(&mut cursor, rows_count, metadata.columns_count);
return BodyResResultRows {
metadata: metadata,
rows_count: rows_count,
rows_content: rows_content
};
}
}
#[derive(Debug, Clone)]
pub struct RowsMetadata {
pub flags: i32,
pub columns_count: i32,
pub paging_state: Option<CBytes>,
pub global_table_space: Option<Vec<CString>>,
pub col_specs: Vec<ColSpec>,
}
impl FromCursor for RowsMetadata {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> RowsMetadata {
let flags = CInt::from_cursor(&mut cursor);
let columns_count = CInt::from_cursor(&mut cursor);
let mut paging_state: Option<CBytes> = None;
if RowsMetadataFlag::has_has_more_pages(flags) {
paging_state = Some(CBytes::from_cursor(&mut cursor))
}
let mut global_table_space: Option<Vec<CString>> = None;
let has_global_table_space = RowsMetadataFlag::has_global_table_space(flags);
if has_global_table_space {
let keyspace = CString::from_cursor(&mut cursor);
let tablename = CString::from_cursor(&mut cursor);
global_table_space = Some(vec![keyspace, tablename])
}
let col_specs = ColSpec::parse_colspecs(&mut cursor, columns_count, has_global_table_space);
return RowsMetadata {
flags: flags,
columns_count: columns_count,
paging_state: paging_state,
global_table_space: global_table_space,
col_specs: col_specs
}
}
}
const GLOBAL_TABLE_SPACE: i32 = 0x0001;
const HAS_MORE_PAGES: i32 = 0x0002;
const NO_METADATA: i32 = 0x0004;
pub enum RowsMetadataFlag {
GlobalTableSpace,
HasMorePages,
NoMetadata
}
impl RowsMetadataFlag {
pub fn has_global_table_space(flag: i32) -> bool {
return (flag & GLOBAL_TABLE_SPACE) != 0;
}
pub fn set_global_table_space(flag: i32) -> i32 {
return flag | GLOBAL_TABLE_SPACE;
}
pub fn has_has_more_pages(flag: i32) -> bool {
return (flag & HAS_MORE_PAGES) != 0;
}
pub fn set_has_more_pages(flag: i32) -> i32 {
return flag | HAS_MORE_PAGES;
}
pub fn has_no_metadata(flag: i32) -> bool {
return (flag & NO_METADATA) != 0;
}
pub fn set_no_metadata(flag: i32) -> i32 {
return flag | NO_METADATA;
}
}
impl IntoBytes for RowsMetadataFlag {
fn into_cbytes(&self) -> Vec<u8> {
return match *self {
RowsMetadataFlag::GlobalTableSpace => to_int(GLOBAL_TABLE_SPACE as i64),
RowsMetadataFlag::HasMorePages => to_int(HAS_MORE_PAGES as i64),
RowsMetadataFlag::NoMetadata => to_int(NO_METADATA as i64)
};
}
}
impl FromBytes for RowsMetadataFlag {
fn from_bytes(bytes: Vec<u8>) -> RowsMetadataFlag {
return match from_bytes(bytes.clone()) as i32 {
GLOBAL_TABLE_SPACE => RowsMetadataFlag::GlobalTableSpace,
HAS_MORE_PAGES => RowsMetadataFlag::HasMorePages,
NO_METADATA => RowsMetadataFlag::NoMetadata,
_ => {
error!("Unexpected Cassandra rows metadata flag: {:?}", bytes);
panic!("Unexpected Cassandra rows metadata flag: {:?}", bytes);
}
};
}
}
#[derive(Debug, Clone)]
pub struct ColSpec {
pub ksname: Option<CString>,
pub tablename: Option<CString>,
pub name: CString,
pub col_type: ColTypeOption
}
impl ColSpec {
pub fn parse_colspecs(mut cursor: &mut Cursor<Vec<u8>>,
column_count: i32,
with_globale_table_spec: bool) -> Vec<ColSpec> {
return (0..column_count).map(|_| {
let mut ksname: Option<CString> = None;
let mut tablename: Option<CString> = None;
if !with_globale_table_spec {
ksname = Some(CString::from_cursor(&mut cursor));
tablename = Some(CString::from_cursor(&mut cursor));
}
let name = CString::from_cursor(&mut cursor);
let col_type = ColTypeOption::from_cursor(&mut cursor);
return ColSpec {
ksname: ksname,
tablename: tablename,
name: name,
col_type: col_type
};
}).collect();
}
}
#[derive(Debug, Clone)]
pub enum ColType {
Custom,
Ascii,
Bigint,
Blob,
Boolean,
Counter,
Decimal,
Double,
Float,
Int,
Timestamp,
Uuid,
Varchar,
Varint,
Timeuuid,
Inet,
Date,
Time,
Smallint,
Tinyint,
List,
Map,
Set,
Udt,
Tuple,
Null
}
impl FromBytes for ColType {
fn from_bytes(bytes: Vec<u8>) -> ColType {
return match from_bytes(bytes.clone()) {
0x0000 => ColType::Custom,
0x0001 => ColType::Ascii,
0x0002 => ColType::Bigint,
0x0003 => ColType::Blob,
0x0004 => ColType::Boolean,
0x0005 => ColType::Counter,
0x0006 => ColType::Decimal,
0x0007 => ColType::Double,
0x0008 => ColType::Float,
0x0009 => ColType::Int,
0x000B => ColType::Timestamp,
0x000C => ColType::Uuid,
0x000D => ColType::Varchar,
0x000E => ColType::Varint,
0x000F => ColType::Timeuuid,
0x0010 => ColType::Inet,
0x0011 => ColType::Date,
0x0012 => ColType::Time,
0x0013 => ColType::Smallint,
0x0014 => ColType::Tinyint,
0x0020 => ColType::List,
0x0021 => ColType::Map,
0x0022 => ColType::Set,
0x0030 => ColType::Udt,
0x0031 => ColType::Tuple,
_ => unreachable!()
};
}
}
impl FromCursor for ColType {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> ColType {
let option_id_bytes = cursor_next_value(&mut cursor, SHORT_LEN as u64);
let col_type = ColType::from_bytes(option_id_bytes);
return col_type;
}
}
#[derive(Debug, Clone)]
pub struct ColTypeOption {
pub id: ColType,
pub value: Option<ColTypeOptionValue>
}
impl FromCursor for ColTypeOption {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> ColTypeOption {
let id = ColType::from_cursor(&mut cursor);
let value = match id {
ColType::Custom => Some(ColTypeOptionValue::CString(CString::from_cursor(&mut cursor))),
ColType::Set => {
let col_type = ColTypeOption::from_cursor(&mut cursor);
Some(ColTypeOptionValue::CSet(Box::new(col_type)))
},
ColType::List => {
let col_type = ColTypeOption::from_cursor(&mut cursor);
Some(ColTypeOptionValue::CList(Box::new(col_type)))
},
ColType::Udt => Some(ColTypeOptionValue::UdtType(CUdt::from_cursor(&mut cursor))),
ColType::Map => {
let name_type = ColTypeOption::from_cursor(&mut cursor);
let value_type = ColTypeOption::from_cursor(&mut cursor);
Some(ColTypeOptionValue::CMap((Box::new(name_type), Box::new(value_type))))
}
_ => None
};
return ColTypeOption {
id: id,
value: value
}
}
}
#[derive(Debug, Clone)]
pub enum ColTypeOptionValue {
CString(CString),
ColType(ColType),
CSet(Box<ColTypeOption>),
CList(Box<ColTypeOption>),
UdtType(CUdt),
CMap((Box<ColTypeOption>, Box<ColTypeOption>))
}
#[derive(Debug, Clone)]
pub struct CUdt {
pub ks: CString,
pub udt_name: CString,
pub descriptions: Vec<(CString, ColTypeOption)>
}
impl FromCursor for CUdt {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CUdt {
let ks = CString::from_cursor(&mut cursor);
let udt_name = CString::from_cursor(&mut cursor);
let n = from_bytes(cursor_next_value(&mut cursor, SHORT_LEN as u64));
let descriptions: Vec<(CString, ColTypeOption)> = (0..n).map(|_| {
let name = CString::from_cursor(&mut cursor);
let col_type = ColTypeOption::from_cursor(&mut cursor);
return (name, col_type);
}).collect();
return CUdt {
ks: ks,
udt_name: udt_name,
descriptions: descriptions
}
}
}
#[derive(Debug)]
pub struct BodyResResultPrepared {
pub id: CBytesShort,
pub metadata: PreparedMetadata,
pub result_metadata: RowsMetadata
}
impl FromCursor for BodyResResultPrepared {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> BodyResResultPrepared {
let id = CBytesShort::from_cursor(&mut cursor);
let metadata = PreparedMetadata::from_cursor(&mut cursor);
let result_metadata = RowsMetadata::from_cursor(&mut cursor);
return BodyResResultPrepared {
id: id,
metadata: metadata,
result_metadata: result_metadata
};
}
}
#[derive(Debug)]
pub struct PreparedMetadata {
pub flags: i32,
pub columns_count: i32,
pub pk_count: i32,
pub pk_indexes: Vec<i16>,
pub global_table_spec: Option<(CString, CString)>,
pub col_specs: Vec<ColSpec>
}
impl FromCursor for PreparedMetadata {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> PreparedMetadata {
let flags = CInt::from_cursor(&mut cursor);
let columns_count = CInt::from_cursor(&mut cursor);
let pk_count = CInt::from_cursor(&mut cursor);
let pk_indexes: Vec<i16> = (0..pk_count)
.fold(vec![], |mut acc, _| {
let idx = from_bytes(cursor_next_value(&mut cursor, SHORT_LEN as u64)) as i16;
acc.push(idx);
return acc;
});
let mut global_table_space: Option<(CString, CString)> = None;
let has_global_table_space = RowsMetadataFlag::has_global_table_space(flags);
if has_global_table_space {
let keyspace = CString::from_cursor(&mut cursor);
let tablename = CString::from_cursor(&mut cursor);
global_table_space = Some((keyspace, tablename))
}
let col_specs = ColSpec::parse_colspecs(&mut cursor, columns_count, has_global_table_space);
return PreparedMetadata {
flags: flags,
columns_count: columns_count,
pk_count: pk_count,
pk_indexes: pk_indexes,
global_table_spec: global_table_space,
col_specs: col_specs
};
}
}
#[derive(Debug)]
pub struct BodyResResultSchemaChange {
pub change_type: ChangeType,
pub target: Target,
pub options: ChangeSchemeOptions
}
impl FromCursor for BodyResResultSchemaChange {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> BodyResResultSchemaChange {
let change_type = ChangeType::from_cursor(&mut cursor);
let target = Target::from_cursor(&mut cursor);
let options = ChangeSchemeOptions::from_cursor_and_target(&mut cursor, &target);
return BodyResResultSchemaChange {
change_type: change_type,
target: target,
options: options
}
}
}
#[derive(Debug)]
pub enum ChangeType {
Created,
Updated,
Dropped
}
impl FromCursor for ChangeType {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> ChangeType {
return match CString::from_cursor(&mut cursor).as_str() {
"CREATED" => ChangeType::Created,
"UPDATED" => ChangeType::Updated,
"DROPPED" => ChangeType::Dropped,
_ => unreachable!()
};
}
}
#[derive(Debug)]
pub enum Target {
Keyspace,
Table,
Type,
Function,
Aggregate
}
impl FromCursor for Target {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> Target {
return match CString::from_cursor(&mut cursor).as_str() {
"KEYSPACE" => Target::Keyspace,
"TABLE" => Target::Table,
"TYPE" => Target::Type,
"FUNCTION" => Target::Function,
"AGGREGATE" => Target::Aggregate,
_ => unreachable!()
};
}
}
#[derive(Debug)]
pub enum ChangeSchemeOptions {
Keyspace(String),
Table((String, String)),
Function((String, String, Vec<String>))
}
impl ChangeSchemeOptions {
fn from_cursor_and_target(mut cursor: &mut Cursor<Vec<u8>>, target: &Target)
-> ChangeSchemeOptions {
return match target {
&Target::Keyspace => ChangeSchemeOptions::from_cursor_keyspace(&mut cursor),
&Target::Table | &Target::Type => ChangeSchemeOptions::from_cursor_table(&mut cursor),
&Target::Function | &Target::Aggregate => ChangeSchemeOptions::from_cursor_function(&mut cursor)
};
}
fn from_cursor_keyspace(mut cursor: &mut Cursor<Vec<u8>>) -> ChangeSchemeOptions {
return ChangeSchemeOptions::Keyspace(CString::from_cursor(&mut cursor).into_plain());
}
fn from_cursor_table(mut cursor: &mut Cursor<Vec<u8>>) -> ChangeSchemeOptions {
let keyspace = CString::from_cursor(&mut cursor).into_plain();
let name = CString::from_cursor(&mut cursor).into_plain();
return ChangeSchemeOptions::Table((keyspace, name));
}
fn from_cursor_function(mut cursor: &mut Cursor<Vec<u8>>) -> ChangeSchemeOptions {
let keyspace = CString::from_cursor(&mut cursor).into_plain();
let name = CString::from_cursor(&mut cursor).into_plain();
let types = CStringList::from_cursor(&mut cursor).into_plain();
return ChangeSchemeOptions::Function((keyspace, name, types));
}
}