]>
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) 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> | |
31f18b77 | 16 | #include <sstream> |
11fdf7f2 TL |
17 | #include <iterator> |
18 | #include <algorithm> | |
31f18b77 | 19 | |
7c673cae | 20 | #include "CompressionPlugin.h" |
31f18b77 | 21 | #include "Compressor.h" |
11fdf7f2 | 22 | #include "include/random.h" |
31f18b77 FG |
23 | #include "common/ceph_context.h" |
24 | #include "common/debug.h" | |
7c673cae FG |
25 | #include "common/dout.h" |
26 | ||
f67539c2 TL |
27 | namespace TOPNSPC { |
28 | ||
1e59de90 TL |
29 | #ifdef HAVE_QATZIP |
30 | QatAccel Compressor::qat_accel; | |
31 | #endif | |
32 | ||
11fdf7f2 TL |
33 | const char* Compressor::get_comp_alg_name(int a) { |
34 | ||
35 | auto p = std::find_if(std::cbegin(compression_algorithms), std::cend(compression_algorithms), | |
36 | [a](const auto& kv) { return kv.second == a; }); | |
37 | ||
38 | if (std::cend(compression_algorithms) == p) | |
39 | return "???"; // It would be nice to revise this... | |
40 | ||
41 | return p->first; | |
7c673cae FG |
42 | } |
43 | ||
1e59de90 | 44 | std::optional<Compressor::CompressionAlgorithm> |
f67539c2 | 45 | Compressor::get_comp_alg_type(std::string_view s) { |
7c673cae | 46 | |
11fdf7f2 TL |
47 | auto p = std::find_if(std::cbegin(compression_algorithms), std::cend(compression_algorithms), |
48 | [&s](const auto& kv) { return kv.first == s; }); | |
49 | if (std::cend(compression_algorithms) == p) | |
50 | return {}; | |
51 | ||
52 | return p->second; | |
7c673cae FG |
53 | } |
54 | ||
55 | const char *Compressor::get_comp_mode_name(int m) { | |
56 | switch (m) { | |
57 | case COMP_NONE: return "none"; | |
58 | case COMP_PASSIVE: return "passive"; | |
59 | case COMP_AGGRESSIVE: return "aggressive"; | |
60 | case COMP_FORCE: return "force"; | |
61 | default: return "???"; | |
62 | } | |
63 | } | |
1e59de90 | 64 | std::optional<Compressor::CompressionMode> |
f67539c2 | 65 | Compressor::get_comp_mode_type(std::string_view s) { |
7c673cae FG |
66 | if (s == "force") |
67 | return COMP_FORCE; | |
68 | if (s == "aggressive") | |
69 | return COMP_AGGRESSIVE; | |
70 | if (s == "passive") | |
71 | return COMP_PASSIVE; | |
72 | if (s == "none") | |
73 | return COMP_NONE; | |
1e59de90 | 74 | return {}; |
7c673cae FG |
75 | } |
76 | ||
77 | CompressorRef Compressor::create(CephContext *cct, const std::string &type) | |
78 | { | |
79 | // support "random" for teuthology testing | |
80 | if (type == "random") { | |
11fdf7f2 | 81 | int alg = ceph::util::generate_random_number(0, COMP_ALG_LAST - 1); |
7c673cae FG |
82 | if (alg == COMP_ALG_NONE) { |
83 | return nullptr; | |
84 | } | |
85 | return create(cct, alg); | |
86 | } | |
87 | ||
88 | CompressorRef cs_impl = NULL; | |
89 | std::stringstream ss; | |
9f95a23c TL |
90 | auto reg = cct->get_plugin_registry(); |
91 | auto factory = dynamic_cast<ceph::CompressionPlugin*>(reg->get_with_load("compressor", type)); | |
7c673cae FG |
92 | if (factory == NULL) { |
93 | lderr(cct) << __func__ << " cannot load compressor of type " << type << dendl; | |
94 | return NULL; | |
95 | } | |
96 | int err = factory->factory(&cs_impl, &ss); | |
97 | if (err) | |
98 | lderr(cct) << __func__ << " factory return error " << err << dendl; | |
99 | return cs_impl; | |
100 | } | |
101 | ||
102 | CompressorRef Compressor::create(CephContext *cct, int alg) | |
103 | { | |
104 | if (alg < 0 || alg >= COMP_ALG_LAST) { | |
105 | lderr(cct) << __func__ << " invalid algorithm value:" << alg << dendl; | |
106 | return CompressorRef(); | |
107 | } | |
108 | std::string type_name = get_comp_alg_name(alg); | |
109 | return create(cct, type_name); | |
110 | } | |
f67539c2 TL |
111 | |
112 | } // namespace TOPNSPC |