]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2014 Haomai Wang <haomaiwang@gmail.com>
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.
20 #include "CompressionPlugin.h"
21 #include "Compressor.h"
22 #include "include/random.h"
23 #include "common/ceph_context.h"
24 #include "common/debug.h"
25 #include "common/dout.h"
29 const char* Compressor::get_comp_alg_name(int a
) {
31 auto p
= std::find_if(std::cbegin(compression_algorithms
), std::cend(compression_algorithms
),
32 [a
](const auto& kv
) { return kv
.second
== a
; });
34 if (std::cend(compression_algorithms
) == p
)
35 return "???"; // It would be nice to revise this...
40 boost::optional
<Compressor::CompressionAlgorithm
>
41 Compressor::get_comp_alg_type(std::string_view s
) {
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
)
51 const char *Compressor::get_comp_mode_name(int 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 "???";
60 boost::optional
<Compressor::CompressionMode
>
61 Compressor::get_comp_mode_type(std::string_view s
) {
64 if (s
== "aggressive")
65 return COMP_AGGRESSIVE
;
70 return boost::optional
<CompressionMode
>();
73 CompressorRef
Compressor::create(CephContext
*cct
, const std::string
&type
)
75 // support "random" for teuthology testing
76 if (type
== "random") {
77 int alg
= ceph::util::generate_random_number(0, COMP_ALG_LAST
- 1);
78 if (alg
== COMP_ALG_NONE
) {
81 return create(cct
, alg
);
84 CompressorRef cs_impl
= NULL
;
86 auto reg
= cct
->get_plugin_registry();
87 auto factory
= dynamic_cast<ceph::CompressionPlugin
*>(reg
->get_with_load("compressor", type
));
88 if (factory
== NULL
) {
89 lderr(cct
) << __func__
<< " cannot load compressor of type " << type
<< dendl
;
92 int err
= factory
->factory(&cs_impl
, &ss
);
94 lderr(cct
) << __func__
<< " factory return error " << err
<< dendl
;
98 CompressorRef
Compressor::create(CephContext
*cct
, int alg
)
100 if (alg
< 0 || alg
>= COMP_ALG_LAST
) {
101 lderr(cct
) << __func__
<< " invalid algorithm value:" << alg
<< dendl
;
102 return CompressorRef();
104 std::string type_name
= get_comp_alg_name(alg
);
105 return create(cct
, type_name
);
108 } // namespace TOPNSPC