]> git.proxmox.com Git - ceph.git/blob - ceph/src/compressor/Compressor.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / compressor / Compressor.h
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
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include <boost/optional.hpp>
22 #include "include/ceph_assert.h" // boost clobbers this
23 #include "include/common_fwd.h"
24 #include "include/buffer.h"
25 #include "include/int_types.h"
26 #ifdef HAVE_QATZIP
27 #include "QatAccel.h"
28 #endif
29
30 class Compressor;
31 typedef std::shared_ptr<Compressor> CompressorRef;
32
33 class Compressor {
34 public:
35 enum CompressionAlgorithm {
36 COMP_ALG_NONE = 0,
37 COMP_ALG_SNAPPY = 1,
38 COMP_ALG_ZLIB = 2,
39 COMP_ALG_ZSTD = 3,
40 #ifdef HAVE_LZ4
41 COMP_ALG_LZ4 = 4,
42 #endif
43 #ifdef HAVE_BROTLI
44 COMP_ALG_BROTLI = 5,
45 #endif
46 COMP_ALG_LAST //the last value for range checks
47 };
48
49 using pair_type = std::pair<const char*, CompressionAlgorithm>;
50 static constexpr std::initializer_list<pair_type> compression_algorithms {
51 { "none", COMP_ALG_NONE },
52 { "snappy", COMP_ALG_SNAPPY },
53 { "zlib", COMP_ALG_ZLIB },
54 { "zstd", COMP_ALG_ZSTD },
55 #ifdef HAVE_LZ4
56 { "lz4", COMP_ALG_LZ4 },
57 #endif
58 #ifdef HAVE_BROTLI
59 { "brotli", COMP_ALG_BROTLI },
60 #endif
61 };
62
63 // compression options
64 enum CompressionMode {
65 COMP_NONE, ///< compress never
66 COMP_PASSIVE, ///< compress if hinted COMPRESSIBLE
67 COMP_AGGRESSIVE, ///< compress unless hinted INCOMPRESSIBLE
68 COMP_FORCE ///< compress always
69 };
70
71 #ifdef HAVE_QATZIP
72 bool qat_enabled;
73 QatAccel qat_accel;
74 #endif
75
76 static const char* get_comp_alg_name(int a);
77 static boost::optional<CompressionAlgorithm> get_comp_alg_type(const std::string &s);
78
79 static const char *get_comp_mode_name(int m);
80 static boost::optional<CompressionMode> get_comp_mode_type(const std::string &s);
81
82 Compressor(CompressionAlgorithm a, const char* t) : alg(a), type(t) {
83 }
84 virtual ~Compressor() {}
85 const std::string& get_type_name() const {
86 return type;
87 }
88 CompressionAlgorithm get_type() const {
89 return alg;
90 }
91 virtual int compress(const ceph::bufferlist &in, ceph::bufferlist &out) = 0;
92 virtual int decompress(const ceph::bufferlist &in, ceph::bufferlist &out) = 0;
93 // this is a bit weird but we need non-const iterator to be in
94 // alignment with decode methods
95 virtual int decompress(ceph::bufferlist::const_iterator &p, size_t compressed_len, ceph::bufferlist &out) = 0;
96
97 static CompressorRef create(CephContext *cct, const std::string &type);
98 static CompressorRef create(CephContext *cct, int alg);
99
100 protected:
101 CompressionAlgorithm alg;
102 std::string type;
103
104 };
105
106 #endif