luoyuxia commented on code in PR #131:
URL: https://github.com/apache/fluss-rust/pull/131#discussion_r2678353134


##########
crates/fluss/src/row/compacted/compacted_row.rs:
##########
@@ -0,0 +1,343 @@
+// 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 std::sync::Arc;
+
+use bytes::Bytes;
+
+use crate::metadata::DataType;
+use crate::row::compacted::compacted_row_reader::{CompactedRowDeserializer, 
CompactedRowReader};
+use crate::row::{GenericRow, InternalRow};
+
+// Reference implementation:
+// 
https://github.com/apache/fluss/blob/main/fluss-common/src/main/java/org/apache/fluss/row/compacted/CompactedRow.java
+#[allow(dead_code)]
+pub struct CompactedRow {
+    arity: usize,
+    segment: Bytes,
+    offset: usize,
+    size_in_bytes: usize,
+    decoded: bool,
+    decoded_row: Option<GenericRow<'static>>,
+    reader: Option<CompactedRowReader>,

Review Comment:
   Let's make `decoded_row` and `reader` non option value to make code clear 
   ```
   pub struct CompactedRow {
       arity: usize,
       segment: Bytes,
       offset: usize,
       size_in_bytes: usize,
       decoded: bool,
       decoded_row: Option<GenericRow<'static>>,
       reader: CompactedRowReader,
       deserializer: CompactedRowDeserializer,
   }
   ```



##########
crates/fluss/src/row/compacted/compacted_row.rs:
##########
@@ -0,0 +1,343 @@
+// 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 std::sync::Arc;
+
+use bytes::Bytes;
+
+use crate::metadata::DataType;
+use crate::row::compacted::compacted_row_reader::{CompactedRowDeserializer, 
CompactedRowReader};
+use crate::row::{GenericRow, InternalRow};
+
+// Reference implementation:
+// 
https://github.com/apache/fluss/blob/main/fluss-common/src/main/java/org/apache/fluss/row/compacted/CompactedRow.java
+#[allow(dead_code)]
+pub struct CompactedRow {
+    arity: usize,
+    segment: Bytes,
+    offset: usize,
+    size_in_bytes: usize,
+    decoded: bool,
+    decoded_row: Option<GenericRow<'static>>,
+    reader: Option<CompactedRowReader>,
+    deserializer: Arc<CompactedRowDeserializer>,
+}
+
+#[allow(dead_code)]
+impl CompactedRow {
+    pub fn calculate_bit_set_width_in_bytes(arity: usize) -> usize {
+        arity.div_ceil(8)
+    }
+
+    pub fn new(types: Vec<DataType>) -> Self {
+        let arity = types.len();
+        Self {
+            arity,
+            segment: Bytes::new(),
+            offset: 0,
+            size_in_bytes: 0,
+            decoded: false,
+            decoded_row: None,
+            reader: None,
+            deserializer: Arc::new(CompactedRowDeserializer::new(types)),
+        }
+    }
+
+    pub fn from_bytes(types: Vec<DataType>, data: Bytes) -> Self {
+        let arity = types.len();
+        let size = data.len();
+        Self {
+            arity,
+            segment: data,
+            offset: 0,
+            size_in_bytes: size,
+            decoded: false,
+            decoded_row: None,
+            reader: None,
+            deserializer: Arc::new(CompactedRowDeserializer::new(types)),
+        }
+    }
+
+    pub fn point_to(&mut self, segment: Bytes, offset: usize, size_in_bytes: 
usize) {
+        self.segment = segment;
+        self.offset = offset;
+        self.size_in_bytes = size_in_bytes;
+        self.decoded = false;
+    }
+
+    pub fn get_segment(&self) -> &Bytes {
+        &self.segment
+    }
+
+    pub fn get_offset(&self) -> usize {
+        self.offset
+    }
+
+    pub fn get_size_in_bytes(&self) -> usize {
+        self.size_in_bytes
+    }
+
+    pub fn get_field_count(&self) -> usize {
+        self.arity
+    }
+
+    pub fn is_null_at(&self, pos: usize) -> bool {
+        let byte_index = pos >> 3;
+        let bit = pos & 7;
+        let idx = self.offset + byte_index;
+        (self.segment[idx] & (1u8 << bit)) != 0
+    }
+
+    fn decoded_row(&mut self) -> &GenericRow<'static> {
+        if !self.decoded {
+            if self.reader.is_none() {
+                self.reader = Some(CompactedRowReader::new(self.arity));
+            }
+
+            let reader = self.reader.as_mut().expect("reader should be 
initialized");
+            reader.point_to(self.segment.clone(), self.offset, 
self.size_in_bytes);
+
+            self.decoded_row = Some(self.deserializer.deserialize(reader));
+            self.decoded = true;
+        }
+        self.decoded_row
+            .as_ref()
+            .expect("decoded_row should be set")
+    }
+
+    pub fn get_boolean(&mut self, pos: usize) -> bool {
+        self.decoded_row().get_boolean(pos)
+    }
+
+    pub fn get_byte(&mut self, pos: usize) -> i8 {
+        self.decoded_row().get_byte(pos)
+    }
+
+    pub fn get_short(&mut self, pos: usize) -> i16 {
+        self.decoded_row().get_short(pos)
+    }
+
+    pub fn get_int(&mut self, pos: usize) -> i32 {
+        self.decoded_row().get_int(pos)
+    }
+
+    pub fn get_long(&mut self, pos: usize) -> i64 {
+        self.decoded_row().get_long(pos)
+    }
+
+    pub fn get_float(&mut self, pos: usize) -> f32 {
+        self.decoded_row().get_float(pos)
+    }
+
+    pub fn get_double(&mut self, pos: usize) -> f64 {
+        self.decoded_row().get_double(pos)
+    }
+
+    pub fn get_string(&mut self, pos: usize) -> &str {
+        self.decoded_row().get_string(pos)
+    }
+
+    pub fn get_bytes(&mut self, pos: usize) -> &[u8] {
+        self.decoded_row().get_bytes(pos)
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::metadata::{
+        BigIntType, BooleanType, BytesType, DoubleType, FloatType, IntType, 
SmallIntType,
+        StringType, TinyIntType,
+    };
+    use crate::row::compacted::compacted_row_writer::CompactedRowWriter;
+
+    #[test]
+    fn test_basic_types() {

Review Comment:
   is it possible to combine `test_basic_types`, 
`test_with_nulls`,`test_all_primitive_types`,`test_string_and_bytes`, 
`test_get_field_count`, `test_multiple_reads` into single one test method?
   
   



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