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 KVStore(const std::string& path);
54 KVStore(const KVStore& store);
58 * Clears all the stored data
63 * @return Is there any data stored
68 * @param key string regexp of the stored values
70 * @return Does this key exist in the database
72 bool exists(const std::string& key);
75 * Removes values corresponding to the passed key.
76 * Many values may correspond to one key, so many values may
79 * @param key string regexp of the stored values
81 void remove(const std::string& key);
84 * Stores a single value corresponding to the passed key
86 * @param key string key of the value
87 * @param value value corresponding to the key
90 void set(const std::string& key, const T& value)
92 return setInternal(key, value);
96 * Gets the value corresponding to the key.
97 * Uses stringstreams to parse.
99 * @param key string key of the value
100 * @tparam T = std::string desired type of the return value
101 * @return value corresponding to the key
103 template<typename T = std::string>
104 T get(const std::string& key)
106 return getInternal(key, static_cast<T*>(nullptr));
110 * Returns all stored keys.
112 std::vector<std::string> getKeys();
114 KVStore::Transaction getTransaction();
117 typedef std::lock_guard<std::mutex> Lock;
119 struct TransactionImpl;
120 std::weak_ptr<TransactionImpl> mTransactionImplPtr;
121 std::mutex getTransactionMutex;
124 void setInternal(const std::string& key, const std::string& value);
125 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
126 void setInternal(const std::string& key, const std::vector<std::string>& values);
128 void setInternal(const std::string& key, const T& value);
130 void setInternal(const std::string& key, const std::vector<T>& values);
132 std::string getInternal(const std::string& key, std::string*);
133 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
135 T getInternal(const std::string& key, T*);
137 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
140 sqlite3::Connection mConn;
141 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
142 std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
143 std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
144 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
145 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
146 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
147 std::unique_ptr<sqlite3::Statement> mGetKeysStmt;
150 void prepareStatements();
151 void createFunctions();
156 std::string toString(const T& value)
158 std::ostringstream oss;
164 T fromString(const std::string& strValue)
166 std::istringstream iss(strValue);
175 void KVStore::setInternal(const std::string& key, const T& value)
177 setInternal(key, toString(value));
181 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
183 std::vector<std::string> strValues(values.size());
185 std::transform(values.begin(),
190 setInternal(key, strValues);
194 T KVStore::getInternal(const std::string& key, T*)
196 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
200 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
202 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
203 std::vector<T> values(strValues.size());
205 std::transform(strValues.begin(),
214 * Concatenates all parameters into one std::string.
215 * Uses '.' to connect the terms.
216 * @param args components of the string
217 * @tparam delim optional delimiter
218 * @tparam typename ... Args any type implementing str
219 * @return string created from he args
221 template<char delim = '.', typename Arg1, typename ... Args>
222 std::string key(const Arg1& a1, const Args& ... args)
224 std::string ret = toString(a1);
225 std::initializer_list<std::string> strings {toString(args)...};
226 for (const std::string& s : strings) {
234 * Function added for key function completeness.
236 * @tparam delim = '.' parameter not used, added for consistency
237 * @return empty string
239 template<char delim = '.'>
242 return std::string();
245 } // namespace config
247 #endif // COMMON_CONFIG_KVSTORE_HPP