aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_stats_registry.cpp
blob: 431737731ac32ee93bfb0f0e11bd3bc7189d9022 (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
#include "mkql_stats_registry.h"

#include <util/generic/yexception.h>
#include <util/generic/vector.h>
#include <util/generic/hash_set.h>
#include <util/generic/singleton.h>


namespace NKikimr {
namespace NMiniKQL {
namespace {

/**
 * vector based implementation of stats registry
 */
class TDefaultStatsRegistry: public IStatsRegistry {
public:
    i64 GetStat(const TStatKey& key) const override {
        return key.GetId() < Values_.size() ? Values_[key.GetId()] : 0;
    }

    void SetStat(const TStatKey& key, i64 value) override {
        EnsureSize(key);
        Values_[key.GetId()] = value;
    }

    void SetMaxStat(const TStatKey& key, i64 value) override {
        EnsureSize(key);
        i64& oldValue = Values_[key.GetId()];
        oldValue = Max(value, oldValue);
    }

    void SetMinStat(const TStatKey& key, i64 value) override {
        EnsureSize(key);
        i64& oldValue = Values_[key.GetId()];
        oldValue = Min(value, oldValue);
    }

    void AddStat(const TStatKey& key, i64 value) override {
        EnsureSize(key);
        Values_[key.GetId()] += value;
    }

private:
    void EnsureSize(const TStatKey& key) {
        if (Y_UNLIKELY(Values_.size() <= key.GetId())) {
            Values_.resize(key.GetId() + 10); // try to avoid too often resizing
        }
    }

private:
    TVector<i64> Values_;
};

/**
 * Key names set to prevent keys accidental duplication.
 */
class TKeyNames: private THashSet<TStringBuf> {
public:
    bool AddIfNotExists(TStringBuf name) {
        return insert(name).second;
    }
};

} // namespace


ui32 TStatKey::IdSequence_ = 0;
TStatKey* TStatKey::KeysChain_ = nullptr;


TStatKey::TStatKey(TStringBuf name, bool deriv)
    : Name_(name)
    , Deriv_(deriv)
    , Id_(IdSequence_++)
{
    bool newOne = Singleton<TKeyNames>()->AddIfNotExists(name);
    Y_ENSURE(newOne, "duplicated stat key: " << name);

    SetNext(KeysChain_);
    KeysChain_ = this;
}

IStatsRegistryPtr CreateDefaultStatsRegistry() {
    return new TDefaultStatsRegistry;
}

} // namespace NMiniKQL
} // namespace NKikimr