-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an option to bind values to each separate statement in a batch #108
Labels
Milestone
Comments
is there any news about it ? |
This issue is currently in a "pull requests welcome" state. It's generally on a roadmap, but the team is currently focused on providing performance benchmarks. |
If someone needs func. like that - it is quite easy to impl according to usecase. use super::{BatchValues, ScyllaError};
use scylla::batch::Batch;
use scylla::frame::value::SerializedValues;
use scylla::prepared_statement::PreparedStatement;
pub struct BatchPrepare {
batch: Batch,
values: Vec<SerializedValues>,
stmt_count: u32,
}
impl BatchPrepare {
pub fn new() -> Self {
Self {
batch: Batch::default(),
values: Vec::new(),
stmt_count: 0,
}
}
pub fn take(mut self) -> (Batch, Vec<SerializedValues>) {
let batch = std::mem::take(&mut self.batch);
let values = std::mem::take(&mut self.values);
self.stmt_count = 0;
(batch, values)
}
pub fn add_batch_stmt(&mut self, prepared_statement: PreparedStatement) {
self.batch.append_statement(prepared_statement);
self.stmt_count += 1;
}
pub fn add_values(&mut self, values: impl BatchValues) -> Result<(), ScyllaError> {
let v = values.as_batch_values()?;
self.values.push(v);
Ok(())
}
pub fn is_empty(&self) -> bool {
self.stmt_count == 0
}
} use super::ScyllaError;
use scylla::frame::value::{SerializedValues, Value, ValueList};
fn add_value(s: &mut SerializedValues, val: &impl Value) -> Result<(), ScyllaError> {
s.add_value(val)
.map_err(|e| ScyllaError::Internal(e.into()))
}
pub trait BatchValues {
fn as_batch_values(&self) -> Result<SerializedValues, ScyllaError>;
}
impl<T: ValueList> BatchValues for &T {
fn as_batch_values(&self) -> Result<SerializedValues, ScyllaError> {
let s = <T as ValueList>::serialized(self).map_err(|e| ScyllaError::Internal(e.into()))?;
Ok(s.into_owned())
}
}
macro_rules! impl_batch_tuple {
( $($Ti:ident),* ; $($FieldI:tt),*) => {
impl<$($Ti),+> BatchValues for ($($Ti,)+)
where
$($Ti: Value),+
{
fn as_batch_values(&self) -> Result<SerializedValues, ScyllaError> {
let mut s = SerializedValues::new();
$(
add_value(&mut s, &self.$FieldI)?;
)*
Ok(s)
}
}
}
}
impl_batch_tuple!(T0; 0);
impl_batch_tuple!(T0, T1; 0, 1);
impl_batch_tuple!(T0, T1, T2; 0, 1, 2);
impl_batch_tuple!(T0, T1, T2, T3; 0, 1, 2, 3);
impl_batch_tuple!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5; 0, 1, 2, 3, 4, 5);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6; 0, 1, 2, 3, 4, 5, 6);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7; 0, 1, 2, 3, 4, 5, 6, 7);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8; 0, 1, 2, 3, 4, 5, 6, 7, 8);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
impl_batch_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); |
Ref: #941 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While using current batch API, it is easy to encounter the problem of statements/values count mismatch. To avoid it, we need to provide an option to bind values to each separate statement in a batch.
New batch API should support:
The text was updated successfully, but these errors were encountered: