Kagome
Polkadot Runtime Engine in C++17
ephemeral_trie_batch_impl.cpp
Go to the documentation of this file.
1 
7 
9 
10 namespace kagome::storage::trie {
11 
13  std::shared_ptr<Codec> codec, std::shared_ptr<PolkadotTrie> trie)
14  : codec_{std::move(codec)}, trie_{std::move(trie)} {
15  BOOST_ASSERT(codec_ != nullptr);
16  BOOST_ASSERT(trie_ != nullptr);
17  }
18 
19  outcome::result<BufferConstRef> EphemeralTrieBatchImpl::get(
20  const BufferView &key) const {
21  return trie_->get(key);
22  }
23 
24  outcome::result<std::optional<BufferConstRef>> EphemeralTrieBatchImpl::tryGet(
25  const BufferView &key) const {
26  return trie_->tryGet(key);
27  }
28 
29  std::unique_ptr<PolkadotTrieCursor> EphemeralTrieBatchImpl::trieCursor() {
30  return std::make_unique<PolkadotTrieCursorImpl>(trie_);
31  }
32 
33  outcome::result<bool> EphemeralTrieBatchImpl::contains(
34  const BufferView &key) const {
35  return trie_->contains(key);
36  }
37 
39  return trie_->empty();
40  }
41 
42  outcome::result<std::tuple<bool, uint32_t>>
44  std::optional<uint64_t> limit) {
45  return trie_->clearPrefix(prefix, limit, [](const auto &, auto &&) {
46  return outcome::success();
47  });
48  }
49 
50  outcome::result<void> EphemeralTrieBatchImpl::put(const BufferView &key,
51  const Buffer &value) {
52  return trie_->put(key, value);
53  }
54 
55  outcome::result<void> EphemeralTrieBatchImpl::put(const BufferView &key,
56  Buffer &&value) {
57  return trie_->put(key, std::move(value));
58  }
59 
60  outcome::result<void> EphemeralTrieBatchImpl::remove(const BufferView &key) {
61  return trie_->remove(key);
62  }
63 
64  outcome::result<RootHash> EphemeralTrieBatchImpl::hash() {
65  static const auto empty_hash = codec_->hash256(common::Buffer{0});
66  if (auto root = trie_->getRoot()) {
67  OUTCOME_TRY(encoded, codec_->encodeNode(*root));
68  auto hash = codec_->hash256(encoded);
69  return hash;
70  }
71  return empty_hash;
72  }
73 
74 } // namespace kagome::storage::trie
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
std::unique_ptr< PolkadotTrieCursor > trieCursor() override
outcome::result< BufferConstRef > get(const BufferView &key) const override
Get value by key.
outcome::result< void > remove(const BufferView &key) override
Remove value by key.
bool empty() const override
Returns true if the storage is empty.
outcome::result< void > put(const BufferView &key, const Buffer &value) override
Store value by key.
outcome::result< std::tuple< bool, uint32_t > > clearPrefix(const BufferView &prefix, std::optional< uint64_t > limit=std::nullopt) override
outcome::result< std::optional< BufferConstRef > > tryGet(const BufferView &key) const override
Get value by key.
EphemeralTrieBatchImpl(std::shared_ptr< Codec > codec, std::shared_ptr< PolkadotTrie > trie)
outcome::result< bool > contains(const BufferView &key) const override
Checks if given key-value binding exists in the storage.