guc_settings.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "guc_settings.h"
  2. #include <library/cpp/json/json_reader.h>
  3. TGUCSettings::TGUCSettings(const TString &serialized) {
  4. if (!serialized.empty()) {
  5. NJson::TJsonValue gucJson;
  6. Y_ENSURE(NJson::ReadJsonTree(serialized, &gucJson), "Error parsing GUCSettings");
  7. this->ImportFromJson(gucJson);
  8. }
  9. }
  10. void TGUCSettings::Setup(const std::unordered_map<std::string, std::string>& runtimeSettings) {
  11. RollbackSettings_ = runtimeSettings;
  12. RollBack();
  13. }
  14. std::optional<std::string> TGUCSettings::Get(const std::string& key) const {
  15. auto it = Settings_.find(key);
  16. if (it == Settings_.end()) {
  17. return std::nullopt;
  18. }
  19. return it->second;
  20. }
  21. void TGUCSettings::Set(const std::string& key, const std::string& val, bool isLocal) {
  22. Settings_[key] = val;
  23. if (!isLocal) {
  24. SessionSettings_[key] = val;
  25. }
  26. }
  27. void TGUCSettings::Commit() {
  28. RollbackSettings_ = SessionSettings_;
  29. }
  30. void TGUCSettings::RollBack() {
  31. Settings_ = SessionSettings_ = RollbackSettings_;
  32. }
  33. void TGUCSettings::ExportToJson(NJson::TJsonValue& value) const {
  34. NJson::TJsonValue settings(NJson::JSON_MAP);
  35. for (const auto& setting : Settings_) {
  36. settings[setting.first] = setting.second;
  37. }
  38. NJson::TJsonValue rollbackSettings(NJson::JSON_MAP);
  39. for (const auto& setting : RollbackSettings_) {
  40. rollbackSettings[setting.first] = setting.second;
  41. }
  42. NJson::TJsonValue sessionSettings(NJson::JSON_MAP);
  43. for (const auto& setting : SessionSettings_) {
  44. sessionSettings[setting.first] = setting.second;
  45. }
  46. NJson::TJsonValue gucSettings(NJson::JSON_MAP);
  47. gucSettings.InsertValue("settings", std::move(settings));
  48. gucSettings.InsertValue("rollback_settings", std::move(rollbackSettings));
  49. gucSettings.InsertValue("session_settings", std::move(sessionSettings));
  50. value.InsertValue("guc_settings", std::move(gucSettings));
  51. }
  52. void TGUCSettings::ImportFromJson(const NJson::TJsonValue& value)
  53. {
  54. Settings_.clear();
  55. RollbackSettings_.clear();
  56. SessionSettings_.clear();
  57. if (value.Has("guc_settings")) {
  58. auto gucSettings = value["guc_settings"];
  59. if (gucSettings.Has("settings")) {
  60. for (const auto& [settingName, settingValue] : gucSettings["settings"].GetMapSafe()) {
  61. Settings_[settingName] = settingValue.GetStringSafe();
  62. }
  63. }
  64. if (gucSettings.Has("rollback_settings")) {
  65. for (const auto& [settingName, settingValue] : gucSettings["rollback_settings"].GetMapSafe()) {
  66. RollbackSettings_[settingName] = settingValue.GetStringSafe();
  67. }
  68. }
  69. if (gucSettings.Has("session_settings")) {
  70. for (const auto& [settingName, settingValue] : gucSettings["session_settings"].GetMapSafe()) {
  71. SessionSettings_[settingName] = settingValue.GetStringSafe();
  72. }
  73. }
  74. }
  75. }
  76. TString TGUCSettings::SerializeToString() const {
  77. NJson::TJsonValue gucJson;
  78. this->ExportToJson(gucJson);
  79. return WriteJson(gucJson);
  80. }
  81. bool TGUCSettings::operator==(const TGUCSettings& other) const {
  82. return Settings_ == other.Settings_ &&
  83. RollbackSettings_ == other.RollbackSettings_ &&
  84. SessionSettings_ == other.SessionSettings_;
  85. }
  86. template <>
  87. struct THash<std::pair<std::string, std::string>> {
  88. inline size_t operator()(const std::pair<std::string, std::string>& value) const {
  89. size_t result = 0;
  90. result = CombineHashes(THash<std::string>()(value.first), result);
  91. result = CombineHashes(THash<std::string>()(value.second), result);
  92. return result;
  93. }
  94. };
  95. template <>
  96. struct THash<std::unordered_map<std::string, std::string>> {
  97. inline size_t operator()(const std::unordered_map<std::string, std::string>& values) const {
  98. size_t result = 0;
  99. for (const auto& value : values) {
  100. result = CombineHashes(THash<std::pair<std::string, std::string>>()(value), result);
  101. }
  102. return result;
  103. }
  104. };
  105. size_t TGUCSettings::GetHash() const noexcept {
  106. size_t hash = 0;
  107. hash = CombineHashes(THash<decltype(Settings_)>()(Settings_), hash);
  108. hash = CombineHashes(THash<decltype(RollbackSettings_)>()(RollbackSettings_), hash);
  109. hash = CombineHashes(THash<decltype(SessionSettings_)>()(SessionSettings_), hash);
  110. return hash;
  111. }