yql_setting.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include <library/cpp/containers/sorted_vector/sorted_vector.h>
  3. #include <util/generic/string.h>
  4. #include <util/generic/strbuf.h>
  5. #include <util/generic/maybe.h>
  6. #include <util/generic/yexception.h>
  7. namespace NYql {
  8. namespace NCommon {
  9. const TString ALL_CLUSTERS = "$all";
  10. template <typename TType, bool RUNTIME = true>
  11. class TConfSetting {
  12. public:
  13. TConfSetting() = default;
  14. TConfSetting(const TType& value) {
  15. PerClusterValue[ALL_CLUSTERS] = value;
  16. }
  17. TConfSetting(const TConfSetting&) = default;
  18. TConfSetting(TConfSetting&&) = default;
  19. ~TConfSetting() = default;
  20. bool IsRuntime() const {
  21. return RUNTIME;
  22. }
  23. TType& operator[](const TString& cluster) {
  24. if (ALL_CLUSTERS == cluster) {
  25. PerClusterValue.clear();
  26. }
  27. return PerClusterValue[cluster];
  28. }
  29. TConfSetting& operator =(const TType& value) {
  30. PerClusterValue.clear();
  31. PerClusterValue[ALL_CLUSTERS] = value;
  32. return *this;
  33. }
  34. TConfSetting& operator =(const TConfSetting&) = default;
  35. TConfSetting& operator =(TConfSetting&&) = default;
  36. template <typename TFunc>
  37. void UpdateAll(TFunc func) {
  38. PerClusterValue[ALL_CLUSTERS]; // insert record for all clusters if it is not present
  39. for (auto& it: PerClusterValue) {
  40. func(it.first, it.second);
  41. }
  42. }
  43. TMaybe<TType> Get(const TString& cluster) const {
  44. if (!PerClusterValue.empty()) {
  45. auto it = PerClusterValue.find(cluster);
  46. if (it != PerClusterValue.end()) {
  47. return MakeMaybe(it->second);
  48. }
  49. it = PerClusterValue.find(ALL_CLUSTERS);
  50. if (it != PerClusterValue.end()) {
  51. return MakeMaybe(it->second);
  52. }
  53. }
  54. return Nothing();
  55. }
  56. void Clear() {
  57. PerClusterValue.clear();
  58. }
  59. void Clear(const TString& cluster) {
  60. if (ALL_CLUSTERS == cluster) {
  61. PerClusterValue.clear();
  62. } else {
  63. PerClusterValue.erase(cluster);
  64. }
  65. }
  66. private:
  67. NSorted::TSimpleMap<TString, TType> PerClusterValue; // Uses special '$all' key for all clusters
  68. };
  69. template <typename TType>
  70. class TConfSetting<TType, false> {
  71. public:
  72. TConfSetting() = default;
  73. TConfSetting(const TType& value)
  74. : Value(value)
  75. {
  76. }
  77. TConfSetting(const TConfSetting&) = default;
  78. TConfSetting(TConfSetting&&) = default;
  79. ~TConfSetting() = default;
  80. bool IsRuntime() const {
  81. return false;
  82. }
  83. TType& operator[](const TString& cluster) {
  84. if (cluster != ALL_CLUSTERS) {
  85. ythrow yexception() << "Static setting cannot be set for specific cluster";
  86. }
  87. Value.ConstructInPlace();
  88. return Value.GetRef();
  89. }
  90. TConfSetting& operator =(const TType& value) {
  91. Value = value;
  92. return *this;
  93. }
  94. TConfSetting& operator =(const TConfSetting&) = default;
  95. TConfSetting& operator =(TConfSetting&&) = default;
  96. TMaybe<TType> Get() const {
  97. return Value;
  98. }
  99. void Clear() {
  100. Value.Clear();
  101. }
  102. void Clear(const TString&) {
  103. Value.Clear();
  104. }
  105. private:
  106. TMaybe<TType> Value;
  107. };
  108. } // namespace NCommon
  109. } // namespace NYql