barretenberg
Loading...
Searching...
No Matches
memory_store.hpp
1#pragma once
2#include "barretenberg/common/streams.hpp"
3#include "hash_path.hpp"
4#include <map>
5#include <set>
6
7namespace proof_system::plonk {
8namespace stdlib {
9namespace merkle_tree {
10
12 public:
13 MemoryStore() {}
14
15 MemoryStore(MemoryStore const& rhs) = default;
16 MemoryStore(MemoryStore&& rhs) = default;
17 MemoryStore& operator=(MemoryStore const& rhs) = default;
18 MemoryStore& operator=(MemoryStore&& rhs) = default;
19
20 bool put(std::vector<uint8_t> const& key, std::vector<uint8_t> const& value)
21 {
22 auto key_str = to_string(key);
23 return put(key_str, value);
24 }
25
26 bool put(std::string const& key, std::vector<uint8_t> const& value)
27 {
28 puts_[key] = to_string(value);
29 deletes_.erase(key);
30 return true;
31 }
32
33 bool del(std::vector<uint8_t> const& key)
34 {
35 auto key_str = to_string(key);
36 puts_.erase(key_str);
37 deletes_.insert(key_str);
38 return true;
39 };
40
41 bool get(std::vector<uint8_t> const& key, std::vector<uint8_t>& value) { return get(to_string(key), value); }
42
43 bool get(std::string const& key, std::vector<uint8_t>& value)
44 {
45 if (deletes_.find(key) != deletes_.end()) {
46 return false;
47 }
48 auto it = puts_.find(key);
49 if (it != puts_.end()) {
50 value = std::vector<uint8_t>(it->second.begin(), it->second.end());
51 return true;
52 } else {
53 std::string result;
54 auto it = store_.find(key);
55 if (it != store_.end()) {
56 value = { it->second.begin(), it->second.end() };
57 return true;
58 }
59 return false;
60 }
61 }
62
63 void commit()
64 {
65 for (auto it : puts_) {
66 store_.insert(it);
67 }
68 for (auto key : deletes_) {
69 store_.erase(key);
70 }
71 puts_.clear();
72 deletes_.clear();
73 }
74
75 void rollback()
76 {
77 puts_.clear();
78 deletes_.clear();
79 }
80
81 private:
82 std::string to_string(std::vector<uint8_t> const& input) { return std::string((char*)input.data(), input.size()); }
83
84 std::map<std::string, std::string> store_;
85 std::map<std::string, std::string> puts_;
86 std::set<std::string> deletes_;
87};
88
89} // namespace merkle_tree
90} // namespace stdlib
91} // namespace proof_system::plonk
Definition: widget.bench.cpp:13