Kagome
Polkadot Runtime Engine in C++17
in_memory_storage.cpp
Go to the documentation of this file.
1 
7 
10 
12 
13 namespace kagome::storage {
14 
15  outcome::result<common::Buffer> InMemoryStorage::load(
16  const BufferView &key) const {
17  if (storage.find(key.toHex()) != storage.end()) {
18  return storage.at(key.toHex());
19  }
20 
22  }
23 
24  outcome::result<std::optional<Buffer>> InMemoryStorage::tryLoad(
25  const common::BufferView &key) const {
26  if (storage.find(key.toHex()) != storage.end()) {
27  return storage.at(key.toHex());
28  }
29 
30  return std::nullopt;
31  }
32 
33  outcome::result<void> InMemoryStorage::put(const BufferView &key,
34  const Buffer &value) {
35  auto it = storage.find(key.toHex());
36  if (it != storage.end()) {
37  size_t old_value_size = it->second.size();
38  BOOST_ASSERT(size_ >= old_value_size);
39  size_ -= old_value_size;
40  }
41  size_ += value.size();
42  storage[key.toHex()] = value;
43  return outcome::success();
44  }
45 
46  outcome::result<void> InMemoryStorage::put(const BufferView &key,
47  Buffer &&value) {
48  auto it = storage.find(key.toHex());
49  if (it != storage.end()) {
50  size_t old_value_size = it->second.size();
51  BOOST_ASSERT(size_ >= old_value_size);
52  size_ -= old_value_size;
53  }
54  size_ += value.size();
55  storage[key.toHex()] = std::move(value);
56  return outcome::success();
57  }
58 
59  outcome::result<bool> InMemoryStorage::contains(const BufferView &key) const {
60  return storage.find(key.toHex()) != storage.end();
61  }
62 
63  bool InMemoryStorage::empty() const {
64  return storage.empty();
65  }
66 
67  outcome::result<void> InMemoryStorage::remove(const BufferView &key) {
68  auto it = storage.find(key.toHex());
69  if (it != storage.end()) {
70  size_ -= it->second.size();
71  storage.erase(it);
72  }
73  return outcome::success();
74  }
75 
76  std::unique_ptr<kagome::storage::face::WriteBatch<BufferView, Buffer>>
78  return std::make_unique<InMemoryBatch>(*this);
79  }
80 
81  std::unique_ptr<InMemoryStorage::Cursor> InMemoryStorage::cursor() {
82  return nullptr;
83  }
84 
85  size_t InMemoryStorage::size() const {
86  return size_;
87  }
88 } // namespace kagome::storage
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< common::Buffer > load(const common::BufferView &key) const override
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
std::unique_ptr< storage::BufferStorage::Cursor > cursor() override
Returns new key-value iterator.
outcome::result< bool > contains(const common::BufferView &key) const override
std::map< std::string, common::Buffer > storage
outcome::result< std::optional< common::Buffer > > tryLoad(const common::BufferView &key) const override
outcome::result< void > remove(const common::BufferView &key) override
bool empty() const override
Returns true if the storage is empty.
outcome::result< void > put(const common::BufferView &key, const common::Buffer &value) override
std::unique_ptr< kagome::storage::face::WriteBatch< common::BufferView, common::Buffer > > batch() override
Creates new Write Batch - an object, which can be used to efficiently write bulk data.
std::string toHex() const
Definition: buffer_view.hpp:30