fresh-borzoni commented on code in PR #161:
URL: https://github.com/apache/fluss-rust/pull/161#discussion_r2692623677


##########
crates/fluss/src/row/encode/compacted_row_encoder.rs:
##########
@@ -0,0 +1,83 @@
+// 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::error::Error::IllegalArgument;
+use crate::error::Result;
+use crate::metadata::DataType;
+use crate::row::Datum;
+use crate::row::binary::{BinaryRowFormat, BinaryWriter, ValueWriter};
+use crate::row::compacted::{CompactedRow, CompactedRowDeserializer, 
CompactedRowWriter};
+use crate::row::encode::{BinaryRow, RowEncoder};

Review Comment:
   do we have BinaryRow in row::encode?



##########
crates/fluss/src/row/encode/compacted_row_encoder.rs:
##########
@@ -0,0 +1,83 @@
+// 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::error::Error::IllegalArgument;
+use crate::error::Result;
+use crate::metadata::DataType;
+use crate::row::Datum;
+use crate::row::binary::{BinaryRowFormat, BinaryWriter, ValueWriter};
+use crate::row::compacted::{CompactedRow, CompactedRowDeserializer, 
CompactedRowWriter};
+use crate::row::encode::{BinaryRow, RowEncoder};
+use std::sync::Arc;
+
+#[allow(dead_code)]
+pub struct CompactedRowEncoder<'a> {
+    arity: usize,
+    writer: CompactedRowWriter,
+    field_writers: Vec<ValueWriter>,
+    compacted_row_deserializer: Arc<CompactedRowDeserializer<'a>>,
+}
+
+impl<'a> CompactedRowEncoder<'a> {
+    pub fn new(field_data_types: Vec<DataType>) -> Result<Self> {
+        let field_writers = field_data_types
+            .iter()
+            .map(|d| ValueWriter::create_value_writer(d, 
Some(&BinaryRowFormat::Compacted)))
+            .collect::<Result<Vec<_>>>()?;
+
+        Ok(Self {
+            arity: field_data_types.len(),
+            writer: CompactedRowWriter::new(field_data_types.len()),
+            field_writers,
+            compacted_row_deserializer: 
Arc::new(CompactedRowDeserializer::new_from_owned(
+                field_data_types,
+            )),
+        })
+    }
+}
+
+impl RowEncoder for CompactedRowEncoder<'_> {
+    fn start_new_row(&mut self) -> Result<()> {
+        self.writer.reset();
+        Ok(())
+    }
+
+    fn encode_field(&mut self, pos: usize, value: Datum) -> Result<()> {
+        self.field_writers
+            .get(pos)
+            .ok_or(IllegalArgument {

Review Comment:
   ok_or_else to avoid allocation on success



##########
crates/fluss/src/row/mod.rs:
##########
@@ -146,6 +146,9 @@ impl<'a> Default for GenericRow<'a> {
     }
 }
 
+/// A binary format [`InternalRow`] that is backed by byte array
+pub trait BinaryRow: InternalRow {}

Review Comment:
   I defined another BinaryRow in my PR with KvBatchRecordBuilder with as_bytes 
method, we may need to consolidate 🤔 



##########
crates/fluss/src/row/encode/mod.rs:
##########
@@ -62,3 +64,65 @@ impl dyn KeyEncoder {
         }
     }
 }
+
+/// An encoder to write [`BinaryRow`]. It's used to write row
+/// multi-times one by one. When writing a new row:
+///
+/// 1. call method [`RowEncoder::start_new_row()`] to start the writing.
+/// 2. call method [`RowEncoder::encode_field()`] to write the row's field.
+/// 3. call method [`RowEncoder::finishRow()`] to finish the writing and get 
the written row.
+#[allow(dead_code)]
+pub trait RowEncoder {
+    /// Start to write a new row.
+    ///
+    /// # Returns
+    /// * Ok(()) if successful
+    fn start_new_row(&mut self) -> Result<()>;
+
+    /// Write the row's field in given pos with given value.
+    ///
+    /// # Arguments
+    /// * pos - the position of the field to write.
+    /// * value - the value of the field to write.
+    ///
+    /// # Returns
+    /// * Ok(()) if successful
+    fn encode_field(&mut self, pos: usize, value: Datum) -> Result<()>;
+
+    /// Finish write the row, returns the written row.
+    ///
+    /// Note that returned row borrows from [`RowEncoder`]'s internal buffer 
which is reused for subsequent rows

Review Comment:
   we may enforce it explicitly with GAT, I think



##########
crates/fluss/src/row/encode/mod.rs:
##########
@@ -62,3 +64,65 @@ impl dyn KeyEncoder {
         }
     }
 }
+
+/// An encoder to write [`BinaryRow`]. It's used to write row
+/// multi-times one by one. When writing a new row:
+///
+/// 1. call method [`RowEncoder::start_new_row()`] to start the writing.
+/// 2. call method [`RowEncoder::encode_field()`] to write the row's field.
+/// 3. call method [`RowEncoder::finishRow()`] to finish the writing and get 
the written row.
+#[allow(dead_code)]
+pub trait RowEncoder {
+    /// Start to write a new row.
+    ///
+    /// # Returns
+    /// * Ok(()) if successful
+    fn start_new_row(&mut self) -> Result<()>;
+
+    /// Write the row's field in given pos with given value.
+    ///
+    /// # Arguments
+    /// * pos - the position of the field to write.
+    /// * value - the value of the field to write.
+    ///
+    /// # Returns
+    /// * Ok(()) if successful
+    fn encode_field(&mut self, pos: usize, value: Datum) -> Result<()>;
+
+    /// Finish write the row, returns the written row.
+    ///
+    /// Note that returned row borrows from [`RowEncoder`]'s internal buffer 
which is reused for subsequent rows
+    /// [`RowEncoder::start_new_row()`] should only be called after the 
returned row goes out of scope.
+    ///
+    /// # Returns
+    /// * the written row
+    fn finish_row(&mut self) -> Result<impl BinaryRow>;
+
+    /// Closes the row encoder
+    ///
+    /// # Returns
+    /// * Ok(()) if successful
+    fn close(&mut self) -> Result<()>;
+}
+
+#[allow(dead_code)]
+pub struct RowEncoderFactory {}
+
+#[allow(dead_code)]
+impl RowEncoderFactory {
+    pub fn create(kv_format: KvFormat, row_type: RowType) -> Result<impl 
RowEncoder> {

Review Comment:
   Factory currently takes RowType by value and get_children() clones all 
DataTypes. Could we instead take &RowType and make the cloning explicit in the 
factory?
   
   RowType is schema metadata and is typically shared/read-only, taking 
ownership forces callers to move or clone it. 
   
   e.g.
   ```rust
   impl RowType {
       pub fn field_types(&self) -> impl Iterator<Item = &DataType> + '_ {
           self.fields.iter().map(|f| &f.data_type)
       }
   }
   
   impl RowEncoderFactory {
       pub fn create(kv_format: KvFormat, row_type: &RowType) -> Result<impl 
RowEncoder> {
           let field_data_types: Vec<DataType> = 
row_type.field_types().cloned().collect();
           Self::create_for_field_types(kv_format, field_data_types)
       }
   }
   ```



-- 
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]

Reply via email to