Saving and loading configurations from KVStore
[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
39 namespace config {
40
41 class KVStore {
42
43 public:
44
45     /**
46      * @param path configuration database file path
47      */
48     KVStore(const std::string& path);
49     KVStore(const KVStore& store);
50     ~KVStore();
51
52     /**
53      * Clears all the stored data
54      */
55     void clear();
56
57     /**
58      * @return Number of all stored values
59      */
60     unsigned int size();
61
62     /**
63      * @param key string regexp of the stored values
64      *
65      * @return Number of values corresponding to the passed key
66      */
67     unsigned int count(const std::string& key);
68
69     /**
70      * Removes values corresponding to the passed key.
71      * Many values may correspond to one key, so many values may
72      * need to be deleted
73      *
74      * @param key string regexp of the stored values
75      */
76     void remove(const std::string& key);
77
78     /**
79      * Stores a single value corresponding to the passed key
80      *
81      * @param key string key of the value
82      * @param value value corresponding to the key
83      */
84     template<typename T>
85     void set(const std::string& key, const T& value)
86     {
87         return setInternal(key, value);
88     }
89
90     /**
91      * Gets the value corresponding to the key.
92      * Uses stringstreams to parse.
93      *
94      * @param key string key of the value
95      * @tparam T = std::string desired type of the return value
96      * @return value corresponding to the key
97      */
98     template<typename T = std::string>
99     T get(const std::string& key)
100     {
101         return getInternal(key, static_cast<T*>(nullptr));
102     }
103
104
105 private:
106     typedef std::lock_guard<std::mutex> Lock;
107
108     void setInternal(const std::string& key, const std::string& value);
109     void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
110     void setInternal(const std::string& key, const std::vector<std::string>& values);
111     template<typename T>
112     void setInternal(const std::string& key, const T& value);
113     template<typename T>
114     void setInternal(const std::string& key, const std::vector<T>& values);
115
116     std::string getInternal(const std::string& key, std::string*);
117     std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
118     template<typename T>
119     T getInternal(const std::string& key, T*);
120     template<typename T>
121     std::vector<T> getInternal(const std::string& key, std::vector<T>*);
122
123     std::mutex mConnMtx;
124
125     std::string mPath;
126     sqlite3::Connection mConn;
127     std::unique_ptr<sqlite3::Statement> mGetValueStmt;
128     std::unique_ptr<sqlite3::Statement> mGetValueCountStmt;
129     std::unique_ptr<sqlite3::Statement> mGetSizeStmt;
130     std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
131     std::unique_ptr<sqlite3::Statement> mSetValueStmt;
132     std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
133
134     void setupDb();
135     void prepareStatements();
136
137     void removeInternal(const std::string& key);
138     unsigned int countInternal(const std::string& key);
139
140 };
141
142 namespace {
143 template<typename T>
144 std::string toString(const T& value)
145 {
146     std::ostringstream oss;
147     oss << value;
148     return oss.str();
149 }
150
151 template<typename T>
152 T fromString(const std::string& strValue)
153 {
154     std::istringstream iss(strValue);
155     T value;
156     iss >> value;
157     return value;
158 }
159
160 } // namespace
161
162 template<typename T>
163 void KVStore::setInternal(const std::string& key, const T& value)
164 {
165     setInternal(key, toString(value));
166 }
167
168 template<typename T>
169 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
170 {
171     std::vector<std::string> strValues(values.size());
172
173     std::transform(values.begin(),
174                    values.end(),
175                    strValues.begin(),
176                    toString<T>);
177
178     setInternal(key, strValues);
179 }
180
181 template<typename T>
182 T KVStore::getInternal(const std::string& key, T*)
183 {
184     return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
185 }
186
187 template<typename T>
188 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
189 {
190     std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
191     std::vector<T> values(strValues.size());
192
193     std::transform(strValues.begin(),
194                    strValues.end(),
195                    values.begin(),
196                    fromString<T>);
197
198     return values;
199 }
200
201 /**
202  * Concatenates all parameters into one std::string.
203  * Uses '.' to connect the terms.
204  * @param args components of the string
205  * @tparam delim optional delimiter
206  * @tparam typename ... Args any type implementing str
207  * @return string created from he args
208  */
209 template<char delim = '.', typename Arg1, typename ... Args>
210 std::string key(const Arg1& a1, const Args& ... args)
211 {
212     std::string ret = toString(a1);
213     std::initializer_list<std::string> strings {toString(args)...};
214     for (const std::string& s : strings) {
215         ret += delim + s;
216     }
217
218     return ret;
219 }
220
221 /**
222  * Function added for key function completeness.
223  *
224  * @tparam delim = '.' parameter not used, added for consistency
225  * @return empty string
226  */
227 template<char delim = '.'>
228 std::string key()
229 {
230     return std::string();
231 }
232
233 } // namespace config
234
235 #endif // COMMON_CONFIG_KVSTORE_HPP
236
237