8 #include <boost/algorithm/string/predicate.hpp> 19 case E::PARENT_EXPIRED:
20 return "Pointer to the parent batch expired";
22 return "Unknown error";
28 const std::shared_ptr<TrieBatch> &parent)
33 OUTCOME_TRY(opt_value,
tryGet(key));
35 return opt_value.value();
40 outcome::result<std::optional<common::BufferConstRef>>
43 if (it->second.has_value()) {
44 return it->second.value();
51 if (
auto p =
parent_.lock(); p !=
nullptr) {
52 return p->tryGet(key);
58 if (
auto p =
parent_.lock(); p !=
nullptr) {
59 return p->trieCursor();
67 return it->second.has_value();
72 if (
auto p =
parent_.lock(); p !=
nullptr) {
73 return p->contains(key);
80 and std::any_of(
cache_.begin(),
cache_.end(), [](
auto &p) {
81 return p.second.has_value();
87 if (
auto p =
parent_.lock(); p !=
nullptr) {
101 return outcome::success();
107 return outcome::success();
111 const BufferView &prefix, std::optional<uint64_t>) {
112 for (
auto it =
cache_.lower_bound(prefix);
113 it !=
cache_.end() && boost::starts_with(it->first, prefix);
115 it->second = std::nullopt;
118 if (
parent_.lock() !=
nullptr) {
119 return outcome::success(std::make_tuple(
true, 0ULL));
127 OUTCOME_TRY(p->clearPrefix(prefix));
129 for (
auto it =
cache_.begin(); it !=
cache_.end(); it++) {
130 if (it->second.has_value()) {
131 OUTCOME_TRY(p->put(it->first, it->second.value()));
133 OUTCOME_TRY(p->remove(it->first));
136 return outcome::success();
143 if (boost::starts_with(key, prefix)) {
Class represents arbitrary (including empty) byte buffer.
outcome::result< common::BufferConstRef > get(const BufferView &key) const override
Get value by key.
outcome::result< std::optional< common::BufferConstRef > > tryGet(const BufferView &key) const override
Get value by key.
std::map< Buffer, std::optional< Buffer >, std::less<> > cache_
outcome::result< void > put(const BufferView &key, const Buffer &value) override
Store value by key.
bool empty() const override
Returns true if the storage is empty.
std::deque< Buffer > cleared_prefixes_
TopperTrieBatchImpl(const std::shared_ptr< TrieBatch > &parent)
common::SLBuffer< std::numeric_limits< size_t >::max()> Buffer
bool wasClearedByPrefix(const BufferView &key) const
outcome::result< void > remove(const BufferView &key) override
Remove value by key.
outcome::result< std::tuple< bool, uint32_t > > clearPrefix(const BufferView &prefix, std::optional< uint64_t > limit) override
std::weak_ptr< TrieBatch > parent_
OUTCOME_CPP_DEFINE_CATEGORY(kagome::storage::trie, TopperTrieBatchImpl::Error, e)
outcome::result< void > writeBack() override
std::unique_ptr< PolkadotTrieCursor > trieCursor() override
outcome::result< bool > contains(const BufferView &key) const override
Checks if given key-value binding exists in the storage.