aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/pg_settings/guc_settings.cpp
blob: 62d171d2edae36cbe6ce63a7f340883a589a1a56 (plain) (blame)
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
#include "guc_settings.h"
#include <library/cpp/json/json_reader.h>

TGUCSettings::TGUCSettings(const TString &serialized) {
    if (!serialized.empty()) {
        NJson::TJsonValue gucJson;
        Y_ENSURE(NJson::ReadJsonTree(serialized, &gucJson), "Error parsing GUCSettings");
        this->ImportFromJson(gucJson);
    }
}


void TGUCSettings::Setup(const std::unordered_map<std::string, std::string>& runtimeSettings) {
    RollbackSettings_ = runtimeSettings;
    RollBack();
}

std::optional<std::string> TGUCSettings::Get(const std::string& key) const {
    auto it = Settings_.find(key);
    if (it == Settings_.end()) {
        return std::nullopt;
    }
    return it->second;
}

void TGUCSettings::Set(const std::string& key, const std::string& val, bool isLocal) {
    Settings_[key] = val;
    if (!isLocal) {
        SessionSettings_[key] = val;
    }
}

void TGUCSettings::Commit() {
    RollbackSettings_ = SessionSettings_;
}

void TGUCSettings::RollBack() {
    Settings_ = SessionSettings_ = RollbackSettings_;
}

void TGUCSettings::ExportToJson(NJson::TJsonValue& value) const {
    NJson::TJsonValue settings(NJson::JSON_MAP);
    for (const auto& setting : Settings_) {
        settings[setting.first] = setting.second;
    }
    NJson::TJsonValue rollbackSettings(NJson::JSON_MAP);
    for (const auto& setting : RollbackSettings_) {
        rollbackSettings[setting.first] = setting.second;
    }
    NJson::TJsonValue sessionSettings(NJson::JSON_MAP);
    for (const auto& setting : SessionSettings_) {
        sessionSettings[setting.first] = setting.second;
    }
    NJson::TJsonValue gucSettings(NJson::JSON_MAP);
    gucSettings.InsertValue("settings", std::move(settings));
    gucSettings.InsertValue("rollback_settings", std::move(rollbackSettings));
    gucSettings.InsertValue("session_settings", std::move(sessionSettings));
    value.InsertValue("guc_settings", std::move(gucSettings));
}

void TGUCSettings::ImportFromJson(const NJson::TJsonValue& value)
{
    Settings_.clear();
    RollbackSettings_.clear();
    SessionSettings_.clear();
    if (value.Has("guc_settings")) {
        auto gucSettings = value["guc_settings"];
        if (gucSettings.Has("settings")) {
            for (const auto& [settingName, settingValue] : gucSettings["settings"].GetMapSafe()) {
                Settings_[settingName] = settingValue.GetStringSafe();
            }
        }
        if (gucSettings.Has("rollback_settings")) {
            for (const auto& [settingName, settingValue] : gucSettings["rollback_settings"].GetMapSafe()) {
                RollbackSettings_[settingName] = settingValue.GetStringSafe();
            }
        }
        if (gucSettings.Has("session_settings")) {
            for (const auto& [settingName, settingValue] : gucSettings["session_settings"].GetMapSafe()) {
                SessionSettings_[settingName] = settingValue.GetStringSafe();
            }
        }
    }
}

TString TGUCSettings::SerializeToString() const {
    NJson::TJsonValue gucJson;
    this->ExportToJson(gucJson);
    return WriteJson(gucJson);
}

bool TGUCSettings::operator==(const TGUCSettings& other) const {
    return Settings_ == other.Settings_ &&
        RollbackSettings_ == other.RollbackSettings_ &&
        SessionSettings_ == other.SessionSettings_;
}

template <>
struct THash<std::pair<std::string, std::string>> {
    inline size_t operator()(const std::pair<std::string, std::string>& value) const {
        size_t result = 0;
        result = CombineHashes(THash<std::string>()(value.first), result);
        result = CombineHashes(THash<std::string>()(value.second), result);
        return result;
    }
};

template <>
struct THash<std::unordered_map<std::string, std::string>> {
    inline size_t operator()(const std::unordered_map<std::string, std::string>& values) const {
        size_t result = 0;
        for (const auto& value : values) {
            result = CombineHashes(THash<std::pair<std::string, std::string>>()(value), result);
        }
        return result;
    }
};

size_t TGUCSettings::GetHash() const noexcept {
    size_t hash = 0;
    hash = CombineHashes(THash<decltype(Settings_)>()(Settings_), hash);
    hash = CombineHashes(THash<decltype(RollbackSettings_)>()(RollbackSettings_), hash);
    hash = CombineHashes(THash<decltype(SessionSettings_)>()(SessionSettings_), hash);
    return  hash;
}