Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
kvstore.hpp
Go to the documentation of this file.
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 
25 #ifndef CARGO_SQLITE_KVSTORE_HPP
26 #define CARGO_SQLITE_KVSTORE_HPP
27 
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 #include <utility>
40 
41 namespace cargo {
42 
43 class KVStore {
44 
45 public:
49  class Transaction {
50  public:
51  Transaction(KVStore& store);
52  ~Transaction();
53 
54  Transaction(const Transaction&) = delete;
55  Transaction& operator=(const Transaction&) = delete;
56 
57  void commit();
58  private:
59  std::unique_lock<std::recursive_mutex> mLock;
61  bool mIsOuter;
62  };
63 
67  explicit KVStore(const std::string& path);
68  ~KVStore();
69 
70  KVStore(const KVStore&) = delete;
71  KVStore& operator=(const KVStore&) = delete;
72 
76  void clear();
77 
81  bool isEmpty();
82 
88  bool exists(const std::string& key);
89 
95  bool prefixExists(const std::string& key);
96 
104  void remove(const std::string& key);
105 
112  void set(const std::string& key, const std::string& value);
113 
120  std::string get(const std::string& key);
121 
125  std::vector<std::string> getKeys();
126 
127 private:
128  typedef std::lock_guard<std::recursive_mutex> Lock;
129 
130  std::recursive_mutex mMutex;
133 
134  std::string mPath;
136  std::unique_ptr<sqlite3::Statement> mGetValueStmt;
137  std::unique_ptr<sqlite3::Statement> mGetKeyExistsStmt;
138  std::unique_ptr<sqlite3::Statement> mGetKeyPrefixExistsStmt;
139  std::unique_ptr<sqlite3::Statement> mGetIsEmptyStmt;
140  std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
141  std::unique_ptr<sqlite3::Statement> mSetValueStmt;
142  std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
143  std::unique_ptr<sqlite3::Statement> mGetKeysStmt;
144 
145  void setupDb();
146  void prepareStatements();
147  void createFunctions();
148 };
149 
150 } // namespace cargo
151 
152 #endif // CARGO_SQLITE_KVSTORE_HPP
std::string mPath
Definition: kvstore.hpp:134
bool prefixExists(const std::string &key)
Definition: kvstore.cpp:259
std::unique_ptr< sqlite3::Statement > mGetKeysStmt
Definition: kvstore.hpp:143
A guard struct for thread synchronization and transaction management.
Definition: kvstore.hpp:49
void createFunctions()
Definition: kvstore.cpp:214
void setupDb()
Definition: kvstore.cpp:190
sqlite3::Connection mConn
Definition: kvstore.hpp:135
std::vector< std::string > getKeys()
Returns all stored keys.
Definition: kvstore.cpp:288
bool mIsTransactionCommited
Definition: kvstore.hpp:132
Definition: kvstore.hpp:43
KVStore & operator=(const KVStore &)=delete
size_t mTransactionDepth
Definition: kvstore.hpp:131
KVStore & mKVStore
Definition: kvstore.hpp:60
Declaration of the class managing a sqlite3 database connection.
Definition: connection.hpp:34
Transaction & operator=(const Transaction &)=delete
std::unique_ptr< sqlite3::Statement > mGetKeyExistsStmt
Definition: kvstore.hpp:137
Declaration of the class managing a sqlite3 statement.
~KVStore()
Definition: kvstore.cpp:148
std::unique_ptr< sqlite3::Statement > mGetKeyPrefixExistsStmt
Definition: kvstore.hpp:138
KVStore(const std::string &path)
Definition: kvstore.cpp:137
std::unique_lock< std::recursive_mutex > mLock
Definition: kvstore.hpp:59
std::unique_ptr< sqlite3::Statement > mSetValueStmt
Definition: kvstore.hpp:141
std::string key(const Arg1 &a1, const Args &...args)
Concatenates all parameters into one std::string.
Definition: kvstore-visitor-utils.hpp:60
void set(const std::string &key, const std::string &value)
Stores a single value corresponding to the passed key.
Definition: kvstore.cpp:153
~Transaction()
Definition: kvstore.cpp:114
std::recursive_mutex mMutex
Definition: kvstore.hpp:130
Transaction(KVStore &store)
Definition: kvstore.cpp:100
bool exists(const std::string &key)
Definition: kvstore.cpp:243
std::lock_guard< std::recursive_mutex > Lock
Definition: kvstore.hpp:128
bool mIsOuter
Definition: kvstore.hpp:61
void prepareStatements()
Definition: kvstore.cpp:196
std::unique_ptr< sqlite3::Statement > mGetValueListStmt
Definition: kvstore.hpp:140
std::unique_ptr< sqlite3::Statement > mGetIsEmptyStmt
Definition: kvstore.hpp:139
std::unique_ptr< sqlite3::Statement > mRemoveValuesStmt
Definition: kvstore.hpp:142
bool isEmpty()
Definition: kvstore.cpp:229
void clear()
Clears all the stored data.
Definition: kvstore.cpp:222
void commit()
Definition: kvstore.cpp:126
std::unique_ptr< sqlite3::Statement > mGetValueStmt
Definition: kvstore.hpp:136