summaryrefslogtreecommitdiffstats
path: root/src/compressor/Compressor.h
blob: a696521ad6745d96d6f067019f342865ee02d74a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2015 Haomai Wang <haomaiwang@gmail.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 *
 */

#ifndef CEPH_COMPRESSOR_H
#define CEPH_COMPRESSOR_H

#include <memory>
#include <string>
#include <string_view>
#include <boost/optional.hpp>
#include "include/ceph_assert.h"    // boost clobbers this
#include "include/buffer.h"
#include "include/int_types.h"
#ifdef HAVE_QATZIP
  #include "QatAccel.h"
#endif

class Compressor;
typedef std::shared_ptr<Compressor> CompressorRef;
class CephContext;

class Compressor {
public:
  enum CompressionAlgorithm {
    COMP_ALG_NONE = 0,
    COMP_ALG_SNAPPY = 1,
    COMP_ALG_ZLIB = 2,
    COMP_ALG_ZSTD = 3,
#ifdef HAVE_LZ4
    COMP_ALG_LZ4 = 4,
#endif
#ifdef HAVE_BROTLI
    COMP_ALG_BROTLI = 5,
#endif
    COMP_ALG_LAST   //the last value for range checks
  };

  using pair_type = std::pair<const char*, CompressionAlgorithm>;
  static constexpr std::initializer_list<pair_type> compression_algorithms {
	{ "none",	COMP_ALG_NONE },
	{ "snappy",	COMP_ALG_SNAPPY },
	{ "zlib",	COMP_ALG_ZLIB },
	{ "zstd",	COMP_ALG_ZSTD },
#ifdef HAVE_LZ4
	{ "lz4",	COMP_ALG_LZ4 },
#endif
#ifdef HAVE_BROTLI
	{ "brotli",	COMP_ALG_BROTLI },
#endif
  };

  // compression options
  enum CompressionMode {
    COMP_NONE,                  ///< compress never
    COMP_PASSIVE,               ///< compress if hinted COMPRESSIBLE
    COMP_AGGRESSIVE,            ///< compress unless hinted INCOMPRESSIBLE
    COMP_FORCE                  ///< compress always
  };

#ifdef HAVE_QATZIP
  bool qat_enabled;
  QatAccel qat_accel;
#endif

  static const char* get_comp_alg_name(int a);
  static boost::optional<CompressionAlgorithm> get_comp_alg_type(const std::string &s);

  static const char *get_comp_mode_name(int m);
  static boost::optional<CompressionMode> get_comp_mode_type(const std::string &s);

  Compressor(CompressionAlgorithm a, const char* t) : alg(a), type(t) {
  }
  virtual ~Compressor() {}
  const std::string& get_type_name() const {
    return type;
  }
  CompressionAlgorithm get_type() const {
    return alg;
  }
  virtual int compress(const ceph::bufferlist &in, ceph::bufferlist &out) = 0;
  virtual int decompress(const ceph::bufferlist &in, ceph::bufferlist &out) = 0;
  // this is a bit weird but we need non-const iterator to be in
  // alignment with decode methods
  virtual int decompress(ceph::bufferlist::const_iterator &p, size_t compressed_len, ceph::bufferlist &out) = 0;

  static CompressorRef create(CephContext *cct, const std::string &type);
  static CompressorRef create(CephContext *cct, int alg);

protected:
  CompressionAlgorithm alg;
  std::string type;

};

#endif