risingwave_storage/row_serde/
row_serde_util.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use 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}