OmniSciDB  c07336695a
Compressor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, 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 
25 #include <memory>
26 #include <mutex>
27 #include <string>
28 #include <vector>
29 
30 #ifndef COMPRESSOR_H
31 #define COMPRESSOR_H
32 
33 class CompressionFailedError : public std::runtime_error {
34  public:
35  CompressionFailedError() : std::runtime_error("Compression Failed") {}
36  CompressionFailedError(const std::string& e) : std::runtime_error(e) {}
37 };
38 
40  public:
41  static BloscCompressor* getCompressor();
42 
43  // Compression algorithm takes extra space for scratch work, it uses the output
44  // buffer to do the scratch work. We have to provide the compressor extra 10%
45  // space for it.
46  // https://github.com/Blosc/c-blosc/blob/c7792d6153eaf3d3d86eb33a28e9c613d2337040/blosc/blosclz.h#L28
47  inline size_t getScratchSpaceSize(const size_t len) const { return len * 1.1; }
48 
49  // requires a compressed buffer at least as large as uncompressed buffer.
50  // use 0 to always force compression for min_compressor_bytes.
51  int64_t compress(const uint8_t* buffer,
52  const size_t buffer_size,
53  uint8_t* compressed_buffer,
54  const size_t compressed_buffer_size,
55  const size_t min_compressor_bytes);
56  std::string compress(const std::string& buffer);
57 
58  size_t decompress(const uint8_t* compressed_buffer,
59  uint8_t* decompressed_buffer,
60  const size_t decompressed_size);
61  std::string decompress(const std::string& buffer, const size_t decompressed_size);
62 
63  size_t compressOrMemcpy(const uint8_t* input_buffer,
64  uint8_t* output_buffer,
65  const size_t uncompressed_size,
66  const size_t min_compressor_bytes);
67 
68  bool decompressOrMemcpy(const uint8_t* compressed_buffer,
69  const size_t compressed_buffer_size,
70  uint8_t* decompressed_buffer,
71  const size_t decompressed_size);
72 
73  void getBloscBufferSizes(const uint8_t* data_ptr,
74  size_t* num_bytes_compressed,
75  size_t* num_bytes_uncompressed,
76  size_t* block_size);
77 
78  int setThreads(size_t num_threads);
79 
80  int setCompressor(std::string& compressor);
81 
82  ~BloscCompressor();
83 
84  private:
86  std::mutex compressor_lock;
88 };
89 
90 #endif
static DEVICE void decompress(const SQLTypeInfo &ti, int8_t *compressed, VarlenDatum *result, Datum *datum)
Definition: ChunkIter.cpp:26
std::mutex compressor_lock
Definition: Compressor.h:86
size_t getScratchSpaceSize(const size_t len) const
Definition: Compressor.h:47
CompressionFailedError(const std::string &e)
Definition: Compressor.h:36
static BloscCompressor * instance
Definition: Compressor.h:87