tsfile_writer/writer/encoding/
mod.rs

1//! Different Encoding Algorithms for TsFiles
2use crate::writer::{IoTDBValue, TSDataType, TsFileError};
3
4pub mod plain;
5pub mod time_encoder;
6
7use crate::writer::encoding::plain::PlainEncoder;
8use crate::writer::encoding::time_encoder::{IntTs2DiffEncoder, LongTs2DiffEncoder};
9use crate::writer::TSEncoding::{PLAIN, TS2DIFF};
10
11#[derive(PartialEq, Copy, Clone, Debug)]
12pub enum TSEncoding {
13    PLAIN,
14    TS2DIFF,
15}
16
17impl TryFrom<u8> for TSEncoding {
18    type Error = ();
19
20    fn try_from(value: u8) -> Result<Self, Self::Error> {
21        match value {
22            0 => Ok(PLAIN),
23            4 => Ok(TS2DIFF),
24            _ => Err(()),
25        }
26    }
27}
28
29impl TSEncoding {
30    pub fn serialize(&self) -> u8 {
31        match self {
32            TSEncoding::PLAIN => 0,
33            TSEncoding::TS2DIFF => 4,
34        }
35    }
36}
37
38pub trait Encoder {
39    fn write(&mut self, value: &IoTDBValue) -> Result<(), TsFileError>;
40    fn size(&mut self) -> u32;
41    fn get_max_byte_size(&self) -> u32;
42    fn serialize(&mut self, buffer: &mut Vec<u8>);
43    fn reset(&mut self);
44}
45
46impl dyn Encoder {
47    pub(crate) fn new(
48        data_type: TSDataType,
49        encoding: TSEncoding,
50    ) -> Result<Box<dyn Encoder>, TsFileError> {
51        match (data_type, encoding) {
52            (_, TSEncoding::PLAIN) => Ok(Box::new(PlainEncoder::new(data_type))),
53            (TSDataType::INT64, TSEncoding::TS2DIFF) => Ok(Box::new(LongTs2DiffEncoder::new())),
54            (TSDataType::INT32, TSEncoding::TS2DIFF) => Ok(Box::new(IntTs2DiffEncoder::new())),
55            (_, TSEncoding::TS2DIFF) => Err(TsFileError::Encoding),
56        }
57    }
58}