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