Kagome
Polkadot Runtime Engine in C++17
persistent_trie_batch_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <memory>
9 
13 
16  e) {
18  switch (e) {
19  case E::NO_TRIE:
20  return "Trie was not created or already was destructed.";
21  }
22  return "Unknown error";
23 }
24 
25 namespace kagome::storage::trie {
26  std::unique_ptr<PersistentTrieBatchImpl> PersistentTrieBatchImpl::create(
27  std::shared_ptr<Codec> codec,
28  std::shared_ptr<TrieSerializer> serializer,
29  std::optional<std::shared_ptr<changes_trie::ChangesTracker>> changes,
30  std::shared_ptr<PolkadotTrie> trie) {
31  std::unique_ptr<PersistentTrieBatchImpl> ptr(
32  new PersistentTrieBatchImpl(std::move(codec),
33  std::move(serializer),
34  std::move(changes),
35  std::move(trie)));
36  return ptr;
37  }
38 
40  std::shared_ptr<Codec> codec,
41  std::shared_ptr<TrieSerializer> serializer,
42  std::optional<std::shared_ptr<changes_trie::ChangesTracker>> changes,
43  std::shared_ptr<PolkadotTrie> trie)
44  : codec_{std::move(codec)},
45  serializer_{std::move(serializer)},
46  changes_{std::move(changes)},
47  trie_{std::move(trie)} {
48  BOOST_ASSERT(codec_ != nullptr);
49  BOOST_ASSERT(serializer_ != nullptr);
50  BOOST_ASSERT((changes_.has_value() && changes_.value() != nullptr)
51  or not changes_.has_value());
52  BOOST_ASSERT(trie_ != nullptr);
53  }
54 
55  outcome::result<RootHash> PersistentTrieBatchImpl::commit() {
56  OUTCOME_TRY(root, serializer_->storeTrie(*trie_));
58  return std::move(root);
59  }
60 
61  std::unique_ptr<TopperTrieBatch> PersistentTrieBatchImpl::batchOnTop() {
62  return std::make_unique<TopperTrieBatchImpl>(shared_from_this());
63  }
64 
65  outcome::result<BufferConstRef> PersistentTrieBatchImpl::get(
66  const BufferView &key) const {
67  return trie_->get(key);
68  }
69 
70  outcome::result<std::optional<BufferConstRef>>
72  return trie_->tryGet(key);
73  }
74 
75  std::unique_ptr<PolkadotTrieCursor> PersistentTrieBatchImpl::trieCursor() {
76  return std::make_unique<PolkadotTrieCursorImpl>(trie_);
77  }
78 
79  outcome::result<bool> PersistentTrieBatchImpl::contains(
80  const BufferView &key) const {
81  return trie_->contains(key);
82  }
83 
85  return trie_->empty();
86  }
87 
88  outcome::result<std::tuple<bool, uint32_t>>
90  std::optional<uint64_t> limit) {
92  return trie_->clearPrefix(
93  prefix, limit, [&](const auto &key, auto &&) -> outcome::result<void> {
94  if (changes_.has_value()) {
95  changes_.value()->onRemove(key);
96  }
97  return outcome::success();
98  });
99  }
100 
101  outcome::result<void> PersistentTrieBatchImpl::put(const BufferView &key,
102  const Buffer &value) {
103  OUTCOME_TRY(contains, trie_->contains(key));
104  bool is_new_entry = not contains;
105  auto res = trie_->put(key, value);
106  if (res and changes_.has_value()) {
107  SL_TRACE_VOID_FUNC_CALL(logger_, key, value);
108 
109  changes_.value()->onPut(key, value, is_new_entry);
110  }
111  return res;
112  }
113 
114  outcome::result<void> PersistentTrieBatchImpl::put(const BufferView &key,
115  Buffer &&value) {
116  return put(key, value); // cannot take possession of value, check the
117  // const-ref version definition
118  }
119 
120  outcome::result<void> PersistentTrieBatchImpl::remove(const BufferView &key) {
121  OUTCOME_TRY(trie_->remove(key));
122  if (changes_.has_value()) {
124  changes_.value()->onRemove(key);
125  }
126  return outcome::success();
127  }
128 
129 } // namespace kagome::storage::trie
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< BufferConstRef > get(const BufferView &key) const override
Get value by key.
PersistentTrieBatchImpl(std::shared_ptr< Codec > codec, std::shared_ptr< TrieSerializer > serializer, std::optional< std::shared_ptr< changes_trie::ChangesTracker >> changes, std::shared_ptr< PolkadotTrie > trie)
#define SL_TRACE_FUNC_CALL(logger, ret,...)
Definition: logger.hpp:142
std::unique_ptr< TopperTrieBatch > batchOnTop() override
bool empty() const override
Returns true if the storage is empty.
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)
OUTCOME_CPP_DEFINE_CATEGORY(kagome::storage::trie, PersistentTrieBatchImpl::Error, e)
std::unique_ptr< PolkadotTrieCursor > trieCursor() override
outcome::result< std::tuple< bool, uint32_t > > clearPrefix(const BufferView &prefix, std::optional< uint64_t > limit=std::nullopt) override
outcome::result< void > put(const BufferView &key, const Buffer &value) override
Store value by key.
outcome::result< std::optional< BufferConstRef > > tryGet(const BufferView &key) const override
Get value by key.
outcome::result< void > remove(const BufferView &key) override
Remove value by key.
outcome::result< bool > contains(const BufferView &key) const override
Checks if given key-value binding exists in the storage.
#define SL_TRACE_VOID_FUNC_CALL(logger,...)
Definition: logger.hpp:146
std::optional< std::shared_ptr< changes_trie::ChangesTracker > > changes_