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
use {AsByte, FromSingleByte, IntoBytes};
use frame::*;
use frame::frame_query::QueryFlags;
use types::*;
use types::value::Value;
use consistency::Consistency;
#[derive(Debug, Clone)]
pub struct BodyReqBatch {
pub batch_type: BatchType,
pub queries: Vec<BatchQuery>,
pub consistency: Consistency,
pub query_flags: Vec<QueryFlags>,
pub serial_consistency: Option<Consistency>,
pub timestamp: Option<i64>,
}
impl IntoBytes for BodyReqBatch {
fn into_cbytes(&self) -> Vec<u8> {
let mut bytes = vec![];
bytes.push(self.batch_type.as_byte());
bytes.extend_from_slice(to_short(self.queries.len() as i16).as_slice());
bytes = self.queries
.iter()
.fold(bytes, |mut _bytes, q| {
_bytes.extend_from_slice(q.into_cbytes().as_slice());
_bytes
});
bytes.extend_from_slice(self.consistency.into_cbytes().as_slice());
let flag_byte = self.query_flags
.iter()
.fold(0, |mut _bytes, f| _bytes | f.as_byte());
bytes.push(flag_byte);
if let Some(ref serial_consistency) = self.serial_consistency {
bytes.extend_from_slice(serial_consistency.into_cbytes().as_slice());
}
if let Some(ref timestamp) = self.timestamp {
bytes.extend_from_slice(to_bigint(timestamp.clone()).as_slice());
}
bytes
}
}
#[derive(Debug, Clone)]
pub enum BatchType {
Logged,
Unlogged,
Counter,
}
impl FromSingleByte for BatchType {
fn from_byte(byte: u8) -> BatchType {
match byte {
0 => BatchType::Logged,
1 => BatchType::Unlogged,
2 => BatchType::Counter,
_ => unreachable!(),
}
}
}
impl AsByte for BatchType {
fn as_byte(&self) -> u8 {
match self {
&BatchType::Logged => 0,
&BatchType::Unlogged => 1,
&BatchType::Counter => 2,
}
}
}
#[derive(Debug, Clone)]
pub struct BatchQuery {
pub is_prepared: bool,
pub subject: BatchQuerySubj,
pub values: Vec<(Option<CString>, Value)>,
}
#[derive(Debug, Clone)]
pub enum BatchQuerySubj {
PreparedId(CBytesShort),
QueryString(CStringLong),
}
impl IntoBytes for BatchQuery {
fn into_cbytes(&self) -> Vec<u8> {
let mut bytes = vec![];
if self.is_prepared {
bytes.push(1);
} else {
bytes.push(0);
}
match self.subject {
BatchQuerySubj::PreparedId(ref s) => {
bytes.extend_from_slice(s.into_cbytes().as_slice());
}
BatchQuerySubj::QueryString(ref s) => {
bytes.extend_from_slice(s.into_cbytes().as_slice());
}
}
bytes.extend_from_slice(to_short(self.values.len() as i16).as_slice());
bytes = self.values
.iter()
.fold(bytes, |mut _bytes, v| {
if let Some(ref name) = v.0 {
_bytes.extend_from_slice(name.into_cbytes().as_slice());
}
_bytes.extend_from_slice(v.1.into_cbytes().as_slice());
_bytes
});
bytes
}
}
impl Frame {
pub fn new_req_batch(query: BodyReqBatch, flags: Vec<Flag>) -> Frame {
let version = Version::Request;
let stream: u64 = 0;
let opcode = Opcode::Batch;
Frame {
version: version,
flags: flags,
stream: stream,
opcode: opcode,
body: query.into_cbytes(),
tracing_id: None,
warnings: vec![],
}
}
}