summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/src/provider_options.cpp
blob: fcd7a95954633770d8cf23691d23ee9845e89460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2021 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "wsrep/provider_options.hpp"
#include "config_service_v1.hpp"
#include "wsrep/logger.hpp"

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstring>

/**
 * Provider options string separators.
 */
struct provider_options_sep
{
    /** Parameter separator. */
    char param{ ';' };
    /** Key value separator. */
    char key_value{ '=' };
};

// Replace dots in option name with underscores
static void sanitize_name(std::string& name)
{
    std::transform(name.begin(), name.end(), name.begin(),
                   [](std::string::value_type c) {
                       if (c == '.')
                           return '_';
                       return c;
                   });
}

bool wsrep::operator==(const wsrep::provider_options::option& left,
                       const wsrep::provider_options::option& right)
{
    return (std::strcmp(left.name(), right.name()) == 0);
}

// wsrep-API lacks better error code for not found, and this is
// what Galera returns when parameter is not recogized, so we
// got with it.
static enum wsrep::provider::status not_found_error{
    wsrep::provider::error_warning
};

wsrep::provider_options::option::option()
    : name_{}
    , real_name_{}
    , value_{}
    , default_value_{}
    , flags_{ 0 }
{
}

wsrep::provider_options::option::option(
    const std::string& name,
    std::unique_ptr<wsrep::provider_options::option_value> value,
    std::unique_ptr<wsrep::provider_options::option_value> default_value,
    int flags)
    : name_{ name }
    , real_name_{ name }
    , value_{ std::move(value) }
    , default_value_{ std::move(default_value) }
    , flags_{ flags }
{
    sanitize_name(name_);
}

void wsrep::provider_options::option::update_value(
    std::unique_ptr<wsrep::provider_options::option_value> value)
{
    value_ = std::move(value);
}

wsrep::provider_options::option::~option() {}

wsrep::provider_options::provider_options(wsrep::provider& provider)
    : provider_(provider)
    , options_()
{
}

enum wsrep::provider::status wsrep::provider_options::initial_options()
{
    options_.clear();
    if (config_service_v1_fetch(provider_, this))
    {
        return wsrep::provider::error_not_implemented;
    }
    else
    {
        return wsrep::provider::success;
    }
}

const wsrep::provider_options::option*
wsrep::provider_options::get_option(const std::string& name) const
{
    auto ret(options_.find(name));
    if (ret == options_.end())
    {
        return nullptr;
    }
    return ret->second.get();
}

enum wsrep::provider::status wsrep::provider_options::set(
    const std::string& name,
    std::unique_ptr<wsrep::provider_options::option_value> value)
{
    auto option(options_.find(name));
    if (option == options_.end())
    {
        return not_found_error;
    }
    provider_options_sep sep;
    auto ret(provider_.options(std::string(option->second->real_name())
                               + sep.key_value + value->as_string()
                               + sep.param));
    if (ret == provider::success)
    {
        option->second->update_value(std::move(value));
    }
    return ret;
}

enum wsrep::provider::status wsrep::provider_options::set_default(
    const std::string& name,
    std::unique_ptr<wsrep::provider_options::option_value> value,
    std::unique_ptr<wsrep::provider_options::option_value> default_value,
    int flags)
{
    auto found(options_.find(name));
    auto opt(std::unique_ptr<provider_options::option>(
        new option{ name, std::move(value), std::move(default_value), flags }));
    if (found != options_.end())
    {
        assert(0);
        return wsrep::provider::error_not_allowed;
    }
    options_.emplace(std::string(opt->name()), std::move(opt));
    return wsrep::provider::success;
}

void wsrep::provider_options::for_each(const std::function<void(option*)>& fn)
{
    std::for_each(
        options_.begin(), options_.end(),
        [&fn](const options_map::value_type& opt) { fn(opt.second.get()); });
}