Browse Source

dbwrapper: Pass parent CDBWrapper into CDBBatch and CDBIterator

Zcash: Makes future extensions easier.

Original description (when this was introduced upstream):

Pass parent wrapper directly instead of obfuscation key. This
makes it possible for other databases which re-use this code
to use other properties from the database.

Add a namespace dbwrapper_private for private functions to be used
only in dbwrapper.h/cpp and dbwrapper_tests.
pull/4/head
Wladimir J. van der Laan 8 years ago
committed by Jack Grigg
parent
commit
809a429ecf
No known key found for this signature in database GPG Key ID: 665DBCD284F7DAFF
  1. 23
      src/dbwrapper.h
  2. 2
      src/test/dbwrapper_tests.cpp
  3. 6
      src/txdb.cpp

23
src/dbwrapper.h

@ -24,15 +24,23 @@ public:
void HandleError(const leveldb::Status& status);
class CDBWrapper;
/** Batch of changes queued to be written to a CDBWrapper */
class CDBBatch
{
friend class CDBWrapper;
private:
const CDBWrapper &parent;
leveldb::WriteBatch batch;
public:
/**
* @param[in] parent CDBWrapper that this batch is to be submitted to
*/
CDBBatch(const CDBWrapper &parent) : parent(parent) { };
template <typename K, typename V>
void Write(const K& key, const V& value)
{
@ -64,15 +72,17 @@ public:
class CDBIterator
{
private:
const CDBWrapper &parent;
leveldb::Iterator *piter;
public:
/**
* @param[in] parent Parent CDBWrapper instance.
* @param[in] piterIn The original leveldb iterator.
*/
CDBIterator(leveldb::Iterator *piterIn) :
piter(piterIn) { };
CDBIterator(const CDBWrapper &parent, leveldb::Iterator *piterIn) :
parent(parent), piter(piterIn) { };
~CDBIterator();
bool Valid();
@ -183,7 +193,7 @@ public:
template <typename K, typename V>
bool Write(const K& key, const V& value, bool fSync = false)
{
CDBBatch batch;
CDBBatch batch(*this);
batch.Write(key, value);
return WriteBatch(batch, fSync);
}
@ -210,7 +220,7 @@ public:
template <typename K>
bool Erase(const K& key, bool fSync = false)
{
CDBBatch batch;
CDBBatch batch(*this);
batch.Erase(key);
return WriteBatch(batch, fSync);
}
@ -225,20 +235,19 @@ public:
bool Sync()
{
CDBBatch batch;
CDBBatch batch(*this);
return WriteBatch(batch, true);
}
CDBIterator *NewIterator()
{
return new CDBIterator(pdb->NewIterator(iteroptions));
return new CDBIterator(*this, pdb->NewIterator(iteroptions));
}
/**
* Return true if the database managed by this class contains no entries.
*/
bool IsEmpty();
};
#endif // BITCOIN_DBWRAPPER_H

2
src/test/dbwrapper_tests.cpp

@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
uint256 in3 = GetRandHash();
uint256 res;
CDBBatch batch;
CDBBatch batch(dbw);
batch.Write(key, in);
batch.Write(key2, in2);

6
src/txdb.cpp

@ -85,7 +85,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashAnchor,
CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers) {
CDBBatch batch;
CDBBatch batch(db);
size_t count = 0;
size_t changed = 0;
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
@ -205,7 +205,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
}
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
CDBBatch batch;
CDBBatch batch(*this);
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
batch.Write(make_pair(DB_BLOCK_FILES, it->first), *it->second);
}
@ -229,7 +229,7 @@ bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
}
bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
CDBBatch batch;
CDBBatch batch(*this);
for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
batch.Write(make_pair(DB_TXINDEX, it->first), it->second);
return WriteBatch(batch);

Loading…
Cancel
Save