Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
memory_store.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
9#include "hash_path.hpp"
10#include <map>
11#include <set>
12
14
16 public:
18
19 MemoryStore(MemoryStore const& rhs) = default;
20 MemoryStore(MemoryStore&& rhs) = default;
21 MemoryStore& operator=(MemoryStore const& rhs) = default;
23
24 bool put(std::vector<uint8_t> const& key, std::vector<uint8_t> const& value)
25 {
26 auto key_str = to_string(key);
27 return put(key_str, value);
28 }
29
30 bool put(std::string const& key, std::vector<uint8_t> const& value)
31 {
33 deletes_.erase(key);
34 return true;
35 }
36
37 bool del(std::vector<uint8_t> const& key)
38 {
39 auto key_str = to_string(key);
40 puts_.erase(key_str);
41 deletes_.insert(key_str);
42 return true;
43 };
44
45 bool get(std::vector<uint8_t> const& key, std::vector<uint8_t>& value) { return get(to_string(key), value); }
46
47 bool get(std::string const& key, std::vector<uint8_t>& value)
48 {
49 if (deletes_.find(key) != deletes_.end()) {
50 return false;
51 }
52 auto it = puts_.find(key);
53 if (it != puts_.end()) {
54 value = std::vector<uint8_t>(it->second.begin(), it->second.end());
55 return true;
56 } else {
57 std::string result;
58 auto it = store_.find(key);
59 if (it != store_.end()) {
60 value = { it->second.begin(), it->second.end() };
61 return true;
62 }
63 return false;
64 }
65 }
66
67 void commit()
68 {
69 for (auto it : puts_) {
70 store_.insert(it);
71 }
72 for (auto key : deletes_) {
73 store_.erase(key);
74 }
75 puts_.clear();
76 deletes_.clear();
77 }
78
79 void rollback()
80 {
81 puts_.clear();
82 deletes_.clear();
83 }
84
85 private:
86 std::string to_string(std::vector<uint8_t> const& input) { return std::string((char*)input.data(), input.size()); }
87
88 std::map<std::string, std::string> store_;
89 std::map<std::string, std::string> puts_;
90 std::set<std::string> deletes_;
91};
92
93} // namespace bb::crypto::merkle_tree
bool put(std::string const &key, std::vector< uint8_t > const &value)
MemoryStore & operator=(MemoryStore &&rhs)=default
bool get(std::string const &key, std::vector< uint8_t > &value)
MemoryStore(MemoryStore &&rhs)=default
std::string to_string(std::vector< uint8_t > const &input)
std::map< std::string, std::string > puts_
bool put(std::vector< uint8_t > const &key, std::vector< uint8_t > const &value)
bool get(std::vector< uint8_t > const &key, std::vector< uint8_t > &value)
std::map< std::string, std::string > store_
MemoryStore & operator=(MemoryStore const &rhs)=default
MemoryStore(MemoryStore const &rhs)=default
bool del(std::vector< uint8_t > const &key)