-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathNativeBufferStore.cpp
More file actions
74 lines (60 loc) · 1.93 KB
/
NativeBufferStore.cpp
File metadata and controls
74 lines (60 loc) · 1.93 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
73
74
/**
* NativeBufferStore implementation.
*
* Pure thread-safe buffer -- no background thread, no SQLite.
* Uses std::shared_mutex for reader-writer locking:
* - shared_lock for reads (get, has, size, entries)
* - unique_lock for writes (set, erase, clear, drain)
*/
#include "NativeBufferStore.h"
#include <utility>
namespace onyx {
const NativeBufferEntry* NativeBufferStore::get(const std::string& key) {
std::shared_lock lock(mutex_);
auto it = buffer_.find(key);
if (it == buffer_.end()) {
return nullptr;
}
return &it->second;
}
void NativeBufferStore::set(const std::string& key, NativeBufferEntry entry) {
std::unique_lock lock(mutex_);
buffer_[key] = std::move(entry);
}
bool NativeBufferStore::erase(const std::string& key) {
std::unique_lock lock(mutex_);
return buffer_.erase(key) > 0;
}
bool NativeBufferStore::has(const std::string& key) {
std::shared_lock lock(mutex_);
return buffer_.find(key) != buffer_.end();
}
size_t NativeBufferStore::size() {
std::shared_lock lock(mutex_);
return buffer_.size();
}
void NativeBufferStore::clear() {
std::unique_lock lock(mutex_);
buffer_.clear();
}
std::vector<std::pair<std::string, NativeBufferEntry>> NativeBufferStore::entries() {
std::shared_lock lock(mutex_);
std::vector<std::pair<std::string, NativeBufferEntry>> result;
result.reserve(buffer_.size());
for (const auto& [key, entry] : buffer_) {
result.emplace_back(key, entry);
}
return result;
}
std::vector<std::pair<std::string, NativeBufferEntry>> NativeBufferStore::drain() {
std::unique_lock lock(mutex_);
// Move all entries out of the buffer atomically
std::vector<std::pair<std::string, NativeBufferEntry>> result;
result.reserve(buffer_.size());
for (auto& [key, entry] : buffer_) {
result.emplace_back(std::move(key), std::move(entry));
}
buffer_.clear();
return result;
}
} // namespace onyx