Helper class to compress data using lz4 or zstd presets.
More...
#include <Compressor.h>
|
| int | compress (const void *data, size_t dataSize, CompressionPreset preset, uint32_t &outCompressedSize, size_t headerSpace=0) |
| int | compress (const DataSlice *dataSlices, size_t dataSliceCount, CompressionPreset preset, uint32_t &outCompressedSize, size_t headerSpace=0) |
| int | startFrame (size_t frameSize, CompressionPreset preset, uint32_t &outSize) |
| int | addFrameData (WriteFileHandler &file, const void *data, size_t dataSize, uint32_t &inOutCompressedSize, size_t maxCompressedSize=0) |
| int | endFrame (WriteFileHandler &file, uint32_t &inOutCompressedSize, size_t maxCompressedSize=0) |
| const void * | getData () const |
|
template<class HeaderType> |
| HeaderType * | getHeader () |
| | Get the space reserved for a header.
|
|
CompressionType | getCompressionType () const |
| void | clear () |
|
|
static const size_t | kMinByteCountForCompression = 250 |
Helper class to compress data using lz4 or zstd presets.
You can switch between presets at no cost. If presets don't work well enough with your data, you can easily experiment with new settings and add your own preset to CompressionPreset.
◆ addFrameData()
| int vrs::Compressor::addFrameData |
( |
WriteFileHandler & | file, |
|
|
const void * | data, |
|
|
size_t | dataSize, |
|
|
uint32_t & | inOutCompressedSize, |
|
|
size_t | maxCompressedSize = 0 ) |
Add data to a frame started before. The total amount of data may not exceed the declared size of the frame when it was started.
- Parameters
-
| file | A file open for writing. |
| data | A pointer to the data to add to the frame. |
| dataSize | Number of bytes to add to the frame. |
| inOutCompressedSize | Updated number of bytes written out to the file. |
| maxCompressedSize | Max number of bytes the compressed record may get. Fail if the compressed data is larger, while guarantying that fewer bytes have been written to disk. |
- Returns
- 0 for success, or an error code. The input buffer may be recycled immediately, but all the data may not have been written out to the file yet. You may add all the data of the frame in as many calls as you wish, one byte at a time, all at once, and anything in between.
◆ clear()
| void vrs::Compressor::clear |
( |
| ) |
|
Deallocate the internal buffer's storage, and the compression context of high presets when it is large. Don't call between startFrame() and endFrame().
◆ compress() [1/2]
| int vrs::Compressor::compress |
( |
const DataSlice * | dataSlices, |
|
|
size_t | dataSliceCount, |
|
|
CompressionPreset | preset, |
|
|
uint32_t & | outCompressedSize, |
|
|
size_t | headerSpace = 0 ) |
Compress several memory buffers (provided by dataSlices) into a single frame that decompresses to their concatenation. The compressed bytes of multiple slices are not necessarily identical to compressing the concatenation in one piece, but decode to the same data.
- Parameters
-
| dataSlices | The data slices, compressed in order as one payload. |
| dataSliceCount | Number of data slices. |
| preset | Compression preset to use. |
| outCompressedSize | Number of compressed bytes, or zero when compression was not used. |
| headerSpace | Number of bytes to reserve at the beginning of the buffer for a header initialized manually later. |
- Returns
- 0 for success, or an error code.
◆ compress() [2/2]
| int vrs::Compressor::compress |
( |
const void * | data, |
|
|
size_t | dataSize, |
|
|
CompressionPreset | preset, |
|
|
uint32_t & | outCompressedSize, |
|
|
size_t | headerSpace = 0 ) |
Compress some data using a specific preset. If the compression setting doesn't lead to a smaller payload, because the data can't be compressed, then the record won't be compressed at all.
- Parameters
-
| data | Pointer to the data to compress. |
| dataSize | Number of bytes in the buffer to compress. |
| preset | Compression preset to use. |
| outCompressedSize | Number of compressed bytes, or zero when compression was not used. |
| headerSpace | Number of bytes to reserve at the beginning of the buffer for a header initialized manually later. |
- Returns
- 0 for success, or an error code.
◆ endFrame()
| int vrs::Compressor::endFrame |
( |
WriteFileHandler & | file, |
|
|
uint32_t & | inOutCompressedSize, |
|
|
size_t | maxCompressedSize = 0 ) |
Write out all the data left in internal compression buffers to disk, and complete the frame. After this call has been made, a new frame maybe started.
- Parameters
-
| file | A file open for writing. |
| inOutCompressedSize | Updated number of bytes written out to the file. |
| maxCompressedSize | Max number of bytes the compressed record may get. Fail if the compressed data is larger, while guarantying that fewer bytes have been written to disk. |
- Returns
- 0 for success, or an error code. After this call, inOutCompressedSize counts the total number of bytes used for the compressed frame, including compression metadata. This number should be smaller than the frame size, but might be slightly larger if the data couldn't be compressed.
◆ getData()
| const void * vrs::Compressor::getData |
( |
| ) |
const |
|
inline |
Get the compressed data after compression. The size to consider was returned by compress().
- Returns
- Pointer to the compressed data.
◆ startFrame()
| int vrs::Compressor::startFrame |
( |
size_t | frameSize, |
|
|
CompressionPreset | preset, |
|
|
uint32_t & | outSize ) |
Frame compression APIs, with streaming to a file. Write to a file a block of data (a "frame") to be compressed. That data will be logically self contained, and its size will be retrievable when decoding the first byte. The frame can be added in multiple calls, making it easy to write a lot of data without needing large intermediate buffers.
Start a new frame, declaring its full size upfront. This size must be respected.
- Parameters
-
| frameSize | exact number of bytes that will be added to the frame in total. |
| preset | compression preset to use (lz4 or zstd). |
| outSize | total number of compressed bytes written. |
- Returns
- 0 for success, or an error code. This call initializes outSize to 0.
The documentation for this class was generated from the following files: