OmniSciDB  1dac507f6e
 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 (buffer_size < min_compressor_bytes && min_compressor_bytes != 0) {
60  return 0;
61  }
62  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
63  const auto compressed_len = blosc_compress(5,
64  1,
65  sizeof(unsigned char),
66  buffer_size,
67  buffer,
68  &compressed_buffer[0],
69  compressed_buffer_size);
70 
71  if (compressed_len <= 0) {
72  // something went wrong. blosc retrun codes simply don't provide enough information
73  // for us to decide what.
74 
75  throw CompressionFailedError(std::string("failed to compress result set of length ") +
76  std::to_string(buffer_size));
77  }
78  // we need to tell the other endpoint the size of the acctual data so it can
79  // decide whether it should decompress data or not. So we pass the original
80  // data length. and only send the compressed result if the output of the
81  // compressed result is smaller than the original
82  return compressed_len;
83 }
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 85 of file Compressor.cpp.

References compress(), and getScratchSpaceSize().

85  {
86  const auto buffer_size = buffer.size();
87  std::vector<uint8_t> compressed_buffer(getScratchSpaceSize(buffer_size));
88  try {
89  const size_t compressed_len = compress((uint8_t*)buffer.c_str(),
90  buffer_size,
91  &compressed_buffer[0],
92  getScratchSpaceSize(buffer_size));
93  if (compressed_len > 0 && compressed_len < buffer_size) {
94  // we need to tell the other endpoint the size of the acctual data so it can
95  // decide whether it should decompress data or not. So we pass the original
96  // data length. and only send the compressed result if the output of the
97  // compressed result is smaller than the original
98  compressed_buffer.resize(compressed_len);
99  return {compressed_buffer.begin(), compressed_buffer.end()};
100  }
101  } catch (const CompressionFailedError& e) {
102  }
103 
104  return buffer;
105 }
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 148 of file Compressor.cpp.

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

151  {
152  try {
153  const auto compressed_size = compress(input_buffer,
154  uncompressed_size,
155  output_buffer,
156  uncompressed_size,
157  min_compressor_bytes);
158  if (compressed_size > 0) {
159  return compressed_size;
160  }
161  } catch (const CompressionFailedError& e) {
162  // catch exceptions from blosc
163  // we copy regardless what happens in compressor
164  if (uncompressed_size > min_compressor_bytes) {
165  LOG(WARNING) << "Compressor failed for byte size of " << uncompressed_size;
166  }
167  }
168  memcpy(output_buffer, input_buffer, uncompressed_size);
169  return uncompressed_size;
170 }
#define LOG(tag)
Definition: Logger.h:185
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 107 of file Compressor.cpp.

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

Referenced by decompress(), and decompressOrMemcpy().

109  {
110  size_t decompressed_buf_len, compressed_buf_len, block_size, decompressed_len = 0;
112  &compressed_buffer[0], &compressed_buf_len, &decompressed_buf_len, &block_size);
113  // check compressed buffer is a blosc compressed buffer.
114  if (compressed_buf_len > 0 && decompressed_size == decompressed_buf_len) {
115  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
116  decompressed_len =
117  blosc_decompress(&compressed_buffer[0], decompressed_buffer, decompressed_size);
118  }
119 
120  if (decompressed_len == 0) {
122  std::string("failed to decompress buffer for compressed size: ") +
123  std::to_string(compressed_buf_len));
124  }
125  if (decompressed_len != decompressed_size) {
127  std::string("decompression buffer size mismatch. Decompressed buffer length: ") +
128  std::to_string(decompressed_len));
129  }
130  return decompressed_len;
131 }
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:191

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

References decompress().

134  {
135  std::vector<uint8_t> decompressed_buffer(decompressed_size);
136  if (buffer.size() == decompressed_size) {
137  return buffer;
138  }
139  try {
140  decompress(
141  (uint8_t*)&buffer[0], (uint8_t*)&decompressed_buffer[0], decompressed_size);
142  return {decompressed_buffer.begin(), decompressed_buffer.end()};
143  } catch (const CompressionFailedError& e) {
144  }
145  return buffer;
146 }
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:107

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

References decompress().

175  {
176  try {
177  decompress(compressed_buffer, decompressed_buffer, decompressed_size);
178  return true;
179  } catch (const CompressionFailedError& e) {
180  // we will memcpy if we find that the buffer is not compressed
181 
182  if (compressed_size > decompressed_size) {
183  throw std::runtime_error(
184  "compressed buffer size is greater than decompressed buffer size.");
185  }
186  }
187  memcpy(decompressed_buffer, compressed_buffer, decompressed_size);
188  return false;
189 }
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:107

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

Referenced by decompress().

194  {
195  blosc_cbuffer_sizes(data_ptr, num_bytes_uncompressed, num_bytes_compressed, block_size);
196 }

+ Here is the caller graph for this function:

BloscCompressor * BloscCompressor::getCompressor ( )
static

Definition at line 200 of file Compressor.cpp.

References BloscCompressor(), and instance.

200  {
201  static std::mutex compressor_singleton_lock;
202  std::lock_guard<std::mutex> singleton_lock(compressor_singleton_lock);
203 
204  if (instance == NULL) {
205  instance = new BloscCompressor();
206  }
207 
208  return instance;
209 }
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 216 of file Compressor.cpp.

References compressor_lock.

216  {
217  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
218  // Blosc is resilent enough to detect that the comprressor that was provided to it was
219  // supported or not. If the compressor is invalid or not supported it will simply keep
220  // current compressor.
221  return blosc_set_compressor(compressor_name.c_str());
222 }
std::mutex compressor_lock
Definition: Compressor.h:86
int BloscCompressor::setThreads ( size_t  num_threads)

Definition at line 211 of file Compressor.cpp.

References compressor_lock.

211  {
212  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
213  return blosc_set_nthreads(num_threads);
214 }
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: