Ethereum  PoC-8
The C++ Implementation of Ethereum
MemoryDB.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include "Common.h"
21 #include "Guards.h"
22 #include "db.h"
23 
24 namespace dev
25 {
26 namespace db
27 {
29 {
30 public:
31  void insert(Slice _key, Slice _value) override;
32  void kill(Slice _key) override;
33 
34  std::unordered_map<std::string, std::string>& writeBatch() { return m_batch; }
35  size_t size() { return m_batch.size(); }
36 
37 private:
38  std::unordered_map<std::string, std::string> m_batch;
39 };
40 
41 class MemoryDB : public DatabaseFace
42 {
43 public:
44  std::string lookup(Slice _key) const override;
45  bool exists(Slice _key) const override;
46  void insert(Slice _key, Slice _value) override;
47  void kill(Slice _key) override;
48 
49  std::unique_ptr<WriteBatchFace> createWriteBatch() const override;
50  void commit(std::unique_ptr<WriteBatchFace> _batch) override;
51 
52  // A database must implement the `forEach` method that allows the caller
53  // to pass in a function `f`, which will be called with the key and value
54  // of each record in the database. If `f` returns false, the `forEach`
55  // method must return immediately.
56  void forEach(std::function<bool(Slice, Slice)> _f) const override;
57 
58  size_t size() const { return m_db.size(); }
59 
60 private:
61  std::unordered_map<std::string, std::string> m_db;
62  mutable Mutex m_mutex;
63 };
64 } // namespace db
65 } // namespace dev
void insert(Slice _key, Slice _value) override
Definition: MemoryDB.cpp:50
void commit(std::unique_ptr< WriteBatchFace > _batch) override
Definition: MemoryDB.cpp:67
Definition: Address.cpp:20
void kill(Slice _key) override
Definition: MemoryDB.cpp:56
size_t size() const
Definition: MemoryDB.h:58
void forEach(std::function< bool(Slice, Slice)> _f) const override
Definition: MemoryDB.cpp:92
void kill(Slice _key) override
Definition: MemoryDB.cpp:30
std::unordered_map< std::string, std::string > & writeBatch()
Definition: MemoryDB.h:34
bool exists(Slice _key) const override
Definition: MemoryDB.cpp:44
std::string lookup(Slice _key) const override
Definition: MemoryDB.cpp:35
std::mutex Mutex
Definition: Guards.h:37
std::unique_ptr< WriteBatchFace > createWriteBatch() const override
Definition: MemoryDB.cpp:62
void insert(Slice _key, Slice _value) override
Definition: MemoryDB.cpp:25