43 blosc_set_nthreads(std::thread::hardware_concurrency());
48 blosc_set_compressor(BLOSC_LZ4HC_COMPNAME);
57 const uint8_t* buffer,
58 const size_t buffer_size,
59 uint8_t* compressed_buffer,
60 const size_t compressed_buffer_size,
62 if (compressed_buffer_size < BLOSC_MIN_HEADER_LENGTH) {
73 if (buffer_size < min_compressor_bytes && min_compressor_bytes != 0) {
77 const auto compressed_len = blosc_compress(5,
79 sizeof(
unsigned char),
82 &compressed_buffer[0],
83 compressed_buffer_size);
85 if (compressed_len <= 0) {
95 return compressed_len;
99 const auto buffer_size = buffer.size();
102 const size_t compressed_len =
compress((uint8_t*)buffer.c_str(),
104 &compressed_buffer[0],
106 if (compressed_len > 0 && compressed_len < buffer_size) {
111 compressed_buffer.resize(compressed_len);
112 return {compressed_buffer.begin(), compressed_buffer.end()};
120 uint8_t* decompressed_buffer,
121 const size_t decompressed_size) {
122 size_t decompressed_buf_len, compressed_buf_len, block_size, decompressed_len = 0;
124 &compressed_buffer[0], &compressed_buf_len, &decompressed_buf_len, &block_size);
126 if (compressed_buf_len > 0 && decompressed_size == decompressed_buf_len) {
129 blosc_decompress(&compressed_buffer[0], decompressed_buffer, decompressed_size);
132 if (decompressed_len == 0) {
134 std::string(
"failed to decompress buffer for compressed size: ") +
137 if (decompressed_len != decompressed_size) {
139 std::string(
"decompression buffer size mismatch. Decompressed buffer length: ") +
142 return decompressed_len;
146 const size_t decompressed_size) {
147 std::vector<uint8_t> decompressed_buffer(decompressed_size);
148 if (buffer.size() == decompressed_size) {
153 (uint8_t*)&buffer[0], (uint8_t*)&decompressed_buffer[0], decompressed_size);
154 return {decompressed_buffer.begin(), decompressed_buffer.end()};
161 uint8_t* output_buffer,
162 size_t uncompressed_size,
163 const size_t min_compressor_bytes) {
165 const auto compressed_size =
compress(input_buffer,
169 min_compressor_bytes);
170 if (compressed_size > 0) {
171 return compressed_size;
176 if (uncompressed_size > min_compressor_bytes) {
177 LOG(
WARNING) <<
"Compressor failed for byte size of " << uncompressed_size;
180 memcpy(output_buffer, input_buffer, uncompressed_size);
181 return uncompressed_size;
185 const size_t compressed_size,
186 uint8_t* decompressed_buffer,
187 const size_t decompressed_size) {
189 decompress(compressed_buffer, decompressed_buffer, decompressed_size);
194 if (compressed_size > decompressed_size) {
195 throw std::runtime_error(
196 "compressed buffer size is greater than decompressed buffer size.");
199 memcpy(decompressed_buffer, compressed_buffer, decompressed_size);
204 size_t* num_bytes_compressed,
205 size_t* num_bytes_uncompressed,
206 size_t* block_size) {
207 blosc_cbuffer_sizes(data_ptr, num_bytes_uncompressed, num_bytes_compressed, block_size);
213 static std::mutex compressor_singleton_lock;
214 std::lock_guard<std::mutex> singleton_lock(compressor_singleton_lock);
224 return blosc_set_nthreads(static_cast<int>(num_threads));
232 return blosc_set_compressor(compressor_name.c_str());
int setThreads(size_t num_threads)
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
size_t getScratchSpaceSize(const size_t len) const
int setCompressor(std::string &compressor)
size_t g_compression_limit_bytes
size_t compressOrMemcpy(const uint8_t *input_buffer, uint8_t *output_buffer, const size_t uncompressed_size, const size_t min_compressor_bytes)
singleton class to handle concurrancy and state for blosc library. A C++ wrapper over a pure C librar...
std::mutex compressor_lock
bool decompressOrMemcpy(const uint8_t *compressed_buffer, const size_t compressed_buffer_size, uint8_t *decompressed_buffer, const size_t decompressed_size)
void getBloscBufferSizes(const uint8_t *data_ptr, size_t *num_bytes_compressed, size_t *num_bytes_uncompressed, size_t *block_size)
static BloscCompressor * getCompressor()
int64_t compress(const uint8_t *buffer, const size_t buffer_size, uint8_t *compressed_buffer, const size_t compressed_buffer_size, const size_t min_compressor_bytes)
static BloscCompressor * instance