Fabcoin Core  0.16.2
P2P Digital Currency
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 */
22 #pragma once
23 
24 #include <unordered_map>
25 #include "Common.h"
26 #include "Guards.h"
27 #include "FixedHash.h"
28 #include "Log.h"
29 #include "RLP.h"
30 #include "SHA3.h"
31 
32 namespace dev
33 {
34 
35 struct DBChannel: public LogChannel { static const char* name(); static const int verbosity = 18; };
36 struct DBWarn: public LogChannel { static const char* name(); static const int verbosity = 1; };
37 
38 #define dbdebug clog(DBChannel)
39 #define dbwarn clog(DBWarn)
40 
41 class MemoryDB
42 {
43  friend class EnforceRefs;
44 
45 public:
46  MemoryDB() {}
47  MemoryDB(MemoryDB const& _c) { operator=(_c); }
48 
49  MemoryDB& operator=(MemoryDB const& _c);
50 
51  void clear() { m_main.clear(); m_aux.clear(); } // WARNING !!!! didn't originally clear m_refCount!!!
52  std::unordered_map<h256, std::string> get() const;
53 
54  std::string lookup(h256 const& _h) const;
55  bool exists(h256 const& _h) const;
56  void insert(h256 const& _h, bytesConstRef _v);
57  bool kill(h256 const& _h);
58  void purge();
59 
60  bytes lookupAux(h256 const& _h) const;
61  void removeAux(h256 const& _h);
62  void insertAux(h256 const& _h, bytesConstRef _v);
63 
64  h256Hash keys() const;
65 
66 protected:
67 #if DEV_GUARDED_DB
68  mutable SharedMutex x_this;
69 #endif
70  std::unordered_map<h256, std::pair<std::string, unsigned>> m_main;
71  std::unordered_map<h256, std::pair<bytes, bool>> m_aux;
72 
73  mutable bool m_enforceRefs = false;
74 };
75 
77 {
78 public:
79  EnforceRefs(MemoryDB const& _o, bool _r): m_o(_o), m_r(_o.m_enforceRefs) { _o.m_enforceRefs = _r; }
80  ~EnforceRefs() { m_o.m_enforceRefs = m_r; }
81 
82 private:
83  MemoryDB const& m_o;
84  bool m_r;
85 };
86 
87 inline std::ostream& operator<<(std::ostream& _out, MemoryDB const& _m)
88 {
89  for (auto const& i: _m.get())
90  {
91  _out << i.first << ": ";
92  _out << RLP(i.second);
93  _out << " " << toHex(i.second);
94  _out << std::endl;
95  }
96  return _out;
97 }
98 
99 }
std::unordered_map< h256, std::string > get() const
Definition: MemoryDB.cpp:33
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
std::unordered_map< h256, std::pair< std::string, unsigned > > m_main
Definition: MemoryDB.h:70
EnforceRefs(MemoryDB const &_o, bool _r)
Definition: MemoryDB.h:79
bool m_enforceRefs
Definition: MemoryDB.h:73
MemoryDB const & m_o
Definition: MemoryDB.h:83
static const int verbosity
Definition: MemoryDB.h:35
std::vector< byte > bytes
Definition: Common.h:75
void clear()
Definition: MemoryDB.h:51
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:80
MemoryDB(MemoryDB const &_c)
Definition: MemoryDB.h:47
boost::shared_mutex SharedMutex
Definition: Guards.h:39
The default logging channels.
Definition: Log.h:130
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:349
static const char * name()
Definition: MemoryDB.cpp:30
std::unordered_map< h256, std::pair< bytes, bool > > m_aux
Definition: MemoryDB.h:71
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64