From b56838313e376c9ab7e44cce50bdb65914953b00 Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Mon, 25 Aug 2014 15:59:51 +0200 Subject: [PATCH] Saving and loading configurations from KVStore [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 | 3 +- src/config/from-kvstore-visitor.hpp | 83 ++++++++++++++++++++++++++++++++++++ src/config/kvstore.cpp | 10 ++++- src/config/kvstore.hpp | 2 + src/config/manager.hpp | 58 +++++++++++++++++++++++++ src/config/to-json-visitor.hpp | 3 +- src/config/to-kvstore-visitor.hpp | 84 +++++++++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 src/config/from-kvstore-visitor.hpp create mode 100644 src/config/to-kvstore-visitor.hpp diff --git a/src/config/from-json-visitor.hpp b/src/config/from-json-visitor.hpp index cc19a8c..9ebe1fc 100644 --- a/src/config/from-json-visitor.hpp +++ b/src/config/from-json-visitor.hpp @@ -55,6 +55,8 @@ public: json_object_put(mObject); } + FromJsonVisitor& operator=(const FromJsonVisitor&) = delete; + template 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 index 0000000..5b63410 --- /dev/null +++ b/src/config/from-kvstore-visitor.hpp @@ -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 +#include + +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 + void visit(const std::string& name, T& value) + { + getInternal(key(mKeyPrefix, name), value); + } + +private: + std::shared_ptr mStorePtr; + std::string mKeyPrefix; + + template::value, int>::type = 0> + void getInternal(const std::string& name, T& value) + { + value = mStorePtr->get(name); + } + + template::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 diff --git a/src/config/kvstore.cpp b/src/config/kvstore.cpp index cc174e4..03231be 100644 --- a/src/config/kvstore.cpp +++ b/src/config/kvstore.cpp @@ -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() { } diff --git a/src/config/kvstore.hpp b/src/config/kvstore.hpp index b01c5da..6e30d99 100644 --- a/src/config/kvstore.hpp +++ b/src/config/kvstore.hpp @@ -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 mGetValueStmt; std::unique_ptr mGetValueCountStmt; diff --git a/src/config/manager.hpp b/src/config/manager.hpp index a1d7dce..688bc54 100644 --- a/src/config/manager.hpp +++ b/src/config/manager.hpp @@ -26,13 +26,21 @@ #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 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 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 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 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 +void loadFromKVStore(const std::string& filename, Config& config, const std::string& configName = "") +{ + static_assert(isVisitable::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 +void saveToKVStore(const std::string& filename, const Config& config, const std::string& configName = "") +{ + static_assert(isVisitable::value, "Use CONFIG_REGISTER macro"); + + ToKVStoreVisitor visitor(filename, configName); + config.accept(visitor); +} + } // namespace config #endif // CONFIG_MANAGER_HPP diff --git a/src/config/to-json-visitor.hpp b/src/config/to-json-visitor.hpp index 5b70eb9..ade4ef3 100644 --- a/src/config/to-json-visitor.hpp +++ b/src/config/to-json-visitor.hpp @@ -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 index 0000000..8e6f577 --- /dev/null +++ b/src/config/to-kvstore-visitor.hpp @@ -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 +#include + +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 + void visit(const std::string& name, const T& value) + { + setInternal(key(mKeyPrefix, name), value); + } + +private: + std::shared_ptr mStorePtr; + std::string mKeyPrefix; + + + template::value, int>::type = 0> + void setInternal(const std::string& name, const T& value) + { + mStorePtr->set(name, value); + } + + template::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 -- 2.7.4