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 * A guard struct for thread synchronization and transaction management.
50 Transaction(KVStore& store);
53 Transaction(const Transaction&) = delete;
54 Transaction& operator=(const Transaction&) = delete;
58 std::unique_lock<std::recursive_mutex> mLock;
64 * @param path configuration database file path
66 explicit KVStore(const std::string& path);
69 KVStore(const KVStore&) = delete;
70 KVStore& operator=(const KVStore&) = delete;
73 * Clears all the stored data
78 * @return Is there any data stored
83 * @param key string regexp of the stored values
85 * @return Does this key exist in the database
87 bool exists(const std::string& key);
90 * Removes values corresponding to the passed key.
91 * Many values may correspond to one key, so many values may
94 * @param key string regexp of the stored values
96 void remove(const std::string& key);
99 * Stores a single value corresponding to the passed key
101 * @param key string key of the value
102 * @param value value corresponding to the key
105 void set(const std::string& key, const T& value)
107 return setInternal(key, value);
111 * Gets the value corresponding to the key.
112 * Uses stringstreams to parse.
114 * @param key string key of the value
115 * @tparam T = std::string desired type of the return value
116 * @return value corresponding to the key
118 template<typename T = std::string>
119 T get(const std::string& key)
121 return getInternal(key, static_cast<T*>(nullptr));
125 * Returns all stored keys.
127 std::vector<std::string> getKeys();
130 typedef std::lock_guard<std::recursive_mutex> Lock;
132 std::recursive_mutex mMutex;
133 size_t mTransactionDepth;
134 bool mIsTransactionCommited;
136 void setInternal(const std::string& key, const std::string& value);
137 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
138 void setInternal(const std::string& key, const std::vector<std::string>& values);
140 void setInternal(const std::string& key, const T& value);
142 void setInternal(const std::string& key, const std::vector<T>& values);
144 std::string getInternal(const std::string& key, std::string*);
145 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
147 T getInternal(const std::string& key, T*);
149 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
152 sqlite3::Connection mConn;
153 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
154 std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
155 std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
156 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
157 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
158 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
159 std::unique_ptr<sqlite3::Statement> mGetKeysStmt;
162 void prepareStatements();
163 void createFunctions();
168 std::string toString(const T& value)
170 std::ostringstream oss;
176 T fromString(const std::string& strValue)
178 std::istringstream iss(strValue);
187 void KVStore::setInternal(const std::string& key, const T& value)
189 setInternal(key, toString(value));
193 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
195 std::vector<std::string> strValues(values.size());
197 std::transform(values.begin(),
202 setInternal(key, strValues);
206 T KVStore::getInternal(const std::string& key, T*)
208 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
212 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
214 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
215 std::vector<T> values(strValues.size());
217 std::transform(strValues.begin(),
226 * Concatenates all parameters into one std::string.
227 * Uses '.' to connect the terms.
228 * @param args components of the string
229 * @tparam delim optional delimiter
230 * @tparam typename ... Args any type implementing str
231 * @return string created from he args
233 template<char delim = '.', typename Arg1, typename ... Args>
234 std::string key(const Arg1& a1, const Args& ... args)
236 std::string ret = toString(a1);
237 std::initializer_list<std::string> strings {toString(args)...};
238 for (const std::string& s : strings) {
246 * Function added for key function completeness.
248 * @tparam delim = '.' parameter not used, added for consistency
249 * @return empty string
251 template<char delim = '.'>
254 return std::string();
257 } // namespace config
259 #endif // COMMON_CONFIG_KVSTORE_HPP