Add 'commit' method to kv transaction
[archive/platform/core/system/libConfig.git] / src / config / kvstore.hpp
index 3a2455b..0717665 100644 (file)
 #include <sstream>
 #include <string>
 #include <vector>
+#include <atomic>
 
 namespace config {
 
 class KVStore {
 
 public:
+    /**
+     * A guard struct for thread synchronization and transaction management.
+     */
+    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
      */
@@ -101,11 +121,17 @@ public:
         return getInternal(key, static_cast<T*>(nullptr));
     }
 
+    /**
+     * Returns all stored keys.
+     */
+    std::vector<std::string> getKeys();
 
 private:
-    struct Transaction;
     typedef std::lock_guard<std::recursive_mutex> Lock;
-    unsigned int mTransactionCounter;
+
+    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);
@@ -122,8 +148,6 @@ private:
     template<typename T>
     std::vector<T> getInternal(const std::string& key, std::vector<T>*);
 
-    std::recursive_mutex mConnMtx;
-
     std::string mPath;
     sqlite3::Connection mConn;
     std::unique_ptr<sqlite3::Statement> mGetValueStmt;
@@ -132,13 +156,11 @@ private:
     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();
     void createFunctions();
-
-    void removeInternal(const std::string& key);
-
 };
 
 namespace {