forked from sfu-dis/pibench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stlmap_wrapper.hpp
190 lines (159 loc) · 5.2 KB
/
stlmap_wrapper.hpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef __STLMAP_WRAPPER_HPP__
#define __STLMAP_WRAPPER_HPP__
#include "tree_api.hpp"
#include <cstdint>
#include <iostream>
#include <type_traits>
#include <map>
#include <cstring>
#include <array>
#include <mutex>
#include <shared_mutex>
template<typename Key, typename T>
class stlmap_wrapper : public tree_api
{
public:
stlmap_wrapper();
virtual ~stlmap_wrapper();
virtual bool find(const char* key, size_t key_sz, char* value_out) override;
virtual bool insert(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(const char* key, size_t key_sz) override;
virtual int scan(const char* key, size_t key_sz, int scan_sz, char*& values_out) override;
private:
std::map<Key,T> map_;
std::shared_mutex mutex_;
};
template<typename Key, typename T>
stlmap_wrapper<Key,T>::stlmap_wrapper()
{
}
template<typename Key, typename T>
stlmap_wrapper<Key,T>::~stlmap_wrapper()
{
}
template<typename Key, typename T>
bool stlmap_wrapper<Key,T>::find(const char* key, size_t key_sz, char* value_out)
{
std::shared_lock lock(mutex_);
if constexpr (std::is_arithmetic<Key>::value)
{
auto it = map_.find(*reinterpret_cast<Key*>(const_cast<char*>(key)));
if (it == map_.end())
return false;
if constexpr (std::is_arithmetic<T>::value)
memcpy(value_out, &it->second, sizeof(T));
else
memcpy(value_out, it->second.c_str(), it->second.size());
}
else
{
auto it = map_.find(std::string(key, key_sz));
if (it == map_.end())
return false;
if constexpr (std::is_arithmetic<T>::value)
memcpy(value_out, &it->second, sizeof(T));
else
memcpy(value_out, it->second.c_str(), it->second.size());
}
return true;
}
template<typename Key, typename T>
bool stlmap_wrapper<Key, T>::insert(const char* key, size_t key_sz, const char* value, size_t value_sz)
{
std::unique_lock lock(mutex_);
Key k;
if constexpr (std::is_arithmetic<Key>::value)
k = *reinterpret_cast<Key*>(const_cast<char*>(key));
else
k = std::string(key, key_sz);
T v;
if constexpr (std::is_arithmetic<T>::value)
v = *reinterpret_cast<T*>(const_cast<char*>(value));
else
v = std::string(value, value_sz);
return map_.insert(std::make_pair(k,v)).second;
}
template<typename Key, typename T>
bool stlmap_wrapper<Key, T>::update(const char* key, size_t key_sz, const char* value, size_t value_sz)
{
std::unique_lock lock(mutex_);
typename std::map<Key,T>::iterator it;
if constexpr (std::is_arithmetic<Key>::value)
it = map_.find(*reinterpret_cast<Key*>(const_cast<char*>(key)));
else
it = map_.find(std::string(key, key_sz));
if (it == map_.end())
return false;
else
{
if constexpr (std::is_arithmetic<T>::value)
it->second = *reinterpret_cast<T*>(const_cast<char*>(value));
else
it->second = std::string(value, value_sz);
return true;
}
}
template<typename Key, typename T>
bool stlmap_wrapper<Key,T>::remove(const char* key, size_t key_sz)
{
std::unique_lock lock(mutex_);
if constexpr (std::is_arithmetic<Key>::value)
return map_.erase(*reinterpret_cast<Key*>(const_cast<char*>(key))) == 1;
else
return map_.erase(std::string(key, key_sz)) == 1;
}
template<typename Key, typename T>
int stlmap_wrapper<Key,T>::scan(const char* key, size_t key_sz, int scan_sz, char*& values_out)
{
std::shared_lock lock(mutex_);
constexpr size_t ONE_MB = 1ULL << 20;
static thread_local std::array<char, ONE_MB> results;
int scanned;
char* dst = reinterpret_cast<char*>(results.data());
if constexpr (std::is_arithmetic<Key>::value)
{
auto it = map_.lower_bound(*reinterpret_cast<Key*>(const_cast<char*>(key)));
if (it == map_.end())
return 0;
for(scanned=0; (scanned < scan_sz) && (it != map_.end()); ++scanned,++it)
{
memcpy(dst, &it->first, sizeof(Key));
dst += sizeof(T);
if constexpr (std::is_arithmetic<T>::value)
{
memcpy(dst, &it->second, sizeof(T));
dst += sizeof(T);
}
else
{
memcpy(dst, it->second.c_str(), it->second.size());
dst += it->second.size();
}
}
}
else
{
auto it = map_.lower_bound(std::string(key, key_sz));
if (it == map_.end())
return 0;
for(scanned=0; (scanned < scan_sz) && (it != map_.end()); ++scanned,++it)
{
memcpy(dst, it->first.c_str(), it->first.size());
dst += sizeof(T);
if constexpr (std::is_arithmetic<T>::value)
{
memcpy(dst, &it->second, sizeof(T));
dst += sizeof(T);
}
else
{
memcpy(dst, it->second.c_str(), it->second.size());
dst += it->second.size();
}
}
}
values_out = results.data();
return scanned;
}
#endif