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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![doc = include_str!("lib.md")]

pub use fallible_streaming_iterator;
pub use fallible_streaming_iterator::FallibleStreamingIterator;

/// Trait denoting a compressed item. Use `is_compressed` to declare if the item is
/// compressed at runtime (in which case the internal buffer is swapped)
pub trait Compressed {
    #[inline]
    fn is_compressed(&self) -> bool {
        true
    }
}

/// Trait denoting an uncompressed item. Use `buffer_mut` to expose a mutable reference to its
/// internal buffer, which [`Decompressor`] will use to recover a decompressed buffer for re-use.
pub trait Decompressed {
    fn buffer_mut(&mut self) -> &mut Vec<u8>;
}

/// A [`FallibleStreamingIterator`] that decompresses items of type `I` into type `O` via an
/// internal [`Vec<u8>`] that is re-used across items.
/// The purpose of this streaming iterator is to be able to decompress parts of an item `I` into `O`.
pub struct Decompressor<I, O, F, E, II>
where
    I: Compressed,
    O: Decompressed,
    E: std::error::Error,
    II: Iterator<Item = Result<I, E>>,
    F: Fn(I, &mut Vec<u8>) -> Result<O, E>,
{
    iter: II,
    f: F,
    buffer: Vec<u8>,
    current: Option<O>,
    was_decompressed: bool,
}

impl<I, O, F, E, II> Decompressor<I, O, F, E, II>
where
    I: Compressed,
    O: Decompressed,
    E: std::error::Error,
    II: Iterator<Item = Result<I, E>>,
    F: Fn(I, &mut Vec<u8>) -> Result<O, E>,
{
    /// Returns a new [`Decompressor`].
    #[inline]
    pub fn new(iter: II, buffer: Vec<u8>, f: F) -> Self {
        Self {
            iter,
            f,
            buffer,
            current: None,
            was_decompressed: false,
        }
    }

    /// Returns its internal buffer, consuming itself.
    #[inline]
    pub fn into_inner(self) -> Vec<u8> {
        self.buffer
    }
}

impl<I, O, F, E, II> FallibleStreamingIterator for Decompressor<I, O, F, E, II>
where
    I: Compressed,
    O: Decompressed,
    E: std::error::Error,
    II: Iterator<Item = Result<I, E>>,
    F: Fn(I, &mut Vec<u8>) -> Result<O, E>,
{
    type Item = O;
    type Error = E;

    #[inline]
    fn advance(&mut self) -> Result<(), E> {
        if let Some(page) = self.current.as_mut() {
            if self.was_decompressed {
                self.buffer = std::mem::take(page.buffer_mut());
            }
        }

        let next = self
            .iter
            .next()
            .map(|maybe_page| {
                maybe_page.and_then(|page| {
                    self.was_decompressed = page.is_compressed();
                    (self.f)(page, &mut self.buffer)
                })
            })
            .transpose()?;
        self.current = next;
        Ok(())
    }

    #[inline]
    fn get(&self) -> Option<&Self::Item> {
        self.current.as_ref()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, PartialEq)]
    struct CompressedItem {
        pub metadata: String,
        pub data: Vec<u8>,
    }
    impl Compressed for CompressedItem {
        fn is_compressed(&self) -> bool {
            self.metadata == "is_compressed"
        }
    }

    #[derive(Debug, PartialEq)]
    struct DecompressedItem {
        pub metadata: String,
        pub data: Vec<u8>,
    }

    impl Decompressed for DecompressedItem {
        fn buffer_mut(&mut self) -> &mut Vec<u8> {
            &mut self.data
        }
    }

    fn decompress(
        mut i: CompressedItem,
        buffer: &mut Vec<u8>,
    ) -> Result<DecompressedItem, std::convert::Infallible> {
        if i.is_compressed() {
            // the actual decompression, more complex stuff can happen.
            buffer.clear();
            buffer.extend(&mut i.data.iter().rev());
        } else {
            std::mem::swap(&mut i.data, buffer);
        };
        Ok(DecompressedItem {
            metadata: i.metadata,
            data: std::mem::take(buffer),
        })
    }

    #[test]
    fn test_basics_uncompressed() {
        let item = CompressedItem {
            metadata: "not_compressed".to_string(),
            data: vec![1, 2, 3],
        };
        let iter = vec![Ok(item)].into_iter();

        let buffer = vec![1];
        let mut decompressor = Decompressor::new(iter, buffer, decompress);

        let item = decompressor.next().unwrap().unwrap();
        assert_eq!(item.data, vec![3, 2, 1]);
        assert_eq!(item.metadata, "not_compressed".to_string());
        assert_eq!(decompressor.next().unwrap(), None);

        // i.e. the internal buffer was not used.
        assert_eq!(decompressor.into_inner(), vec![]);
    }

    #[test]
    fn test_basics_compressed() {
        let item = CompressedItem {
            metadata: "is_compressed".to_string(),
            data: vec![1, 2, 3],
        };
        let iter = vec![Ok(item)].into_iter();

        let buffer = vec![1, 2];
        let mut decompressor = Decompressor::new(iter, buffer, decompress);

        let item = decompressor.next().unwrap().unwrap();
        assert_eq!(item.data, vec![3, 2, 1]);
        assert_eq!(item.metadata, "is_compressed".to_string());
        assert_eq!(decompressor.next().unwrap(), None);

        // i.e. after the last `next`, the last item is consumed and the internal buffer
        // contains its data
        assert_eq!(decompressor.into_inner(), vec![3, 2, 1]);
    }
}