Refactoring of settings profiles to store setting_index instead of setting_name.

This commit is contained in:
Vitaly Baranov 2020-04-21 13:21:03 +03:00
parent 66e348a93f
commit b93a15ef36
9 changed files with 98 additions and 56 deletions

View File

@ -30,16 +30,23 @@ void SettingsConstraints::clear()
}
void SettingsConstraints::setMinValue(const StringRef & name, const Field & min_value)
void SettingsConstraints::setMinValue(const StringRef & setting_name, const Field & min_value)
{
setMinValue(Settings::findIndexStrict(setting_name), min_value);
}
void SettingsConstraints::setMinValue(size_t setting_index, const Field & min_value)
{
size_t setting_index = Settings::findIndexStrict(name);
getConstraintRef(setting_index).min_value = Settings::valueToCorrespondingType(setting_index, min_value);
}
Field SettingsConstraints::getMinValue(const StringRef & name) const
Field SettingsConstraints::getMinValue(const StringRef & setting_name) const
{
return getMinValue(Settings::findIndexStrict(setting_name));
}
Field SettingsConstraints::getMinValue(size_t setting_index) const
{
size_t setting_index = Settings::findIndexStrict(name);
const auto * ptr = tryGetConstraint(setting_index);
if (ptr)
return ptr->min_value;
@ -50,14 +57,21 @@ Field SettingsConstraints::getMinValue(const StringRef & name) const
void SettingsConstraints::setMaxValue(const StringRef & name, const Field & max_value)
{
size_t setting_index = Settings::findIndexStrict(name);
setMaxValue(Settings::findIndexStrict(name), max_value);
}
void SettingsConstraints::setMaxValue(size_t setting_index, const Field & max_value)
{
getConstraintRef(setting_index).max_value = Settings::valueToCorrespondingType(setting_index, max_value);
}
Field SettingsConstraints::getMaxValue(const StringRef & name) const
Field SettingsConstraints::getMaxValue(const StringRef & setting_name) const
{
return getMaxValue(Settings::findIndexStrict(setting_name));
}
Field SettingsConstraints::getMaxValue(size_t setting_index) const
{
size_t setting_index = Settings::findIndexStrict(name);
const auto * ptr = tryGetConstraint(setting_index);
if (ptr)
return ptr->max_value;
@ -66,16 +80,23 @@ Field SettingsConstraints::getMaxValue(const StringRef & name) const
}
void SettingsConstraints::setReadOnly(const StringRef & name, bool read_only)
void SettingsConstraints::setReadOnly(const StringRef & setting_name, bool read_only)
{
setReadOnly(Settings::findIndexStrict(setting_name), read_only);
}
void SettingsConstraints::setReadOnly(size_t setting_index, bool read_only)
{
size_t setting_index = Settings::findIndexStrict(name);
getConstraintRef(setting_index).read_only = read_only;
}
bool SettingsConstraints::isReadOnly(const StringRef & name) const
bool SettingsConstraints::isReadOnly(const StringRef & setting_name) const
{
return isReadOnly(Settings::findIndexStrict(setting_name));
}
bool SettingsConstraints::isReadOnly(size_t setting_index) const
{
size_t setting_index = Settings::findIndexStrict(name);
const auto * ptr = tryGetConstraint(setting_index);
if (ptr)
return ptr->read_only;
@ -83,20 +104,26 @@ bool SettingsConstraints::isReadOnly(const StringRef & name) const
return false;
}
void SettingsConstraints::set(const StringRef & name, const Field & min_value, const Field & max_value, bool read_only)
void SettingsConstraints::set(const StringRef & setting_name, const Field & min_value, const Field & max_value, bool read_only)
{
set(Settings::findIndexStrict(setting_name), min_value, max_value, read_only);
}
void SettingsConstraints::set(size_t setting_index, const Field & min_value, const Field & max_value, bool read_only)
{
size_t setting_index = Settings::findIndexStrict(name);
auto & ref = getConstraintRef(setting_index);
ref.min_value = min_value;
ref.max_value = max_value;
ref.read_only = read_only;
}
void SettingsConstraints::get(const StringRef & name, Field & min_value, Field & max_value, bool & read_only) const
void SettingsConstraints::get(const StringRef & setting_name, Field & min_value, Field & max_value, bool & read_only) const
{
get(Settings::findIndexStrict(setting_name), min_value, max_value, read_only);
}
void SettingsConstraints::get(size_t setting_index, Field & min_value, Field & max_value, bool & read_only) const
{
size_t setting_index = Settings::findIndexStrict(name);
const auto * ptr = tryGetConstraint(setting_index);
if (ptr)
{
@ -112,7 +139,6 @@ void SettingsConstraints::get(const StringRef & name, Field & min_value, Field &
}
}
void SettingsConstraints::merge(const SettingsConstraints & other)
{
for (const auto & [setting_index, other_constraint] : other.constraints_by_index)

View File

@ -1,7 +1,10 @@
#pragma once
#include <Core/Field.h>
#include <Common/SettingsChanges.h>
#include <Core/Settings.h>
#include <common/StringRef.h>
#include <unordered_map>
namespace Poco
{
@ -60,17 +63,25 @@ public:
void clear();
bool empty() const { return constraints_by_index.empty(); }
void setMinValue(const StringRef & name, const Field & min_value);
Field getMinValue(const StringRef & name) const;
void setMinValue(const StringRef & setting_name, const Field & min_value);
void setMinValue(size_t setting_index, const Field & min_value);
Field getMinValue(const StringRef & setting_name) const;
Field getMinValue(size_t setting_index) const;
void setMaxValue(const StringRef & name, const Field & max_value);
Field getMaxValue(const StringRef & name) const;
void setMaxValue(const StringRef & setting_name, const Field & max_value);
void setMaxValue(size_t setting_index, const Field & max_value);
Field getMaxValue(const StringRef & setting_name) const;
Field getMaxValue(size_t setting_index) const;
void setReadOnly(const StringRef & name, bool read_only);
bool isReadOnly(const StringRef & name) const;
void setReadOnly(const StringRef & setting_name, bool read_only);
void setReadOnly(size_t setting_index, bool read_only);
bool isReadOnly(const StringRef & setting_name) const;
bool isReadOnly(size_t setting_index) const;
void set(const StringRef & name, const Field & min_value, const Field & max_value, bool read_only);
void get(const StringRef & name, Field & min_value, Field & max_value, bool & read_only) const;
void set(const StringRef & setting_name, const Field & min_value, const Field & max_value, bool read_only);
void set(size_t setting_index, const Field & min_value, const Field & max_value, bool read_only);
void get(const StringRef & setting_name, Field & min_value, Field & max_value, bool & read_only) const;
void get(size_t setting_index, Field & min_value, Field & max_value, bool & read_only) const;
void merge(const SettingsConstraints & other);

View File

@ -33,21 +33,20 @@ void SettingsProfileElement::init(const ASTSettingsProfileElement & ast, const A
if (!ast.parent_profile.empty())
parent_profile = name_to_id(ast.parent_profile);
if (!ast.name.empty())
if (!ast.setting_name.empty())
{
name = ast.name;
setting_index = Settings::findIndexStrict(ast.setting_name);
value = ast.value;
min_value = ast.min_value;
max_value = ast.max_value;
readonly = ast.readonly;
size_t index = Settings::findIndexStrict(name);
if (!value.isNull())
value = Settings::valueToCorrespondingType(index, value);
value = Settings::valueToCorrespondingType(setting_index, value);
if (!min_value.isNull())
min_value = Settings::valueToCorrespondingType(index, min_value);
min_value = Settings::valueToCorrespondingType(setting_index, min_value);
if (!max_value.isNull())
max_value = Settings::valueToCorrespondingType(index, max_value);
max_value = Settings::valueToCorrespondingType(setting_index, max_value);
}
}
@ -60,7 +59,9 @@ std::shared_ptr<ASTSettingsProfileElement> SettingsProfileElement::toAST() const
if (parent_profile)
ast->parent_profile = ::DB::toString(*parent_profile);
ast->name = name;
if (setting_index != static_cast<size_t>(-1))
ast->setting_name = Settings::getName(setting_index).toString();
ast->value = value;
ast->min_value = min_value;
ast->max_value = max_value;
@ -81,7 +82,9 @@ std::shared_ptr<ASTSettingsProfileElement> SettingsProfileElement::toASTWithName
ast->parent_profile = *parent_profile_name;
}
ast->name = name;
if (setting_index != static_cast<size_t>(-1))
ast->setting_name = Settings::getName(setting_index).toString();
ast->value = value;
ast->min_value = min_value;
ast->max_value = max_value;
@ -132,8 +135,8 @@ Settings SettingsProfileElements::toSettings() const
Settings res;
for (const auto & elem : *this)
{
if (!elem.name.empty() && !elem.value.isNull())
res.set(elem.name, elem.value);
if ((elem.setting_index != static_cast<size_t>(-1)) && !elem.value.isNull())
res.set(elem.setting_index, elem.value);
}
return res;
}
@ -143,8 +146,8 @@ SettingsChanges SettingsProfileElements::toSettingsChanges() const
SettingsChanges res;
for (const auto & elem : *this)
{
if (!elem.name.empty() && !elem.value.isNull())
res.push_back({elem.name, elem.value});
if ((elem.setting_index != static_cast<size_t>(-1)) && !elem.value.isNull())
res.push_back({Settings::getName(elem.setting_index).toString(), elem.value});
}
return res;
}
@ -154,14 +157,14 @@ SettingsConstraints SettingsProfileElements::toSettingsConstraints() const
SettingsConstraints res;
for (const auto & elem : *this)
{
if (!elem.name.empty())
if (elem.setting_index != static_cast<size_t>(-1))
{
if (!elem.min_value.isNull())
res.setMinValue(elem.name, elem.min_value);
res.setMinValue(elem.setting_index, elem.min_value);
if (!elem.max_value.isNull())
res.setMaxValue(elem.name, elem.max_value);
res.setMaxValue(elem.setting_index, elem.max_value);
if (elem.readonly)
res.setReadOnly(elem.name, *elem.readonly);
res.setReadOnly(elem.setting_index, *elem.readonly);
}
}
return res;

View File

@ -20,13 +20,13 @@ class AccessControlManager;
struct SettingsProfileElement
{
std::optional<UUID> parent_profile;
String name;
size_t setting_index = static_cast<size_t>(-1);
Field value;
Field min_value;
Field max_value;
std::optional<bool> readonly;
auto toTuple() const { return std::tie(parent_profile, name, value, min_value, max_value, readonly); }
auto toTuple() const { return std::tie(parent_profile, setting_index, value, min_value, max_value, readonly); }
friend bool operator==(const SettingsProfileElement & lhs, const SettingsProfileElement & rhs) { return lhs.toTuple() == rhs.toTuple(); }
friend bool operator!=(const SettingsProfileElement & lhs, const SettingsProfileElement & rhs) { return !(lhs == rhs); }
friend bool operator <(const SettingsProfileElement & lhs, const SettingsProfileElement & rhs) { return lhs.toTuple() < rhs.toTuple(); }

View File

@ -1,6 +1,7 @@
#include <Access/SettingsProfilesCache.h>
#include <Access/AccessControlManager.h>
#include <Access/SettingsProfile.h>
#include <Core/Settings.h>
#include <Common/quoteString.h>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>

View File

@ -4,6 +4,7 @@
#include <Access/User.h>
#include <Access/SettingsProfile.h>
#include <Dictionaries/IDictionary.h>
#include <Core/Settings.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/quoteString.h>
#include <Poco/Util/AbstractConfiguration.h>
@ -362,7 +363,7 @@ namespace
for (const String & name : names)
{
SettingsProfileElement profile_element;
profile_element.name = name;
profile_element.setting_index = Settings::findIndexStrict(name);
Poco::Util::AbstractConfiguration::Keys constraint_types;
String path_to_name = path_to_constraints + "." + name;
config.keys(path_to_name, constraint_types);
@ -411,7 +412,7 @@ namespace
}
SettingsProfileElement profile_element;
profile_element.name = key;
profile_element.setting_index = Settings::findIndexStrict(key);
profile_element.value = config.getString(profile_config + "." + key);
profile->elements.emplace_back(std::move(profile_element));
}

View File

@ -31,7 +31,7 @@ void ASTSettingsProfileElement::formatImpl(const FormatSettings & settings, Form
return;
}
settings.ostr << name;
settings.ostr << setting_name;
if (!value.isNull())
{

View File

@ -13,7 +13,7 @@ class ASTSettingsProfileElement : public IAST
{
public:
String parent_profile;
String name;
String setting_name;
Field value;
Field min_value;
Field max_value;
@ -21,7 +21,7 @@ public:
bool id_mode = false; /// If true then `parent_profile` keeps UUID, not a name.
bool use_inherit_keyword = false; /// If true then this element is a part of ASTCreateSettingsProfileQuery.
bool empty() const { return parent_profile.empty() && name.empty(); }
bool empty() const { return parent_profile.empty() && setting_name.empty(); }
String getID(char) const override { return "SettingsProfileElement"; }
ASTPtr clone() const override { return std::make_shared<ASTSettingsProfileElement>(*this); }

View File

@ -102,7 +102,7 @@ namespace
bool ParserSettingsProfileElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
String parent_profile;
String name;
String setting_name;
Field value;
Field min_value;
Field max_value;
@ -119,7 +119,7 @@ bool ParserSettingsProfileElement::parseImpl(Pos & pos, ASTPtr & node, Expected
ASTPtr name_ast;
if (!ParserIdentifier{}.parse(pos, name_ast, expected))
return false;
name = getIdentifierName(name_ast);
setting_name = getIdentifierName(name_ast);
bool has_value_or_constraint = false;
while (parseValue(pos, expected, value) || parseMinMaxValue(pos, expected, min_value, max_value)
@ -134,7 +134,7 @@ bool ParserSettingsProfileElement::parseImpl(Pos & pos, ASTPtr & node, Expected
auto result = std::make_shared<ASTSettingsProfileElement>();
result->parent_profile = std::move(parent_profile);
result->name = std::move(name);
result->setting_name = std::move(setting_name);
result->value = std::move(value);
result->min_value = std::move(min_value);
result->max_value = std::move(max_value);