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/statement.hpp"
29 #include "config/sqlite3/connection.hpp"
35 #include <initializer_list>
45 * @param path configuration database file path
47 KVStore(const std::string& path);
51 * Clears all the stored data
57 * @return Number of all stored values
62 * @param key string regexp of the stored values
64 * @return Number of values corresponding to the passed key
66 unsigned int count(const std::string& key);
69 * Removes values corresponding to the passed key.
70 * Many values may correspond to one key, so many values may
73 * @param key string regexp of the stored values
75 void remove(const std::string& key);
78 * Stores a single value corresponding to the passed key
79 * @param key string key of the value
80 * @param value string value
82 void set(const std::string& key, const std::string& value);
85 * Stores a vector of values.
86 * Generates new keys using appending a '.' and consecutive integers.
87 * Removes values corresponding to this key before anything is added.
89 * @param key string key of the value
90 * @param value string value
92 void set(const std::string& key, const std::vector<std::string>& values);
95 * Stores values from the list.
97 * @see KVStore::set(const std::string& key, const std::vector<std::string>& values)
98 * @param key string key of the value
99 * @param values [description]
101 void set(const std::string& key, const std::initializer_list<std::string>& values);
104 * @param key string key of the value
105 * @return string value corresponding to this particular key
107 std::string get(const std::string& key);
110 * @param key string key of the value
111 * @return vector of values corresponding to the key
113 std::vector<std::string> list(const std::string& key);
116 typedef std::lock_guard<std::mutex> Lock;
120 sqlite3::Connection mConn;
121 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
122 std::unique_ptr<sqlite3::Statement> mGetValueCountStmt;
123 std::unique_ptr<sqlite3::Statement> mGetSizeStmt;
124 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
125 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
126 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
129 void prepareStatements();
131 void removeInternal(const std::string& key);
132 unsigned int countInternal(const std::string& key);
136 } // namespace config
138 #endif // COMMON_CONFIG_KVSTORE_HPP