1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::{
    strategy::{DeltaRleDecoder, DeltaRleEncoder},
    ColumnAttr, ColumnarDecoder, ColumnarEncoder, ColumnarError, Strategy,
};

use super::{rle::Rleable, ColumnTrait};

pub trait DeltaRleable: Rleable + Copy + TryFrom<i128> + TryInto<i128> {}

impl<T> DeltaRleable for T where T: Rleable + Copy + TryFrom<i128> + TryInto<i128> {}

/// The Column that is scheduled to be compressed using [DeltaRleEncoder]
#[derive(Debug)]
pub struct DeltaRleColumn<T> {
    pub data: Vec<T>,
    pub attr: ColumnAttr,
}

impl<T> DeltaRleColumn<T> {
    pub fn new(data: Vec<T>, attr: ColumnAttr) -> Self {
        Self { data, attr }
    }
}

impl<T> ColumnTrait for DeltaRleColumn<T>
where
    T: DeltaRleable,
{
    const STRATEGY: Strategy = Strategy::DeltaRle;

    fn attr(&self) -> ColumnAttr {
        self.attr
    }
    fn len(&self) -> usize {
        self.data.len()
    }

    fn encode(&self, columnar_encoder: &mut ColumnarEncoder) -> Result<(), ColumnarError> {
        let mut delta_rle = DeltaRleEncoder::new(columnar_encoder);
        for &data in self.data.iter() {
            delta_rle
                .append(data.try_into().map_err(|_e| {
                    ColumnarError::RleEncodeError("cannot into i128".to_string())
                })?)?
        }

        delta_rle.finish()
    }

    fn decode(columnar_decoder: &mut ColumnarDecoder) -> Result<Self, ColumnarError>
    where
        Self: Sized,
    {
        let mut delta_rle_decoder = DeltaRleDecoder::new(columnar_decoder);
        let data = delta_rle_decoder.decode()?;
        Ok(Self {
            data,
            attr: ColumnAttr::empty(),
        })
    }
}