Ethereum  PoC-8
The C++ Implementation of Ethereum
DBFactory.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 "DBFactory.h"
19 #include "FileSystem.h"
20 #include "LevelDB.h"
21 #include "RocksDB.h"
22 #include "MemoryDB.h"
23 #include "libethcore/Exceptions.h"
24 
25 namespace dev
26 {
27 namespace db
28 {
29 namespace fs = boost::filesystem;
30 namespace po = boost::program_options;
31 
33 fs::path g_dbPath;
34 
40 {
42  char const* name;
43 };
44 
50  {DatabaseKind::LevelDB, "leveldb"},
51  {DatabaseKind::RocksDB, "rocksdb"},
52  {DatabaseKind::MemoryDB, "memorydb"},
53 };
54 
55 void setDatabaseKindByName(std::string const& _name)
56 {
57  for (auto& entry : dbKindsTable)
58  {
59  if (_name == entry.name)
60  {
61  g_kind = entry.kind;
62  return;
63  }
64  }
65 
66  BOOST_THROW_EXCEPTION(
67  eth::InvalidDatabaseKind() << errinfo_comment("invalid database name supplied: " + _name));
68 }
69 
71 {
72  g_kind = _kind;
73 }
74 
75 void setDatabasePath(std::string const& _path)
76 {
77  g_dbPath = fs::path(_path);
78 }
79 
81 {
82  switch (g_kind)
83  {
86  return true;
87  default:
88  return false;
89  }
90 }
91 
93 {
94  return g_kind;
95 }
96 
97 fs::path databasePath()
98 {
99  return g_dbPath.empty() ? getDataDir() : g_dbPath;
100 }
101 
102 po::options_description databaseProgramOptions(unsigned _lineLength)
103 {
104  // It must be a static object because boost expects const char*.
105  static std::string const description = [] {
106  std::string names;
107  for (auto const& entry : dbKindsTable)
108  {
109  if (!names.empty())
110  names += ", ";
111  names += entry.name;
112  }
113 
114  return "Select database implementation. Available options are: " + names + ".";
115  }();
116 
117  po::options_description opts("DATABASE OPTIONS", _lineLength);
118  auto add = opts.add_options();
119 
120  add("db",
121  po::value<std::string>()->value_name("<name>")->default_value("leveldb")->notifier(
123  description.data());
124 
125  add("db-path",
126  po::value<std::string>()
127  ->value_name("<path>")
128  ->default_value(getDataDir().string())
129  ->notifier(setDatabasePath),
130  "Database path (for non-memory database options)\n");
131 
132  return opts;
133 }
134 
135 std::unique_ptr<DatabaseFace> DBFactory::create()
136 {
137  return create(databasePath());
138 }
139 
140 std::unique_ptr<DatabaseFace> DBFactory::create(fs::path const& _path)
141 {
142  return create(g_kind, _path);
143 }
144 
145 std::unique_ptr<DatabaseFace> DBFactory::create(DatabaseKind _kind)
146 {
147  return create(_kind, databasePath());
148 }
149 
150 std::unique_ptr<DatabaseFace> DBFactory::create(DatabaseKind _kind, fs::path const& _path)
151 {
152  switch (_kind)
153  {
155  return std::unique_ptr<DatabaseFace>(new LevelDB(_path));
156  break;
158  return std::unique_ptr<DatabaseFace>(new RocksDB(_path));
159  break;
161  // Silently ignore path since the concept of a db path doesn't make sense
162  // when using an in-memory database
163  return std::unique_ptr<DatabaseFace>(new MemoryDB());
164  break;
165  default:
166  assert(false);
167  return {};
168  }
169 }
170 
171 
172 } // namespace db
173 } // namespace dev
dev::db::DBKindTableEntry
Definition: DBFactory.cpp:40
dev::db::DatabaseKind
DatabaseKind
Definition: DBFactory.h:31
dev::db::DBKindTableEntry::kind
DatabaseKind kind
Definition: DBFactory.cpp:41
dev::db::databasePath
fs::path databasePath()
Definition: DBFactory.cpp:97
dev::db::isDiskDatabase
bool isDiskDatabase()
Definition: DBFactory.cpp:80
dev::db::setDatabasePath
void setDatabasePath(std::string const &_path)
Definition: DBFactory.cpp:75
dev::db::DatabaseKind::MemoryDB
@ MemoryDB
dev::db::databaseKind
DatabaseKind databaseKind()
Definition: DBFactory.cpp:92
dev::db::RocksDB
Definition: RocksDB.h:31
dev::db::databaseProgramOptions
po::options_description databaseProgramOptions(unsigned _lineLength)
Definition: DBFactory.cpp:102
dev::db::MemoryDB
Definition: MemoryDB.h:42
Exceptions.h
dev::db::DatabaseKind::RocksDB
@ RocksDB
LevelDB.h
dev::db::DatabaseKind::LevelDB
@ LevelDB
dev::db::DBKindTableEntry::name
char const * name
Definition: DBFactory.cpp:42
dev::db::g_dbPath
fs::path g_dbPath
Definition: DBFactory.cpp:33
dev::getDataDir
boost::filesystem::path getDataDir(std::string _prefix="ethereum")
DBFactory.h
FileSystem.h
dev::db::dbKindsTable
DBKindTableEntry dbKindsTable[]
Definition: DBFactory.cpp:49
dev::db::DBFactory::create
static std::unique_ptr< DatabaseFace > create()
Definition: DBFactory.cpp:135
MemoryDB.h
dev::db::setDatabaseKind
void setDatabaseKind(DatabaseKind _kind)
Definition: DBFactory.cpp:70
dev::db::LevelDB
Definition: LevelDB.h:31
dev
Definition: Address.cpp:21
dev::db::setDatabaseKindByName
void setDatabaseKindByName(std::string const &_name)
Definition: DBFactory.cpp:55
RocksDB.h
dev::db::g_kind
auto g_kind
Definition: DBFactory.cpp:32
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69