Kagome
Polkadot Runtime Engine in C++17
trie_storage_provider_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_RUNTIME_COMMON_TRIE_STORAGE_PROVIDER_IMPL
7 #define KAGOME_CORE_RUNTIME_COMMON_TRIE_STORAGE_PROVIDER_IMPL
8 
10 
11 #include <stack>
12 #include <unordered_map>
13 
14 #include "common/buffer.hpp"
15 #include "log/logger.hpp"
19 
20 namespace kagome::runtime {
21 
23  public:
24  enum class Error {
25  NO_BATCH = 1,
26  };
27 
28  explicit TrieStorageProviderImpl(
29  std::shared_ptr<storage::trie::TrieStorage> trie_storage,
30  std::shared_ptr<storage::trie::TrieSerializer> trie_serializer);
31 
32  ~TrieStorageProviderImpl() override = default;
33 
34  outcome::result<void> setToEphemeralAt(
35  const common::Hash256 &state_root) override;
36 
37  outcome::result<void> setToPersistentAt(
38  const common::Hash256 &state_root) override;
39 
40  std::shared_ptr<Batch> getCurrentBatch() const override;
41  std::optional<std::shared_ptr<PersistentBatch>> tryGetPersistentBatch()
42  const override;
43  bool isCurrentlyPersistent() const override;
44 
45  outcome::result<std::shared_ptr<PersistentBatch>> getChildBatchAt(
46  const common::Buffer &root_path) override;
47 
48  outcome::result<storage::trie::RootHash> forceCommit() override;
49 
50  outcome::result<void> startTransaction() override;
51  outcome::result<void> rollbackTransaction() override;
52  outcome::result<void> commitTransaction() override;
53 
54  void clearChildBatches() noexcept override;
55 
56  private:
57  std::shared_ptr<storage::trie::TrieStorage> trie_storage_;
58  std::shared_ptr<storage::trie::TrieSerializer> trie_serializer_;
59 
60  std::stack<std::shared_ptr<Batch>> stack_of_batches_;
61 
62  std::shared_ptr<Batch> current_batch_;
63 
64  // need to store it because it has to be the same in different runtime calls
65  // to keep accumulated changes for commit to the main storage
66  std::shared_ptr<PersistentBatch> persistent_batch_;
67 
68  std::unordered_map<common::Buffer, std::shared_ptr<PersistentBatch>>
70 
72  };
73 
74 } // namespace kagome::runtime
75 
77 
78 #endif // KAGOME_CORE_RUNTIME_COMMON_TRIE_STORAGE_PROVIDER_IMPL
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< void > commitTransaction() override
Commit and finish last started transaction.
std::shared_ptr< PersistentBatch > persistent_batch_
outcome::result< void > startTransaction() override
Start nested transaction.
outcome::result< std::shared_ptr< PersistentBatch > > getChildBatchAt(const common::Buffer &root_path) override
Get (or create new) Child Batch with given root hash.
OUTCOME_HPP_DECLARE_ERROR(kagome::runtime, TrieStorageProviderImpl::Error)
outcome::result< void > setToPersistentAt(const common::Hash256 &state_root) override
outcome::result< void > setToEphemeralAt(const common::Hash256 &state_root) override
std::shared_ptr< storage::trie::TrieSerializer > trie_serializer_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< storage::trie::TrieStorage > trie_storage_
std::unordered_map< common::Buffer, std::shared_ptr< PersistentBatch > > child_batches_
std::stack< std::shared_ptr< Batch > > stack_of_batches_
std::shared_ptr< Batch > getCurrentBatch() const override
outcome::result< void > rollbackTransaction() override
Rollback and finish last started transaction.
std::optional< std::shared_ptr< PersistentBatch > > tryGetPersistentBatch() const override
TrieStorageProviderImpl(std::shared_ptr< storage::trie::TrieStorage > trie_storage, std::shared_ptr< storage::trie::TrieSerializer > trie_serializer)
outcome::result< storage::trie::RootHash > forceCommit() override