OmniSciDB  dfae7c3b14
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 #pragma once
26 
27 #include <cstddef>
28 #include <cstdint>
29 #include <mutex>
30 #include <stdexcept>
31 #include <string>
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 {
48  return static_cast<size_t>(len * 1.1);
49  }
50 
51  // requires a compressed buffer at least as large as uncompressed buffer.
52  // use 0 to always force compression for min_compressor_bytes.
53  int64_t compress(const uint8_t* buffer,
54  const size_t buffer_size,
55  uint8_t* compressed_buffer,
56  const size_t compressed_buffer_size,
57  const size_t min_compressor_bytes);
58  std::string compress(const std::string& buffer);
59 
60  size_t decompress(const uint8_t* compressed_buffer,
61  uint8_t* decompressed_buffer,
62  const size_t decompressed_size);
63  std::string decompress(const std::string& buffer, const size_t decompressed_size);
64 
65  size_t compressOrMemcpy(const uint8_t* input_buffer,
66  uint8_t* output_buffer,
67  const size_t uncompressed_size,
68  const size_t min_compressor_bytes);
69 
70  bool decompressOrMemcpy(const uint8_t* compressed_buffer,
71  const size_t compressed_buffer_size,
72  uint8_t* decompressed_buffer,
73  const size_t decompressed_size);
74 
75  void getBloscBufferSizes(const uint8_t* data_ptr,
76  size_t* num_bytes_compressed,
77  size_t* num_bytes_uncompressed,
78  size_t* block_size);
79 
80  int setThreads(size_t num_threads);
81 
82  int setCompressor(std::string& compressor);
83 
84  ~BloscCompressor();
85 
86  private:
88  std::mutex compressor_lock;
90 };
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:88
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:89