Struct q_compress::standalone::Compressor
source · pub struct Compressor<T: NumberLike>(_);
Expand description
Converts vectors of numbers into compressed bytes in .qco format.
Most compressor methods leave its state unchanged if they return an error.
You can configure behavior like compression level by instantiating with
.from_config()
You can use the standalone compressor at a file or chunk level.
use q_compress::standalone::Compressor;
let my_nums = vec![1, 2, 3];
// FILE LEVEL
let mut compressor = Compressor::<i32>::default();
let bytes = compressor.simple_compress(&my_nums);
// CHUNK LEVEL
let mut compressor = Compressor::<i32>::default();
compressor.header().expect("header");
compressor.chunk(&my_nums).expect("chunk");
compressor.footer().expect("footer");
let bytes = compressor.drain_bytes();
Note that in practice we would need larger chunks than this to achieve good compression, preferably containing 2k-10M numbers.
Implementations§
source§impl<T: NumberLike> Compressor<T>
impl<T: NumberLike> Compressor<T>
sourcepub fn from_config(config: CompressorConfig) -> Self
pub fn from_config(config: CompressorConfig) -> Self
Creates a new compressor, given a CompressorConfig
.
Internally, the compressor builds Flags
as well as an internal
configuration that doesn’t show up in the output file.
You can inspect the flags it chooses with .flags()
.
sourcepub fn header(&mut self) -> QCompressResult<()>
pub fn header(&mut self) -> QCompressResult<()>
Writes out a header using the compressor’s data type and flags. Will return an error if the compressor has already written the header.
Each .qco file must start with such a header, which contains:
- a 4-byte magic header for “qco!” in ascii,
- a byte for the data type (e.g.
i64
has byte 1 andf64
has byte 5), and - bytes for the flags used to compress.
sourcepub fn chunk(&mut self, nums: &[T]) -> QCompressResult<ChunkMetadata<T>>
pub fn chunk(&mut self, nums: &[T]) -> QCompressResult<ChunkMetadata<T>>
Writes out a chunk of data representing the provided numbers. Will return an error if the compressor has not yet written the header or already written the footer.
Each chunk contains a ChunkMetadata
section followed by the chunk body.
The chunk body encodes the numbers passed in here.
Writes out a single footer byte indicating that the .qco file has ended. Will return an error if the compressor has not yet written the header or already written the footer.
sourcepub fn simple_compress(&mut self, nums: &[T]) -> Vec<u8>
pub fn simple_compress(&mut self, nums: &[T]) -> Vec<u8>
Takes in a slice of numbers and returns compressed bytes.
Unlike most methods, this does not guarantee atomicity of the compressor’s state.
sourcepub fn drain_bytes(&mut self) -> Vec<u8>
pub fn drain_bytes(&mut self) -> Vec<u8>
Returns all bytes produced by the compressor so far that have not yet been read.
Trait Implementations§
source§impl<T: Clone + NumberLike> Clone for Compressor<T>
impl<T: Clone + NumberLike> Clone for Compressor<T>
source§fn clone(&self) -> Compressor<T>
fn clone(&self) -> Compressor<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more