risingwave_storage/row_serde/
row_serde_util.rs1use bytes::{BufMut, Bytes, BytesMut};
16use risingwave_common::hash::VirtualNode;
17use risingwave_common::row::{OwnedRow, Row};
18use risingwave_common::util::row_serde::OrderedRowSerde;
19use risingwave_hummock_sdk::key::TableKey;
20
21pub fn serialize_pk(pk: impl Row, serializer: &OrderedRowSerde) -> Bytes {
22 let mut buf = BytesMut::with_capacity(pk.len());
23 pk.memcmp_serialize_into(serializer, &mut buf);
24 buf.freeze()
25}
26
27pub fn serialize_pk_with_vnode(
28 pk: impl Row,
29 serializer: &OrderedRowSerde,
30 vnode: VirtualNode,
31) -> TableKey<Bytes> {
32 let mut buffer = BytesMut::new();
33 buffer.put_slice(&vnode.to_be_bytes()[..]);
34 pk.memcmp_serialize_into(serializer, &mut buffer);
35 TableKey(buffer.freeze())
36}
37
38pub fn deserialize_pk_with_vnode(
39 key: &[u8],
40 deserializer: &OrderedRowSerde,
41) -> memcomparable::Result<(VirtualNode, OwnedRow)> {
42 let vnode = VirtualNode::from_be_bytes(key[0..VirtualNode::SIZE].try_into().unwrap());
43 let pk = deserializer.deserialize(&key[VirtualNode::SIZE..])?;
44 Ok((vnode, pk))
45}