]> git.proxmox.com Git - ceph.git/blob - ceph/src/compressor/Compressor.cc
update sources to v12.2.0
[ceph.git] / ceph / src / compressor / Compressor.cc
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) 2014 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 #include <random>
16 #include <sstream>
17
18 #include "CompressionPlugin.h"
19 #include "Compressor.h"
20 #include "common/ceph_context.h"
21 #include "common/debug.h"
22 #include "common/dout.h"
23
24 const char * Compressor::get_comp_alg_name(int a) {
25 switch (a) {
26 case COMP_ALG_NONE: return "none";
27 case COMP_ALG_SNAPPY: return "snappy";
28 case COMP_ALG_ZLIB: return "zlib";
29 case COMP_ALG_ZSTD: return "zstd";
30 #ifdef HAVE_LZ4
31 case COMP_ALG_LZ4: return "lz4";
32 #endif
33 default: return "???";
34 }
35 }
36
37 boost::optional<Compressor::CompressionAlgorithm> Compressor::get_comp_alg_type(const std::string &s) {
38 if (s == "snappy")
39 return COMP_ALG_SNAPPY;
40 if (s == "zlib")
41 return COMP_ALG_ZLIB;
42 if (s == "zstd")
43 return COMP_ALG_ZSTD;
44 #ifdef HAVE_LZ4
45 if (s == "lz4")
46 return COMP_ALG_LZ4;
47 #endif
48 if (s == "" || s == "none")
49 return COMP_ALG_NONE;
50
51 return boost::optional<CompressionAlgorithm>();
52 }
53
54 const char *Compressor::get_comp_mode_name(int m) {
55 switch (m) {
56 case COMP_NONE: return "none";
57 case COMP_PASSIVE: return "passive";
58 case COMP_AGGRESSIVE: return "aggressive";
59 case COMP_FORCE: return "force";
60 default: return "???";
61 }
62 }
63 boost::optional<Compressor::CompressionMode> Compressor::get_comp_mode_type(const std::string &s) {
64 if (s == "force")
65 return COMP_FORCE;
66 if (s == "aggressive")
67 return COMP_AGGRESSIVE;
68 if (s == "passive")
69 return COMP_PASSIVE;
70 if (s == "none")
71 return COMP_NONE;
72 return boost::optional<CompressionMode>();
73 }
74
75 CompressorRef Compressor::create(CephContext *cct, const std::string &type)
76 {
77 // support "random" for teuthology testing
78 if (type == "random") {
79 static std::random_device seed;
80 static std::default_random_engine engine(seed());
81 static Spinlock mutex;
82
83 int alg = COMP_ALG_NONE;
84 std::uniform_int_distribution<> dist(0, COMP_ALG_LAST - 1);
85 {
86 std::lock_guard<Spinlock> lock(mutex);
87 alg = dist(engine);
88 }
89 if (alg == COMP_ALG_NONE) {
90 return nullptr;
91 }
92 return create(cct, alg);
93 }
94
95 CompressorRef cs_impl = NULL;
96 std::stringstream ss;
97 PluginRegistry *reg = cct->get_plugin_registry();
98 CompressionPlugin *factory = dynamic_cast<CompressionPlugin*>(reg->get_with_load("compressor", type));
99 if (factory == NULL) {
100 lderr(cct) << __func__ << " cannot load compressor of type " << type << dendl;
101 return NULL;
102 }
103 int err = factory->factory(&cs_impl, &ss);
104 if (err)
105 lderr(cct) << __func__ << " factory return error " << err << dendl;
106 return cs_impl;
107 }
108
109 CompressorRef Compressor::create(CephContext *cct, int alg)
110 {
111 if (alg < 0 || alg >= COMP_ALG_LAST) {
112 lderr(cct) << __func__ << " invalid algorithm value:" << alg << dendl;
113 return CompressorRef();
114 }
115 std::string type_name = get_comp_alg_name(alg);
116 return create(cct, type_name);
117 }