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 62 63 64 65 66
use super::super::delta_bitpacked;
/// Decodes [Delta-length byte array](https://github.com/apache/parquet-format/blob/master/Encodings.md#delta-length-byte-array-delta_length_byte_array--6)
/// lengths and values.
/// # Implementation
/// This struct does not allocate on the heap.
/// # Example
/// ```
/// use parquet2::encoding::delta_length_byte_array::Decoder;
///
/// let expected = &["Hello", "World"];
/// let expected_lengths = expected.iter().map(|x| x.len() as i32).collect::<Vec<_>>();
/// let expected_values = expected.join("");
/// let expected_values = expected_values.as_bytes();
/// let data = &[
/// 128, 1, 4, 2, 10, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100,
/// ];
///
/// let mut decoder = Decoder::new(data);
///
/// // Extract the lengths
/// let lengths = decoder.by_ref().collect::<Vec<_>>();
/// assert_eq!(lengths, expected_lengths);
///
/// // Extract the values. This _must_ be called after consuming all lengths by reference (see above).
/// let values = decoder.into_values();
///
/// assert_eq!(values, expected_values);
#[derive(Debug)]
pub struct Decoder<'a> {
values: &'a [u8],
lengths: delta_bitpacked::Decoder<'a>,
total_length: u32,
}
impl<'a> Decoder<'a> {
pub fn new(values: &'a [u8]) -> Self {
let lengths = delta_bitpacked::Decoder::new(values);
Self {
values,
lengths,
total_length: 0,
}
}
/// Consumes this decoder and returns the slice of concatenated values.
/// # Panics
/// This function panics if this iterator has not been fully consumed.
pub fn into_values(self) -> &'a [u8] {
assert_eq!(self.lengths.size_hint().0, 0);
let start = self.lengths.consumed_bytes();
&self.values[start..start + self.total_length as usize]
}
}
impl<'a> Iterator for Decoder<'a> {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> {
let result = self.lengths.next();
if let Some(result) = result {
self.total_length += result as u32
}
result.map(|x| x as i32)
}
}