1 /**
2  * @file compressor.h
3  *
4  * @section LICENSE
5  *
6  * The MIT License
7  *
8  * @copyright Copyright (c) 2017-2021 TileDB, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  * @section DESCRIPTION
29  *
30  * This defines the tiledb CompressorType enum that maps to tiledb_compressor_t
31  * C-api enum.
32  */
33 
34 #ifndef TILEDB_COMPRESSOR_H
35 #define TILEDB_COMPRESSOR_H
36 
37 #include "tiledb/common/status.h"
38 #include "tiledb/sm/misc/constants.h"
39 
40 #include <cassert>
41 
42 using namespace tiledb::common;
43 
44 namespace tiledb {
45 namespace sm {
46 
47 /** Defines the compressor type. */
48 enum class Compressor : uint8_t {
49   /** No compressor */
50   NO_COMPRESSION = 0,
51   /** Gzip compressor */
52   GZIP = 1,
53   /** Zstandard compressor */
54   ZSTD = 2,
55   /** LZ4 compressor */
56   LZ4 = 3,
57   /** Run-length encoding compressor */
58   RLE = 4,
59   /** Bzip2 compressor */
60   BZIP2 = 5,
61   /** Double-delta compressor */
62   DOUBLE_DELTA = 6
63 };
64 
65 /** Returns the string representation of the input compressor. */
compressor_str(Compressor type)66 inline const std::string& compressor_str(Compressor type) {
67   switch (type) {
68     case Compressor::NO_COMPRESSION:
69       return constants::no_compression_str;
70     case Compressor::GZIP:
71       return constants::gzip_str;
72     case Compressor::ZSTD:
73       return constants::zstd_str;
74     case Compressor::LZ4:
75       return constants::lz4_str;
76     case Compressor::RLE:
77       return constants::rle_str;
78     case Compressor::BZIP2:
79       return constants::bzip2_str;
80     case Compressor::DOUBLE_DELTA:
81       return constants::double_delta_str;
82     default:
83       return constants::empty_str;
84   }
85 }
86 
87 /** Returns the compressor based on the string representation. */
compressor_enum(const std::string & compressor_type_str,Compressor * compressor)88 inline Status compressor_enum(
89     const std::string& compressor_type_str, Compressor* compressor) {
90   if (compressor_type_str == constants::no_compression_str)
91     *compressor = Compressor::NO_COMPRESSION;
92   else if (compressor_type_str == constants::gzip_str)
93     *compressor = Compressor::GZIP;
94   else if (compressor_type_str == constants::zstd_str)
95     *compressor = Compressor::ZSTD;
96   else if (compressor_type_str == constants::lz4_str)
97     *compressor = Compressor::LZ4;
98   else if (compressor_type_str == constants::rle_str)
99     *compressor = Compressor::RLE;
100   else if (compressor_type_str == constants::bzip2_str)
101     *compressor = Compressor::BZIP2;
102   else if (compressor_type_str == constants::double_delta_str)
103     *compressor = Compressor::DOUBLE_DELTA;
104   else {
105     return Status::Error("Invalid Compressor " + compressor_type_str);
106   }
107   return Status::Ok();
108 }
109 }  // namespace sm
110 }  // namespace tiledb
111 
112 #endif  // TILEDB_COMPRESSOR_H
113