Skip to content

Commit

Permalink
SerializeRow: Add is_empty method
Browse files Browse the repository at this point in the history
Currently ongoing serialization refactor requires to only perform
queries with values using prepared statements. Session interface still
allows to pass values in query* methods, so we need a way to know if we
can perform unprepared query or not.

One way is to always perform prepared queries, but that is not optimal.
Before ongoing refactor we could serialize values and check if they are
empty. This will no longer be possible soon, because serializing will
require knowing column types, which in this case means having a prepared
statement.

This commit implements third way - additional method on SerializeRow
trait that allows us to check if values are empty (in which case we can
skip the prepare).
  • Loading branch information
Lorak-mmk committed Dec 5, 2023
1 parent bdcf349 commit cd6536b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub trait SerializeRow {
ctx: &RowSerializationContext<'_>,
writer: &mut W,
) -> Result<(), SerializationError>;

fn is_empty(&self) -> bool;
}

macro_rules! fallback_impl_contents {
Expand All @@ -69,6 +71,9 @@ macro_rules! fallback_impl_contents {
) -> Result<(), SerializationError> {
serialize_legacy_row(self, ctx, writer)
}
fn is_empty(&self) -> bool {
SerializedValues::is_empty(self)
}
};
}

Expand Down Expand Up @@ -96,6 +101,10 @@ macro_rules! impl_serialize_row_for_unit {
// Row is empty - do nothing
Ok(())
}

fn is_empty(&self) -> bool {
true
}
};
}

Expand Down Expand Up @@ -152,6 +161,10 @@ macro_rules! impl_serialize_row_for_slice {
}
Ok(())
}

fn is_empty(&self) -> bool {
<[T]>::is_empty(self.as_ref())
}
};
}

Expand Down Expand Up @@ -227,6 +240,12 @@ macro_rules! impl_serialize_row_for_map {

Ok(())
}

fn is_empty(&self) -> bool {
// Not sure how to make sure I call `is_empty` method from specific, not from a trait recursively,
// so I used `len()` which is not present in the trait.
self.len() == 0
}
};
}

Expand Down Expand Up @@ -258,6 +277,10 @@ impl<T: SerializeRow> SerializeRow for &T {
) -> Result<(), SerializationError> {
<T as SerializeRow>::serialize(self, ctx, writer)
}

fn is_empty(&self) -> bool {
<T as SerializeRow>::is_empty(self)
}
}

impl SerializeRow for SerializedValues {
Expand Down Expand Up @@ -325,6 +348,10 @@ macro_rules! impl_tuple {
)*
Ok(())
}

fn is_empty(&self) -> bool {
$length == 0
}
}
};
}
Expand Down Expand Up @@ -428,6 +455,13 @@ macro_rules! impl_serialize_row_via_value_list {
) -> ::std::result::Result<(), $crate::types::serialize::SerializationError> {
$crate::types::serialize::row::serialize_legacy_row(self, ctx, writer)
}

fn is_empty(&self) -> bool {
match $crate::frame::value::ValueList::serialized(self) {
Ok(s) => s.is_empty(),
Err(e) => false
}
}
}
};
}
Expand Down

0 comments on commit cd6536b

Please sign in to comment.