Skip to main content

fluss/row/compacted/
compacted_key_writer.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 crate::row::compacted::compacted_row_writer::CompactedRowWriter;
19use bytes::Bytes;
20
21use crate::error::Result;
22use crate::metadata::DataType;
23use crate::row::Decimal;
24use crate::row::binary::{BinaryRowFormat, BinaryWriter, ValueWriter};
25use delegate::delegate;
26
27/// A wrapping of [`CompactedRowWriter`] used to encode key columns.
28/// The encoding is the same as [`CompactedRowWriter`], but is without header of null bits to
29/// represent whether the field value is null or not since the key columns must be not null.
30pub struct CompactedKeyWriter {
31    delegate: CompactedRowWriter,
32}
33
34impl Default for CompactedKeyWriter {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl CompactedKeyWriter {
41    pub fn new() -> CompactedKeyWriter {
42        CompactedKeyWriter {
43            // in compacted key encoder, we don't need to set null bits as the key columns must be not
44            // null, to use field count 0 to init to make the null bits 0
45            delegate: CompactedRowWriter::new(0),
46        }
47    }
48
49    pub fn create_value_writer(field_type: &DataType) -> Result<ValueWriter> {
50        ValueWriter::create_value_writer(field_type, Some(&BinaryRowFormat::Compacted))
51    }
52
53    delegate! {
54        to self.delegate {
55            pub fn reset(&mut self);
56
57            #[allow(dead_code)]
58            pub fn position(&self) -> usize;
59
60            #[allow(dead_code)]
61            pub fn buffer(&self) -> &[u8];
62
63            pub fn to_bytes(&self) -> Bytes;
64        }
65    }
66}
67
68impl BinaryWriter for CompactedKeyWriter {
69    delegate! {
70        to self.delegate {
71            fn reset(&mut self);
72
73            fn set_null_at(&mut self, pos: usize);
74
75            fn write_boolean(&mut self, value: bool);
76
77            fn write_byte(&mut self, value: u8);
78
79            fn write_binary(&mut self, bytes: &[u8], length: usize);
80
81            fn write_bytes(&mut self, value: &[u8]);
82
83            fn write_char(&mut self, value: &str, _length: usize);
84
85            fn write_string(&mut self, value: &str);
86
87            fn write_short(&mut self, value: i16);
88
89            fn write_int(&mut self, value: i32);
90
91            fn write_long(&mut self, value: i64);
92
93            fn write_float(&mut self, value: f32);
94
95            fn write_double(&mut self, value: f64);
96
97            fn write_decimal(&mut self, value: &Decimal, precision: u32);
98
99            fn write_time(&mut self, value: i32, precision: u32);
100
101            fn write_timestamp_ntz(&mut self, value: &crate::row::datum::TimestampNtz, precision: u32);
102
103            fn write_timestamp_ltz(&mut self, value: &crate::row::datum::TimestampLtz, precision: u32);
104        }
105    }
106
107    fn complete(&mut self) {
108        // do nothing
109    }
110}