AOSharedServiceLibrary
kv_store.h
1 /*
2 MIT License Block
3 
4 Copyright (c) 2016 Alex Barry
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 
25 #include <string>
26 #include <unordered_map>
27 #include <iostream>
28 #include "Poco/RWLock.h"
29 
30 #include "buffers.h"
31 #include "kv_store_interface.h"
32 
33 #ifndef AOSSL_CORE_INCLUDE_KV_STORE_H_
34 #define AOSSL_CORE_INCLUDE_KV_STORE_H_
35 
36 namespace AOSSL {
37 
39 
43  std::unordered_map<std::string, std::string> opts;
44  Poco::RWLock rw_mutex;
45  public:
46  KeyValueStore() {}
47  virtual ~KeyValueStore() {}
49  inline bool opt_exist(std::string key) {
50  Poco::ScopedReadRWLock lock(rw_mutex);
51  auto search = opts.find(key);
52  if (search != opts.end()) {
53  return true;
54  } else {
55  return false;
56  }
57  }
58 
60  inline StringBuffer* get_opt(std::string key) {
61  StringBuffer *ret_buf = new StringBuffer();
62  get_opt(key, *ret_buf);
63  return ret_buf;
64  }
65 
67  inline void get_opt(std::string key, StringBuffer& val) {
68  Poco::ScopedReadRWLock lock(rw_mutex);
69  val.val.assign(opts[key]);
70  val.success = true;
71  }
72 
73  inline std::unordered_map<std::string, std::string> get_opts() {
74  Poco::ScopedReadRWLock lock(rw_mutex);
75  return opts;
76  }
77 
79  void add_opt(const std::string& key, std::string value) \
80  {opts.emplace(key, value);}
81 
83  inline void set_opt(std::string& key, std::string& value) {
84  Poco::ScopedWriteRWLock lock(rw_mutex);
85  opts[key] = value;
86  }
87 };
88 
89 } // namespace AOSSL
90 
91 #endif // AOSSL_CORE_INCLUDE_KV_STORE_H_
A Structure for holding a single value.
Definition: buffers.h:42
void add_opt(const std::string &key, std::string value)
Add an option.
Definition: kv_store.h:79
std::string val
Value stored.
Definition: buffers.h:44
Key Value Store.
Definition: kv_store.h:42
void get_opt(std::string key, StringBuffer &val)
Get an option by key.
Definition: kv_store.h:67
void set_opt(std::string &key, std::string &value)
Set an option.
Definition: kv_store.h:83
StringBuffer * get_opt(std::string key)
Get an option by key.
Definition: kv_store.h:60
Key Value Store.
Definition: kv_store_interface.h:38
bool success
Success flag.
Definition: buffers.h:35
Definition: cli.h:35
bool opt_exist(std::string key)
Does a key exist?
Definition: kv_store.h:49