-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_value_store.cpp
More file actions
49 lines (42 loc) · 1.11 KB
/
key_value_store.cpp
File metadata and controls
49 lines (42 loc) · 1.11 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
#include "key_value_store.h"
// Fetch value from the db.
optional<string> KV_store::get_value(string key) {
unordered_map<string, unique_ptr<string>>::const_iterator it =
kv_map.find (key);
if (it != kv_map.end()) {
return *(it->second);
}
return nullopt;
}
// Insert value into the db.
void KV_store::insert_value(string key, string value) {
if (kv_map.find (key) == kv_map.end()) {
kv_map.emplace(key, make_unique<string>(value));
} else {
/* Modify the existing key, since value could be of different size, let
dealloc and allocate again */
kv_map[key].reset();
kv_map[key] = make_unique<string>(value);
}
}
// Delete value in the db.
bool KV_store::delete_key(string key) {
// Unimplemented
return true;
}
int KV_store::get_kv_map_size() {
return kv_map.size();
}
shared_ptr<KV_store> KV_store::kv_obj = nullptr;
mutex KV_store::mtx;
shared_ptr<KV_store> KV_store::get_kv_obj() {
if (kv_obj == nullptr) {
// Mutex is only called 1 time.
if (kv_obj == nullptr) {
mtx.lock();
kv_obj = make_shared<KV_store>();
mtx.unlock();
}
}
return kv_obj;
}