/**
* A guard struct for thread synchronization and transaction management.
*/
- typedef std::shared_ptr<void> Transaction;
+ class Transaction {
+ public:
+ Transaction(KVStore& store);
+ ~Transaction();
+
+ Transaction(const Transaction&) = delete;
+ Transaction& operator=(const Transaction&) = delete;
+
+ void commit();
+ private:
+ std::unique_lock<std::recursive_mutex> mLock;
+ KVStore& mKVStore;
+ bool mIsOuter;
+ };
/**
* @param path configuration database file path
*/
- KVStore(const std::string& path);
- KVStore(const KVStore& store);
+ explicit KVStore(const std::string& path);
~KVStore();
+ KVStore(const KVStore&) = delete;
+ KVStore& operator=(const KVStore&) = delete;
+
/**
* Clears all the stored data
*/
return getInternal(key, static_cast<T*>(nullptr));
}
- KVStore::Transaction getTransaction();
+ /**
+ * Returns all stored keys.
+ */
+ std::vector<std::string> getKeys();
private:
- typedef std::lock_guard<std::mutex> Lock;
+ typedef std::lock_guard<std::recursive_mutex> Lock;
- struct TransactionImpl;
- std::weak_ptr<TransactionImpl> mTransactionImplPtr;
- std::mutex getTransactionMutex;
- std::mutex mConnMtx;
+ std::recursive_mutex mMutex;
+ size_t mTransactionDepth;
+ bool mIsTransactionCommited;
void setInternal(const std::string& key, const std::string& value);
void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
std::unique_ptr<sqlite3::Statement> mSetValueStmt;
std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
+ std::unique_ptr<sqlite3::Statement> mGetKeysStmt;
void setupDb();
void prepareStatements();