Skip to content

Commit

Permalink
add serialized_size to StorageEncode
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Nov 6, 2024
1 parent c490b19 commit 3397bc7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit 3397bc7

Please sign in to comment.