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