-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataEngine.h
More file actions
72 lines (58 loc) · 2.05 KB
/
DataEngine.h
File metadata and controls
72 lines (58 loc) · 2.05 KB
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
#pragma once
#include <atomic>
#include <cstdint>
#include <functional>
#include <optional>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <unordered_map>
class DataEngine
{
public:
using IntegerCounter = std::uint64_t;
struct AccessStatistics
{
IntegerCounter m_reads = 0;
IntegerCounter m_writes = 0;
};
struct ExtendedValue
{
std::string m_value;
AccessStatistics m_stats;
};
public:
std::optional<ExtendedValue> get(const std::string_view name) const;
void initial_set(const std::string_view name, const std::string_view value); // no statistics update
void set(const std::string_view name, const std::string_view value);
using EnumerateVisitorProc = void(const std::string_view name, const std::string_view value);
void enumerate(const std::function<EnumerateVisitorProc>& visitor) const;
AccessStatistics get_global_statistics() const;
protected:
class AtomicCounter : public std::atomic<IntegerCounter>
{
public:
// Define copy-constructor to allow easier emplacing of such values into unordered_map values
AtomicCounter(const IntegerCounter value = 0) noexcept
{
this->store(value, std::memory_order_relaxed);
}
AtomicCounter(const AtomicCounter& obj) noexcept
{
const auto value = obj.load(std::memory_order_relaxed);
this->store(value, std::memory_order_relaxed);
}
};
struct ExtendedValueInternal
{
std::string m_value;
mutable AtomicCounter m_reads = ATOMIC_VAR_INIT(0);
AtomicCounter m_writes = ATOMIC_VAR_INIT(0);
};
using DataCollection = std::unordered_map<std::string, ExtendedValueInternal>;
mutable std::shared_mutex m_protectData;
DataCollection m_data;
// Global statistics:
mutable std::atomic<IntegerCounter> m_reads = ATOMIC_VAR_INIT(0);
std::atomic<IntegerCounter> m_writes = ATOMIC_VAR_INIT(0);
};