leekeiabstraction commented on code in PR #124: URL: https://github.com/apache/fluss-rust/pull/124#discussion_r2667451911
########## crates/fluss/src/row/compacted/compacted_key_writer.rs: ########## @@ -0,0 +1,128 @@ +// 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. + +use crate::row::compacted::compacted_row_writer::CompactedRowWriter; +use bytes::Bytes; + +use crate::error::Error::IllegalArgument; +use crate::error::Result; +use crate::metadata::DataType; +use crate::row::Datum; +use crate::row::binary::{BinaryRowFormat, BinaryWriter, ValueWriter}; +use delegate::delegate; + +/// A wrapping of [`CompactedRowWriter`] used to encode key columns. +/// The encoding is the same as [`CompactedRowWriter`], but is without header of null bits to +/// represent whether the field value is null or not since the key columns must be not null. +pub struct CompactedKeyWriter { + delegate: CompactedRowWriter, +} + +impl CompactedKeyWriter { + pub fn new() -> CompactedKeyWriter { + CompactedKeyWriter { + // in compacted key encoder, we don't need to set null bits as the key columns must be not + // null, to use field count 0 to init to make the null bits 0 + delegate: CompactedRowWriter::new(0), + } + } + + pub fn create_value_writer(field_type: &DataType) -> Result<Box<dyn ValueWriter>> { + let inner = <dyn ValueWriter>::create_not_null_value_writer( + field_type, + Some(&BinaryRowFormat::Compacted), + )?; + Ok(RejectNullValueWriter::wrap(field_type.clone(), inner)) + } + + delegate! { + to self.delegate { + pub fn reset(&mut self); + + #[allow(dead_code)] + pub fn position(&self) -> usize; + + #[allow(dead_code)] + pub fn buffer(&self) -> &[u8]; + + pub fn to_bytes(&self) -> Bytes; + } + } +} + +pub struct RejectNullValueWriter { + field_type: DataType, + delegate: Box<dyn ValueWriter>, +} + +impl RejectNullValueWriter { + fn wrap(field_type: DataType, delegate: Box<dyn ValueWriter>) -> Box<dyn ValueWriter> { + Box::new(RejectNullValueWriter { + field_type, + delegate, + }) + } +} +impl ValueWriter for RejectNullValueWriter { + fn write_value(&self, writer: &mut dyn BinaryWriter, pos: usize, value: &Datum) -> Result<()> { + if let Datum::Null = value { + return Err(IllegalArgument { + message: format!( + "Null value is not allowed for compacted key encoder in position {} with type {}", + pos, self.field_type + ), + }); + } + self.delegate.write_value(writer, pos, value) + } +} + +impl BinaryWriter for CompactedKeyWriter { + delegate! { + to self.delegate { + fn reset(&mut self); Review Comment: I believe the delegate crate already does that automatically > Inserts #[inline(always)] automatically (unless you specify #[inline] manually on the method) ref: https://docs.rs/delegate/latest/delegate/ -- 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]
