OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BloscCompressor Class Reference

#include <Compressor.h>

+ Collaboration diagram for BloscCompressor:

Public Member Functions

size_t getScratchSpaceSize (const size_t len) const
 
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)
 
std::string compress (const std::string &buffer)
 
size_t decompress (const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
 
std::string decompress (const std::string &buffer, const size_t decompressed_size)
 
size_t compressOrMemcpy (const uint8_t *input_buffer, uint8_t *output_buffer, const size_t uncompressed_size, const size_t min_compressor_bytes)
 
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)
 
int setThreads (size_t num_threads)
 
int setCompressor (std::string &compressor)
 
 ~BloscCompressor ()
 

Static Public Member Functions

static BloscCompressorgetCompressor ()
 

Private Member Functions

 BloscCompressor ()
 

Private Attributes

std::mutex compressor_lock
 

Static Private Attributes

static BloscCompressorinstance = NULL
 

Detailed Description

Definition at line 38 of file Compressor.h.

Constructor & Destructor Documentation

BloscCompressor::~BloscCompressor ( )

Definition at line 51 of file Compressor.cpp.

References compressor_lock.

51  {
52  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
53  blosc_destroy();
54 }
std::mutex compressor_lock
Definition: Compressor.h:87
BloscCompressor::BloscCompressor ( )
private

Definition at line 37 of file Compressor.cpp.

References compressor_lock.

Referenced by getCompressor().

37  {
38  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
39  blosc_init();
40  // We use maximum number of threads here since with tests we found that compression
41  // speed gets lear scalling with corresponding to the number of threads being used.
42 
43  blosc_set_nthreads(std::thread::hardware_concurrency());
44 
45  // We chosse faster compressor, accepting slightly lower compression ratio
46  // https://lz4.github.io/lz4/
47 
48  blosc_set_compressor(BLOSC_LZ4HC_COMPNAME);
49 }
std::mutex compressor_lock
Definition: Compressor.h:87

+ Here is the caller graph for this function:

Member Function Documentation

int64_t BloscCompressor::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 = g_compression_limit_bytes 
)

Definition at line 56 of file Compressor.cpp.

References compressor_lock, and to_string().

Referenced by compress(), and compressOrMemcpy().

61  {
62  if (compressed_buffer_size < BLOSC_MIN_HEADER_LENGTH) {
63  // Blosc compressor checks this condition during the initialization
64  // and throw "Output buffer size should be larger than 16 bytes" error
65  // if compressed_buffer_size < 16 (BLOSC_MIN_HEADER_LENGTH)
66  // but after sending interrupt signal, blosc compress function hangs until
67  // thrift timed out error and could not check this code.
68  // here, we can early return by explicitly checking this condition
69  // so as to avoid hangs in query runtime
70  return 0;
71  }
72 
73  if (buffer_size < min_compressor_bytes && min_compressor_bytes != 0) {
74  return 0;
75  }
76  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
77  const auto compressed_len = blosc_compress(5,
78  1,
79  sizeof(unsigned char),
80  buffer_size,
81  buffer,
82  &compressed_buffer[0],
83  compressed_buffer_size);
84 
85  if (compressed_len <= 0) {
86  // something went wrong. blosc retrun codes simply don't provide enough information
87  // for us to decide what.
88  throw CompressionFailedError(std::string("failed to compress result set of length ") +
89  std::to_string(buffer_size));
90  }
91  // we need to tell the other endpoint the size of the acctual data so it can
92  // decide whether it should decompress data or not. So we pass the original
93  // data length. and only send the compressed result if the output of the
94  // compressed result is smaller than the original
95  return compressed_len;
96 }
std::string to_string(char const *&&v)
std::mutex compressor_lock
Definition: Compressor.h:87

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string BloscCompressor::compress ( const std::string &  buffer)

Definition at line 98 of file Compressor.cpp.

References compress(), and getScratchSpaceSize().

98  {
99  const auto buffer_size = buffer.size();
100  std::vector<uint8_t> compressed_buffer(getScratchSpaceSize(buffer_size));
101  try {
102  const size_t compressed_len = compress((uint8_t*)buffer.c_str(),
103  buffer_size,
104  &compressed_buffer[0],
105  getScratchSpaceSize(buffer_size));
106  if (compressed_len > 0 && compressed_len < buffer_size) {
107  // we need to tell the other endpoint the size of the acctual data so it can
108  // decide whether it should decompress data or not. So we pass the original
109  // data length. and only send the compressed result if the output of the
110  // compressed result is smaller than the original
111  compressed_buffer.resize(compressed_len);
112  return {compressed_buffer.begin(), compressed_buffer.end()};
113  }
114  } catch (const CompressionFailedError&) {
115  }
116  return buffer;
117 }
size_t getScratchSpaceSize(const size_t len) const
Definition: Compressor.h:46
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)
Definition: Compressor.cpp:56

+ Here is the call graph for this function:

size_t BloscCompressor::compressOrMemcpy ( const uint8_t *  input_buffer,
uint8_t *  output_buffer,
const size_t  uncompressed_size,
const size_t  min_compressor_bytes 
)

Definition at line 160 of file Compressor.cpp.

References compress(), LOG, and logger::WARNING.

163  {
164  try {
165  const auto compressed_size = compress(input_buffer,
166  uncompressed_size,
167  output_buffer,
168  uncompressed_size,
169  min_compressor_bytes);
170  if (compressed_size > 0) {
171  return compressed_size;
172  }
173  } catch (const CompressionFailedError&) {
174  // catch exceptions from blosc
175  // we copy regardless what happens in compressor
176  if (uncompressed_size > min_compressor_bytes) {
177  LOG(WARNING) << "Compressor failed for byte size of " << uncompressed_size;
178  }
179  }
180  memcpy(output_buffer, input_buffer, uncompressed_size);
181  return uncompressed_size;
182 }
#define LOG(tag)
Definition: Logger.h:285
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)
Definition: Compressor.cpp:56

+ Here is the call graph for this function:

size_t BloscCompressor::decompress ( const uint8_t *  compressed_buffer,
uint8_t *  decompressed_buffer,
const size_t  decompressed_size 
)

Definition at line 119 of file Compressor.cpp.

References compressor_lock, getBloscBufferSizes(), and to_string().

Referenced by decompress(), and decompressOrMemcpy().

121  {
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);
125  // check compressed buffer is a blosc compressed buffer.
126  if (compressed_buf_len > 0 && decompressed_size == decompressed_buf_len) {
127  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
128  decompressed_len =
129  blosc_decompress(&compressed_buffer[0], decompressed_buffer, decompressed_size);
130  }
131 
132  if (decompressed_len == 0) {
134  std::string("failed to decompress buffer for compressed size: ") +
135  std::to_string(compressed_buf_len));
136  }
137  if (decompressed_len != decompressed_size) {
139  std::string("decompression buffer size mismatch. Decompressed buffer length: ") +
140  std::to_string(decompressed_len));
141  }
142  return decompressed_len;
143 }
std::string to_string(char const *&&v)
std::mutex compressor_lock
Definition: Compressor.h:87
void getBloscBufferSizes(const uint8_t *data_ptr, size_t *num_bytes_compressed, size_t *num_bytes_uncompressed, size_t *block_size)
Definition: Compressor.cpp:203

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string BloscCompressor::decompress ( const std::string &  buffer,
const size_t  decompressed_size 
)

Definition at line 145 of file Compressor.cpp.

References decompress().

146  {
147  std::vector<uint8_t> decompressed_buffer(decompressed_size);
148  if (buffer.size() == decompressed_size) {
149  return buffer;
150  }
151  try {
152  decompress(
153  (uint8_t*)&buffer[0], (uint8_t*)&decompressed_buffer[0], decompressed_size);
154  return {decompressed_buffer.begin(), decompressed_buffer.end()};
155  } catch (const CompressionFailedError&) {
156  }
157  return buffer;
158 }
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:119

+ Here is the call graph for this function:

bool BloscCompressor::decompressOrMemcpy ( const uint8_t *  compressed_buffer,
const size_t  compressed_buffer_size,
uint8_t *  decompressed_buffer,
const size_t  decompressed_size 
)

Definition at line 184 of file Compressor.cpp.

References decompress().

187  {
188  try {
189  decompress(compressed_buffer, decompressed_buffer, decompressed_size);
190  return true;
191  } catch (const CompressionFailedError&) {
192  // we will memcpy if we find that the buffer is not compressed
193 
194  if (compressed_size > decompressed_size) {
195  throw std::runtime_error(
196  "compressed buffer size is greater than decompressed buffer size.");
197  }
198  }
199  memcpy(decompressed_buffer, compressed_buffer, decompressed_size);
200  return false;
201 }
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:119

+ Here is the call graph for this function:

void BloscCompressor::getBloscBufferSizes ( const uint8_t *  data_ptr,
size_t *  num_bytes_compressed,
size_t *  num_bytes_uncompressed,
size_t *  block_size 
)

Definition at line 203 of file Compressor.cpp.

Referenced by decompress().

206  {
207  blosc_cbuffer_sizes(data_ptr, num_bytes_uncompressed, num_bytes_compressed, block_size);
208 }

+ Here is the caller graph for this function:

BloscCompressor * BloscCompressor::getCompressor ( )
static

Definition at line 212 of file Compressor.cpp.

References BloscCompressor(), and instance.

212  {
213  static std::mutex compressor_singleton_lock;
214  std::lock_guard<std::mutex> singleton_lock(compressor_singleton_lock);
215  if (instance == NULL) {
216  instance = new BloscCompressor();
217  }
218 
219  return instance;
220 }
static BloscCompressor * instance
Definition: Compressor.h:88

+ Here is the call graph for this function:

size_t BloscCompressor::getScratchSpaceSize ( const size_t  len) const
inline

Definition at line 46 of file Compressor.h.

Referenced by compress().

46  {
47  return static_cast<size_t>(len * 1.1);
48  }

+ Here is the caller graph for this function:

int BloscCompressor::setCompressor ( std::string &  compressor)

Definition at line 227 of file Compressor.cpp.

References compressor_lock.

227  {
228  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
229  // Blosc is resilent enough to detect that the comprressor that was provided to it was
230  // supported or not. If the compressor is invalid or not supported it will simply keep
231  // current compressor.
232  return blosc_set_compressor(compressor_name.c_str());
233 }
std::mutex compressor_lock
Definition: Compressor.h:87
int BloscCompressor::setThreads ( size_t  num_threads)

Definition at line 222 of file Compressor.cpp.

References compressor_lock.

222  {
223  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
224  return blosc_set_nthreads(static_cast<int>(num_threads));
225 }
std::mutex compressor_lock
Definition: Compressor.h:87

Member Data Documentation

std::mutex BloscCompressor::compressor_lock
private
BloscCompressor * BloscCompressor::instance = NULL
staticprivate

Definition at line 88 of file Compressor.h.

Referenced by getCompressor().


The documentation for this class was generated from the following files: