6 #ifndef KAGOME_STORAGE_TRIE_POLKADOT_NODE 7 #define KAGOME_STORAGE_TRIE_POLKADOT_NODE 11 #include <fmt/format.h> 25 using Buffer::operator==;
26 using Buffer::operator<;
39 if (key.size() == 1 && key[0] == 0) {
43 auto l = key.size() * 2;
45 for (ssize_t i = 0; i < key.size(); i++) {
46 res[2 * i] = key[i] >> 4u;
47 res[2 * i + 1] = key[i] & 0xfu;
59 if (size() % 2 == 0) {
60 res =
Buffer(size() / 2, 0);
61 for (
size_t i = 0; i < size() / 2; i++) {
62 res[i] =
toByte((*
this)[i * 2], (*
this)[i * 2 + 1]);
65 res =
Buffer(size() / 2 + 1, 0);
67 for (
size_t i = 1; i < size() / 2 + 1; i++) {
68 res[i] =
toByte((*
this)[i * 2 - 1], (*
this)[i * 2]);
74 static uint8_t
toByte(uint8_t high, uint8_t low) {
75 return (high << 4u) | (low & 0xfu);
93 : key_nibbles{std::move(key_nibbles)}, value{std::move(value)} {}
102 LeafContainingHashes,
103 BranchContainingHashes,
105 ReservedForCompactEncoding
110 return static_cast<Type>(getType());
114 auto type = getTrieType();
115 return type == Type::BranchWithValue or type == Type::BranchEmptyValue
116 or type == Type::BranchContainingHashes;
120 std::optional<common::Buffer>
value;
124 static constexpr uint8_t kMaxChildren = 16;
128 std::optional<common::Buffer> value = std::nullopt)
129 :
TrieNode{std::move(key_nibbles), std::move(value)} {}
133 int getType()
const override;
135 uint16_t childrenBitmap()
const;
136 uint8_t childrenNum()
const;
141 std::array<std::shared_ptr<OpaqueTrieNode>, kMaxChildren>
children;
147 :
TrieNode{std::move(key_nibbles), std::move(value)} {}
151 int getType()
const override;
155 static constexpr uint8_t kMaxChildren = 16;
160 std::optional<common::Buffer> value = std::nullopt)
161 :
TrieNode{std::move(key_nibbles), std::move(value)} {}
165 int getType()
const override;
167 uint16_t childrenBitmap()
const;
168 uint8_t childrenNum()
const;
173 std::array<std::shared_ptr<OpaqueTrieNode>, kMaxChildren>
children;
179 std::optional<common::Buffer> value)
180 :
TrieNode{std::move(key_nibbles), std::move(value)} {}
184 int getType()
const override;
212 struct fmt::formatter<
kagome::storage::trie::KeyNibbles> {
213 template <
typename FormatContext>
215 -> decltype(ctx.out()) {
216 if (p.size() % 2 != 0) {
217 format_to(ctx.out(),
"{:x}", p[0]);
219 for (
size_t i = p.size() % 2; i < p.size() - 1; i += 2) {
220 format_to(ctx.out(),
"{:02x}", p.toByte(p[i], p[i + 1]));
225 #endif // KAGOME_STORAGE_TRIE_POLKADOT_NODE Class represents arbitrary (including empty) byte buffer.
std::array< std::shared_ptr< OpaqueTrieNode >, kMaxChildren > children
int getType() const override
BranchContainingHashesNode(KeyNibbles key_nibbles, std::optional< common::Buffer > value=std::nullopt)
common::BufferView BufferView
BranchNode(KeyNibbles key_nibbles, std::optional< common::Buffer > value=std::nullopt)
LeafNode(KeyNibbles key_nibbles, std::optional< common::Buffer > value)
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
DummyNode(common::Buffer key)
std::array< std::shared_ptr< OpaqueTrieNode >, kMaxChildren > children
auto toByteBuffer() const
TrieNode(KeyNibbles key_nibbles, std::optional< common::Buffer > value)
KeyNibbles(common::Buffer b)
NibblesView subspan(size_t offset=0, size_t length=-1) const
Type getTrieType() const noexcept
LeafContainingHashesNode(KeyNibbles key_nibbles, std::optional< common::Buffer > value)
std::optional< common::Buffer > value
bool isBranch() const noexcept
static KeyNibbles fromByteBuffer(const common::BufferView &key)
static uint8_t toByte(uint8_t high, uint8_t low)