Kagome
Polkadot Runtime Engine in C++17
trie_storage_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <memory>
9 
10 #include "outcome/outcome.hpp"
13 
14 namespace kagome::storage::trie {
15 
16  outcome::result<std::unique_ptr<TrieStorageImpl>>
18  const std::shared_ptr<PolkadotTrieFactory> &trie_factory,
19  std::shared_ptr<Codec> codec,
20  std::shared_ptr<TrieSerializer> serializer,
21  std::optional<std::shared_ptr<changes_trie::ChangesTracker>> changes) {
22  // will never be used, so content of the callback doesn't matter
23  auto empty_trie =
24  trie_factory->createEmpty([](auto &) { return outcome::success(); });
25  // ensure retrieval of empty trie succeeds
26  OUTCOME_TRY(serializer->storeTrie(*empty_trie));
27  return std::unique_ptr<TrieStorageImpl>(new TrieStorageImpl(
28  std::move(codec), std::move(serializer), std::move(changes)));
29  }
30 
31  outcome::result<std::unique_ptr<TrieStorageImpl>>
33  std::shared_ptr<Codec> codec,
34  std::shared_ptr<TrieSerializer> serializer,
35  std::optional<std::shared_ptr<changes_trie::ChangesTracker>> changes) {
36  return std::unique_ptr<TrieStorageImpl>(new TrieStorageImpl(
37  std::move(codec), std::move(serializer), std::move(changes)));
38  }
39 
41  std::shared_ptr<Codec> codec,
42  std::shared_ptr<TrieSerializer> serializer,
43  std::optional<std::shared_ptr<changes_trie::ChangesTracker>> changes)
44  : codec_{std::move(codec)},
45  serializer_{std::move(serializer)},
46  changes_{std::move(changes)},
47  logger_{log::createLogger("TrieStorage", "storage")} {
48  BOOST_ASSERT(codec_ != nullptr);
49  BOOST_ASSERT(serializer_ != nullptr);
50  BOOST_ASSERT((changes_.has_value() and changes_.value() != nullptr)
51  or not changes_.has_value());
52  }
53 
54  outcome::result<std::unique_ptr<PersistentTrieBatch>>
56  SL_DEBUG(logger_,
57  "Initialize persistent trie batch with root: {}",
58  root.toHex());
59  OUTCOME_TRY(trie, serializer_->retrieveTrie(Buffer{root}));
61  codec_, serializer_, changes_, std::move(trie));
62  }
63 
64  outcome::result<std::unique_ptr<EphemeralTrieBatch>>
66  SL_DEBUG(
67  logger_, "Initialize ephemeral trie batch with root: {}", root.toHex());
68  OUTCOME_TRY(trie, serializer_->retrieveTrie(Buffer{root}));
69  return std::make_unique<EphemeralTrieBatchImpl>(codec_, std::move(trie));
70  }
71 } // namespace kagome::storage::trie
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
TrieStorageImpl(TrieStorageImpl const &)=delete
static outcome::result< std::unique_ptr< TrieStorageImpl > > createFromStorage(std::shared_ptr< Codec > codec, std::shared_ptr< TrieSerializer > serializer, std::optional< std::shared_ptr< changes_trie::ChangesTracker >> changes)
std::shared_ptr< TrieSerializer > serializer_
outcome::result< std::unique_ptr< EphemeralTrieBatch > > getEphemeralBatchAt(const RootHash &root) const override
static std::unique_ptr< PersistentTrieBatchImpl > create(std::shared_ptr< Codec > codec, std::shared_ptr< TrieSerializer > serializer, std::optional< std::shared_ptr< changes_trie::ChangesTracker >> changes, std::shared_ptr< PolkadotTrie > trie)
std::string toHex() const noexcept
Definition: blob.hpp:160
outcome::result< std::unique_ptr< PersistentTrieBatch > > getPersistentBatchAt(const RootHash &root) override
static outcome::result< std::unique_ptr< TrieStorageImpl > > createEmpty(const std::shared_ptr< PolkadotTrieFactory > &trie_factory, std::shared_ptr< Codec > codec, std::shared_ptr< TrieSerializer > serializer, std::optional< std::shared_ptr< changes_trie::ChangesTracker >> changes)
std::optional< std::shared_ptr< changes_trie::ChangesTracker > > changes_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112