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>
17 
23 #include "logger/logger.hpp"
24 
25 namespace iroha {
26  namespace ametsuchi {
27 
28  class FlatFile;
29 
31  explicit ConnectionContext(std::unique_ptr<KeyValueStorage> block_store);
32 
33  std::unique_ptr<KeyValueStorage> block_store;
34  };
35 
36  class StorageImpl : public Storage {
37  protected:
38  static expected::Result<bool, std::string> createDatabaseIfNotExist(
39  const std::string &dbname,
40  const std::string &options_str_without_dbname);
41 
42  static expected::Result<ConnectionContext, std::string> initConnections(
43  std::string block_store_dir);
44 
45  static expected::Result<std::shared_ptr<soci::connection_pool>,
47  initPostgresConnection(std::string &options_str, size_t pool_size);
48 
49  public:
50  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
51  std::string block_store_dir,
52  std::string postgres_connection,
53  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
54  factory,
55  std::shared_ptr<shared_model::interface::BlockJsonConverter>
56  converter,
57  std::shared_ptr<shared_model::interface::PermissionToString>
58  perm_converter,
59  size_t pool_size = 10);
60 
61  expected::Result<std::unique_ptr<TemporaryWsv>, std::string>
62  createTemporaryWsv() override;
63 
64  expected::Result<std::unique_ptr<MutableStorage>, std::string>
65  createMutableStorage() override;
66 
67  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
68  const override;
69 
70  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
71  const override;
72 
73  boost::optional<std::shared_ptr<QueryExecutor>> createQueryExecutor(
74  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
75  std::shared_ptr<shared_model::interface::QueryResponseFactory>
76  response_factory) const override;
77 
83  bool insertBlock(const shared_model::interface::Block &block) override;
84 
90  bool insertBlocks(
91  const std::vector<std::shared_ptr<shared_model::interface::Block>>
92  &blocks) override;
93 
94  void reset() override;
95 
96  void dropStorage() override;
97 
98  void freeConnections() override;
99 
100  boost::optional<std::unique_ptr<LedgerState>> commit(
101  std::unique_ptr<MutableStorage> mutableStorage) override;
102 
103  boost::optional<std::unique_ptr<LedgerState>> commitPrepared(
104  const shared_model::interface::Block &block) override;
105 
106  std::shared_ptr<WsvQuery> getWsvQuery() const override;
107 
108  std::shared_ptr<BlockQuery> getBlockQuery() const override;
109 
110  rxcpp::observable<std::shared_ptr<shared_model::interface::Block>>
111  on_commit() override;
112 
113  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
114 
115  ~StorageImpl() override;
116 
117  protected:
118  StorageImpl(std::string block_store_dir,
119  PostgresOptions postgres_options,
120  std::unique_ptr<KeyValueStorage> block_store,
121  std::shared_ptr<soci::connection_pool> connection,
122  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
123  factory,
124  std::shared_ptr<shared_model::interface::BlockJsonConverter>
125  converter,
126  std::shared_ptr<shared_model::interface::PermissionToString>
127  perm_converter,
128  size_t pool_size,
129  bool enable_prepared_blocks,
130  logger::Logger log = logger::log("StorageImpl"));
131 
136 
137  // db info
139 
140  private:
144  void rollbackPrepared(soci::session &sql);
145 
149  bool storeBlock(const shared_model::interface::Block &block);
150 
151  std::unique_ptr<KeyValueStorage> block_store_;
152 
153  std::shared_ptr<soci::connection_pool> connection_;
154 
155  std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory_;
156 
157  rxcpp::subjects::subject<std::shared_ptr<shared_model::interface::Block>>
158  notifier_;
159 
160  std::shared_ptr<shared_model::interface::BlockJsonConverter> converter_;
161 
162  std::shared_ptr<shared_model::interface::PermissionToString>
163  perm_converter_;
164 
165  logger::Logger log_;
166 
167  mutable std::shared_timed_mutex drop_mutex;
168 
169  size_t pool_size_;
170 
171  bool prepared_blocks_enabled_;
172 
173  std::atomic<bool> block_is_prepared;
174 
175  std::string prepared_block_name_;
176 
177  protected:
178  static const std::string &drop_;
179  static const std::string &reset_;
180  static const std::string &init_;
181  };
182  } // namespace ametsuchi
183 } // namespace iroha
184 
185 #endif // IROHA_STORAGE_IMPL_HPP
Definition: postgres_options.hpp:20
static const std::string & init_
Definition: storage_impl.hpp:180
static const std::string & drop_
Definition: storage_impl.hpp:178
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
static const std::string & reset_
Definition: storage_impl.hpp:179
std::shared_ptr< spdlog::logger > Logger
Definition: logger.hpp:26
const PostgresOptions postgres_options_
Definition: storage_impl.hpp:138
Definition: storage.hpp:36
Definition: block.hpp:15
Definition: block_query.hpp:16
Logger log(const std::string &tag)
Definition: logger.cpp:48
Definition: storage_impl.hpp:30
ConnectionContext(std::unique_ptr< KeyValueStorage > block_store)
Definition: storage_impl.cpp:53
std::unique_ptr< KeyValueStorage > block_store
Definition: storage_impl.hpp:33
const std::string block_store_dir_
Definition: storage_impl.hpp:135
Definition: storage_impl.hpp:36