-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: artyom-yurin <artem_yrin@mail.ru>
- Loading branch information
Showing
49 changed files
with
908 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "ametsuchi/impl/postgres_setting_query.hpp" | ||
|
||
#include <boost/lexical_cast.hpp> | ||
#include "interfaces/common_objects/types.hpp" | ||
#include "logger/logger.hpp" | ||
|
||
using namespace iroha::ametsuchi; | ||
|
||
namespace { | ||
template <typename T> | ||
bool getValueFromDb(soci::session &sql, | ||
const shared_model::interface::types::SettingKeyType &key, | ||
T &destination) { | ||
boost::optional<shared_model::interface::types::SettingValueType> value; | ||
|
||
sql << "SELECT setting_value FROM setting WHERE setting_key = :key", | ||
soci::into(value), soci::use(key, "key"); | ||
|
||
if (value) { | ||
destination = boost::lexical_cast<T>(value.get()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} // namespace | ||
|
||
PostgresSettingQuery::PostgresSettingQuery(std::unique_ptr<soci::session> sql, | ||
logger::LoggerPtr log) | ||
: psql_(std::move(sql)), sql_(*psql_), log_(std::move(log)) {} | ||
|
||
iroha::expected::Result< | ||
std::unique_ptr<const shared_model::validation::Settings>, | ||
std::string> | ||
PostgresSettingQuery::get() { | ||
return update(shared_model::validation::getDefaultSettings()); | ||
} | ||
|
||
iroha::expected::Result< | ||
std::unique_ptr<const shared_model::validation::Settings>, | ||
std::string> | ||
PostgresSettingQuery::update( | ||
std::unique_ptr<shared_model::validation::Settings> base) { | ||
auto get_and_log = | ||
[this](const shared_model::interface::types::SettingKeyType &key, | ||
auto &destination) { | ||
if (getValueFromDb(sql_, key, destination)) { | ||
log_->info("Updated value for " + key + ": {}", destination); | ||
} else { | ||
log_->info("Kept value for " + key + ": {}", destination); | ||
} | ||
}; | ||
|
||
try { | ||
get_and_log(kMaxDescriptionSizeKey, base->max_description_size); | ||
} catch (std::exception &e) { | ||
return expected::makeError(e.what()); | ||
} | ||
|
||
return std::move(base); | ||
} | ||
|
||
const shared_model::interface::types::SettingKeyType | ||
iroha::ametsuchi::kMaxDescriptionSizeKey = "MaxDescriptionSize"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_POSTGRES_SETTING_QUERY_HPP | ||
#define IROHA_POSTGRES_SETTING_QUERY_HPP | ||
|
||
#include "ametsuchi/setting_query.hpp" | ||
|
||
#include <soci/soci.h> | ||
#include <boost/optional.hpp> | ||
#include "logger/logger_fwd.hpp" | ||
|
||
namespace iroha { | ||
namespace ametsuchi { | ||
|
||
/** | ||
* Class which implements SettingQuery with a Postgres backend. | ||
*/ | ||
class PostgresSettingQuery : public SettingQuery { | ||
public: | ||
PostgresSettingQuery(std::unique_ptr<soci::session> sql, | ||
logger::LoggerPtr log); | ||
|
||
expected::Result< | ||
std::unique_ptr<const shared_model::validation::Settings>, | ||
std::string> | ||
get() override; | ||
|
||
private: | ||
expected::Result< | ||
std::unique_ptr<const shared_model::validation::Settings>, | ||
std::string> | ||
update(std::unique_ptr<shared_model::validation::Settings> base); | ||
|
||
std::unique_ptr<soci::session> psql_; | ||
soci::session &sql_; | ||
|
||
logger::LoggerPtr log_; | ||
}; | ||
|
||
extern const shared_model::interface::types::SettingKeyType | ||
kMaxDescriptionSizeKey; | ||
} // namespace ametsuchi | ||
} // namespace iroha | ||
|
||
#endif // IROHA_POSTGRES_SETTING_QUERY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_SETTING_QUERY_HPP | ||
#define IROHA_SETTING_QUERY_HPP | ||
|
||
#include <boost/optional.hpp> | ||
#include "common/result.hpp" | ||
#include "validators/settings.hpp" | ||
|
||
namespace iroha { | ||
|
||
namespace ametsuchi { | ||
/** | ||
* Public interface for get settings structure | ||
*/ | ||
class SettingQuery { | ||
public: | ||
virtual ~SettingQuery() = default; | ||
|
||
virtual expected::Result< | ||
std::unique_ptr<const shared_model::validation::Settings>, | ||
std::string> | ||
get() = 0; | ||
}; | ||
} // namespace ametsuchi | ||
} // namespace iroha | ||
|
||
#endif // IROHA_SETTING_QUERY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_SETTING_QUERY_FACTORY_HPP | ||
#define IROHA_SETTING_QUERY_FACTORY_HPP | ||
|
||
#include <boost/optional.hpp> | ||
|
||
#include "ametsuchi/setting_query.hpp" | ||
|
||
namespace iroha { | ||
namespace ametsuchi { | ||
class SettingQueryFactory { | ||
public: | ||
/** | ||
* Creates a setting query | ||
* @return Created setting query | ||
*/ | ||
virtual boost::optional<std::unique_ptr<SettingQuery>> | ||
createSettingQuery() const = 0; | ||
|
||
virtual ~SettingQueryFactory() = default; | ||
}; | ||
} // namespace ametsuchi | ||
} // namespace iroha | ||
#endif // IROHA_SETTING_QUERY_FACTORY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.