]> Git Repo - VerusCoin.git/blob - src/paymentdisclosuredb.cpp
Merge pull request #7 from jl777/beta
[VerusCoin.git] / src / paymentdisclosuredb.cpp
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.
4
5 #include "paymentdisclosuredb.h"
6
7 #include "util.h"
8 #include "leveldbwrapper.h"
9
10 #include <boost/filesystem.hpp>
11
12 using namespace std;
13
14 static boost::filesystem::path emptyPath;
15
16 /**
17  * Static method to return the shared/default payment disclosure database.
18  */
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>();
22     return ptr;
23 }
24
25 // C++11 delegated constructor
26 PaymentDisclosureDB::PaymentDisclosureDB() : PaymentDisclosureDB(emptyPath) {
27 }
28
29 PaymentDisclosureDB::PaymentDisclosureDB(const boost::filesystem::path& dbPath) {
30     boost::filesystem::path path(dbPath);
31     if (path.empty()) {
32         path = GetDataDir() / "paymentdisclosure";
33         LogPrintf("PaymentDisclosure: using default path for database: %s\n", path.string());
34     } else {
35         LogPrintf("PaymentDisclosure: using custom path for database: %s\n", path.string());
36     }
37
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");
43 }
44
45 PaymentDisclosureDB::~PaymentDisclosureDB() {
46     if (db != nullptr) {
47         delete db;
48     }
49 }
50
51 bool PaymentDisclosureDB::Put(const PaymentDisclosureKey& key, const PaymentDisclosureInfo& info)
52 {
53     if (db == nullptr) {
54         return false;
55     }
56
57     std::lock_guard<std::mutex> guard(lock_);
58
59     CDataStream ssValue(SER_DISK, CLIENT_VERSION);
60     ssValue.reserve(ssValue.GetSerializeSize(info));
61     ssValue << info;
62     leveldb::Slice slice(&ssValue[0], ssValue.size());
63
64     leveldb::Status status = db->Put(writeOptions, key.ToString(), slice);
65     HandleError(status);
66     return true;
67 }
68
69 bool PaymentDisclosureDB::Get(const PaymentDisclosureKey& key, PaymentDisclosureInfo& info)
70 {
71     if (db == nullptr) {
72         return false;
73     }
74
75     std::lock_guard<std::mutex> guard(lock_);
76
77     std::string strValue;
78     leveldb::Status status = db->Get(readOptions, key.ToString(), &strValue);
79     if (!status.ok()) {
80         if (status.IsNotFound())
81             return false;
82         LogPrintf("PaymentDisclosure: LevelDB read failure: %s\n", status.ToString());
83         HandleError(status);
84     }
85
86     try {
87         CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
88         ssValue >> info;
89     } catch (const std::exception&) {
90         return false;
91     }
92     return true;
93 }
This page took 0.029704 seconds and 4 git commands to generate.