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 <cmath>
13 #include <shared_mutex>
14 
15 #include <soci/soci.h>
16 #include <boost/optional.hpp>
23 #include "logger/logger_fwd.hpp"
25 
26 namespace iroha {
27  namespace ametsuchi {
28 
29  class FlatFile;
30 
32  explicit ConnectionContext(std::unique_ptr<KeyValueStorage> block_store);
33 
34  std::unique_ptr<KeyValueStorage> block_store;
35  };
36 
37  class StorageImpl : public Storage {
38  protected:
39  static expected::Result<bool, std::string> createDatabaseIfNotExist(
40  const std::string &dbname,
41  const std::string &options_str_without_dbname);
42 
43  static expected::Result<ConnectionContext, std::string> initConnections(
44  std::string block_store_dir, logger::LoggerPtr log);
45 
46  static expected::Result<std::shared_ptr<soci::connection_pool>,
47  std::string>
48  initPostgresConnection(std::string &options_str, size_t pool_size);
49 
50  public:
51  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
52  std::string block_store_dir,
53  std::string postgres_connection,
54  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
55  factory,
56  std::shared_ptr<shared_model::interface::BlockJsonConverter>
57  converter,
58  std::shared_ptr<shared_model::interface::PermissionToString>
59  perm_converter,
60  std::unique_ptr<BlockStorageFactory> block_storage_factory,
61  logger::LoggerManagerTreePtr log_manager,
62  size_t pool_size = 10);
63 
64  expected::Result<std::unique_ptr<TemporaryWsv>, std::string>
65  createTemporaryWsv() override;
66 
67  expected::Result<std::unique_ptr<MutableStorage>, std::string>
68  createMutableStorage() override;
69 
70  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
71  const override;
72 
73  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
74  const override;
75 
76  boost::optional<std::shared_ptr<QueryExecutor>> createQueryExecutor(
77  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
78  std::shared_ptr<shared_model::interface::QueryResponseFactory>
79  response_factory) const override;
80 
86  bool insertBlock(
87  std::shared_ptr<const shared_model::interface::Block> block) override;
88 
94  bool insertBlocks(
95  const std::vector<std::shared_ptr<shared_model::interface::Block>>
96  &blocks) override;
97 
98  void reset() override;
99 
100  void dropStorage() override;
101 
102  void freeConnections() override;
103 
104  boost::optional<std::unique_ptr<LedgerState>> commit(
105  std::unique_ptr<MutableStorage> mutable_storage) override;
106 
107  boost::optional<std::unique_ptr<LedgerState>> commitPrepared(
108  std::shared_ptr<const shared_model::interface::Block> block) override;
109 
110  std::shared_ptr<WsvQuery> getWsvQuery() const override;
111 
112  std::shared_ptr<BlockQuery> getBlockQuery() const override;
113 
114  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
115  on_commit() override;
116 
117  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
118 
119  ~StorageImpl() override;
120 
121  protected:
122  StorageImpl(std::string block_store_dir,
123  PostgresOptions postgres_options,
124  std::unique_ptr<KeyValueStorage> block_store,
125  std::shared_ptr<soci::connection_pool> connection,
126  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
127  factory,
128  std::shared_ptr<shared_model::interface::BlockJsonConverter>
129  converter,
130  std::shared_ptr<shared_model::interface::PermissionToString>
131  perm_converter,
132  std::unique_ptr<BlockStorageFactory> block_storage_factory,
133  size_t pool_size,
134  bool enable_prepared_blocks,
135  logger::LoggerManagerTreePtr log_manager);
136 
140  const std::string block_store_dir_;
141 
142  // db info
144 
145  private:
149  void rollbackPrepared(soci::session &sql);
150 
154  bool storeBlock(
155  std::shared_ptr<const shared_model::interface::Block> block);
156 
157  std::unique_ptr<KeyValueStorage> block_store_;
158 
159  std::shared_ptr<soci::connection_pool> connection_;
160 
161  std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory_;
162 
163  rxcpp::composite_subscription notifier_lifetime_;
164  rxcpp::subjects::subject<
165  std::shared_ptr<const shared_model::interface::Block>>
166  notifier_;
167 
168  std::shared_ptr<shared_model::interface::BlockJsonConverter> converter_;
169 
170  std::shared_ptr<shared_model::interface::PermissionToString>
171  perm_converter_;
172 
173  std::unique_ptr<BlockStorageFactory> block_storage_factory_;
174 
175  logger::LoggerManagerTreePtr log_manager_;
176  logger::LoggerPtr log_;
177 
178  mutable std::shared_timed_mutex drop_mutex;
179 
180  size_t pool_size_;
181 
182  bool prepared_blocks_enabled_;
183 
184  std::atomic<bool> block_is_prepared;
185 
186  std::string prepared_block_name_;
187 
188  protected:
189  static const std::string &drop_;
190  static const std::string &reset_;
191  static const std::string &init_;
192  };
193  } // namespace ametsuchi
194 } // namespace iroha
195 
196 #endif // IROHA_STORAGE_IMPL_HPP
Definition: postgres_options.hpp:20
static const std::string & init_
Definition: storage_impl.hpp:191
static const std::string & drop_
Definition: storage_impl.hpp:189
static const std::string & reset_
Definition: storage_impl.hpp:190
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
const PostgresOptions postgres_options_
Definition: storage_impl.hpp:143
Definition: storage.hpp:36
Definition: block_query.hpp:16
Definition: storage_impl.hpp:31
std::shared_ptr< LoggerManagerTree > LoggerManagerTreePtr
Definition: logger_manager_fwd.hpp:14
ConnectionContext(std::unique_ptr< KeyValueStorage > block_store)
Definition: storage_impl.cpp:68
std::unique_ptr< KeyValueStorage > block_store
Definition: storage_impl.hpp:34
const std::string block_store_dir_
Definition: storage_impl.hpp:140
Definition: storage_impl.hpp:37