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