OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Compressor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
24 #pragma once
25 
26 #include <cstddef>
27 #include <cstdint>
28 #include <mutex>
29 #include <stdexcept>
30 #include <string>
31 
32 class CompressionFailedError : public std::runtime_error {
33  public:
34  CompressionFailedError() : std::runtime_error("Compression Failed") {}
35  CompressionFailedError(const std::string& e) : std::runtime_error(e) {}
36 };
37 
39  public:
41 
42  // Compression algorithm takes extra space for scratch work, it uses the output
43  // buffer to do the scratch work. We have to provide the compressor extra 10%
44  // space for it.
45  // https://github.com/Blosc/c-blosc/blob/c7792d6153eaf3d3d86eb33a28e9c613d2337040/blosc/blosclz.h#L28
46  inline size_t getScratchSpaceSize(const size_t len) const {
47  return static_cast<size_t>(len * 1.1);
48  }
49 
50  // requires a compressed buffer at least as large as uncompressed buffer.
51  // use 0 to always force compression for min_compressor_bytes.
52  int64_t compress(const uint8_t* buffer,
53  const size_t buffer_size,
54  uint8_t* compressed_buffer,
55  const size_t compressed_buffer_size,
56  const size_t min_compressor_bytes);
57  std::string compress(const std::string& buffer);
58 
59  size_t decompress(const uint8_t* compressed_buffer,
60  uint8_t* decompressed_buffer,
61  const size_t decompressed_size);
62  std::string decompress(const std::string& buffer, const size_t decompressed_size);
63 
64  size_t compressOrMemcpy(const uint8_t* input_buffer,
65  uint8_t* output_buffer,
66  const size_t uncompressed_size,
67  const size_t min_compressor_bytes);
68 
69  bool decompressOrMemcpy(const uint8_t* compressed_buffer,
70  const size_t compressed_buffer_size,
71  uint8_t* decompressed_buffer,
72  const size_t decompressed_size);
73 
74  void getBloscBufferSizes(const uint8_t* data_ptr,
75  size_t* num_bytes_compressed,
76  size_t* num_bytes_uncompressed,
77  size_t* block_size);
78 
79  int setThreads(size_t num_threads);
80 
81  int setCompressor(std::string& compressor);
82 
84 
85  private:
87  std::mutex compressor_lock;
89 };
int setThreads(size_t num_threads)
Definition: Compressor.cpp:222
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:119
size_t getScratchSpaceSize(const size_t len) const
Definition: Compressor.h:46
int setCompressor(std::string &compressor)
Definition: Compressor.cpp:227
size_t compressOrMemcpy(const uint8_t *input_buffer, uint8_t *output_buffer, const size_t uncompressed_size, const size_t min_compressor_bytes)
Definition: Compressor.cpp:160
std::mutex compressor_lock
Definition: Compressor.h:87
bool decompressOrMemcpy(const uint8_t *compressed_buffer, const size_t compressed_buffer_size, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:184
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
static BloscCompressor * getCompressor()
Definition: Compressor.cpp:212
CompressionFailedError(const std::string &e)
Definition: Compressor.h:35
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
static BloscCompressor * instance
Definition: Compressor.h:88