mkql_stats_registry.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "mkql_stats_registry.h"
  2. #include <util/generic/yexception.h>
  3. #include <util/generic/vector.h>
  4. #include <util/generic/hash_set.h>
  5. #include <util/generic/singleton.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. namespace {
  9. /**
  10. * vector based implementation of stats registry
  11. */
  12. class TDefaultStatsRegistry: public IStatsRegistry {
  13. public:
  14. i64 GetStat(const TStatKey& key) const override {
  15. return key.GetId() < Values_.size() ? Values_[key.GetId()] : 0;
  16. }
  17. void SetStat(const TStatKey& key, i64 value) override {
  18. EnsureSize(key);
  19. Values_[key.GetId()] = value;
  20. }
  21. void SetMaxStat(const TStatKey& key, i64 value) override {
  22. EnsureSize(key);
  23. i64& oldValue = Values_[key.GetId()];
  24. oldValue = Max(value, oldValue);
  25. }
  26. void SetMinStat(const TStatKey& key, i64 value) override {
  27. EnsureSize(key);
  28. i64& oldValue = Values_[key.GetId()];
  29. oldValue = Min(value, oldValue);
  30. }
  31. void AddStat(const TStatKey& key, i64 value) override {
  32. EnsureSize(key);
  33. Values_[key.GetId()] += value;
  34. }
  35. private:
  36. void EnsureSize(const TStatKey& key) {
  37. if (Y_UNLIKELY(Values_.size() <= key.GetId())) {
  38. Values_.resize(key.GetId() + 10); // try to avoid too often resizing
  39. }
  40. }
  41. private:
  42. TVector<i64> Values_;
  43. };
  44. /**
  45. * Key names set to prevent keys accidental duplication.
  46. */
  47. class TKeyNames: private THashSet<TStringBuf> {
  48. public:
  49. bool AddIfNotExists(TStringBuf name) {
  50. return insert(name).second;
  51. }
  52. };
  53. } // namespace
  54. ui32 TStatKey::IdSequence_ = 0;
  55. TStatKey* TStatKey::KeysChain_ = nullptr;
  56. TStatKey::TStatKey(TStringBuf name, bool deriv)
  57. : Name_(name)
  58. , Deriv_(deriv)
  59. , Id_(IdSequence_++)
  60. {
  61. bool newOne = Singleton<TKeyNames>()->AddIfNotExists(name);
  62. Y_ENSURE(newOne, "duplicated stat key: " << name);
  63. SetNext(KeysChain_);
  64. KeysChain_ = this;
  65. }
  66. IStatsRegistryPtr CreateDefaultStatsRegistry() {
  67. return new TDefaultStatsRegistry;
  68. }
  69. } // namespace NMiniKQL
  70. } // namespace NKikimr