Ethereum  PoC-8
The C++ Implementation of Ethereum
MemoryDB.cpp
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 #include "MemoryDB.h"
19 
20 namespace dev
21 {
22 namespace db
23 {
24 
26 {
27  m_batch[_key.toString()] = _value.toString();
28 }
29 
31 {
32  m_batch.erase(_key.toString());
33 }
34 
35 std::string MemoryDB::lookup(Slice _key) const
36 {
37  Guard lock(m_mutex);
38  auto const& it = m_db.find(_key.toString());
39  if (it != m_db.end())
40  return it->second;
41  return {};
42 }
43 
44 bool MemoryDB::exists(Slice _key) const
45 {
46  Guard lock(m_mutex);
47  return m_db.count(_key.toString()) != 0;
48 }
49 
50 void MemoryDB::insert(Slice _key, Slice _value)
51 {
52  Guard lock(m_mutex);
53  m_db[_key.toString()] = _value.toString();
54 }
55 
57 {
58  Guard lock(m_mutex);
59  m_db.erase(_key.toString());
60 }
61 
62 std::unique_ptr<WriteBatchFace> MemoryDB::createWriteBatch() const
63 {
64  return std::unique_ptr<WriteBatchFace>(new MemoryDBWriteBatch);
65 }
66 
67 void MemoryDB::commit(std::unique_ptr<WriteBatchFace> _batch)
68 {
69  if (!_batch)
70  {
71  BOOST_THROW_EXCEPTION(DatabaseError() << errinfo_comment("Cannot commit null batch"));
72  }
73 
74  auto* batchPtr = dynamic_cast<MemoryDBWriteBatch*>(_batch.get());
75  if (!batchPtr)
76  {
77  BOOST_THROW_EXCEPTION(
78  DatabaseError() << errinfo_comment("Invalid batch type passed to MemoryDB::commit"));
79  }
80  auto const& batch = batchPtr->writeBatch();
81  Guard lock(m_mutex);
82  for (auto& e : batch)
83  {
84  m_db[e.first] = e.second;
85  }
86 }
87 
88 // A database must implement the `forEach` method that allows the caller
89 // to pass in a function `f`, which will be called with the key and value
90 // of each record in the database. If `f` returns false, the `forEach`
91 // method must return immediately.
92 void MemoryDB::forEach(std::function<bool(Slice, Slice)> _f) const
93 {
94  Guard lock(m_mutex);
95  for (auto const& e : m_db)
96  {
97  if (!_f(Slice(e.first), Slice(e.second)))
98  {
99  return;
100  }
101  }
102 }
103 
104 } // namespace db
105 } // namespace dev
dev::vector_ref
Definition: vector_ref.h:22
dev::db::MemoryDB::exists
bool exists(Slice _key) const override
Definition: MemoryDB.cpp:44
dev::db::MemoryDB::commit
void commit(std::unique_ptr< WriteBatchFace > _batch) override
Definition: MemoryDB.cpp:67
dev::db::MemoryDBWriteBatch::kill
void kill(Slice _key) override
Definition: MemoryDB.cpp:30
dev::Guard
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
dev::db::MemoryDB::lookup
std::string lookup(Slice _key) const override
Definition: MemoryDB.cpp:35
dev::db::MemoryDB::insert
void insert(Slice _key, Slice _value) override
Definition: MemoryDB.cpp:50
dev::db::MemoryDBWriteBatch::insert
void insert(Slice _key, Slice _value) override
Definition: MemoryDB.cpp:25
dev::vector_ref::toString
std::string toString() const
Definition: vector_ref.h:44
dev::db::MemoryDB::kill
void kill(Slice _key) override
Definition: MemoryDB.cpp:56
MemoryDB.h
dev::db::MemoryDB::createWriteBatch
std::unique_ptr< WriteBatchFace > createWriteBatch() const override
Definition: MemoryDB.cpp:62
dev
Definition: Address.cpp:21
dev::db::MemoryDBWriteBatch
Definition: MemoryDB.h:29
dev::db::MemoryDB::forEach
void forEach(std::function< bool(Slice, Slice)> _f) const override
Definition: MemoryDB.cpp:92
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69
dev::db::Slice
vector_ref< char const > Slice
Definition: dbfwd.h:26