Skip to content
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

add serialized_size to StorageEncode #2227

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/storage-api/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ macro_rules! protobuf_storage_encode_decode {
restate_types::storage::StorageEncodeError::EncodeValue(err.into())
})
}
fn serialized_size(&self) -> usize {
bytes::BytesMut::default().len() // todo: to be calculated
}
}

impl restate_types::storage::StorageDecode for $ty {
Expand Down
33 changes: 31 additions & 2 deletions crates/types/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,16 @@ impl StorageCodec {
value: &T,
buf: &mut BytesMut,
) -> Result<(), StorageEncodeError> {
// write codec
// Write codec version byte
buf.put_u8(value.default_codec().into());
// encode value

// Calculate the exact size required for serialization
let size = value.serialized_size();

// Ensure the buffer has enough capacity
buf.reserve(size);

// Encode the value into the buffer
value.encode(buf)
}

Expand Down Expand Up @@ -119,6 +126,9 @@ pub trait StorageEncode: DowncastSync {

/// Codec which is used when encode new values.
fn default_codec(&self) -> StorageCodecKind;

/// Returns the expected serialized size in bytes of the encoded value.
fn serialized_size(&self) -> usize;
}
impl_downcast!(sync StorageEncode);

Expand Down Expand Up @@ -153,6 +163,12 @@ macro_rules! flexbuffers_storage_encode_decode {
$crate::storage::encode_as_flexbuffers(self, buf)
.map_err(|err| $crate::storage::StorageEncodeError::EncodeValue(err.into()))
}


fn serialized_size(&self) -> usize {
bytes::BytesMut::default().len() // todo: to be calculated
}

}

impl $crate::storage::StorageDecode for $name {
Expand Down Expand Up @@ -204,6 +220,12 @@ impl StorageEncode for PolyBytes {
fn default_codec(&self) -> StorageCodecKind {
StorageCodecKind::FlexbuffersSerde
}
fn serialized_size(&self) -> usize {
match self {
PolyBytes::Bytes(bytes) => bytes.len(),
PolyBytes::Typed(typed) => typed.serialized_size(),
}
}
}

/// SerializeAs/DeserializeAs to implement ser/de trait for [`PolyBytes`]
Expand Down Expand Up @@ -266,6 +288,10 @@ impl StorageEncode for String {
buf.put_slice(my_bytes);
Ok(())
}

fn serialized_size(&self) -> usize {
4 + self.len() // 4 bytes for length + string byte length
}
}
impl StorageDecode for String {
fn decode<B: ::bytes::Buf>(
Expand Down Expand Up @@ -332,6 +358,9 @@ impl StorageEncode for bytes::Bytes {
buf.put_slice(&self[..]);
Ok(())
}
fn serialized_size(&self) -> usize {
4 + self.len() // 4 bytes for length + byte slice length
}
}

/// Utility method to encode a [`Serialize`] type as flexbuffers using serde.
Expand Down
Loading