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.
This commit is contained in:
parent
e3da7a572d
commit
809a429ecf
|
@ -24,15 +24,23 @@ public:
|
||||||
|
|
||||||
void HandleError(const leveldb::Status& status);
|
void HandleError(const leveldb::Status& status);
|
||||||
|
|
||||||
|
class CDBWrapper;
|
||||||
|
|
||||||
/** Batch of changes queued to be written to a CDBWrapper */
|
/** Batch of changes queued to be written to a CDBWrapper */
|
||||||
class CDBBatch
|
class CDBBatch
|
||||||
{
|
{
|
||||||
friend class CDBWrapper;
|
friend class CDBWrapper;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const CDBWrapper &parent;
|
||||||
leveldb::WriteBatch batch;
|
leveldb::WriteBatch batch;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @param[in] parent CDBWrapper that this batch is to be submitted to
|
||||||
|
*/
|
||||||
|
CDBBatch(const CDBWrapper &parent) : parent(parent) { };
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
void Write(const K& key, const V& value)
|
void Write(const K& key, const V& value)
|
||||||
{
|
{
|
||||||
|
@ -64,15 +72,17 @@ public:
|
||||||
class CDBIterator
|
class CDBIterator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
const CDBWrapper &parent;
|
||||||
leveldb::Iterator *piter;
|
leveldb::Iterator *piter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param[in] parent Parent CDBWrapper instance.
|
||||||
* @param[in] piterIn The original leveldb iterator.
|
* @param[in] piterIn The original leveldb iterator.
|
||||||
*/
|
*/
|
||||||
CDBIterator(leveldb::Iterator *piterIn) :
|
CDBIterator(const CDBWrapper &parent, leveldb::Iterator *piterIn) :
|
||||||
piter(piterIn) { };
|
parent(parent), piter(piterIn) { };
|
||||||
~CDBIterator();
|
~CDBIterator();
|
||||||
|
|
||||||
bool Valid();
|
bool Valid();
|
||||||
|
@ -183,7 +193,7 @@ public:
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
bool Write(const K& key, const V& value, bool fSync = false)
|
bool Write(const K& key, const V& value, bool fSync = false)
|
||||||
{
|
{
|
||||||
CDBBatch batch;
|
CDBBatch batch(*this);
|
||||||
batch.Write(key, value);
|
batch.Write(key, value);
|
||||||
return WriteBatch(batch, fSync);
|
return WriteBatch(batch, fSync);
|
||||||
}
|
}
|
||||||
|
@ -210,7 +220,7 @@ public:
|
||||||
template <typename K>
|
template <typename K>
|
||||||
bool Erase(const K& key, bool fSync = false)
|
bool Erase(const K& key, bool fSync = false)
|
||||||
{
|
{
|
||||||
CDBBatch batch;
|
CDBBatch batch(*this);
|
||||||
batch.Erase(key);
|
batch.Erase(key);
|
||||||
return WriteBatch(batch, fSync);
|
return WriteBatch(batch, fSync);
|
||||||
}
|
}
|
||||||
|
@ -225,20 +235,19 @@ public:
|
||||||
|
|
||||||
bool Sync()
|
bool Sync()
|
||||||
{
|
{
|
||||||
CDBBatch batch;
|
CDBBatch batch(*this);
|
||||||
return WriteBatch(batch, true);
|
return WriteBatch(batch, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
CDBIterator *NewIterator()
|
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.
|
* Return true if the database managed by this class contains no entries.
|
||||||
*/
|
*/
|
||||||
bool IsEmpty();
|
bool IsEmpty();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_DBWRAPPER_H
|
#endif // BITCOIN_DBWRAPPER_H
|
||||||
|
|
|
@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
|
||||||
uint256 in3 = GetRandHash();
|
uint256 in3 = GetRandHash();
|
||||||
|
|
||||||
uint256 res;
|
uint256 res;
|
||||||
CDBBatch batch;
|
CDBBatch batch(dbw);
|
||||||
|
|
||||||
batch.Write(key, in);
|
batch.Write(key, in);
|
||||||
batch.Write(key2, in2);
|
batch.Write(key2, in2);
|
||||||
|
|
|
@ -85,7 +85,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
|
||||||
const uint256 &hashAnchor,
|
const uint256 &hashAnchor,
|
||||||
CAnchorsMap &mapAnchors,
|
CAnchorsMap &mapAnchors,
|
||||||
CNullifiersMap &mapNullifiers) {
|
CNullifiersMap &mapNullifiers) {
|
||||||
CDBBatch batch;
|
CDBBatch batch(db);
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
size_t changed = 0;
|
size_t changed = 0;
|
||||||
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
|
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) {
|
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++) {
|
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);
|
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) {
|
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++)
|
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);
|
batch.Write(make_pair(DB_TXINDEX, it->first), it->second);
|
||||||
return WriteBatch(batch);
|
return WriteBatch(batch);
|
||||||
|
|
Loading…
Reference in New Issue