diff --git a/src/dbwrapper.h b/src/dbwrapper.h index ea59dd40c..5beaccbd0 100644 --- a/src/dbwrapper.h +++ b/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 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 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 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 diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index 3df1e1585..496ae4be5 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/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); diff --git a/src/txdb.cpp b/src/txdb.cpp index b05e27489..7059dd6f8 100644 --- a/src/txdb.cpp +++ b/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 >& fileInfo, int nLastFile, const std::vector& blockinfo) { - CDBBatch batch; + CDBBatch batch(*this); for (std::vector >::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 >&vect) { - CDBBatch batch; + CDBBatch batch(*this); for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) batch.Write(make_pair(DB_TXINDEX, it->first), it->second); return WriteBatch(batch);