Skip to main content

fory_core/row/
reader.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use super::{bit_util::calculate_bitmap_width_in_bytes, row::Row};
19use byteorder::{ByteOrder, LittleEndian};
20
21struct FieldAccessorHelper<'a> {
22    row: &'a [u8],
23    get_field_offset: Box<dyn Fn(usize) -> usize>,
24}
25
26impl<'a> FieldAccessorHelper<'a> {
27    fn get_offset_size(&self, idx: usize) -> (u32, u32) {
28        let row = self.row;
29        let field_offset = (self.get_field_offset)(idx);
30        let offset = LittleEndian::read_u32(&row[field_offset..field_offset + 4]);
31        let size = LittleEndian::read_u32(&row[field_offset + 4..field_offset + 8]);
32        (offset, size)
33    }
34
35    pub fn new(
36        row: &'a [u8],
37        get_field_offset: Box<dyn Fn(usize) -> usize>,
38    ) -> FieldAccessorHelper<'a> {
39        FieldAccessorHelper {
40            row,
41            get_field_offset,
42        }
43    }
44
45    pub fn get_field_bytes(&self, idx: usize) -> &'a [u8] {
46        let row = self.row;
47        let (offset, size) = self.get_offset_size(idx);
48        &row[(offset as usize)..(offset + size) as usize]
49    }
50}
51
52pub struct StructViewer<'r> {
53    field_accessor_helper: FieldAccessorHelper<'r>,
54}
55
56impl<'r> StructViewer<'r> {
57    pub fn new(row: &'r [u8], num_fields: usize) -> StructViewer<'r> {
58        let bit_map_width_in_bytes = calculate_bitmap_width_in_bytes(num_fields);
59        StructViewer {
60            field_accessor_helper: FieldAccessorHelper::new(
61                row,
62                Box::new(move |idx: usize| bit_map_width_in_bytes + idx * 8),
63            ),
64        }
65    }
66
67    pub fn get_field_bytes(&self, idx: usize) -> &'r [u8] {
68        self.field_accessor_helper.get_field_bytes(idx)
69    }
70}
71
72pub struct ArrayViewer<'r> {
73    num_elements: usize,
74    field_accessor_helper: FieldAccessorHelper<'r>,
75}
76
77impl<'r> ArrayViewer<'r> {
78    pub fn new(row: &'r [u8]) -> ArrayViewer<'r> {
79        let num_elements = LittleEndian::read_u64(&row[0..8]) as usize;
80        let bit_map_width_in_bytes = calculate_bitmap_width_in_bytes(num_elements);
81        ArrayViewer {
82            num_elements,
83            field_accessor_helper: FieldAccessorHelper::new(
84                row,
85                Box::new(move |idx: usize| 8 + bit_map_width_in_bytes + idx * 8),
86            ),
87        }
88    }
89
90    pub fn num_elements(&self) -> usize {
91        self.num_elements
92    }
93
94    pub fn get_field_bytes(&self, idx: usize) -> &'r [u8] {
95        self.field_accessor_helper.get_field_bytes(idx)
96    }
97}
98
99pub struct MapViewer<'r> {
100    key_row: &'r [u8],
101    value_row: &'r [u8],
102}
103
104impl<'r> MapViewer<'r> {
105    pub fn new(row: &'r [u8]) -> MapViewer<'r> {
106        let key_byte_size = LittleEndian::read_u64(&row[0..8]) as usize;
107        MapViewer {
108            value_row: &row[key_byte_size + 8..row.len()],
109            key_row: &row[8..key_byte_size + 8],
110        }
111    }
112
113    pub fn get_key_row(&self) -> &[u8] {
114        self.key_row
115    }
116
117    pub fn get_value_row(&self) -> &[u8] {
118        self.value_row
119    }
120}
121
122pub fn from_row<'a, T: Row<'a>>(row: &'a [u8]) -> T::ReadResult {
123    T::cast(row)
124}