Skip to main content

coip_format/
column.rs

1use crate::{Codec, Cursor, FormatError, IdWidth};
2
3pub const COLUMN_TABLE_HEADER_SIZE: usize = 20;
4pub const FLAT_DICT_HEADER_SIZE: usize = 24;
5pub const LIST_DICT_HEADER_SIZE: usize = 24;
6
7/// Chunk nullability mode (byte 1 of chunk header).
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[repr(u8)]
10pub enum NullMode {
11    AllValid = 0,
12    AllNull = 1,
13    ValidityBitmap = 2,
14}
15
16impl NullMode {
17    #[inline]
18    pub fn from_u8(v: u8) -> Result<Self, FormatError> {
19        match v {
20            0 => Ok(Self::AllValid),
21            1 => Ok(Self::AllNull),
22            2 => Ok(Self::ValidityBitmap),
23            _ => Err(FormatError::InvalidNullMode(v)),
24        }
25    }
26}
27
28/// Chunk value encoding mode (byte 2 of chunk header).
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[repr(u8)]
31pub enum EncMode {
32    Constant = 0,
33    Plain = 1,
34    Delta = 2,
35    Rle = 3,
36}
37
38impl EncMode {
39    #[inline]
40    pub fn from_u8(v: u8) -> Result<Self, FormatError> {
41        match v {
42            0 => Ok(Self::Constant),
43            1 => Ok(Self::Plain),
44            2 => Ok(Self::Delta),
45            3 => Ok(Self::Rle),
46            _ => Err(FormatError::InvalidEncMode(v)),
47        }
48    }
49}
50
51/// 3-byte header prefixed to every chunk in a `ColumnTable`.
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub struct ChunkHeader {
54    pub codec: Codec,
55    pub null_mode: NullMode,
56    pub enc_mode: EncMode,
57}
58
59impl ChunkHeader {
60    #[inline]
61    pub const fn encode(self) -> [u8; 3] {
62        [self.codec as u8, self.null_mode as u8, self.enc_mode as u8]
63    }
64
65    #[inline]
66    pub fn decode(buf: &[u8]) -> Result<Self, FormatError> {
67        if buf.len() < 3 {
68            return Err(FormatError::UnexpectedEof);
69        }
70        Ok(Self {
71            codec: Codec::from_u8(buf[0])?,
72            null_mode: NullMode::from_u8(buf[1])?,
73            enc_mode: EncMode::from_u8(buf[2])?,
74        })
75    }
76}
77
78/// 20-byte header of a `ColumnTable` (used by Section 8 and `StructDict`).
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub struct ColumnTableHeader {
81    pub row_count: u64,
82    pub row_group_size: u32,
83    pub row_group_count: u32,
84    pub column_count: u16,
85}
86
87impl ColumnTableHeader {
88    pub fn encode(&self) -> [u8; COLUMN_TABLE_HEADER_SIZE] {
89        let mut out = [0u8; COLUMN_TABLE_HEADER_SIZE];
90        out[0..8].copy_from_slice(&self.row_count.to_le_bytes());
91        out[8..12].copy_from_slice(&self.row_group_size.to_le_bytes());
92        out[12..16].copy_from_slice(&self.row_group_count.to_le_bytes());
93        out[16..18].copy_from_slice(&self.column_count.to_le_bytes());
94        out
95    }
96
97    pub fn decode(c: &mut Cursor<'_>) -> Result<Self, FormatError> {
98        let row_count = c.read_u64()?;
99        let row_group_size = c.read_u32()?;
100        let row_group_count = c.read_u32()?;
101        let column_count = c.read_u16()?;
102        let _reserved = c.read_u16()?;
103        Ok(Self {
104            row_count,
105            row_group_size,
106            row_group_count,
107            column_count,
108        })
109    }
110
111    /// Number of rows stored in RowGroup `rg`.
112    #[inline]
113    pub fn rows_in_rg(&self, rg: u32) -> usize {
114        if self.row_count == 0 || rg >= self.row_group_count {
115            return 0;
116        }
117        let start = (rg as u64) * (self.row_group_size as u64);
118        let rem = self.row_count.saturating_sub(start);
119        rem.min(self.row_group_size as u64) as usize
120    }
121}
122
123/// 24-byte header of a `FlatDict` blob in Section 9.
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
125pub struct FlatDictHeader {
126    pub entry_count: u64,
127    pub codec: Codec,
128    pub sorted_front_coded: bool,
129    pub block_size: u32,
130    pub block_count: u32,
131}
132
133impl FlatDictHeader {
134    pub fn encode(&self) -> [u8; FLAT_DICT_HEADER_SIZE] {
135        let mut out = [0u8; FLAT_DICT_HEADER_SIZE];
136        out[0..8].copy_from_slice(&self.entry_count.to_le_bytes());
137        out[8] = self.codec as u8;
138        out[9] = u8::from(self.sorted_front_coded);
139        out[10..12].copy_from_slice(&0u16.to_le_bytes());
140        out[12..16].copy_from_slice(&self.block_size.to_le_bytes());
141        out[16..20].copy_from_slice(&self.block_count.to_le_bytes());
142        out[20..24].copy_from_slice(&0u32.to_le_bytes());
143        out
144    }
145
146    pub fn decode(c: &mut Cursor<'_>) -> Result<Self, FormatError> {
147        let entry_count = c.read_u64()?;
148        let codec = Codec::from_u8(c.read_u8()?)?;
149        let flags = c.read_u8()?;
150        let _reserved = c.read_u16()?;
151        let block_size = c.read_u32()?;
152        let block_count = c.read_u32()?;
153        let _reserved2 = c.read_u32()?;
154        Ok(Self {
155            entry_count,
156            codec,
157            sorted_front_coded: (flags & 1) != 0,
158            block_size,
159            block_count,
160        })
161    }
162
163    #[inline]
164    pub fn entries_in_block(&self, block_idx: u32) -> usize {
165        if self.entry_count == 0 || block_idx >= self.block_count {
166            return 0;
167        }
168        if self.block_size == 0 {
169            return self.entry_count as usize;
170        }
171        let start = (block_idx as u64) * (self.block_size as u64);
172        let rem = self.entry_count.saturating_sub(start);
173        rem.min(self.block_size as u64) as usize
174    }
175}
176
177/// 24-byte header of a `ListDict` blob in Section 9.
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub struct ListDictHeader {
180    pub entry_count: u64,
181    pub total_elems: u64,
182    pub codec: Codec,
183    pub id_width: IdWidth,
184    pub offsets_len: u32,
185}
186
187impl ListDictHeader {
188    pub fn encode(&self) -> [u8; LIST_DICT_HEADER_SIZE] {
189        let mut out = [0u8; LIST_DICT_HEADER_SIZE];
190        out[0..8].copy_from_slice(&self.entry_count.to_le_bytes());
191        out[8..16].copy_from_slice(&self.total_elems.to_le_bytes());
192        out[16] = self.codec as u8;
193        out[17] = self.id_width as u8;
194        out[18..20].copy_from_slice(&0u16.to_le_bytes());
195        out[20..24].copy_from_slice(&self.offsets_len.to_le_bytes());
196        out
197    }
198
199    pub fn decode(c: &mut Cursor<'_>) -> Result<Self, FormatError> {
200        let entry_count = c.read_u64()?;
201        let total_elems = c.read_u64()?;
202        let codec = Codec::from_u8(c.read_u8()?)?;
203        let id_width = IdWidth::from_u8(c.read_u8()?)?;
204        let _reserved = c.read_u16()?;
205        let offsets_len = c.read_u32()?;
206        Ok(Self {
207            entry_count,
208            total_elems,
209            codec,
210            id_width,
211            offsets_len,
212        })
213    }
214}