Kagome
Polkadot Runtime Engine in C++17
in_memory_batch.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_IN_MEMORY_BATCH_HPP
7 #define KAGOME_IN_MEMORY_BATCH_HPP
8 
9 #include "common/buffer.hpp"
11 
12 namespace kagome::storage {
14 
16  : public kagome::storage::face::WriteBatch<BufferView, Buffer> {
17  public:
18  explicit InMemoryBatch(InMemoryStorage &db) : db{db} {}
19 
20  outcome::result<void> put(const BufferView &key,
21  const Buffer &value) override {
22  entries[key.toHex()] = value;
23  return outcome::success();
24  }
25 
26  outcome::result<void> put(const BufferView &key, Buffer &&value) override {
27  entries[key.toHex()] = std::move(value);
28  return outcome::success();
29  }
30 
31  outcome::result<void> remove(const BufferView &key) override {
32  entries.erase(key.toHex());
33  return outcome::success();
34  }
35 
36  outcome::result<void> commit() override {
37  for (auto &entry : entries) {
38  OUTCOME_TRY(db.put(Buffer::fromHex(entry.first).value(), entry.second));
39  }
40  return outcome::success();
41  }
42 
43  void clear() override {
44  entries.clear();
45  }
46 
47  private:
48  std::map<std::string, Buffer> entries;
50  };
51 } // namespace kagome::storage
52 
53 #endif // KAGOME_IN_MEMORY_BATCH_HPP
void clear() override
Clear batch.
static outcome::result< SLBuffer > fromHex(std::string_view hex)
Construct SLBuffer from hex string.
Definition: buffer.hpp:177
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
An abstraction over a storage, which can be used for batch writes.
Definition: write_batch.hpp:19
InMemoryBatch(InMemoryStorage &db)
outcome::result< void > put(const BufferView &key, Buffer &&value) override
std::map< std::string, Buffer > entries
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
outcome::result< void > put(const BufferView &key, const Buffer &value) override
Store value by key.
outcome::result< void > commit() override
Writes batch.
outcome::result< void > put(const common::BufferView &key, const common::Buffer &value) override
std::string toHex() const
Definition: buffer_view.hpp:30