Skip to content

Commit a389ed5

Browse files
committed
walletdb: refactor Read, Write, Erase, and Exists into non-template func
In order to override these later, the specific details of how the Read, Write, Erase, and Exists functions interact with the actual database file need to go into functions that are not templated.
1 parent 39bd9dd commit a389ed5

File tree

2 files changed

+77
-33
lines changed

2 files changed

+77
-33
lines changed

src/wallet/bdb.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,67 @@ std::string BerkeleyDatabaseVersion()
803803
{
804804
return DbEnv::version(nullptr, nullptr, nullptr);
805805
}
806+
807+
bool BerkeleyBatch::ReadKey(CDataStream& key, CDataStream& value)
808+
{
809+
if (!pdb)
810+
return false;
811+
812+
// Key
813+
SafeDbt datKey(key.data(), key.size());
814+
815+
// Read
816+
SafeDbt datValue;
817+
int ret = pdb->get(activeTxn, datKey, datValue, 0);
818+
if (ret == 0 && datValue.get_data() != nullptr) {
819+
value.write((char*)datValue.get_data(), datValue.get_size());
820+
return true;
821+
}
822+
return false;
823+
}
824+
825+
bool BerkeleyBatch::WriteKey(CDataStream& key, CDataStream& value, bool overwrite)
826+
{
827+
if (!pdb)
828+
return true;
829+
if (fReadOnly)
830+
assert(!"Write called on database in read-only mode");
831+
832+
// Key
833+
SafeDbt datKey(key.data(), key.size());
834+
835+
// Value
836+
SafeDbt datValue(value.data(), value.size());
837+
838+
// Write
839+
int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE));
840+
return (ret == 0);
841+
}
842+
843+
bool BerkeleyBatch::EraseKey(CDataStream& key)
844+
{
845+
if (!pdb)
846+
return false;
847+
if (fReadOnly)
848+
assert(!"Erase called on database in read-only mode");
849+
850+
// Key
851+
SafeDbt datKey(key.data(), key.size());
852+
853+
// Erase
854+
int ret = pdb->del(activeTxn, datKey, 0);
855+
return (ret == 0 || ret == DB_NOTFOUND);
856+
}
857+
858+
bool BerkeleyBatch::HasKey(CDataStream& key)
859+
{
860+
if (!pdb)
861+
return false;
862+
863+
// Key
864+
SafeDbt datKey(key.data(), key.size());
865+
866+
// Exists
867+
int ret = pdb->exists(activeTxn, datKey, 0);
868+
return ret == 0;
869+
}

src/wallet/bdb.h

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ class BerkeleyBatch
207207
operator Dbt*();
208208
};
209209

210+
private:
211+
bool ReadKey(CDataStream& key, CDataStream& value);
212+
bool WriteKey(CDataStream& key, CDataStream& value, bool overwrite=true);
213+
bool EraseKey(CDataStream& key);
214+
bool HasKey(CDataStream& key);
215+
210216
protected:
211217
Db* pdb;
212218
std::string strFile;
@@ -236,91 +242,65 @@ class BerkeleyBatch
236242
template <typename K, typename T>
237243
bool Read(const K& key, T& value)
238244
{
239-
if (!pdb)
240-
return false;
241-
242245
// Key
243246
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
244247
ssKey.reserve(1000);
245248
ssKey << key;
246-
SafeDbt datKey(ssKey.data(), ssKey.size());
247249

248-
// Read
249-
SafeDbt datValue;
250-
int ret = pdb->get(activeTxn, datKey, datValue, 0);
250+
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
251251
bool success = false;
252-
if (datValue.get_data() != nullptr) {
252+
bool ret = ReadKey(ssKey, ssValue);
253+
if (ret) {
253254
// Unserialize value
254255
try {
255-
CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
256256
ssValue >> value;
257257
success = true;
258258
} catch (const std::exception&) {
259259
// In this case success remains 'false'
260260
}
261261
}
262-
return ret == 0 && success;
262+
return ret && success;
263263
}
264264

265265
template <typename K, typename T>
266266
bool Write(const K& key, const T& value, bool fOverwrite = true)
267267
{
268-
if (!pdb)
269-
return true;
270-
if (fReadOnly)
271-
assert(!"Write called on database in read-only mode");
272-
273268
// Key
274269
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
275270
ssKey.reserve(1000);
276271
ssKey << key;
277-
SafeDbt datKey(ssKey.data(), ssKey.size());
278272

279273
// Value
280274
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
281275
ssValue.reserve(10000);
282276
ssValue << value;
283-
SafeDbt datValue(ssValue.data(), ssValue.size());
284277

285278
// Write
286-
int ret = pdb->put(activeTxn, datKey, datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
287-
return (ret == 0);
279+
return WriteKey(ssKey, ssValue, fOverwrite);
288280
}
289281

290282
template <typename K>
291283
bool Erase(const K& key)
292284
{
293-
if (!pdb)
294-
return false;
295-
if (fReadOnly)
296-
assert(!"Erase called on database in read-only mode");
297-
298285
// Key
299286
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
300287
ssKey.reserve(1000);
301288
ssKey << key;
302-
SafeDbt datKey(ssKey.data(), ssKey.size());
303289

304290
// Erase
305-
int ret = pdb->del(activeTxn, datKey, 0);
306-
return (ret == 0 || ret == DB_NOTFOUND);
291+
return EraseKey(ssKey);
307292
}
308293

309294
template <typename K>
310295
bool Exists(const K& key)
311296
{
312-
if (!pdb)
313-
return false;
314-
315297
// Key
316298
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
317299
ssKey.reserve(1000);
318300
ssKey << key;
319-
SafeDbt datKey(ssKey.data(), ssKey.size());
320301

321302
// Exists
322-
int ret = pdb->exists(activeTxn, datKey, 0);
323-
return (ret == 0);
303+
return HasKey(ssKey);
324304
}
325305

326306
Dbc* GetCursor();

0 commit comments

Comments
 (0)