Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp"
19 :
20 : #include <soci/postgresql/soci-postgresql.h>
21 : #include <boost/format.hpp>
22 : #include <boost/optional.hpp>
23 :
24 : namespace iroha {
25 : namespace ametsuchi {
26 :
27 : bool PostgresOrderingServicePersistentState::execute_(std::string query) {
28 : try {
29 2178 : *sql_ << query;
30 2178 : } catch (std::exception &e) {
31 0 : log_->error("Failed to execute query: " + query
32 0 : + ". Reason: " + e.what());
33 0 : return false;
34 0 : }
35 2178 : return true;
36 2178 : }
37 :
38 : expected::Result<std::shared_ptr<PostgresOrderingServicePersistentState>,
39 : std::string>
40 : PostgresOrderingServicePersistentState::create(
41 : const std::string &postgres_options) {
42 0 : std::unique_ptr<soci::session> sql;
43 : try {
44 0 : sql =
45 0 : std::make_unique<soci::session>(soci::postgresql, postgres_options);
46 :
47 0 : } catch (std::exception &e) {
48 0 : return expected::makeError(
49 0 : (boost::format("Connection to PostgreSQL broken: %s") % e.what())
50 0 : .str());
51 0 : }
52 : expected::Result<std::shared_ptr<PostgresOrderingServicePersistentState>,
53 : std::string>
54 0 : storage;
55 0 : storage = expected::makeValue(
56 0 : std::make_shared<PostgresOrderingServicePersistentState>(
57 0 : std::move(sql)));
58 0 : return storage;
59 0 : }
60 :
61 : PostgresOrderingServicePersistentState::
62 : PostgresOrderingServicePersistentState(
63 : std::unique_ptr<soci::session> sql)
64 1204 : : sql_(std::move(sql)),
65 1204 : log_(logger::log("PostgresOrderingServicePersistentState")) {}
66 :
67 : bool PostgresOrderingServicePersistentState::initStorage() {
68 250 : return execute_(
69 250 : "CREATE TABLE IF NOT EXISTS ordering_service_state "
70 : "(proposal_height bigserial)")
71 250 : && execute_("INSERT INTO ordering_service_state VALUES (2)");
72 0 : }
73 :
74 : bool PostgresOrderingServicePersistentState::dropStorgage() {
75 250 : log_->info("Drop storage");
76 250 : return execute_("DROP TABLE IF EXISTS ordering_service_state");
77 0 : }
78 :
79 : bool PostgresOrderingServicePersistentState::saveProposalHeight(
80 : size_t height) {
81 714 : log_->info("Save proposal_height in ordering_service_state "
82 : + std::to_string(height));
83 714 : return execute_("DELETE FROM ordering_service_state")
84 714 : && execute_("INSERT INTO ordering_service_state VALUES ("
85 714 : + std::to_string(height) + ")");
86 0 : }
87 :
88 : boost::optional<size_t>
89 : PostgresOrderingServicePersistentState::loadProposalHeight() const {
90 260 : boost::optional<size_t> height;
91 260 : *sql_ << "SELECT * FROM ordering_service_state LIMIT 1",
92 : soci::into(height);
93 :
94 260 : if (not height) {
95 0 : log_->error(
96 : "There is no proposal_height in ordering_service_state. "
97 : "Use default value 2.");
98 0 : height = 2;
99 0 : }
100 260 : return height;
101 0 : }
102 :
103 : bool PostgresOrderingServicePersistentState::resetState() {
104 250 : return dropStorgage() & initStorage();
105 : }
106 :
107 : } // namespace ametsuchi
108 : } // namespace iroha
|