|
| 1 | +// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::io::{self, Write}; |
| 5 | + |
| 6 | +/// This type wraps a [`Vec`] to provide a [`Write`] interface that has a max |
| 7 | +/// capacity that won't be exceeded. Additionally, it gracefully handles |
| 8 | +/// out-of-memory conditions instead of panicking (unfortunately not compatible |
| 9 | +/// with the `no-panic` crate, though). |
| 10 | +pub struct SizeRestrictedBuffer { |
| 11 | + vec: Vec<u8>, |
| 12 | + max_capacity: usize, |
| 13 | +} |
| 14 | + |
| 15 | +impl SizeRestrictedBuffer { |
| 16 | + pub fn try_new(size_hint: usize, max_capacity: usize) -> io::Result<Self> { |
| 17 | + if size_hint > max_capacity { |
| 18 | + return Err(io::Error::new( |
| 19 | + io::ErrorKind::InvalidInput, |
| 20 | + "size hint shouldn't be larger than max capacity", |
| 21 | + )); |
| 22 | + } |
| 23 | + // Round up to the next power of 2, but don't exceed the max_capacity. |
| 24 | + let initial_capacity = size_hint.next_power_of_two().min(max_capacity); |
| 25 | + let mut vec = Vec::new(); |
| 26 | + vec.try_reserve(initial_capacity)?; |
| 27 | + Ok(SizeRestrictedBuffer { vec, max_capacity }) |
| 28 | + } |
| 29 | + |
| 30 | + pub fn as_slice(&self) -> &[u8] { |
| 31 | + self.vec.as_slice() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl From<SizeRestrictedBuffer> for Vec<u8> { |
| 36 | + fn from(buf: SizeRestrictedBuffer) -> Self { |
| 37 | + buf.vec |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Write for SizeRestrictedBuffer { |
| 42 | + #[inline] |
| 43 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 44 | + let additional = buf.len(); |
| 45 | + if additional <= self.max_capacity.wrapping_sub(self.vec.len()) { |
| 46 | + self.vec.try_reserve(additional)?; |
| 47 | + self.vec.extend(buf); |
| 48 | + Ok(additional) |
| 49 | + } else { |
| 50 | + Err(io::ErrorKind::StorageFull.into()) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + #[inline] |
| 55 | + fn flush(&mut self) -> io::Result<()> { |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type Encoder = zstd::Encoder<'static, SizeRestrictedBuffer>; |
| 61 | + |
| 62 | +/// Used to compress profile data. |
| 63 | +pub struct Compressor { |
| 64 | + encoder: Encoder, |
| 65 | +} |
| 66 | + |
| 67 | +impl Compressor { |
| 68 | + /// Creates a new compressor with the provided configuration. |
| 69 | + /// |
| 70 | + /// - `size_hint`: beginning capacity for the output buffer. This is a |
| 71 | + /// hint for the starting size, and a different one may |
| 72 | + /// be used. |
| 73 | + /// - `max_capacity`: the maximum size for the output buffer (hard limit). |
| 74 | + /// - `compression_level`: see [`zstd::Encoder::new`] for the valid range. |
| 75 | + pub fn try_new( |
| 76 | + size_hint: usize, |
| 77 | + max_capacity: usize, |
| 78 | + compression_level: i32, |
| 79 | + ) -> io::Result<Compressor> { |
| 80 | + let buffer = SizeRestrictedBuffer::try_new(size_hint, max_capacity)?; |
| 81 | + let encoder = Encoder::new(buffer, compression_level)?; |
| 82 | + Ok(Compressor { encoder }) |
| 83 | + } |
| 84 | + |
| 85 | + /// Finish the compression, and return the compressed data. The compressor |
| 86 | + /// remains valid but is missing its encoder, so it will fail to encode |
| 87 | + /// data. |
| 88 | + /// |
| 89 | + /// # Errors |
| 90 | + /// |
| 91 | + /// 1. Fails if the compressor's encoder is missing. |
| 92 | + /// 2. Fails if the encoder fails, e.g., the output buffer is full. |
| 93 | + pub fn finish(self) -> io::Result<Vec<u8>> { |
| 94 | + match self.encoder.try_finish() { |
| 95 | + Ok(buffer) => Ok(buffer.vec), |
| 96 | + Err(err) => Err(err.1), |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Write for Compressor { |
| 102 | + #[inline] |
| 103 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 104 | + let encoder = &mut self.encoder; |
| 105 | + encoder.write(buf) |
| 106 | + } |
| 107 | + |
| 108 | + #[inline] |
| 109 | + fn flush(&mut self) -> io::Result<()> { |
| 110 | + self.encoder.flush() |
| 111 | + } |
| 112 | +} |
0 commit comments