Skip to content

Commit

Permalink
connection: quietly prepare unprepared queries in batch before sending
Browse files Browse the repository at this point in the history
Before sending an unprepared statement in batch, quietly prepare it on one connection to obtain the information about the
bind markers. If the list of values to be bound is empty, we
just send it as an unprepared query.
  • Loading branch information
sylwiaszunejko committed Sep 21, 2023
1 parent e9af9a1 commit 27c733d
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions scylla/src/transport/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use crate::frame::{
request::{self, batch, execute, query, register, SerializableRequest},
response::{event::Event, result, NonErrorResponse, Response, ResponseOpcode},
server_event_type::EventType,
value::{BatchValues, ValueList},
value::{BatchValues, BatchValuesIterator, ValueList},
FrameParams, SerializedRequest,
};
use crate::query::Query;
Expand Down Expand Up @@ -772,11 +772,60 @@ impl Connection {

pub(crate) async fn batch_with_consistency(
&self,
batch: &Batch,
init_batch: &Batch,
values: impl BatchValues,
consistency: Consistency,
serial_consistency: Option<SerialConsistency>,
) -> Result<QueryResult, QueryError> {
let batch = {

let mut to_prepare = Vec::<usize>::new();

{
let mut value_iter = values.batch_values_iter();
for (id, stmt) in init_batch.statements.iter().enumerate() {
if let BatchStatement::Query(_) = stmt {
let value = value_iter.next_serialized().transpose()?;
if let Some(v) = value {
if v.len() > 0 {
to_prepare.push(id);
}
}
} else {
value_iter.skip_next();
}
}
}

let mut batch: Batch = Default::default();

if to_prepare.is_empty() {
batch = init_batch.clone();
} else {
batch.config = init_batch.config.clone();

let mut vec_id = 0;
for (id, stmt) in init_batch.statements.iter().enumerate() {
match stmt {
BatchStatement::Query(query) => {
if vec_id < to_prepare.len() && id == to_prepare[vec_id] {
vec_id += 1;
let prepared = self.prepare(query).await?;
batch.append_statement(prepared.clone());
} else {
batch.append_statement(query.clone());
}
}
BatchStatement::PreparedStatement(prepared) => {
batch.append_statement(prepared.clone());
}
}
}
}

batch
};

let batch_frame = batch::Batch {
statements: Cow::Borrowed(&batch.statements),
values,
Expand Down

0 comments on commit 27c733d

Please sign in to comment.