fresh-borzoni commented on code in PR #156: URL: https://github.com/apache/fluss-rust/pull/156#discussion_r2687355924
########## crates/fluss/src/record/kv/kv_record.rs: ########## @@ -0,0 +1,469 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Key-Value record implementation. +//! +//! This module provides the KvRecord struct which represents an immutable key-value record. +//! The record format is: +//! - Length => Int32 +//! - KeyLength => Unsigned VarInt +//! - Key => bytes +//! - Row => BinaryRow (optional, if null then this is a deletion record) + +use bytes::{BufMut, Bytes, BytesMut}; +use std::io::{self, Write}; + +use crate::util::varint::{ + read_unsigned_varint_bytes, size_of_unsigned_varint, write_unsigned_varint, + write_unsigned_varint_buf, +}; + +/// Length field size in bytes +pub const LENGTH_LENGTH: usize = 4; + +/// A key-value record. +/// +/// The schema is: +/// - Length => Int32 +/// - KeyLength => Unsigned VarInt +/// - Key => bytes +/// - ValueLength => Unsigned VarInt (only present if value exists, even for empty values) +/// - Value => bytes (if ValueLength > 0) +/// +/// When the value is None (deletion), no ValueLength or Value bytes are present. +/// When the value is Some(&[]) (empty value), ValueLength is present with value 0. +#[derive(Debug, Clone)] +pub struct KvRecord { + key: Bytes, + value: Option<Bytes>, +} + +impl KvRecord { + /// Create a new KvRecord with the given key and optional value. + pub fn new(key: Bytes, value: Option<Bytes>) -> Self { + Self { key, value } + } + + /// Get the key bytes. + pub fn key(&self) -> &Bytes { + &self.key + } + + /// Get the value bytes (None indicates a deletion). + pub fn value(&self) -> Option<&Bytes> { + self.value.as_ref() + } + + /// Calculate the total size of the record when serialized (including length prefix). + pub fn size_of(key: &[u8], value: Option<&[u8]>) -> usize { + Self::size_without_length(key, value) + LENGTH_LENGTH + } + + /// Calculate the size without the length prefix. + fn size_without_length(key: &[u8], value: Option<&[u8]>) -> usize { + let key_len = key.len(); + let key_len_size = size_of_unsigned_varint(key_len as u32); + + match value { + Some(v) => { + let value_len_size = size_of_unsigned_varint(v.len() as u32); + key_len_size + .saturating_add(key_len) + .saturating_add(value_len_size) Review Comment: Mistake, thanks for spotting -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
