-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataEngine.h
100 lines (75 loc) · 2.89 KB
/
DataEngine.h
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
#pragma once
#include "Allocator.h"
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "utils/stl.h" // for std::atomic<std::shared_ptr> in non-compatible compilers
class DataEngine
{
public:
using IntegerCounter = std::uint64_t;
template<typename T>
using Allocator = SeparateHeapAllocator<T>;
using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
struct AccessStatistics
{
IntegerCounter m_successOperations = 0;
IntegerCounter m_failedOperations = 0;
};
public:
DataEngine(const size_t bucketCount);
~DataEngine();
bool is_lock_free() const;
std::optional<String> get(const std::string_view key) const;
void set(const std::string_view key, const std::string_view value);
using EnumerateVisitorProc = void(const std::string_view key, const std::string_view value);
void enumerate(const std::function<EnumerateVisitorProc>& visitor) const;
AccessStatistics get_read_statistics() const;
protected:
class ListNode
{
public:
using NodeAllocator = Allocator<ListNode>;
using AtomicSharedConstStringPtr = std::atomic<std::shared_ptr<const String>>;
public:
const String m_key;
AtomicSharedConstStringPtr m_ptrValue;
std::atomic<ListNode*> m_next = nullptr;
NodeAllocator m_allocator;
//static_assert(AtomicSharedConstStringPtr::is_always_lock_free);
static_assert(std::atomic<ListNode*>::is_always_lock_free);
public:
ListNode(const NodeAllocator& allocator, String&& key, std::shared_ptr<const String>&& ptrValue) :
m_key(key), m_ptrValue(ptrValue), m_allocator(allocator)
{
}
void delete_self()
{
NodeAllocator alloc(m_allocator);
std::allocator_traits<NodeAllocator>::destroy(alloc, std::to_address(this));
std::allocator_traits<NodeAllocator>::deallocate(alloc, this, 1);
}
std::shared_ptr<const String> get_value_const_ref() const
{
return m_ptrValue.load(std::memory_order_relaxed);
}
};
using AtomicNodePtr = std::atomic<ListNode*>;
using Hash = std::hash<std::string_view>;
using NodeDeleter = void(ListNode* const ptr);
using NodeUniquePtr = std::unique_ptr<ListNode, NodeDeleter*>;
protected:
NodeUniquePtr create_node(const std::string_view key, const std::string_view value);
protected:
std::vector<AtomicNodePtr> m_buckets;
// Global statistics:
mutable std::atomic<IntegerCounter> m_successReads = 0;
mutable std::atomic<IntegerCounter> m_failedReads = 0;
static_assert(AtomicNodePtr::is_always_lock_free);
static_assert(std::atomic<IntegerCounter>::is_always_lock_free);
};