Kagome
Polkadot Runtime Engine in C++17
polkadot_trie_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_STORAGE_TRIE_IMPL_POLKADOT_TRIE_IMPL
7 #define KAGOME_STORAGE_TRIE_IMPL_POLKADOT_TRIE_IMPL
8 
10 
11 #include "log/logger.hpp"
14 
15 namespace kagome::storage::trie {
16 
17  class OpaqueNodeStorage;
18 
19  class PolkadotTrieImpl final
20  : public PolkadotTrie,
21  public std::enable_shared_from_this<PolkadotTrieImpl> {
22  public:
23  enum class Error { INVALID_NODE_TYPE = 1 };
24 
33 
34  explicit PolkadotTrieImpl(NodePtr root,
38 
39  NodePtr getRoot() override;
40  ConstNodePtr getRoot() const override;
41 
42  outcome::result<NodePtr> getNode(ConstNodePtr parent,
43  const NibblesView &key_nibbles) override;
44  outcome::result<ConstNodePtr> getNode(
45  ConstNodePtr parent, const NibblesView &key_nibbles) const override;
46 
47  outcome::result<void> forNodeInPath(
48  ConstNodePtr parent,
49  const NibblesView &path,
50  const std::function<outcome::result<void>(
51  BranchNode const &, uint8_t idx)> &callback) const override;
52 
56  outcome::result<std::tuple<bool, uint32_t>> clearPrefix(
57  const common::BufferView &prefix,
58  std::optional<uint64_t> limit,
59  const OnDetachCallback &callback) override;
60 
61  // value will be copied
62  outcome::result<void> put(const common::BufferView &key,
63  const common::Buffer &value) override;
64 
65  outcome::result<void> put(const common::BufferView &key,
66  common::Buffer &&value) override;
67 
68  outcome::result<void> remove(const common::BufferView &key) override;
69 
70  outcome::result<common::BufferConstRef> get(
71  const common::BufferView &key) const override;
72 
73  outcome::result<std::optional<common::BufferConstRef>> tryGet(
74  const common::BufferView &key) const override;
75 
76  std::unique_ptr<PolkadotTrieCursor> trieCursor() override;
77 
78  outcome::result<bool> contains(
79  const common::BufferView &key) const override;
80 
81  bool empty() const override;
82 
83  outcome::result<ConstNodePtr> retrieveChild(const BranchNode &parent,
84  uint8_t idx) const override;
85  outcome::result<NodePtr> retrieveChild(const BranchNode &parent,
86  uint8_t idx) override;
87 
88  private:
89  outcome::result<NodePtr> insert(const NodePtr &parent,
90  const NibblesView &key_nibbles,
91  NodePtr node);
92 
93  outcome::result<NodePtr> updateBranch(BranchPtr parent,
94  const NibblesView &key_nibbles,
95  const NodePtr &node);
96 
97  std::unique_ptr<OpaqueNodeStorage> nodes_;
98 
100  };
101 
102 } // namespace kagome::storage::trie
103 
105 
106 #endif // KAGOME_STORAGE_TRIE_IMPL_POLKADOT_TRIE_IMPL
outcome::result< std::tuple< bool, uint32_t > > clearPrefix(const common::BufferView &prefix, std::optional< uint64_t > limit, const OnDetachCallback &callback) override
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
OUTCOME_HPP_DECLARE_ERROR(kagome::storage::trie, PolkadotTrieImpl::Error)
outcome::result< ConstNodePtr > retrieveChild(const BranchNode &parent, uint8_t idx) const override
outcome::result< void > forNodeInPath(ConstNodePtr parent, const NibblesView &path, const std::function< outcome::result< void >(BranchNode const &, uint8_t idx)> &callback) const override
outcome::result< NodePtr > updateBranch(BranchPtr parent, const NibblesView &key_nibbles, const NodePtr &node)
std::shared_ptr< TrieNode > NodePtr
outcome::result< NodePtr > insert(const NodePtr &parent, const NibblesView &key_nibbles, NodePtr node)
std::function< outcome::result< void >(const common::BufferView &key, std::optional< common::Buffer > &&value)> OnDetachCallback
PolkadotTrieImpl(PolkadotTrie::NodeRetrieveFunctor f=PolkadotTrie::defaultNodeRetrieveFunctor)
std::shared_ptr< BranchNode > BranchPtr
bool empty() const override
Returns true if the storage is empty.
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::function< outcome::result< NodePtr >(std::shared_ptr< OpaqueTrieNode > const &)> NodeRetrieveFunctor
std::shared_ptr< const TrieNode > ConstNodePtr
outcome::result< std::optional< common::BufferConstRef > > tryGet(const common::BufferView &key) const override
Get value by key.
outcome::result< bool > contains(const common::BufferView &key) const override
Checks if given key-value binding exists in the storage.
std::unique_ptr< PolkadotTrieCursor > trieCursor() override
std::unique_ptr< OpaqueNodeStorage > nodes_
outcome::result< NodePtr > getNode(ConstNodePtr parent, const NibblesView &key_nibbles) override
static outcome::result< NodePtr > defaultNodeRetrieveFunctor(const std::shared_ptr< OpaqueTrieNode > &node)
outcome::result< void > put(const common::BufferView &key, const common::Buffer &value) override
Store value by key.