Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/db.h
Show First 20 Lines • Show All 221 Lines • ▼ Show 20 Lines | |||||
/** RAII class that provides access to a Berkeley database */ | /** RAII class that provides access to a Berkeley database */ | ||||
class BerkeleyBatch { | class BerkeleyBatch { | ||||
/** RAII class that automatically cleanses its data on destruction */ | /** RAII class that automatically cleanses its data on destruction */ | ||||
class SafeDbt final { | class SafeDbt final { | ||||
Dbt m_dbt; | Dbt m_dbt; | ||||
public: | public: | ||||
// construct Dbt with data or flags | // construct Dbt with internally-managed data | ||||
SafeDbt(u_int32_t flags = 0); | SafeDbt(); | ||||
// construct Dbt with provided data | |||||
SafeDbt(void *data, size_t size); | SafeDbt(void *data, size_t size); | ||||
~SafeDbt(); | ~SafeDbt(); | ||||
// delegate to Dbt | // delegate to Dbt | ||||
const void *get_data() const; | const void *get_data() const; | ||||
u_int32_t get_size() const; | u_int32_t get_size() const; | ||||
// conversion operator to access the underlying Dbt | // conversion operator to access the underlying Dbt | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | template <typename K, typename T> bool Read(const K &key, T &value) { | ||||
// Key | // Key | ||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION); | CDataStream ssKey(SER_DISK, CLIENT_VERSION); | ||||
ssKey.reserve(1000); | ssKey.reserve(1000); | ||||
ssKey << key; | ssKey << key; | ||||
SafeDbt datKey(ssKey.data(), ssKey.size()); | SafeDbt datKey(ssKey.data(), ssKey.size()); | ||||
// Read | // Read | ||||
SafeDbt datValue(DB_DBT_MALLOC); | SafeDbt datValue; | ||||
int ret = pdb->get(activeTxn, datKey, datValue, 0); | int ret = pdb->get(activeTxn, datKey, datValue, 0); | ||||
bool success = false; | bool success = false; | ||||
if (datValue.get_data() != nullptr) { | if (datValue.get_data() != nullptr) { | ||||
// Unserialize value | // Unserialize value | ||||
try { | try { | ||||
CDataStream ssValue((char *)datValue.get_data(), | CDataStream ssValue((char *)datValue.get_data(), | ||||
(char *)datValue.get_data() + | (char *)datValue.get_data() + | ||||
datValue.get_size(), | datValue.get_size(), | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | Dbc *GetCursor() { | ||||
if (ret != 0) { | if (ret != 0) { | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
return pcursor; | return pcursor; | ||||
} | } | ||||
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue) { | int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue) { | ||||
// Read at cursor | // Read at cursor | ||||
SafeDbt datKey(DB_DBT_MALLOC); | SafeDbt datKey; | ||||
SafeDbt datValue(DB_DBT_MALLOC); | SafeDbt datValue; | ||||
int ret = pcursor->get(datKey, datValue, DB_NEXT); | int ret = pcursor->get(datKey, datValue, DB_NEXT); | ||||
if (ret != 0) { | if (ret != 0) { | ||||
return ret; | return ret; | ||||
} else if (datKey.get_data() == nullptr || | } else if (datKey.get_data() == nullptr || | ||||
datValue.get_data() == nullptr) { | datValue.get_data() == nullptr) { | ||||
return 99999; | return 99999; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |