hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
storage_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_STORAGE_IMPL_HPP
7 #define IROHA_STORAGE_IMPL_HPP
8 
9 #include "ametsuchi/storage.hpp"
10 
11 #include <atomic>
12 #include <shared_mutex>
13 
14 #include <soci/soci.h>
15 #include <boost/optional.hpp>
16 #include <rxcpp/rx-lite.hpp>
24 #include "logger/logger_fwd.hpp"
26 
27 namespace shared_model {
28  namespace interface {
29  class QueryResponseFactory;
30  } // namespace interface
31 } // namespace shared_model
32 
33 namespace iroha {
34 
35  class PendingTransactionStorage;
36 
37  namespace ametsuchi {
38  class StorageImpl : public Storage {
39  public:
40  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
41  std::unique_ptr<ametsuchi::PostgresOptions> postgres_options,
42  std::shared_ptr<PoolWrapper> pool_wrapper,
43  std::shared_ptr<shared_model::interface::PermissionToString>
44  perm_converter,
45  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
46  std::shared_ptr<shared_model::interface::QueryResponseFactory>
47  query_response_factory,
48  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
49  std::unique_ptr<BlockStorage> persistent_block_storage,
50  logger::LoggerManagerTreePtr log_manager,
51  size_t pool_size = 10);
52 
54  createCommandExecutor() override;
55 
56  std::unique_ptr<TemporaryWsv> createTemporaryWsv(
57  std::shared_ptr<CommandExecutor> command_executor) override;
58 
59  std::unique_ptr<MutableStorage> createMutableStorage(
60  std::shared_ptr<CommandExecutor> command_executor) override;
61 
62  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
63  const override;
64 
65  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
66  const override;
67 
68  boost::optional<std::unique_ptr<SettingQuery>> createSettingQuery()
69  const override;
70 
72  createQueryExecutor(
73  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
74  std::shared_ptr<shared_model::interface::QueryResponseFactory>
75  response_factory) const override;
76 
77  bool insertBlock(
78  std::shared_ptr<const shared_model::interface::Block> block) override;
79 
81  const shared_model::interface::Peer &peer) override;
82 
83  std::unique_ptr<MutableStorage> createMutableStorage(
84  std::shared_ptr<CommandExecutor> command_executor,
85  BlockStorageFactory &storage_factory) override;
86 
87  void reset() override;
88 
89  expected::Result<void, std::string> resetWsv() override;
90 
91  void resetPeers() override;
92 
93  void dropStorage() override;
94 
95  void freeConnections() override;
96 
97  CommitResult commit(
98  std::unique_ptr<MutableStorage> mutable_storage) override;
99 
100  bool preparedCommitEnabled() const override;
101 
102  CommitResult commitPrepared(
103  std::shared_ptr<const shared_model::interface::Block> block) override;
104 
105  std::shared_ptr<WsvQuery> getWsvQuery() const override;
106 
107  std::shared_ptr<BlockQuery> getBlockQuery() const override;
108 
109  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
110  on_commit() override;
111 
112  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
113 
114  ~StorageImpl() override;
115 
116  protected:
117  StorageImpl(
118  boost::optional<std::shared_ptr<const iroha::LedgerState>>
119  ledger_state,
120  std::unique_ptr<ametsuchi::PostgresOptions> postgres_options,
121  std::unique_ptr<BlockStorage> block_store,
122  std::shared_ptr<PoolWrapper> pool_wrapper,
123  std::shared_ptr<shared_model::interface::PermissionToString>
124  perm_converter,
125  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
126  std::shared_ptr<shared_model::interface::QueryResponseFactory>
127  query_response_factory,
128  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
129  size_t pool_size,
130  logger::LoggerManagerTreePtr log_manager);
131 
132  // db info
133  const std::unique_ptr<ametsuchi::PostgresOptions> postgres_options_;
134 
135  private:
137 
141  StoreBlockResult storeBlock(
142  std::shared_ptr<const shared_model::interface::Block> block);
143 
147  void tryRollback(soci::session &session);
148 
149  std::unique_ptr<BlockStorage> block_store_;
150 
151  std::shared_ptr<PoolWrapper> pool_wrapper_;
152 
154  std::shared_ptr<soci::connection_pool> &connection_;
155 
156  rxcpp::composite_subscription notifier_lifetime_;
157  rxcpp::subjects::subject<
158  std::shared_ptr<const shared_model::interface::Block>>
159  notifier_;
160 
161  std::shared_ptr<shared_model::interface::PermissionToString>
162  perm_converter_;
163 
164  std::shared_ptr<PendingTransactionStorage> pending_txs_storage_;
165 
166  std::shared_ptr<shared_model::interface::QueryResponseFactory>
167  query_response_factory_;
168 
169  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory_;
170 
171  logger::LoggerManagerTreePtr log_manager_;
172  logger::LoggerPtr log_;
173 
174  mutable std::shared_timed_mutex drop_mutex_;
175 
176  const size_t pool_size_;
177 
178  bool prepared_blocks_enabled_;
179 
180  std::atomic<bool> block_is_prepared_;
181 
182  std::string prepared_block_name_;
183 
184  boost::optional<std::shared_ptr<const iroha::LedgerState>> ledger_state_;
185  };
186  } // namespace ametsuchi
187 } // namespace iroha
188 
189 #endif // IROHA_STORAGE_IMPL_HPP
const std::unique_ptr< ametsuchi::PostgresOptions > postgres_options_
Definition: storage_impl.hpp:133
Definition: peer.hpp:20
Definition: result_fwd.hpp:27
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: storage.hpp:38
Definition: block_query.hpp:15
std::shared_ptr< LoggerManagerTree > LoggerManagerTreePtr
Definition: logger_manager_fwd.hpp:14
Definition: block_storage_factory.hpp:18
Definition: command_executor.hpp:12
Definition: storage_impl.hpp:38