@@ -16,6 +16,7 @@ use bytes::Bytes;
16
16
17
17
pub use auth_response:: AuthResponse ;
18
18
pub use batch:: Batch ;
19
+ pub use batch:: BatchV2 ;
19
20
pub use execute:: Execute ;
20
21
pub use options:: Options ;
21
22
pub use prepare:: Prepare ;
@@ -138,6 +139,7 @@ pub enum Request<'r> {
138
139
Query ( Query < ' r > ) ,
139
140
Execute ( Execute < ' r > ) ,
140
141
Batch ( Batch < ' r , BatchStatement < ' r > , Vec < SerializedValues > > ) ,
142
+ BatchV2 ( BatchV2 < ' r > ) ,
141
143
}
142
144
143
145
impl Request < ' _ > {
@@ -148,7 +150,7 @@ impl Request<'_> {
148
150
match opcode {
149
151
RequestOpcode :: Query => Query :: deserialize ( buf) . map ( Self :: Query ) ,
150
152
RequestOpcode :: Execute => Execute :: deserialize ( buf) . map ( Self :: Execute ) ,
151
- RequestOpcode :: Batch => Batch :: deserialize ( buf) . map ( Self :: Batch ) ,
153
+ RequestOpcode :: Batch => BatchV2 :: deserialize ( buf) . map ( Self :: BatchV2 ) ,
152
154
_ => unimplemented ! (
153
155
"Deserialization of opcode {:?} is not yet supported" ,
154
156
opcode
@@ -162,6 +164,7 @@ impl Request<'_> {
162
164
Request :: Query ( q) => Some ( q. parameters . consistency ) ,
163
165
Request :: Execute ( e) => Some ( e. parameters . consistency ) ,
164
166
Request :: Batch ( b) => Some ( b. consistency ) ,
167
+ Request :: BatchV2 ( b) => Some ( b. consistency ) ,
165
168
#[ allow( unreachable_patterns) ] // until other opcodes are supported
166
169
_ => None ,
167
170
}
@@ -173,6 +176,7 @@ impl Request<'_> {
173
176
Request :: Query ( q) => Some ( q. parameters . serial_consistency ) ,
174
177
Request :: Execute ( e) => Some ( e. parameters . serial_consistency ) ,
175
178
Request :: Batch ( b) => Some ( b. serial_consistency ) ,
179
+ Request :: BatchV2 ( b) => Some ( b. serial_consistency ) ,
176
180
#[ allow( unreachable_patterns) ] // until other opcodes are supported
177
181
_ => None ,
178
182
}
@@ -181,15 +185,15 @@ impl Request<'_> {
181
185
182
186
#[ cfg( test) ]
183
187
mod tests {
184
- use std:: { borrow:: Cow , ops :: Deref } ;
188
+ use std:: borrow:: Cow ;
185
189
186
190
use bytes:: Bytes ;
187
191
188
192
use crate :: serialize:: row:: SerializedValues ;
189
193
use crate :: {
190
194
frame:: {
191
195
request:: {
192
- batch:: { Batch , BatchStatement , BatchType } ,
196
+ batch:: { BatchStatement , BatchType , BatchV2 } ,
193
197
execute:: Execute ,
194
198
query:: { Query , QueryParameters } ,
195
199
DeserializableRequest , SerializableRequest ,
@@ -261,32 +265,39 @@ mod tests {
261
265
}
262
266
263
267
// Batch
264
- let statements = vec ! [
265
- BatchStatement :: Query {
266
- text: query. contents,
267
- } ,
268
- BatchStatement :: Prepared {
269
- id: Cow :: Borrowed ( & execute. id) ,
270
- } ,
271
- ] ;
272
- let batch = Batch {
273
- statements : Cow :: Owned ( statements) ,
268
+ // Not execute's values, because named values are not supported in batches.
269
+ let mut statements_and_values = vec ! [ ] ;
270
+ BatchStatement :: Query {
271
+ text : query. contents ,
272
+ }
273
+ . serialize ( & mut statements_and_values)
274
+ . unwrap ( ) ;
275
+ statements_and_values
276
+ . extend_from_slice ( & query. parameters . values . element_count ( ) . to_be_bytes ( ) ) ;
277
+ statements_and_values. extend_from_slice ( query. parameters . values . get_contents ( ) ) ;
278
+
279
+ BatchStatement :: Prepared {
280
+ id : Cow :: Borrowed ( & execute. id ) ,
281
+ }
282
+ . serialize ( & mut statements_and_values)
283
+ . unwrap ( ) ;
284
+ statements_and_values
285
+ . extend_from_slice ( & query. parameters . values . element_count ( ) . to_be_bytes ( ) ) ;
286
+ statements_and_values. extend_from_slice ( query. parameters . values . get_contents ( ) ) ;
287
+
288
+ let batch = BatchV2 {
289
+ statements_and_values : Cow :: Owned ( statements_and_values) ,
274
290
batch_type : BatchType :: Logged ,
275
291
consistency : Consistency :: EachQuorum ,
276
292
serial_consistency : Some ( SerialConsistency :: LocalSerial ) ,
277
293
timestamp : Some ( 32432 ) ,
278
-
279
- // Not execute's values, because named values are not supported in batches.
280
- values : vec ! [
281
- query. parameters. values. deref( ) . clone( ) ,
282
- query. parameters. values. deref( ) . clone( ) ,
283
- ] ,
294
+ statements_len : 2 ,
284
295
} ;
285
296
{
286
297
let mut buf = Vec :: new ( ) ;
287
298
batch. serialize ( & mut buf) . unwrap ( ) ;
288
299
289
- let batch_deserialized = Batch :: deserialize ( & mut & buf[ ..] ) . unwrap ( ) ;
300
+ let batch_deserialized = BatchV2 :: deserialize ( & mut & buf[ ..] ) . unwrap ( ) ;
290
301
assert_eq ! ( & batch_deserialized, & batch) ;
291
302
}
292
303
}
@@ -341,24 +352,30 @@ mod tests {
341
352
}
342
353
343
354
// Batch
344
- let statements = vec ! [ BatchStatement :: Query {
355
+ let mut statements_and_values = vec ! [ ] ;
356
+ BatchStatement :: Query {
345
357
text : query. contents ,
346
- } ] ;
347
- let batch = Batch {
348
- statements : Cow :: Owned ( statements) ,
358
+ }
359
+ . serialize ( & mut statements_and_values)
360
+ . unwrap ( ) ;
361
+ statements_and_values
362
+ . extend_from_slice ( & query. parameters . values . element_count ( ) . to_be_bytes ( ) ) ;
363
+ statements_and_values. extend_from_slice ( query. parameters . values . get_contents ( ) ) ;
364
+
365
+ let batch = BatchV2 {
349
366
batch_type : BatchType :: Logged ,
350
367
consistency : Consistency :: EachQuorum ,
351
368
serial_consistency : None ,
352
369
timestamp : None ,
353
-
354
- values : vec ! [ query . parameters . values . deref ( ) . clone ( ) ] ,
370
+ statements_and_values : Cow :: Owned ( statements_and_values ) ,
371
+ statements_len : 1 ,
355
372
} ;
356
373
{
357
374
let mut buf = Vec :: new ( ) ;
358
375
batch. serialize ( & mut buf) . unwrap ( ) ;
359
376
360
377
// Sanity check: batch deserializes to the equivalent.
361
- let batch_deserialized = Batch :: deserialize ( & mut & buf[ ..] ) . unwrap ( ) ;
378
+ let batch_deserialized = BatchV2 :: deserialize ( & mut & buf[ ..] ) . unwrap ( ) ;
362
379
assert_eq ! ( batch, batch_deserialized) ;
363
380
364
381
// Now modify flags by adding an unknown one.
@@ -370,7 +387,7 @@ mod tests {
370
387
371
388
// Unknown flag should lead to frame rejection, as unknown flags can be new protocol extensions
372
389
// leading to different semantics.
373
- let _parse_error = Batch :: deserialize ( & mut & buf[ ..] ) . unwrap_err ( ) ;
390
+ let _parse_error = BatchV2 :: deserialize ( & mut & buf[ ..] ) . unwrap_err ( ) ;
374
391
}
375
392
}
376
393
}
0 commit comments