Fabcoin Core  0.16.2
P2P Digital Currency
LevelDB.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 */
24 #include "LevelDB.h"
25 #include <leveldb/db.h>
26 #include <boost/filesystem.hpp>
27 #include <libdevcore/FileSystem.h>
28 #include <libdevcore/SHA3.h>
29 
30 using namespace std;
31 using namespace dev;
32 using namespace dev::rpc;
33 namespace fs = boost::filesystem;
34 namespace ldb = leveldb;
35 
36 LevelDB::LevelDB()
37 {
38  auto path = getDataDir() + "/.web3";
39  fs::create_directories(path);
40  DEV_IGNORE_EXCEPTIONS(fs::permissions(path, fs::owner_all));
41  ldb::Options o;
42  o.create_if_missing = true;
43  ldb::DB::Open(o, path, &m_db);
44 }
45 
46 bool LevelDB::db_put(std::string const& _name, std::string const& _key, std::string const& _value)
47 {
48  bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
49  auto status = m_db->Put(ldb::WriteOptions(),
50  ldb::Slice((char const*)k.data(), k.size()),
51  ldb::Slice((char const*)_value.data(), _value.size()));
52  return status.ok();
53 }
54 
55 std::string LevelDB::db_get(std::string const& _name, std::string const& _key)
56 {
57  bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
58  string ret;
59  m_db->Get(ldb::ReadOptions(), ldb::Slice((char const*)k.data(), k.size()), &ret);
60  return ret;
61 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::hash for asio::adress
Definition: Common.h:323
std::string getDataDir(std::string _prefix="ethereum")
std::vector< byte > bytes
Definition: Common.h:75
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63