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.
18 #include "CompressionPlugin.h"
19 #include "Compressor.h"
20 #include "common/ceph_context.h"
21 #include "common/debug.h"
22 #include "common/dout.h"
24 const char * Compressor::get_comp_alg_name(int 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";
31 case COMP_ALG_LZ4: return "lz4";
33 default: return "???";
37 boost::optional<Compressor::CompressionAlgorithm> Compressor::get_comp_alg_type(const std::string &s) {
39 return COMP_ALG_SNAPPY;
48 if (s == "" || s == "none")
51 return boost::optional<CompressionAlgorithm>();
54 const char *Compressor::get_comp_mode_name(int m) {
56 case COMP_NONE: return "none";
57 case COMP_PASSIVE: return "passive";
58 case COMP_AGGRESSIVE: return "aggressive";
59 case COMP_FORCE: return "force";
60 default: return "???";
63 boost::optional<Compressor::CompressionMode> Compressor::get_comp_mode_type(const std::string &s) {
66 if (s == "aggressive")
67 return COMP_AGGRESSIVE;
72 return boost::optional<CompressionMode>();
75 CompressorRef Compressor::create(CephContext *cct, const std::string &type)
77 // support "random" for teuthology testing
78 if (type == "random") {
79 static std::random_device seed;
80 static std::default_random_engine engine(seed());
81 static Spinlock mutex;
83 int alg = COMP_ALG_NONE;
84 std::uniform_int_distribution<> dist(0, COMP_ALG_LAST - 1);
86 std::lock_guard<Spinlock> lock(mutex);
89 if (alg == COMP_ALG_NONE) {
92 return create(cct, alg);
95 CompressorRef cs_impl = NULL;
97 PluginRegistry *reg = cct->get_plugin_registry();
98 CompressionPlugin *factory = dynamic_cast<CompressionPlugin*>(reg->get_with_load("compressor", type));
99 if (factory == NULL) {
100 lderr(cct) << __func__ << " cannot load compressor of type " << type << dendl;
103 int err = factory->factory(&cs_impl, &ss);
105 lderr(cct) << __func__ << " factory return error " << err << dendl;
109 CompressorRef Compressor::create(CephContext *cct, int alg)
111 if (alg < 0 || alg >= COMP_ALG_LAST) {
112 lderr(cct) << __func__ << " invalid algorithm value:" << alg << dendl;
113 return CompressorRef();
115 std::string type_name = get_comp_alg_name(alg);
116 return create(cct, type_name);