]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2015 Haomai Wang <haomaiwang@gmail.com> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef CEPH_COMPRESSOR_H | |
16 | #define CEPH_COMPRESSOR_H | |
17 | ||
31f18b77 | 18 | #include <memory> |
1e59de90 | 19 | #include <optional> |
7c673cae | 20 | #include <string> |
11fdf7f2 | 21 | #include <string_view> |
11fdf7f2 | 22 | #include "include/ceph_assert.h" // boost clobbers this |
9f95a23c | 23 | #include "include/common_fwd.h" |
7c673cae | 24 | #include "include/buffer.h" |
31f18b77 | 25 | #include "include/int_types.h" |
11fdf7f2 TL |
26 | #ifdef HAVE_QATZIP |
27 | #include "QatAccel.h" | |
28 | #endif | |
7c673cae | 29 | |
f67539c2 TL |
30 | namespace TOPNSPC { |
31 | ||
7c673cae | 32 | class Compressor; |
31f18b77 | 33 | typedef std::shared_ptr<Compressor> CompressorRef; |
7c673cae FG |
34 | |
35 | class Compressor { | |
36 | public: | |
37 | enum CompressionAlgorithm { | |
38 | COMP_ALG_NONE = 0, | |
39 | COMP_ALG_SNAPPY = 1, | |
40 | COMP_ALG_ZLIB = 2, | |
41 | COMP_ALG_ZSTD = 3, | |
b5b8bbf5 | 42 | #ifdef HAVE_LZ4 |
31f18b77 | 43 | COMP_ALG_LZ4 = 4, |
b5b8bbf5 | 44 | #endif |
11fdf7f2 TL |
45 | #ifdef HAVE_BROTLI |
46 | COMP_ALG_BROTLI = 5, | |
47 | #endif | |
48 | COMP_ALG_LAST //the last value for range checks | |
7c673cae | 49 | }; |
11fdf7f2 TL |
50 | |
51 | using pair_type = std::pair<const char*, CompressionAlgorithm>; | |
52 | static constexpr std::initializer_list<pair_type> compression_algorithms { | |
53 | { "none", COMP_ALG_NONE }, | |
54 | { "snappy", COMP_ALG_SNAPPY }, | |
55 | { "zlib", COMP_ALG_ZLIB }, | |
56 | { "zstd", COMP_ALG_ZSTD }, | |
57 | #ifdef HAVE_LZ4 | |
58 | { "lz4", COMP_ALG_LZ4 }, | |
59 | #endif | |
60 | #ifdef HAVE_BROTLI | |
61 | { "brotli", COMP_ALG_BROTLI }, | |
62 | #endif | |
63 | }; | |
64 | ||
7c673cae FG |
65 | // compression options |
66 | enum CompressionMode { | |
67 | COMP_NONE, ///< compress never | |
68 | COMP_PASSIVE, ///< compress if hinted COMPRESSIBLE | |
69 | COMP_AGGRESSIVE, ///< compress unless hinted INCOMPRESSIBLE | |
70 | COMP_FORCE ///< compress always | |
71 | }; | |
72 | ||
11fdf7f2 TL |
73 | #ifdef HAVE_QATZIP |
74 | bool qat_enabled; | |
1e59de90 | 75 | static QatAccel qat_accel; |
11fdf7f2 TL |
76 | #endif |
77 | ||
78 | static const char* get_comp_alg_name(int a); | |
1e59de90 | 79 | static std::optional<CompressionAlgorithm> get_comp_alg_type(std::string_view s); |
7c673cae FG |
80 | |
81 | static const char *get_comp_mode_name(int m); | |
1e59de90 | 82 | static std::optional<CompressionMode> get_comp_mode_type(std::string_view s); |
7c673cae FG |
83 | |
84 | Compressor(CompressionAlgorithm a, const char* t) : alg(a), type(t) { | |
85 | } | |
86 | virtual ~Compressor() {} | |
87 | const std::string& get_type_name() const { | |
88 | return type; | |
89 | } | |
90 | CompressionAlgorithm get_type() const { | |
91 | return alg; | |
92 | } | |
1e59de90 TL |
93 | virtual int compress(const ceph::bufferlist &in, ceph::bufferlist &out, std::optional<int32_t> &compressor_message) = 0; |
94 | virtual int decompress(const ceph::bufferlist &in, ceph::bufferlist &out, std::optional<int32_t> compressor_message) = 0; | |
7c673cae FG |
95 | // this is a bit weird but we need non-const iterator to be in |
96 | // alignment with decode methods | |
1e59de90 | 97 | virtual int decompress(ceph::bufferlist::const_iterator &p, size_t compressed_len, ceph::bufferlist &out, std::optional<int32_t> compressor_message) = 0; |
7c673cae FG |
98 | |
99 | static CompressorRef create(CephContext *cct, const std::string &type); | |
100 | static CompressorRef create(CephContext *cct, int alg); | |
101 | ||
102 | protected: | |
103 | CompressionAlgorithm alg; | |
104 | std::string type; | |
105 | ||
106 | }; | |
107 | ||
f67539c2 | 108 | } // namespace TOPNSPC |
7c673cae | 109 | #endif |