Transaction and synchronization guard
[archive/platform/core/system/libConfig.git] / src / config / kvstore.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Jan Olszak <j.olszak@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file
21  * @author Jan Olszak (j.olszak@samsung.com)
22  * @brief  Declaration of a class for key-value storage in a sqlite3 database
23  */
24
25 #ifndef COMMON_CONFIG_KVSTORE_HPP
26 #define COMMON_CONFIG_KVSTORE_HPP
27
28 #include "config/sqlite3/connection.hpp"
29 #include "config/sqlite3/statement.hpp"
30
31 #include <algorithm>
32 #include <initializer_list>
33 #include <memory>
34 #include <mutex>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 #include <atomic>
39
40 namespace config {
41
42 class KVStore {
43
44 public:
45     /**
46      * A guard struct for thread synchronization and transaction management.
47      */
48     typedef std::shared_ptr<void> Transaction;
49
50     /**
51      * @param path configuration database file path
52      */
53     KVStore(const std::string& path);
54     KVStore(const KVStore& store);
55     ~KVStore();
56
57     /**
58      * Clears all the stored data
59      */
60     void clear();
61
62     /**
63      * @return Is there any data stored
64      */
65     bool isEmpty();
66
67     /**
68      * @param key string regexp of the stored values
69      *
70      * @return Does this key exist in the database
71      */
72     bool exists(const std::string& key);
73
74     /**
75      * Removes values corresponding to the passed key.
76      * Many values may correspond to one key, so many values may
77      * need to be deleted
78      *
79      * @param key string regexp of the stored values
80      */
81     void remove(const std::string& key);
82
83     /**
84      * Stores a single value corresponding to the passed key
85      *
86      * @param key string key of the value
87      * @param value value corresponding to the key
88      */
89     template<typename T>
90     void set(const std::string& key, const T& value)
91     {
92         return setInternal(key, value);
93     }
94
95     /**
96      * Gets the value corresponding to the key.
97      * Uses stringstreams to parse.
98      *
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
102      */
103     template<typename T = std::string>
104     T get(const std::string& key)
105     {
106         return getInternal(key, static_cast<T*>(nullptr));
107     }
108
109     KVStore::Transaction getTransaction();
110
111 private:
112     typedef std::lock_guard<std::mutex> Lock;
113
114     struct TransactionImpl;
115     std::weak_ptr<TransactionImpl> mTransactionImplPtr;
116     std::mutex getTransactionMutex;
117     std::mutex mConnMtx;
118
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);
122     template<typename T>
123     void setInternal(const std::string& key, const T& value);
124     template<typename T>
125     void setInternal(const std::string& key, const std::vector<T>& values);
126
127     std::string getInternal(const std::string& key, std::string*);
128     std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
129     template<typename T>
130     T getInternal(const std::string& key, T*);
131     template<typename T>
132     std::vector<T> getInternal(const std::string& key, std::vector<T>*);
133
134     std::string mPath;
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;
142
143     void setupDb();
144     void prepareStatements();
145     void createFunctions();
146 };
147
148 namespace {
149 template<typename T>
150 std::string toString(const T& value)
151 {
152     std::ostringstream oss;
153     oss << value;
154     return oss.str();
155 }
156
157 template<typename T>
158 T fromString(const std::string& strValue)
159 {
160     std::istringstream iss(strValue);
161     T value;
162     iss >> value;
163     return value;
164 }
165
166 } // namespace
167
168 template<typename T>
169 void KVStore::setInternal(const std::string& key, const T& value)
170 {
171     setInternal(key, toString(value));
172 }
173
174 template<typename T>
175 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
176 {
177     std::vector<std::string> strValues(values.size());
178
179     std::transform(values.begin(),
180                    values.end(),
181                    strValues.begin(),
182                    toString<T>);
183
184     setInternal(key, strValues);
185 }
186
187 template<typename T>
188 T KVStore::getInternal(const std::string& key, T*)
189 {
190     return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
191 }
192
193 template<typename T>
194 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
195 {
196     std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
197     std::vector<T> values(strValues.size());
198
199     std::transform(strValues.begin(),
200                    strValues.end(),
201                    values.begin(),
202                    fromString<T>);
203
204     return values;
205 }
206
207 /**
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
214  */
215 template<char delim = '.', typename Arg1, typename ... Args>
216 std::string key(const Arg1& a1, const Args& ... args)
217 {
218     std::string ret = toString(a1);
219     std::initializer_list<std::string> strings {toString(args)...};
220     for (const std::string& s : strings) {
221         ret += delim + s;
222     }
223
224     return ret;
225 }
226
227 /**
228  * Function added for key function completeness.
229  *
230  * @tparam delim = '.' parameter not used, added for consistency
231  * @return empty string
232  */
233 template<char delim = '.'>
234 std::string key()
235 {
236     return std::string();
237 }
238
239 } // namespace config
240
241 #endif // COMMON_CONFIG_KVSTORE_HPP
242
243