2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Declaration of a class for key-value storage in a sqlite3 database
25 #ifndef COMMON_CONFIG_KVSTORE_HPP
26 #define COMMON_CONFIG_KVSTORE_HPP
28 #include "config/sqlite3/connection.hpp"
29 #include "config/sqlite3/statement.hpp"
32 #include <initializer_list>
46 * @param path configuration database file path
48 KVStore(const std::string& path);
49 KVStore(const KVStore& store);
53 * Clears all the stored data
58 * @return Is there any data stored
63 * @param key string regexp of the stored values
65 * @return Does this key exist in the database
67 bool exists(const std::string& key);
70 * Removes values corresponding to the passed key.
71 * Many values may correspond to one key, so many values may
74 * @param key string regexp of the stored values
76 void remove(const std::string& key);
79 * Stores a single value corresponding to the passed key
81 * @param key string key of the value
82 * @param value value corresponding to the key
85 void set(const std::string& key, const T& value)
87 return setInternal(key, value);
91 * Gets the value corresponding to the key.
92 * Uses stringstreams to parse.
94 * @param key string key of the value
95 * @tparam T = std::string desired type of the return value
96 * @return value corresponding to the key
98 template<typename T = std::string>
99 T get(const std::string& key)
101 return getInternal(key, static_cast<T*>(nullptr));
107 typedef std::lock_guard<std::recursive_mutex> Lock;
108 unsigned int mTransactionCounter;
110 void setInternal(const std::string& key, const std::string& value);
111 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
112 void setInternal(const std::string& key, const std::vector<std::string>& values);
114 void setInternal(const std::string& key, const T& value);
116 void setInternal(const std::string& key, const std::vector<T>& values);
118 std::string getInternal(const std::string& key, std::string*);
119 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
121 T getInternal(const std::string& key, T*);
123 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
125 std::recursive_mutex mConnMtx;
128 sqlite3::Connection mConn;
129 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
130 std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
131 std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
132 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
133 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
134 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
137 void prepareStatements();
138 void createFunctions();
140 void removeInternal(const std::string& key);
146 std::string toString(const T& value)
148 std::ostringstream oss;
154 T fromString(const std::string& strValue)
156 std::istringstream iss(strValue);
165 void KVStore::setInternal(const std::string& key, const T& value)
167 setInternal(key, toString(value));
171 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
173 std::vector<std::string> strValues(values.size());
175 std::transform(values.begin(),
180 setInternal(key, strValues);
184 T KVStore::getInternal(const std::string& key, T*)
186 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
190 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
192 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
193 std::vector<T> values(strValues.size());
195 std::transform(strValues.begin(),
204 * Concatenates all parameters into one std::string.
205 * Uses '.' to connect the terms.
206 * @param args components of the string
207 * @tparam delim optional delimiter
208 * @tparam typename ... Args any type implementing str
209 * @return string created from he args
211 template<char delim = '.', typename Arg1, typename ... Args>
212 std::string key(const Arg1& a1, const Args& ... args)
214 std::string ret = toString(a1);
215 std::initializer_list<std::string> strings {toString(args)...};
216 for (const std::string& s : strings) {
224 * Function added for key function completeness.
226 * @tparam delim = '.' parameter not used, added for consistency
227 * @return empty string
229 template<char delim = '.'>
232 return std::string();
235 } // namespace config
237 #endif // COMMON_CONFIG_KVSTORE_HPP