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));
109 KVStore::Transaction getTransaction();
112 typedef std::lock_guard<std::mutex> Lock;
114 struct TransactionImpl;
115 std::weak_ptr<TransactionImpl> mTransactionImplPtr;
116 std::mutex getTransactionMutex;
119 void setInternal(const std::string& key, const std::string& value);
120 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
121 void setInternal(const std::string& key, const std::vector<std::string>& values);
123 void setInternal(const std::string& key, const T& value);
125 void setInternal(const std::string& key, const std::vector<T>& values);
127 std::string getInternal(const std::string& key, std::string*);
128 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
130 T getInternal(const std::string& key, T*);
132 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
135 sqlite3::Connection mConn;
136 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
137 std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
138 std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
139 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
140 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
141 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
144 void prepareStatements();
145 void createFunctions();
150 std::string toString(const T& value)
152 std::ostringstream oss;
158 T fromString(const std::string& strValue)
160 std::istringstream iss(strValue);
169 void KVStore::setInternal(const std::string& key, const T& value)
171 setInternal(key, toString(value));
175 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
177 std::vector<std::string> strValues(values.size());
179 std::transform(values.begin(),
184 setInternal(key, strValues);
188 T KVStore::getInternal(const std::string& key, T*)
190 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
194 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
196 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
197 std::vector<T> values(strValues.size());
199 std::transform(strValues.begin(),
208 * Concatenates all parameters into one std::string.
209 * Uses '.' to connect the terms.
210 * @param args components of the string
211 * @tparam delim optional delimiter
212 * @tparam typename ... Args any type implementing str
213 * @return string created from he args
215 template<char delim = '.', typename Arg1, typename ... Args>
216 std::string key(const Arg1& a1, const Args& ... args)
218 std::string ret = toString(a1);
219 std::initializer_list<std::string> strings {toString(args)...};
220 for (const std::string& s : strings) {
228 * Function added for key function completeness.
230 * @tparam delim = '.' parameter not used, added for consistency
231 * @return empty string
233 template<char delim = '.'>
236 return std::string();
239 } // namespace config
241 #endif // COMMON_CONFIG_KVSTORE_HPP