1 // Copyright (c) 2017 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "paymentdisclosuredb.h"
8 #include "leveldbwrapper.h"
10 #include <boost/filesystem.hpp>
14 static boost::filesystem::path emptyPath;
17 * Static method to return the shared/default payment disclosure database.
19 shared_ptr<PaymentDisclosureDB> PaymentDisclosureDB::sharedInstance() {
20 // Thread-safe in C++11 and gcc 4.3
21 static shared_ptr<PaymentDisclosureDB> ptr = std::make_shared<PaymentDisclosureDB>();
25 // C++11 delegated constructor
26 PaymentDisclosureDB::PaymentDisclosureDB() : PaymentDisclosureDB(emptyPath) {
29 PaymentDisclosureDB::PaymentDisclosureDB(const boost::filesystem::path& dbPath) {
30 boost::filesystem::path path(dbPath);
32 path = GetDataDir() / "paymentdisclosure";
33 LogPrintf("PaymentDisclosure: using default path for database: %s\n", path.string());
35 LogPrintf("PaymentDisclosure: using custom path for database: %s\n", path.string());
38 TryCreateDirectory(path);
39 options.create_if_missing = true;
40 leveldb::Status status = leveldb::DB::Open(options, path.string(), &db);
41 HandleError(status); // throws exception
42 LogPrintf("PaymentDisclosure: Opened LevelDB successfully\n");
45 PaymentDisclosureDB::~PaymentDisclosureDB() {
51 bool PaymentDisclosureDB::Put(const PaymentDisclosureKey& key, const PaymentDisclosureInfo& info)
57 std::lock_guard<std::mutex> guard(lock_);
59 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
60 ssValue.reserve(ssValue.GetSerializeSize(info));
62 leveldb::Slice slice(&ssValue[0], ssValue.size());
64 leveldb::Status status = db->Put(writeOptions, key.ToString(), slice);
69 bool PaymentDisclosureDB::Get(const PaymentDisclosureKey& key, PaymentDisclosureInfo& info)
75 std::lock_guard<std::mutex> guard(lock_);
78 leveldb::Status status = db->Get(readOptions, key.ToString(), &strValue);
80 if (status.IsNotFound())
82 LogPrintf("PaymentDisclosure: LevelDB read failure: %s\n", status.ToString());
87 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
89 } catch (const std::exception&) {