Saving and loading configurations from KVStore 08/26508/2
authorJan Olszak <j.olszak@samsung.com>
Mon, 25 Aug 2014 13:59:51 +0000 (15:59 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 25 Aug 2014 15:23:24 +0000 (17:23 +0200)
[Bug/Feature]   Configuration structs are serializable to
                and from a KVStore file
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install, run tests

Change-Id: Ide83199431cd1357de1f7a9865984033fce47547

src/config/from-json-visitor.hpp
src/config/from-kvstore-visitor.hpp [new file with mode: 0644]
src/config/kvstore.cpp
src/config/kvstore.hpp
src/config/manager.hpp
src/config/to-json-visitor.hpp
src/config/to-kvstore-visitor.hpp [new file with mode: 0644]

index cc19a8c..9ebe1fc 100644 (file)
@@ -55,6 +55,8 @@ public:
         json_object_put(mObject);
     }
 
+    FromJsonVisitor& operator=(const FromJsonVisitor&) = delete;
+
     template<class T>
     void visit(const std::string& name, T& value)
     {
@@ -68,7 +70,6 @@ public:
 private:
     json_object* mObject;
 
-    FromJsonVisitor& operator=(const FromJsonVisitor&) = delete;
 
     explicit FromJsonVisitor(json_object* object)
         : mObject(json_object_get(object))
diff --git a/src/config/from-kvstore-visitor.hpp b/src/config/from-kvstore-visitor.hpp
new file mode 100644 (file)
index 0000000..5b63410
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak (j.olszak@samsung.com)
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Visitor for loading from KVStore
+ */
+
+#ifndef CONFIG_FROM_KVSTORE_VISITOR_HPP
+#define CONFIG_FROM_KVSTORE_VISITOR_HPP
+
+#include "config/is-visitable.hpp"
+#include "config/exception.hpp"
+#include "config/kvstore.hpp"
+
+#include <string>
+#include <memory>
+
+namespace config {
+
+class FromKVStoreVisitor {
+public:
+    explicit FromKVStoreVisitor(const std::string& filePath, const std::string& prefix)
+        : mStorePtr(new KVStore(filePath)),
+          mKeyPrefix(prefix)
+    {
+    }
+
+    FromKVStoreVisitor(const FromKVStoreVisitor& visitor, const std::string& prefix)
+        : mStorePtr(visitor.mStorePtr),
+          mKeyPrefix(prefix)
+    {
+    }
+
+    ~FromKVStoreVisitor()
+    {
+    }
+
+    FromKVStoreVisitor& operator=(const FromKVStoreVisitor&) = delete;
+
+    template<class T>
+    void visit(const std::string& name, T& value)
+    {
+        getInternal(key(mKeyPrefix, name), value);
+    }
+
+private:
+    std::shared_ptr<KVStore> mStorePtr;
+    std::string mKeyPrefix;
+
+    template<class T, class std::enable_if<!isVisitable<T>::value, int>::type = 0>
+    void getInternal(const std::string& name, T& value)
+    {
+        value = mStorePtr->get<T>(name);
+    }
+
+    template<class T, class std::enable_if<isVisitable<T>::value, int>::type = 0>
+    void getInternal(const std::string& name, T& value)
+    {
+        FromKVStoreVisitor visitor(*this, name);
+        value.accept(visitor);
+    }
+};
+
+} // namespace config
+
+#endif // CONFIG_FROM_KVSTORE_VISITOR_HPP
index cc174e4..03231be 100644 (file)
@@ -104,12 +104,20 @@ std::string escape(const std::string& in)
 
 
 KVStore::KVStore(const std::string& path)
-    : mConn(path)
+    : mPath(path),
+      mConn(path)
 {
     setupDb();
     prepareStatements();
 }
 
+KVStore::KVStore(const KVStore& store)
+    : mPath(store.mPath),
+      mConn(mPath)
+{
+    prepareStatements();
+}
+
 KVStore::~KVStore()
 {
 }
index b01c5da..6e30d99 100644 (file)
@@ -46,6 +46,7 @@ public:
      * @param path configuration database file path
      */
     KVStore(const std::string& path);
+    KVStore(const KVStore& store);
     ~KVStore();
 
     /**
@@ -121,6 +122,7 @@ private:
 
     std::mutex mConnMtx;
 
+    std::string mPath;
     sqlite3::Connection mConn;
     std::unique_ptr<sqlite3::Statement> mGetValueStmt;
     std::unique_ptr<sqlite3::Statement> mGetValueCountStmt;
index a1d7dce..688bc54 100644 (file)
 #define CONFIG_MANAGER_HPP
 
 #include "config/to-json-visitor.hpp"
+#include "config/to-kvstore-visitor.hpp"
 #include "config/from-json-visitor.hpp"
+#include "config/from-kvstore-visitor.hpp"
 #include "config/is-visitable.hpp"
 #include "config/fs-utils.hpp"
 
 
 namespace config {
 
+/**
+ * Fills the configuration with data stored in the json string
+ *
+ * @param jsonString configuration in a json format
+ * @param config     visitable structure to fill
+ */
 template <class Config>
 void loadFromString(const std::string& jsonString, Config& config)
 {
@@ -42,6 +50,12 @@ void loadFromString(const std::string& jsonString, Config& config)
     config.accept(visitor);
 }
 
+
+/**
+ * Creates a string representation of the configuration in json format
+ *
+ * @param config   visitable structure to convert
+ */
 template <class Config>
 std::string saveToString(const Config& config)
 {
@@ -52,6 +66,12 @@ std::string saveToString(const Config& config)
     return visitor.toString();
 }
 
+/**
+ * Loads the config from a json file
+ *
+ * @param filename path to the file
+ * @param config   visitable structure to load
+ */
 template <class Config>
 void loadFromFile(const std::string& filename, Config& config)
 {
@@ -66,6 +86,12 @@ void loadFromFile(const std::string& filename, Config& config)
     }
 }
 
+/**
+ * Saves the config in a json file
+ *
+ * @param filename path to the file
+ * @param config   visitable structure to save
+ */
 template <class Config>
 void saveToFile(const std::string& filename, const Config& config)
 {
@@ -75,6 +101,38 @@ void saveToFile(const std::string& filename, const Config& config)
     }
 }
 
+/**
+ * Loads a visitable configuration from KVStore.
+ *
+ * @param filename   path to the KVStore db
+ * @param config     visitable structure to load
+ * @param configName name of the configuration inside the KVStore db
+ */
+template <class Config>
+void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "")
+{
+    static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
+
+    FromKVStoreVisitor visitor(filename, configName);
+    config.accept(visitor);
+}
+
+/**
+ * Saves the config to a KVStore.
+ *
+ * @param filename   path to the KVStore db
+ * @param config     visitable structure to load
+ * @param configName name of the config inside the KVStore db
+ */
+template <class Config>
+void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "")
+{
+    static_assert(isVisitable<Config>::value, "Use CONFIG_REGISTER macro");
+
+    ToKVStoreVisitor visitor(filename, configName);
+    config.accept(visitor);
+}
+
 } // namespace config
 
 #endif // CONFIG_MANAGER_HPP
index 5b70eb9..ade4ef3 100644 (file)
@@ -51,6 +51,8 @@ public:
         json_object_put(mObject);
     }
 
+    ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
+
     std::string toString() const
     {
         return json_object_to_json_string(mObject);
@@ -64,7 +66,6 @@ public:
 private:
     json_object* mObject;
 
-    ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
 
     json_object* detach()
     {
diff --git a/src/config/to-kvstore-visitor.hpp b/src/config/to-kvstore-visitor.hpp
new file mode 100644 (file)
index 0000000..8e6f577
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak (j.olszak@samsung.com)
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Visitor for saving to KVStore
+ */
+
+#ifndef CONFIG_TO_KVSTORE_VISITOR_HPP
+#define CONFIG_TO_KVSTORE_VISITOR_HPP
+
+#include "config/is-visitable.hpp"
+#include "config/kvstore.hpp"
+
+#include <string>
+#include <memory>
+
+namespace config {
+
+class ToKVStoreVisitor {
+
+public:
+    ToKVStoreVisitor(const std::string& filePath, const std::string& prefix)
+        : mStorePtr(new KVStore(filePath)),
+          mKeyPrefix(prefix)
+    {
+    }
+
+    ToKVStoreVisitor(const ToKVStoreVisitor& visitor, const std::string& prefix)
+        : mStorePtr(visitor.mStorePtr),
+          mKeyPrefix(prefix)
+    {
+    }
+
+    ~ToKVStoreVisitor()
+    {
+    }
+
+    ToKVStoreVisitor& operator=(const ToKVStoreVisitor&) = delete;
+
+    template<class T>
+    void visit(const std::string& name, const T& value)
+    {
+        setInternal(key(mKeyPrefix, name), value);
+    }
+
+private:
+    std::shared_ptr<KVStore> mStorePtr;
+    std::string mKeyPrefix;
+
+
+    template<class T, class std::enable_if<!isVisitable<T>::value, int>::type = 0>
+    void setInternal(const std::string& name, const T& value)
+    {
+        mStorePtr->set(name, value);
+    }
+
+    template<class T, class std::enable_if<isVisitable<T>::value, int>::type = 0>
+    void setInternal(const std::string& name, const T& value)
+    {
+        ToKVStoreVisitor visitor(*this, name);
+        value.accept(visitor);
+    }
+};
+
+} // namespace config
+
+#endif // CONFIG_TO_KVSTORE_VISITOR_HPP