From b93a15ef36beef3c3bcea1fdb892fe79c10bfd9a Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Tue, 21 Apr 2020 13:21:03 +0300 Subject: [PATCH] Refactoring of settings profiles to store setting_index instead of setting_name. --- src/Access/SettingsConstraints.cpp | 68 ++++++++++++++------ src/Access/SettingsConstraints.h | 29 ++++++--- src/Access/SettingsProfileElement.cpp | 35 +++++----- src/Access/SettingsProfileElement.h | 4 +- src/Access/SettingsProfilesCache.cpp | 1 + src/Access/UsersConfigAccessStorage.cpp | 5 +- src/Parsers/ASTSettingsProfileElement.cpp | 2 +- src/Parsers/ASTSettingsProfileElement.h | 4 +- src/Parsers/ParserSettingsProfileElement.cpp | 6 +- 9 files changed, 98 insertions(+), 56 deletions(-) diff --git a/src/Access/SettingsConstraints.cpp b/src/Access/SettingsConstraints.cpp index 21ca2c8ab26..11dbd016e64 100644 --- a/src/Access/SettingsConstraints.cpp +++ b/src/Access/SettingsConstraints.cpp @@ -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) diff --git a/src/Access/SettingsConstraints.h b/src/Access/SettingsConstraints.h index 074dc66d123..65537250957 100644 --- a/src/Access/SettingsConstraints.h +++ b/src/Access/SettingsConstraints.h @@ -1,7 +1,10 @@ #pragma once +#include #include -#include +#include +#include + 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); diff --git a/src/Access/SettingsProfileElement.cpp b/src/Access/SettingsProfileElement.cpp index b052f8b5e75..d4f6ff5d0f2 100644 --- a/src/Access/SettingsProfileElement.cpp +++ b/src/Access/SettingsProfileElement.cpp @@ -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 SettingsProfileElement::toAST() const if (parent_profile) ast->parent_profile = ::DB::toString(*parent_profile); - ast->name = name; + if (setting_index != static_cast(-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 SettingsProfileElement::toASTWithName ast->parent_profile = *parent_profile_name; } - ast->name = name; + if (setting_index != static_cast(-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(-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(-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(-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; diff --git a/src/Access/SettingsProfileElement.h b/src/Access/SettingsProfileElement.h index abcac2567c8..f64317344b8 100644 --- a/src/Access/SettingsProfileElement.h +++ b/src/Access/SettingsProfileElement.h @@ -20,13 +20,13 @@ class AccessControlManager; struct SettingsProfileElement { std::optional parent_profile; - String name; + size_t setting_index = static_cast(-1); Field value; Field min_value; Field max_value; std::optional 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(); } diff --git a/src/Access/SettingsProfilesCache.cpp b/src/Access/SettingsProfilesCache.cpp index f283715e129..516b47213fe 100644 --- a/src/Access/SettingsProfilesCache.cpp +++ b/src/Access/SettingsProfilesCache.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Access/UsersConfigAccessStorage.cpp b/src/Access/UsersConfigAccessStorage.cpp index c4335640b25..0389734e236 100644 --- a/src/Access/UsersConfigAccessStorage.cpp +++ b/src/Access/UsersConfigAccessStorage.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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)); } diff --git a/src/Parsers/ASTSettingsProfileElement.cpp b/src/Parsers/ASTSettingsProfileElement.cpp index 24f1aa60813..a7955411347 100644 --- a/src/Parsers/ASTSettingsProfileElement.cpp +++ b/src/Parsers/ASTSettingsProfileElement.cpp @@ -31,7 +31,7 @@ void ASTSettingsProfileElement::formatImpl(const FormatSettings & settings, Form return; } - settings.ostr << name; + settings.ostr << setting_name; if (!value.isNull()) { diff --git a/src/Parsers/ASTSettingsProfileElement.h b/src/Parsers/ASTSettingsProfileElement.h index ee1ee28c383..6a54bca3213 100644 --- a/src/Parsers/ASTSettingsProfileElement.h +++ b/src/Parsers/ASTSettingsProfileElement.h @@ -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(*this); } diff --git a/src/Parsers/ParserSettingsProfileElement.cpp b/src/Parsers/ParserSettingsProfileElement.cpp index 31bc339f544..37044e8ccbe 100644 --- a/src/Parsers/ParserSettingsProfileElement.cpp +++ b/src/Parsers/ParserSettingsProfileElement.cpp @@ -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(); 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);