OmniSciDB  7bf56492aa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 39 of file Compressor.h.

Constructor & Destructor Documentation

BloscCompressor::~BloscCompressor ( )

Definition at line 48 of file Compressor.cpp.

References compressor_lock.

48  {
49  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
50  blosc_destroy();
51 }
std::mutex compressor_lock
Definition: Compressor.h:86
BloscCompressor::BloscCompressor ( )
private

Definition at line 34 of file Compressor.cpp.

References compressor_lock.

Referenced by getCompressor().

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

+ 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 53 of file Compressor.cpp.

References compressor_lock, and to_string().

Referenced by compress(), and compressOrMemcpy().

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

+ 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 95 of file Compressor.cpp.

References compress(), and getScratchSpaceSize().

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

+ 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 157 of file Compressor.cpp.

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

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

+ 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 116 of file Compressor.cpp.

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

Referenced by decompress(), and decompressOrMemcpy().

118  {
119  size_t decompressed_buf_len, compressed_buf_len, block_size, decompressed_len = 0;
121  &compressed_buffer[0], &compressed_buf_len, &decompressed_buf_len, &block_size);
122  // check compressed buffer is a blosc compressed buffer.
123  if (compressed_buf_len > 0 && decompressed_size == decompressed_buf_len) {
124  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
125  decompressed_len =
126  blosc_decompress(&compressed_buffer[0], decompressed_buffer, decompressed_size);
127  }
128 
129  if (decompressed_len == 0) {
131  std::string("failed to decompress buffer for compressed size: ") +
132  std::to_string(compressed_buf_len));
133  }
134  if (decompressed_len != decompressed_size) {
136  std::string("decompression buffer size mismatch. Decompressed buffer length: ") +
137  std::to_string(decompressed_len));
138  }
139  return decompressed_len;
140 }
std::string to_string(char const *&&v)
std::mutex compressor_lock
Definition: Compressor.h:86
void getBloscBufferSizes(const uint8_t *data_ptr, size_t *num_bytes_compressed, size_t *num_bytes_uncompressed, size_t *block_size)
Definition: Compressor.cpp:200

+ 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 142 of file Compressor.cpp.

References decompress().

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

+ 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 181 of file Compressor.cpp.

References decompress().

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

+ 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 200 of file Compressor.cpp.

Referenced by decompress().

203  {
204  blosc_cbuffer_sizes(data_ptr, num_bytes_uncompressed, num_bytes_compressed, block_size);
205 }

+ Here is the caller graph for this function:

BloscCompressor * BloscCompressor::getCompressor ( )
static

Definition at line 209 of file Compressor.cpp.

References BloscCompressor(), and instance.

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

+ Here is the call graph for this function:

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

Definition at line 47 of file Compressor.h.

Referenced by compress().

47 { return len * 1.1; }

+ Here is the caller graph for this function:

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

Definition at line 224 of file Compressor.cpp.

References compressor_lock.

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

Definition at line 219 of file Compressor.cpp.

References compressor_lock.

219  {
220  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
221  return blosc_set_nthreads(num_threads);
222 }
std::mutex compressor_lock
Definition: Compressor.h:86

Member Data Documentation

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

Definition at line 87 of file Compressor.h.

Referenced by getCompressor().


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