|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use databend_common_column::binview::BinaryViewColumn; |
| 16 | +use databend_common_column::bitmap::Bitmap; |
| 17 | +use databend_common_expression::types::Buffer; |
| 18 | + |
| 19 | +use super::basic::CommonCompression; |
| 20 | +use crate::compression::integer::compress_integer; |
| 21 | +use crate::error::Result; |
| 22 | +use crate::write::WriteOptions; |
| 23 | + |
| 24 | +pub fn compress_view( |
| 25 | + col: &BinaryViewColumn, |
| 26 | + validity: Option<Bitmap>, |
| 27 | + buf: &mut Vec<u8>, |
| 28 | + write_options: WriteOptions, |
| 29 | +) -> Result<()> { |
| 30 | + // choose compressor |
| 31 | + let col = col.gc_with_dict(validity.clone()); |
| 32 | + let view_array: Buffer<i128> = col.views().iter().map(|x| x.as_i128()).collect(); |
| 33 | + compress_integer(&view_array, validity, write_options.clone(), buf)?; |
| 34 | + compress_buffers(&col, buf, write_options.default_compression) |
| 35 | +} |
| 36 | + |
| 37 | +fn compress_buffers( |
| 38 | + array: &BinaryViewColumn, |
| 39 | + buf: &mut Vec<u8>, |
| 40 | + c: CommonCompression, |
| 41 | +) -> Result<()> { |
| 42 | + let codec = c.to_compression() as u8; |
| 43 | + buf.extend_from_slice(&(array.data_buffers().len() as u32).to_le_bytes()); |
| 44 | + // let's encode the buffers |
| 45 | + for buffer in array.data_buffers().iter() { |
| 46 | + buf.extend_from_slice(&[codec]); |
| 47 | + let pos = buf.len(); |
| 48 | + buf.extend_from_slice(&[0u8; 8]); |
| 49 | + |
| 50 | + let compressed_size = c.compress(buffer.as_slice(), buf)?; |
| 51 | + buf[pos..pos + 4].copy_from_slice(&(compressed_size as u32).to_le_bytes()); |
| 52 | + buf[pos + 4..pos + 8].copy_from_slice(&(buffer.len() as u32).to_le_bytes()); |
| 53 | + } |
| 54 | + Ok(()) |
| 55 | +} |
0 commit comments