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.
48 typedef std::shared_ptr<void> Transaction;
51 * @param path configuration database file path
53 explicit KVStore(const std::string& path);
56 KVStore(const KVStore&) = delete;
57 KVStore& operator=(const KVStore&) = delete;
60 * Clears all the stored data
65 * @return Is there any data stored
70 * @param key string regexp of the stored values
72 * @return Does this key exist in the database
74 bool exists(const std::string& key);
77 * Removes values corresponding to the passed key.
78 * Many values may correspond to one key, so many values may
81 * @param key string regexp of the stored values
83 void remove(const std::string& key);
86 * Stores a single value corresponding to the passed key
88 * @param key string key of the value
89 * @param value value corresponding to the key
92 void set(const std::string& key, const T& value)
94 return setInternal(key, value);
98 * Gets the value corresponding to the key.
99 * Uses stringstreams to parse.
101 * @param key string key of the value
102 * @tparam T = std::string desired type of the return value
103 * @return value corresponding to the key
105 template<typename T = std::string>
106 T get(const std::string& key)
108 return getInternal(key, static_cast<T*>(nullptr));
112 * Returns all stored keys.
114 std::vector<std::string> getKeys();
116 KVStore::Transaction getTransaction();
119 typedef std::lock_guard<std::recursive_mutex> Lock;
121 struct TransactionImpl;
122 std::weak_ptr<TransactionImpl> mTransactionImplPtr;
123 std::recursive_mutex mMutex;
125 void setInternal(const std::string& key, const std::string& value);
126 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
127 void setInternal(const std::string& key, const std::vector<std::string>& values);
129 void setInternal(const std::string& key, const T& value);
131 void setInternal(const std::string& key, const std::vector<T>& values);
133 std::string getInternal(const std::string& key, std::string*);
134 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
136 T getInternal(const std::string& key, T*);
138 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
141 sqlite3::Connection mConn;
142 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
143 std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
144 std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
145 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
146 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
147 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
148 std::unique_ptr<sqlite3::Statement> mGetKeysStmt;
151 void prepareStatements();
152 void createFunctions();
157 std::string toString(const T& value)
159 std::ostringstream oss;
165 T fromString(const std::string& strValue)
167 std::istringstream iss(strValue);
176 void KVStore::setInternal(const std::string& key, const T& value)
178 setInternal(key, toString(value));
182 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
184 std::vector<std::string> strValues(values.size());
186 std::transform(values.begin(),
191 setInternal(key, strValues);
195 T KVStore::getInternal(const std::string& key, T*)
197 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
201 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
203 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
204 std::vector<T> values(strValues.size());
206 std::transform(strValues.begin(),
215 * Concatenates all parameters into one std::string.
216 * Uses '.' to connect the terms.
217 * @param args components of the string
218 * @tparam delim optional delimiter
219 * @tparam typename ... Args any type implementing str
220 * @return string created from he args
222 template<char delim = '.', typename Arg1, typename ... Args>
223 std::string key(const Arg1& a1, const Args& ... args)
225 std::string ret = toString(a1);
226 std::initializer_list<std::string> strings {toString(args)...};
227 for (const std::string& s : strings) {
235 * Function added for key function completeness.
237 * @tparam delim = '.' parameter not used, added for consistency
238 * @return empty string
240 template<char delim = '.'>
243 return std::string();
246 } // namespace config
248 #endif // COMMON_CONFIG_KVSTORE_HPP